use of com.ibm.streamsx.rest.internal.ICP4DAuthenticator in project streamsx.topology by IBMStreams.
the class Instance method createIntegratedConnection.
/*
* Create a connection to an instance with an integrated configuration,
* using CP4D authentication.
*/
private static StreamsConnection createIntegratedConnection(String endpoint, String name, String userName, String password, boolean verify) throws IOException {
ICP4DAuthenticator authenticator = ICP4DAuthenticator.of(endpoint, name, userName, password);
JsonObject deploy = new JsonObject();
deploy.add(StreamsKeys.SERVICE_DEFINITION, authenticator.config(verify));
URL instanceUrl = new URL(getStreamsInstanceURL(deploy));
URL restUrl;
final String restResourcesUrl = StreamsKeys.getStreamsRestResourcesUrl(deploy);
if (restResourcesUrl != null) {
// here we end in CPD >= 3.0
restUrl = new URL(restResourcesUrl);
} else {
// legacy way
restUrl = new URL(instanceUrl.getProtocol(), instanceUrl.getHost(), instanceUrl.getPort(), STREAMS_REST_RESOURCES);
}
return StreamsConnection.ofAuthenticator(restUrl.toExternalForm(), authenticator);
}
use of com.ibm.streamsx.rest.internal.ICP4DAuthenticator 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);
}
}
use of com.ibm.streamsx.rest.internal.ICP4DAuthenticator 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);
}
}
use of com.ibm.streamsx.rest.internal.ICP4DAuthenticator 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);
}
}
Aggregations