Search in sources :

Example 1 with AbstractSecuredStoragePluginConfig

use of org.apache.drill.common.logical.AbstractSecuredStoragePluginConfig in project drill by apache.

the class StorageResources method updateAuthToken.

@GET
@Path("/storage/{name}/update_oath2_authtoken")
@Produces(MediaType.TEXT_HTML)
public Response updateAuthToken(@PathParam("name") String name, @QueryParam("code") String code) {
    try {
        if (storage.getPlugin(name).getConfig() instanceof AbstractSecuredStoragePluginConfig) {
            AbstractSecuredStoragePluginConfig securedStoragePluginConfig = (AbstractSecuredStoragePluginConfig) storage.getPlugin(name).getConfig();
            CredentialsProvider credentialsProvider = securedStoragePluginConfig.getCredentialsProvider();
            String callbackURL = this.request.getRequestURL().toString();
            // Now exchange the authorization token for an access token
            Builder builder = new OkHttpClient.Builder();
            OkHttpClient client = builder.build();
            Request accessTokenRequest = OAuthUtils.getAccessTokenRequest(credentialsProvider, code, callbackURL);
            Map<String, String> updatedTokens = OAuthUtils.getOAuthTokens(client, accessTokenRequest);
            // Add to token registry
            TokenRegistry tokenRegistry = ((AbstractStoragePlugin) storage.getPlugin(name)).getContext().getoAuthTokenProvider().getOauthTokenRegistry();
            // Add a token registry table if none exists
            tokenRegistry.createTokenTable(name);
            PersistentTokenTable tokenTable = tokenRegistry.getTokenTable(name);
            // Add tokens to persistent storage
            tokenTable.setAccessToken(updatedTokens.get(OAuthTokenCredentials.ACCESS_TOKEN));
            tokenTable.setRefreshToken(updatedTokens.get(OAuthTokenCredentials.REFRESH_TOKEN));
            // Get success page
            String successPage = null;
            try (InputStream inputStream = Resource.newClassPathResource(OAUTH_SUCCESS_PAGE).getInputStream()) {
                InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
                BufferedReader bufferedReader = new BufferedReader(reader);
                successPage = bufferedReader.lines().collect(Collectors.joining("\n"));
                bufferedReader.close();
                reader.close();
            } catch (IOException e) {
                Response.status(Status.OK).entity("You may close this window.").build();
            }
            return Response.status(Status.OK).entity(successPage).build();
        } else {
            logger.error("{} is not a HTTP plugin. You can only add auth code to HTTP plugins.", name);
            return Response.status(Status.INTERNAL_SERVER_ERROR).entity(message("Unable to add authorization code: %s", name)).build();
        }
    } catch (PluginException e) {
        logger.error("Error when adding auth token to {}", name);
        return Response.status(Status.INTERNAL_SERVER_ERROR).entity(message("Unable to add authorization code: %s", e.getMessage())).build();
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) InputStreamReader(java.io.InputStreamReader) TokenRegistry(org.apache.drill.exec.oauth.TokenRegistry) InputStream(java.io.InputStream) Builder(okhttp3.OkHttpClient.Builder) PluginException(org.apache.drill.exec.store.StoragePluginRegistry.PluginException) Request(okhttp3.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) CredentialsProvider(org.apache.drill.common.logical.security.CredentialsProvider) IOException(java.io.IOException) BufferedReader(java.io.BufferedReader) PersistentTokenTable(org.apache.drill.exec.oauth.PersistentTokenTable) AbstractSecuredStoragePluginConfig(org.apache.drill.common.logical.AbstractSecuredStoragePluginConfig) AbstractStoragePlugin(org.apache.drill.exec.store.AbstractStoragePlugin) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with AbstractSecuredStoragePluginConfig

use of org.apache.drill.common.logical.AbstractSecuredStoragePluginConfig in project drill by apache.

the class PluginConfigWrapper method isOauth.

/**
 * Determines whether the storage plugin in question needs the OAuth button in the UI.  In
 * order to be considered an OAuth plugin, the plugin must:
 * 1. Use AbstractSecuredStoragePluginConfig
 * 2. The credential provider must not be null
 * 3. The credentialsProvider must contain a client_id and client_secret
 * @return true if the plugin uses OAuth, false if not.
 */
@JsonIgnore
public boolean isOauth() {
    if (!(config instanceof AbstractSecuredStoragePluginConfig)) {
        return false;
    }
    AbstractSecuredStoragePluginConfig securedStoragePluginConfig = (AbstractSecuredStoragePluginConfig) config;
    CredentialsProvider credentialsProvider = securedStoragePluginConfig.getCredentialsProvider();
    if (credentialsProvider == null) {
        return false;
    }
    OAuthTokenCredentials tokenCredentials = new OAuthTokenCredentials(credentialsProvider);
    return !StringUtils.isEmpty(tokenCredentials.getClientID()) || !StringUtils.isEmpty(tokenCredentials.getClientSecret());
}
Also used : OAuthTokenCredentials(org.apache.drill.exec.store.security.oauth.OAuthTokenCredentials) CredentialsProvider(org.apache.drill.common.logical.security.CredentialsProvider) AbstractSecuredStoragePluginConfig(org.apache.drill.common.logical.AbstractSecuredStoragePluginConfig) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore)

Aggregations

AbstractSecuredStoragePluginConfig (org.apache.drill.common.logical.AbstractSecuredStoragePluginConfig)2 CredentialsProvider (org.apache.drill.common.logical.security.CredentialsProvider)2 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 OkHttpClient (okhttp3.OkHttpClient)1 Builder (okhttp3.OkHttpClient.Builder)1 Request (okhttp3.Request)1 PersistentTokenTable (org.apache.drill.exec.oauth.PersistentTokenTable)1 TokenRegistry (org.apache.drill.exec.oauth.TokenRegistry)1 AbstractStoragePlugin (org.apache.drill.exec.store.AbstractStoragePlugin)1 PluginException (org.apache.drill.exec.store.StoragePluginRegistry.PluginException)1 OAuthTokenCredentials (org.apache.drill.exec.store.security.oauth.OAuthTokenCredentials)1