Search in sources :

Example 1 with CLIConfig

use of io.pravega.cli.admin.utils.CLIConfig in project pravega by pravega.

the class ControllerCommand method createContext.

/**
 * Creates a context for child classes consisting of a REST client to execute calls against the Controller.
 *
 * @return REST client.
 */
protected Context createContext() {
    CLIConfig config = getCLIControllerConfig();
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.register(JacksonJsonProvider.class);
    clientConfig.property("sun.net.http.allowRestrictedHeaders", "true");
    ClientBuilder builder = ClientBuilder.newBuilder().withConfig(clientConfig);
    // If TLS parameters are configured, set them in client.
    if (config.isTlsEnabled()) {
        SSLContext tlsContext;
        try {
            KeyStore ks = createTrustStore(config.getTruststore());
            TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            tmf.init(ks);
            tlsContext = SSLContext.getInstance("TLS");
            tlsContext.init(null, tmf.getTrustManagers(), null);
        } catch (KeyStoreException | IOException | NoSuchAlgorithmException | CertificateException | KeyManagementException e) {
            String message = String.format("Encountered exception while trying to use the given truststore: %s", config.getTruststore());
            log.error(message, e);
            return null;
        }
        builder.sslContext(tlsContext);
    }
    Client client = builder.build();
    // If authorization parameters are configured, set them in the client.
    if (config.isAuthEnabled()) {
        HttpAuthenticationFeature auth = HttpAuthenticationFeature.basic(config.getUserName(), config.getPassword());
        client = client.register(auth);
    }
    return new Context(client);
}
Also used : SSLContext(javax.net.ssl.SSLContext) CertificateException(java.security.cert.CertificateException) SSLContext(javax.net.ssl.SSLContext) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStore(java.security.KeyStore) KeyManagementException(java.security.KeyManagementException) CLIConfig(io.pravega.cli.admin.utils.CLIConfig) HttpAuthenticationFeature(org.glassfish.jersey.client.authentication.HttpAuthenticationFeature) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) ClientBuilder(javax.ws.rs.client.ClientBuilder)

Example 2 with CLIConfig

use of io.pravega.cli.admin.utils.CLIConfig in project pravega by pravega.

the class AdminCommandStateTest method testAdminCommandStateCreationWithConfigFile.

@Test
public void testAdminCommandStateCreationWithConfigFile() throws IOException {
    System.setProperty("pravega.configurationFile", "../../config/admin-cli.properties");
    @Cleanup AdminCommandState commandState = new AdminCommandState();
    CLIConfig config = commandState.getConfigBuilder().build().getConfig(CLIConfig::builder);
    Assert.assertNotNull(config.getMetadataBackend());
}
Also used : Cleanup(lombok.Cleanup) CLIConfig(io.pravega.cli.admin.utils.CLIConfig) Test(org.junit.Test)

Aggregations

CLIConfig (io.pravega.cli.admin.utils.CLIConfig)2 IOException (java.io.IOException)1 KeyManagementException (java.security.KeyManagementException)1 KeyStore (java.security.KeyStore)1 KeyStoreException (java.security.KeyStoreException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 CertificateException (java.security.cert.CertificateException)1 SSLContext (javax.net.ssl.SSLContext)1 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)1 Client (javax.ws.rs.client.Client)1 ClientBuilder (javax.ws.rs.client.ClientBuilder)1 Cleanup (lombok.Cleanup)1 ClientConfig (org.glassfish.jersey.client.ClientConfig)1 HttpAuthenticationFeature (org.glassfish.jersey.client.authentication.HttpAuthenticationFeature)1 Test (org.junit.Test)1