You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@drill.apache.org by GitBox <gi...@apache.org> on 2022/02/23 00:58:35 UTC
[GitHub] [drill] cgivre opened a new pull request #2473: DRILL-8148: Add REST Endpoints to Update OAuth Tokens
cgivre opened a new pull request #2473:
URL: https://github.com/apache/drill/pull/2473
# [DRILL-8148](https://issues.apache.org/jira/browse/DRILL-8148): Add REST Endpoints to Update OAuth Tokens
## Description
This PR adds three REST endpoints to Drill to facilitate updating OAuth tokens for REST API calls.
## Documentation
The following API calls are now available for updating an HTTP Storage Plugin's OAuth tokens if OAuth is used. These three new endpoints are `POST` requests and accept the tokens as part of the form body.
* `/storage/{plugin name}/update_access_token`
* `/storage/{plugin name}/update_refresh_token`
* `/storage/{plugin name}/update_oauth_tokens`
These endpoints take as arguments: `access_token`, `refresh_token` or both.
## Testing
Added unit tests.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@drill.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [drill] cgivre commented on a change in pull request #2473: DRILL-8148: Add REST Endpoints to Update OAuth Tokens
Posted by GitBox <gi...@apache.org>.
cgivre commented on a change in pull request #2473:
URL: https://github.com/apache/drill/pull/2473#discussion_r815910342
##########
File path: exec/java-exec/src/main/java/org/apache/drill/exec/server/rest/StorageResources.java
##########
@@ -200,6 +202,100 @@ public Response enablePlugin(@PathParam("name") String name, @PathParam("val") B
}
}
+ @POST
+ @Path("/storage/{name}/update_refresh_token")
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response updateRefreshToken(@PathParam("name") String name,
+ @FormParam("refresh_token") String refreshToken) {
+ try {
+ if (storage.getPlugin(name).getConfig() instanceof AbstractSecuredStoragePluginConfig) {
+ DrillbitContext context = ((AbstractStoragePlugin) storage.getPlugin(name)).getContext();
+ OAuthTokenProvider tokenProvider = context.getoAuthTokenProvider();
+ PersistentTokenTable tokenTable = tokenProvider.getOauthTokenRegistry().getTokenTable(name);
+
+ // Set the access token
+ tokenTable.setRefreshToken(refreshToken);
+
+ return Response.status(Status.OK)
+ .entity("Refresh token have been updated.")
+ .build();
+ } else {
+ logger.error("{} is not a HTTP plugin. You can only add access tokens to HTTP plugins.", name);
+ return Response.status(Status.INTERNAL_SERVER_ERROR)
+ .entity(message("Unable to add tokens: %s", name))
+ .build();
+ }
+ } catch (PluginException e) {
+ logger.error("Error when adding tokens to {}", name);
+ return Response.status(Status.INTERNAL_SERVER_ERROR)
+ .entity(message("Unable to add tokens: %s", e.getMessage()))
+ .build();
+ }
+ }
Review comment:
Fixed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@drill.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [drill] cgivre merged pull request #2473: DRILL-8148: Add REST Endpoints to Update OAuth Tokens
Posted by GitBox <gi...@apache.org>.
cgivre merged pull request #2473:
URL: https://github.com/apache/drill/pull/2473
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@drill.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [drill] jnturton commented on a change in pull request #2473: DRILL-8148: Add REST Endpoints to Update OAuth Tokens
Posted by GitBox <gi...@apache.org>.
jnturton commented on a change in pull request #2473:
URL: https://github.com/apache/drill/pull/2473#discussion_r815689882
##########
File path: exec/java-exec/src/main/java/org/apache/drill/exec/server/rest/StorageResources.java
##########
@@ -200,6 +202,100 @@ public Response enablePlugin(@PathParam("name") String name, @PathParam("val") B
}
}
+ @POST
+ @Path("/storage/{name}/update_refresh_token")
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response updateRefreshToken(@PathParam("name") String name,
+ @FormParam("refresh_token") String refreshToken) {
+ try {
+ if (storage.getPlugin(name).getConfig() instanceof AbstractSecuredStoragePluginConfig) {
+ DrillbitContext context = ((AbstractStoragePlugin) storage.getPlugin(name)).getContext();
+ OAuthTokenProvider tokenProvider = context.getoAuthTokenProvider();
+ PersistentTokenTable tokenTable = tokenProvider.getOauthTokenRegistry().getTokenTable(name);
+
+ // Set the access token
+ tokenTable.setRefreshToken(refreshToken);
+
+ return Response.status(Status.OK)
+ .entity("Refresh token have been updated.")
+ .build();
+ } else {
+ logger.error("{} is not a HTTP plugin. You can only add access tokens to HTTP plugins.", name);
+ return Response.status(Status.INTERNAL_SERVER_ERROR)
+ .entity(message("Unable to add tokens: %s", name))
+ .build();
+ }
+ } catch (PluginException e) {
+ logger.error("Error when adding tokens to {}", name);
+ return Response.status(Status.INTERNAL_SERVER_ERROR)
+ .entity(message("Unable to add tokens: %s", e.getMessage()))
+ .build();
+ }
+ }
Review comment:
```suggestion
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@drill.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [drill] cgivre commented on pull request #2473: DRILL-8148: Add REST Endpoints to Update OAuth Tokens
Posted by GitBox <gi...@apache.org>.
cgivre commented on pull request #2473:
URL: https://github.com/apache/drill/pull/2473#issuecomment-1054283708
Thanks @jnturton for the review. I addressed all your comments. Will merge later.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@drill.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [drill] jnturton commented on a change in pull request #2473: DRILL-8148: Add REST Endpoints to Update OAuth Tokens
Posted by GitBox <gi...@apache.org>.
jnturton commented on a change in pull request #2473:
URL: https://github.com/apache/drill/pull/2473#discussion_r815691049
##########
File path: exec/java-exec/src/main/java/org/apache/drill/exec/server/rest/StorageResources.java
##########
@@ -200,6 +202,100 @@ public Response enablePlugin(@PathParam("name") String name, @PathParam("val") B
}
}
+ @POST
+ @Path("/storage/{name}/update_refresh_token")
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response updateRefreshToken(@PathParam("name") String name,
+ @FormParam("refresh_token") String refreshToken) {
+ try {
+ if (storage.getPlugin(name).getConfig() instanceof AbstractSecuredStoragePluginConfig) {
+ DrillbitContext context = ((AbstractStoragePlugin) storage.getPlugin(name)).getContext();
+ OAuthTokenProvider tokenProvider = context.getoAuthTokenProvider();
+ PersistentTokenTable tokenTable = tokenProvider.getOauthTokenRegistry().getTokenTable(name);
+
+ // Set the access token
+ tokenTable.setRefreshToken(refreshToken);
+
+ return Response.status(Status.OK)
+ .entity("Refresh token have been updated.")
+ .build();
+ } else {
+ logger.error("{} is not a HTTP plugin. You can only add access tokens to HTTP plugins.", name);
+ return Response.status(Status.INTERNAL_SERVER_ERROR)
+ .entity(message("Unable to add tokens: %s", name))
+ .build();
+ }
+ } catch (PluginException e) {
+ logger.error("Error when adding tokens to {}", name);
+ return Response.status(Status.INTERNAL_SERVER_ERROR)
+ .entity(message("Unable to add tokens: %s", e.getMessage()))
+ .build();
+ }
+ }
+ @POST
+ @Path("/storage/{name}/update_access_token")
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response updateAccessToken(@PathParam("name") String name,
+ @FormParam("access_token") String accessToken) {
+ try {
+ if (storage.getPlugin(name).getConfig() instanceof AbstractSecuredStoragePluginConfig) {
+ DrillbitContext context = ((AbstractStoragePlugin) storage.getPlugin(name)).getContext();
+ OAuthTokenProvider tokenProvider = context.getoAuthTokenProvider();
+ PersistentTokenTable tokenTable = tokenProvider.getOauthTokenRegistry().getTokenTable(name);
+
+ // Set the access token
+ tokenTable.setAccessToken(accessToken);
+
+ return Response.status(Status.OK)
+ .entity("Access tokens have been updated.")
+ .build();
+ } else {
+ logger.error("{} is not a HTTP plugin. You can only add access tokens to HTTP plugins.", name);
+ return Response.status(Status.INTERNAL_SERVER_ERROR)
+ .entity(message("Unable to add tokens: %s", name))
+ .build();
+ }
+ } catch (PluginException e) {
+ logger.error("Error when adding tokens to {}", name);
+ return Response.status(Status.INTERNAL_SERVER_ERROR)
+ .entity(message("Unable to add tokens: %s", e.getMessage()))
+ .build();
+ }
+ }
+
+ @POST
+ @Path("/storage/{name}/update_oauth_tokens")
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response updateOAuthTokens(@PathParam("name") String name,
+ @FormParam("access_token") String accessToken,
+ @FormParam("refresh_token") String refreshToken) {
+ try {
+ if (storage.getPlugin(name).getConfig() instanceof AbstractSecuredStoragePluginConfig) {
+ DrillbitContext context = ((AbstractStoragePlugin) storage.getPlugin(name)).getContext();
+ OAuthTokenProvider tokenProvider = context.getoAuthTokenProvider();
+ PersistentTokenTable tokenTable = tokenProvider.getOauthTokenRegistry().getTokenTable(name);
+
+ // Set the access and refresh token
+ tokenTable.setAccessToken(accessToken);
+ tokenTable.setRefreshToken(refreshToken);
+
+ return Response.status(Status.OK)
+ .entity("Access tokens have been updated.")
+ .build();
+ } else {
+ logger.error("{} is not a HTTP plugin. You can only add access tokens to HTTP plugins.", name);
+ return Response.status(Status.INTERNAL_SERVER_ERROR)
+ .entity(message("Unable to add tokens: %s", name))
+ .build();
+ }
+ } catch (PluginException e) {
+ logger.error("Error when adding tokens to {}", name);
+ return Response.status(Status.INTERNAL_SERVER_ERROR)
+ .entity(message("Unable to add tokens: %s", e.getMessage()))
+ .build();
+ }
+ }
+
@GET
@Path("/storage/{name}/update_oath2_authtoken")
Review comment:
```suggestion
@Path("/storage/{name}/update_oauth2_authtoken")
```
##########
File path: contrib/storage-http/src/test/java/org/apache/drill/exec/store/http/TestOAuthTokenUpdate.java
##########
@@ -0,0 +1,164 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.drill.exec.store.http;
+
+import okhttp3.Call;
+import okhttp3.FormBody;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.RequestBody;
+import okhttp3.Response;
+import org.apache.drill.common.logical.security.CredentialsProvider;
+import org.apache.drill.common.logical.security.PlainCredentialsProvider;
+import org.apache.drill.exec.ExecConstants;
+import org.apache.drill.exec.oauth.PersistentTokenTable;
+import org.apache.drill.exec.store.StoragePluginRegistry.PluginException;
+import org.apache.drill.exec.store.security.oauth.OAuthTokenCredentials;
+import org.apache.drill.test.ClusterFixtureBuilder;
+import org.apache.drill.test.ClusterTest;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+import static org.junit.Assert.assertEquals;
+
+public class TestOAuthTokenUpdate extends ClusterTest {
+
+ private static final String CONNECTION_NAME = "localOauth";
+ private static final int MOCK_SERVER_PORT = 47770;
+ private static final int TIMEOUT = 30;
+ private static String hostname;
+
+ private final OkHttpClient httpClient = new OkHttpClient.Builder()
+ .connectTimeout(TIMEOUT, TimeUnit.SECONDS)
+ .writeTimeout(TIMEOUT, TimeUnit.SECONDS)
+ .readTimeout(TIMEOUT, TimeUnit.SECONDS).build();
+
+ @BeforeClass
+ public static void setup() throws Exception {
+ ClusterFixtureBuilder builder = new ClusterFixtureBuilder(dirTestWatcher)
+ .configProperty(ExecConstants.HTTP_ENABLE, true)
+ .configProperty(ExecConstants.HTTP_PORT_HUNT, true);
+ startCluster(builder);
+ int portNumber = cluster.drillbit().getWebServerPort();
+ hostname = "http://localhost:" + portNumber + "/storage/" + CONNECTION_NAME;
+
+ Map<String, String> creds = new HashMap<>();
+ creds.put("clientID", "12345");
+ creds.put("clientSecret", "54321");
+ creds.put("accessToken", null);
+ creds.put("refreshToken", null);
+ creds.put(OAuthTokenCredentials.TOKEN_URI, "http://localhost:" + MOCK_SERVER_PORT + "/get_access_token");
+
+ CredentialsProvider credentialsProvider = new PlainCredentialsProvider(creds);
+
+ HttpApiConfig connectionConfig = HttpApiConfig.builder()
+ .url("http://localhost:" + MOCK_SERVER_PORT + "/getdata")
+ .method("get")
+ .requireTail(false)
+ .inputType("json")
+ .build();
+
+ HttpOAuthConfig oAuthConfig = HttpOAuthConfig.builder()
+ .callbackURL(hostname + "/update_oath2_authtoken")
Review comment:
```suggestion
.callbackURL(hostname + "/update_oauth2_authtoken")
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@drill.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org