Search in sources :

Example 1 with StandaloneAuthenticator

use of com.ibm.streamsx.rest.internal.StandaloneAuthenticator in project streamsx.topology by IBMStreams.

the class Instance method createStandaloneConnection.

private static StreamsConnection createStandaloneConnection(String endpoint, String userName, String password, boolean verify) throws IOException {
    if (!endpoint.endsWith(STREAMS_REST_RESOURCES)) {
        URL url = new URL(endpoint);
        URL resourcesUrl = new URL(url.getProtocol(), url.getHost(), url.getPort(), STREAMS_REST_RESOURCES);
        endpoint = resourcesUrl.toExternalForm();
    }
    StandaloneAuthenticator auth = StandaloneAuthenticator.of(endpoint, userName, password);
    if (auth.config(verify) != null) {
        return StreamsConnection.ofAuthenticator(endpoint, auth);
    } else {
        // Couldn't configure standalone authenticator, try Basic
        return StreamsConnection.createInstance(userName, password, endpoint);
    }
}
Also used : URL(java.net.URL) StreamsKeys.getStreamsInstanceURL(com.ibm.streamsx.topology.internal.context.streamsrest.StreamsKeys.getStreamsInstanceURL) StandaloneAuthenticator(com.ibm.streamsx.rest.internal.StandaloneAuthenticator)

Example 2 with StandaloneAuthenticator

use of com.ibm.streamsx.rest.internal.StandaloneAuthenticator in project streamsx.topology by IBMStreams.

the class RemoteDistributedStreamsContext method setSubmissionInstance.

static void setSubmissionInstance(AppEntity entity) throws IOException {
    Instance cfgInstance = getConfigInstance(entity);
    if (cfgInstance != null) {
        StreamsConnection sc = cfgInstance.getStreamsConnection();
        boolean verify = cfgInstance.getStreamsConnection().isVerify();
        JsonObject deploy = deploy(entity.submission);
        Function<Executor, String> authenticatorO = sc.getAuthenticator();
        deploy.addProperty(ContextProperties.SSL_VERIFY, verify);
        JsonObject service;
        if (authenticatorO instanceof ICP4DAuthenticator) {
            ICP4DAuthenticator authenticator = (ICP4DAuthenticator) authenticatorO;
            service = authenticator.config(verify);
        } else if (authenticatorO instanceof StandaloneAuthenticator) {
            StandaloneAuthenticator authenticator = (StandaloneAuthenticator) authenticatorO;
            service = authenticator.config(verify);
            String buildServiceUrl = getConfigBuildServiceUrl(entity);
            if (buildServiceUrl == null) {
                buildServiceUrl = System.getenv(Util.STREAMS_BUILD_URL);
            }
            if (buildServiceUrl != null) {
                // Copy so we don't affect instance. Version of gson we
                // use lacks deepCopy() so we serialize / parse to copy.
                String json = service.toString();
                service = new JsonParser().parse(json).getAsJsonObject();
                JsonObject connInfo = service.getAsJsonObject(CONNECTION_INFO);
                if (connInfo.has(SERVICE_BUILD_ENDPOINT)) {
                    connInfo.remove(SERVICE_BUILD_ENDPOINT);
                }
                connInfo.addProperty(SERVICE_BUILD_ENDPOINT, buildServiceUrl);
            }
        } else {
            throw new IllegalStateException("Invalid Instance for Streams V5: " + cfgInstance);
        }
        deploy.add(StreamsKeys.SERVICE_DEFINITION, service);
    }
}
Also used : ICP4DAuthenticator(com.ibm.streamsx.rest.internal.ICP4DAuthenticator) Executor(org.apache.http.client.fluent.Executor) Instance(com.ibm.streamsx.rest.Instance) JsonObject(com.google.gson.JsonObject) StreamsConnection(com.ibm.streamsx.rest.StreamsConnection) StandaloneAuthenticator(com.ibm.streamsx.rest.internal.StandaloneAuthenticator) JsonParser(com.google.gson.JsonParser)

Example 3 with StandaloneAuthenticator

use of com.ibm.streamsx.rest.internal.StandaloneAuthenticator in project streamsx.topology by IBMStreams.

the class RemoteEdgeContext method setSubmissionInstance.

static void setSubmissionInstance(AppEntity entity) throws IOException {
    Instance cfgInstance = getConfigInstance(entity);
    if (cfgInstance != null) {
        StreamsConnection sc = cfgInstance.getStreamsConnection();
        boolean verify = cfgInstance.getStreamsConnection().isVerify();
        JsonObject deploy = deploy(entity.submission);
        Function<Executor, String> authenticatorO = sc.getAuthenticator();
        deploy.addProperty(ContextProperties.SSL_VERIFY, verify);
        JsonObject service;
        if (authenticatorO instanceof ICP4DAuthenticator) {
            ICP4DAuthenticator authenticator = (ICP4DAuthenticator) authenticatorO;
            service = authenticator.config(verify);
        } else if (authenticatorO instanceof StandaloneAuthenticator) {
            StandaloneAuthenticator authenticator = (StandaloneAuthenticator) authenticatorO;
            service = authenticator.config(verify);
            String buildServiceUrl = getConfigBuildServiceUrl(entity);
            if (buildServiceUrl == null) {
                buildServiceUrl = System.getenv(Util.STREAMS_BUILD_URL);
            }
            if (buildServiceUrl != null) {
                // Copy so we don't affect instance. Version of gson we
                // use lacks deepCopy() so we serialize / parse to copy.
                String json = service.toString();
                service = new JsonParser().parse(json).getAsJsonObject();
                JsonObject connInfo = service.getAsJsonObject(CONNECTION_INFO);
                if (connInfo.has(SERVICE_BUILD_ENDPOINT)) {
                    connInfo.remove(SERVICE_BUILD_ENDPOINT);
                }
                connInfo.addProperty(SERVICE_BUILD_ENDPOINT, buildServiceUrl);
            }
        } else {
            throw new IllegalStateException("Invalid Instance for Streams V5: " + cfgInstance);
        }
        deploy.add(StreamsKeys.SERVICE_DEFINITION, service);
    }
}
Also used : ICP4DAuthenticator(com.ibm.streamsx.rest.internal.ICP4DAuthenticator) Executor(org.apache.http.client.fluent.Executor) Instance(com.ibm.streamsx.rest.Instance) JsonObject(com.google.gson.JsonObject) StreamsConnection(com.ibm.streamsx.rest.StreamsConnection) StandaloneAuthenticator(com.ibm.streamsx.rest.internal.StandaloneAuthenticator) JsonParser(com.google.gson.JsonParser)

Example 4 with StandaloneAuthenticator

use of com.ibm.streamsx.rest.internal.StandaloneAuthenticator in project streamsx.topology by IBMStreams.

the class BuildService method ofEndpoint.

public static BuildService ofEndpoint(String endpoint, String name, String userName, String password, boolean verify) throws IOException {
    if (name == null && System.getenv(Util.STREAMS_INSTANCE_ID) == null) {
        // StandaloneAuthenticator, needs resources endpoint.
        if (endpoint == null) {
            endpoint = Util.getenv(Util.STREAMS_BUILD_URL);
        }
        URL url = new URL(endpoint);
        // TODO: standalone: Not tested
        String path;
        if (url.getPath().startsWith("/streams/rest/")) {
            // internal URL, CPD < 3.5
            // https://build-sample-streams-build.ivan34:8445/streams/rest/builds
            // https://build-sample-streams-build.ivan34:8445/streams/rest/resources
            path = url.getPath().replaceFirst("/builds[/]?$", "/resources");
        } else if (url.getPath().startsWith("/streams/v1/")) {
            // internal URL, CPD >= 3.5
            // https://build-sample-streams-build.nbgf2:8445/streams/v1/builds
            // https://build-sample-streams-build.nbgf2:8445/streams/v1/roots
            path = url.getPath().replaceFirst("/builds[/]?$", "/roots");
        } else {
            throw new RESTException("build endpoint '" + endpoint + "' cannot be transformed to build resources URL");
        }
        URL resourcesUrl = new URL(url.getProtocol(), url.getHost(), url.getPort(), path);
        String resourcesEndpoint = resourcesUrl.toExternalForm();
        StandaloneAuthenticator auth = StandaloneAuthenticator.of(resourcesEndpoint, userName, password);
        JsonObject serviceDefinition = auth.config(verify);
        if (serviceDefinition == null) {
            // user and password are required, and endpoint is builds path
            if (userName == null || password == null) {
                String[] values = Util.getDefaultUserPassword(userName, password);
                userName = values[0];
                password = values[1];
            }
            String basicAuth = RestUtils.createBasicAuth(userName, password);
            String buildsEndpoint = endpoint;
            if (!buildsEndpoint.endsWith(STREAMS_BUILD_PATH)) {
                URL buildUrl = new URL(url.getProtocol(), url.getHost(), url.getPort(), STREAMS_BUILD_PATH);
                buildsEndpoint = buildUrl.toExternalForm();
            }
            return StreamsBuildService.of(e -> basicAuth, buildsEndpoint, verify);
        }
        return StreamsBuildService.of(auth, serviceDefinition, verify);
    } else {
        ICP4DAuthenticator auth = ICP4DAuthenticator.of(endpoint, name, userName, password);
        return StreamsBuildService.of(auth, auth.config(verify), verify);
    }
}
Also used : RESTException(com.ibm.streamsx.rest.RESTException) ICP4DAuthenticator(com.ibm.streamsx.rest.internal.ICP4DAuthenticator) JsonObject(com.google.gson.JsonObject) URL(java.net.URL) StandaloneAuthenticator(com.ibm.streamsx.rest.internal.StandaloneAuthenticator)

Example 5 with StandaloneAuthenticator

use of com.ibm.streamsx.rest.internal.StandaloneAuthenticator in project streamsx.topology by IBMStreams.

the class StreamsBuildService method of.

static BuildService of(Function<Executor, String> authenticator, JsonObject serviceDefinition, boolean verify) throws IOException {
    String buildServiceEndpoint = jstring(object(serviceDefinition, "connection_info"), "serviceBuildEndpoint");
    String buildServicePoolsEndpoint = jstring(object(serviceDefinition, "connection_info"), "serviceBuildPoolsEndpoint");
    // buildServicePoolsEndpoint is null when "connection_info" JSON element has no "serviceBuildPoolsEndpoint"
    if (authenticator instanceof StandaloneAuthenticator) {
        if (buildServiceEndpoint == null) {
            buildServiceEndpoint = Util.getenv(Util.STREAMS_BUILD_URL);
        }
        if (!buildServiceEndpoint.endsWith(STREAMS_BUILD_PATH)) {
            // URL was user-provided root of service, add the path
            URL url = new URL(buildServiceEndpoint);
            URL buildsUrl = new URL(url.getProtocol(), url.getHost(), url.getPort(), STREAMS_BUILD_PATH);
            buildServiceEndpoint = buildsUrl.toExternalForm();
        }
        return StreamsBuildService.of(authenticator, buildServiceEndpoint, verify);
    }
    return new StreamsBuildService(buildServiceEndpoint, buildServicePoolsEndpoint, authenticator, verify);
}
Also used : StandaloneAuthenticator(com.ibm.streamsx.rest.internal.StandaloneAuthenticator) URL(java.net.URL)

Aggregations

StandaloneAuthenticator (com.ibm.streamsx.rest.internal.StandaloneAuthenticator)5 JsonObject (com.google.gson.JsonObject)3 ICP4DAuthenticator (com.ibm.streamsx.rest.internal.ICP4DAuthenticator)3 URL (java.net.URL)3 JsonParser (com.google.gson.JsonParser)2 Instance (com.ibm.streamsx.rest.Instance)2 StreamsConnection (com.ibm.streamsx.rest.StreamsConnection)2 Executor (org.apache.http.client.fluent.Executor)2 RESTException (com.ibm.streamsx.rest.RESTException)1 StreamsKeys.getStreamsInstanceURL (com.ibm.streamsx.topology.internal.context.streamsrest.StreamsKeys.getStreamsInstanceURL)1