Search in sources :

Example 1 with PluginException

use of org.apache.drill.exec.store.StoragePluginRegistry.PluginException 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 PluginException

use of org.apache.drill.exec.store.StoragePluginRegistry.PluginException in project drill by apache.

the class DynamicRootSchema method loadSchemaFactory.

/**
 * Loads schema factory(storage plugin) for specified {@code schemaName}
 * @param schemaName the name of the schema
 * @param caseSensitive whether matching for the schema name is case sensitive
 */
private void loadSchemaFactory(String schemaName, boolean caseSensitive) {
    try {
        SchemaPlus schemaPlus = this.plus();
        StoragePlugin plugin = storages.getPlugin(schemaName);
        if (plugin != null) {
            plugin.registerSchemas(schemaConfig, schemaPlus);
            return;
        }
        // Could not find the plugin of schemaName. The schemaName could be `dfs.tmp`, a 2nd level schema under 'dfs'
        List<String> paths = SchemaUtilites.getSchemaPathAsList(schemaName);
        if (paths.size() == 2) {
            plugin = storages.getPlugin(paths.get(0));
            if (plugin == null) {
                return;
            }
            // Looking for the SchemaPlus for the top level (e.g. 'dfs') of schemaName (e.g. 'dfs.tmp')
            SchemaPlus firstLevelSchema = schemaPlus.getSubSchema(paths.get(0));
            if (firstLevelSchema == null) {
                // register schema for this storage plugin to 'this'.
                plugin.registerSchemas(schemaConfig, schemaPlus);
                firstLevelSchema = schemaPlus.getSubSchema(paths.get(0));
            }
            // Load second level schemas for this storage plugin
            List<SchemaPlus> secondLevelSchemas = new ArrayList<>();
            for (String secondLevelSchemaName : firstLevelSchema.getSubSchemaNames()) {
                secondLevelSchemas.add(firstLevelSchema.getSubSchema(secondLevelSchemaName));
            }
            for (SchemaPlus schema : secondLevelSchemas) {
                org.apache.drill.exec.store.AbstractSchema drillSchema;
                try {
                    drillSchema = schema.unwrap(AbstractSchema.class);
                } catch (ClassCastException e) {
                    throw new RuntimeException(String.format("Schema '%s' is not expected under root schema", schema.getName()));
                }
                SubSchemaWrapper wrapper = new SubSchemaWrapper(drillSchema);
                schemaPlus.add(wrapper.getName(), wrapper);
            }
        }
    } catch (PluginException | IOException ex) {
        logger.warn("Failed to load schema for \"" + schemaName + "\"!", ex);
        // We can't proceed further without a schema, throw a runtime exception.
        UserException.Builder exceptBuilder = UserException.resourceError(ex).message("Failed to load schema for \"" + schemaName + "\"!").addContext(ex.getClass().getName() + ": " + ex.getMessage()).addContext(// Provide hint if it exists
        UserExceptionUtils.getUserHint(ex));
        throw exceptBuilder.build(logger);
    }
}
Also used : SubSchemaWrapper(org.apache.drill.exec.store.SubSchemaWrapper) PluginException(org.apache.drill.exec.store.StoragePluginRegistry.PluginException) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ArrayList(java.util.ArrayList) IOException(java.io.IOException) StoragePlugin(org.apache.drill.exec.store.StoragePlugin) AbstractSchema(org.apache.drill.exec.store.AbstractSchema) AbstractSchema(org.apache.drill.exec.store.AbstractSchema)

Example 3 with PluginException

use of org.apache.drill.exec.store.StoragePluginRegistry.PluginException in project drill by apache.

the class TestPluginRegistry method testFormatPlugin.

@Test
public void testFormatPlugin() throws Exception {
    ClusterFixtureBuilder builder = ClusterFixture.builder(dirTestWatcher);
    try (ClusterFixture cluster = builder.build()) {
        StoragePluginRegistry registry = cluster.storageRegistry();
        StoragePluginConfig config = registry.getStoredConfig(CP_PLUGIN_NAME);
        FileSystemConfig fsConfig = (FileSystemConfig) config;
        assertFalse(fsConfig.getFormats().containsKey("bsv"));
        // Add a new format
        TextFormatConfig bsv = new TextFormatConfig(null, // line delimiter
        null, // field delimiter
        "!", // quote
        null, // escape
        null, // comment
        null, // skip first line
        false, // extract header
        false);
        registry.putFormatPlugin(CP_PLUGIN_NAME, "bsv", bsv);
        config = registry.getStoredConfig(CP_PLUGIN_NAME);
        fsConfig = (FileSystemConfig) config;
        assertTrue(fsConfig.getFormats().containsKey("bsv"));
        assertSame(bsv, fsConfig.getFormats().get("bsv"));
        // Remove the format
        registry.putFormatPlugin(CP_PLUGIN_NAME, "bsv", null);
        config = registry.getStoredConfig(CP_PLUGIN_NAME);
        fsConfig = (FileSystemConfig) config;
        assertFalse(fsConfig.getFormats().containsKey("bsv"));
        // Undefined plugin
        try {
            registry.putFormatPlugin("bogus", "bsv", bsv);
            fail();
        } catch (PluginException e) {
        // Expected
        }
        // Try to set a non-FS plugin
        try {
            registry.putFormatPlugin(SYS_PLUGIN_NAME, "bsv", bsv);
            fail();
        } catch (PluginException e) {
        // Expected
        }
    }
}
Also used : ClusterFixture(org.apache.drill.test.ClusterFixture) TextFormatConfig(org.apache.drill.exec.store.easy.text.TextFormatPlugin.TextFormatConfig) PluginException(org.apache.drill.exec.store.StoragePluginRegistry.PluginException) StoragePluginConfig(org.apache.drill.common.logical.StoragePluginConfig) FileSystemConfig(org.apache.drill.exec.store.dfs.FileSystemConfig) ClusterFixtureBuilder(org.apache.drill.test.ClusterFixtureBuilder) BaseTest(org.apache.drill.test.BaseTest) Test(org.junit.Test)

Example 4 with PluginException

use of org.apache.drill.exec.store.StoragePluginRegistry.PluginException in project drill by apache.

the class TestPluginRegistry method testLifecycle.

@Test
public void testLifecycle() throws Exception {
    ClusterFixtureBuilder builder = ClusterFixture.builder(dirTestWatcher);
    try (ClusterFixture cluster = builder.build()) {
        StoragePluginRegistry registry = cluster.storageRegistry();
        // Bootstrap file loaded.
        // Normal
        assertNotNull(registry.getPlugin(CP_PLUGIN_NAME));
        // System
        assertNotNull(registry.getPlugin(SYS_PLUGIN_NAME));
        // Not editable
        assertNull(registry.getStoredConfig(SYS_PLUGIN_NAME));
        assertNull(registry.getPlugin("bogus"));
        // Enabled plugins
        Map<String, StoragePluginConfig> configMap = registry.enabledConfigs();
        assertTrue(configMap.containsKey(CP_PLUGIN_NAME));
        // Disabled, but still appears
        assertFalse(configMap.containsKey(S3_PLUGIN_NAME));
        assertFalse(configMap.containsKey(SYS_PLUGIN_NAME));
        assertNotNull(registry.getDefinedConfig(CP_PLUGIN_NAME));
        assertNull(registry.getDefinedConfig(S3_PLUGIN_NAME));
        assertNotNull(registry.getDefinedConfig(SYS_PLUGIN_NAME));
        // All stored plugins, including disabled
        configMap = registry.storedConfigs();
        assertTrue(configMap.containsKey(CP_PLUGIN_NAME));
        // Disabled, but still appears
        assertTrue(configMap.containsKey(S3_PLUGIN_NAME));
        assertNotNull(configMap.get(S3_PLUGIN_NAME));
        assertSame(registry.getStoredConfig(S3_PLUGIN_NAME), configMap.get(S3_PLUGIN_NAME));
        assertFalse(configMap.containsKey(SYS_PLUGIN_NAME));
        int bootstrapCount = configMap.size();
        // Enabled only
        configMap = registry.storedConfigs(PluginFilter.ENABLED);
        assertTrue(configMap.containsKey(CP_PLUGIN_NAME));
        assertFalse(configMap.containsKey(S3_PLUGIN_NAME));
        // Disabled only
        configMap = registry.storedConfigs(PluginFilter.DISABLED);
        assertFalse(configMap.containsKey(CP_PLUGIN_NAME));
        assertTrue(configMap.containsKey(S3_PLUGIN_NAME));
        // Create a new plugin
        FileSystemConfig pConfig1 = myConfig1();
        registry.put(MY_PLUGIN_NAME, pConfig1);
        StoragePlugin plugin1 = registry.getPlugin(MY_PLUGIN_NAME);
        assertNotNull(plugin1);
        assertSame(plugin1, registry.getPluginByConfig(pConfig1));
        configMap = registry.storedConfigs();
        // Names converted to lowercase in persistent storage
        assertTrue(configMap.containsKey(MY_PLUGIN_KEY));
        assertEquals(bootstrapCount + 1, configMap.size());
        // Names are case-insensitive
        assertSame(plugin1, registry.getPlugin(MY_PLUGIN_KEY));
        assertSame(plugin1, registry.getPlugin(MY_PLUGIN_NAME.toUpperCase()));
        // Update the plugin
        FileSystemConfig pConfig2 = myConfig2();
        registry.put(MY_PLUGIN_NAME, pConfig2);
        StoragePlugin plugin2 = registry.getPlugin(MY_PLUGIN_NAME);
        assertNotSame(plugin1, plugin2);
        assertTrue(plugin2 instanceof FileSystemPlugin);
        FileSystemPlugin fsStorage = (FileSystemPlugin) plugin2;
        assertSame(pConfig2, fsStorage.getConfig());
        assertSame(plugin2, registry.getPluginByConfig(pConfig2));
        // Cannot create/update a plugin with null or blank name
        FileSystemConfig pConfig3 = myConfig1();
        try {
            registry.put(null, pConfig3);
            fail();
        } catch (PluginException e) {
        // Expected
        }
        try {
            registry.put("  ", pConfig3);
            fail();
        } catch (PluginException e) {
        // Expected
        }
    }
}
Also used : ClusterFixture(org.apache.drill.test.ClusterFixture) FileSystemPlugin(org.apache.drill.exec.store.dfs.FileSystemPlugin) PluginException(org.apache.drill.exec.store.StoragePluginRegistry.PluginException) StoragePluginConfig(org.apache.drill.common.logical.StoragePluginConfig) FileSystemConfig(org.apache.drill.exec.store.dfs.FileSystemConfig) ClusterFixtureBuilder(org.apache.drill.test.ClusterFixtureBuilder) BaseTest(org.apache.drill.test.BaseTest) Test(org.junit.Test)

Example 5 with PluginException

use of org.apache.drill.exec.store.StoragePluginRegistry.PluginException in project drill by apache.

the class ClusterMockStorageFixture method insertMockStorage.

/**
 * This should be called after bits are started
 * @param name the mock storage name we are going to create
 */
public void insertMockStorage(String name, boolean breakRegisterSchema) {
    for (Drillbit bit : drillbits()) {
        // Bit name and registration.
        final StoragePluginRegistry pluginRegistry = bit.getContext().getStorage();
        MockBreakageStorage plugin;
        try {
            MockBreakageStorageEngineConfig config = MockBreakageStorageEngineConfig.INSTANCE;
            config.setEnabled(true);
            pluginRegistry.put(name, config);
            plugin = (MockBreakageStorage) pluginRegistry.getPlugin(name);
        } catch (PluginException e) {
            throw new IllegalStateException(e);
        }
        plugin.setBreakRegister(breakRegisterSchema);
    }
}
Also used : StoragePluginRegistry(org.apache.drill.exec.store.StoragePluginRegistry) MockBreakageStorage(org.apache.drill.exec.store.mock.MockBreakageStorage) Drillbit(org.apache.drill.exec.server.Drillbit) PluginException(org.apache.drill.exec.store.StoragePluginRegistry.PluginException) MockBreakageStorageEngineConfig(org.apache.drill.exec.store.mock.MockBreakageStorage.MockBreakageStorageEngineConfig)

Aggregations

PluginException (org.apache.drill.exec.store.StoragePluginRegistry.PluginException)12 BaseTest (org.apache.drill.test.BaseTest)5 ClusterFixture (org.apache.drill.test.ClusterFixture)5 ClusterFixtureBuilder (org.apache.drill.test.ClusterFixtureBuilder)5 Test (org.junit.Test)5 StoragePluginConfig (org.apache.drill.common.logical.StoragePluginConfig)3 FileSystemConfig (org.apache.drill.exec.store.dfs.FileSystemConfig)3 FileSystemPlugin (org.apache.drill.exec.store.dfs.FileSystemPlugin)3 IOException (java.io.IOException)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 SchemaPlus (org.apache.calcite.schema.SchemaPlus)2 UserException (org.apache.drill.common.exceptions.UserException)2 TokenRegistry (org.apache.drill.exec.oauth.TokenRegistry)2 Drillbit (org.apache.drill.exec.server.Drillbit)2 AbstractSchema (org.apache.drill.exec.store.AbstractSchema)2 AbstractStoragePlugin (org.apache.drill.exec.store.AbstractStoragePlugin)2 StoragePlugin (org.apache.drill.exec.store.StoragePlugin)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 SerializableString (com.fasterxml.jackson.core.SerializableString)1