AIRAVATA-2402 For testing migration, code to recreate a tenant
Project: http://git-wip-us.apache.org/repos/asf/airavata/repo Commit: http://git-wip-us.apache.org/repos/asf/airavata/commit/020ecaeb Tree: http://git-wip-us.apache.org/repos/asf/airavata/tree/020ecaeb Diff: http://git-wip-us.apache.org/repos/asf/airavata/diff/020ecaeb Branch: refs/heads/develop Commit: 020ecaeb6a2ebbcd8557a7d952ec2efdd8523410 Parents: c076a56 Author: Marcus Christie <[email protected]> Authored: Wed May 31 17:16:56 2017 -0400 Committer: Marcus Christie <[email protected]> Committed: Wed May 31 17:16:56 2017 -0400 ---------------------------------------------------------------------- .../client/ProfileServiceClientFactory.java | 15 ++++ .../airavata/KeycloakTenantCreationManager.java | 92 ++++++++++++++++++++ 2 files changed, 107 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/airavata/blob/020ecaeb/airavata-services/profile-service/profile-service-stubs/src/main/java/org/apache/airavata/service/profile/client/ProfileServiceClientFactory.java ---------------------------------------------------------------------- diff --git a/airavata-services/profile-service/profile-service-stubs/src/main/java/org/apache/airavata/service/profile/client/ProfileServiceClientFactory.java b/airavata-services/profile-service/profile-service-stubs/src/main/java/org/apache/airavata/service/profile/client/ProfileServiceClientFactory.java index 8a74d25..f41bc77 100644 --- a/airavata-services/profile-service/profile-service-stubs/src/main/java/org/apache/airavata/service/profile/client/ProfileServiceClientFactory.java +++ b/airavata-services/profile-service/profile-service-stubs/src/main/java/org/apache/airavata/service/profile/client/ProfileServiceClientFactory.java @@ -20,6 +20,9 @@ */ package org.apache.airavata.service.profile.client; +import org.apache.airavata.service.profile.iam.admin.services.cpi.IamAdminServices; +import org.apache.airavata.service.profile.iam.admin.services.cpi.exception.IamAdminServicesException; +import org.apache.airavata.service.profile.iam.admin.services.cpi.iam_admin_services_cpiConstants; import org.apache.airavata.service.profile.tenant.cpi.TenantProfileService; import org.apache.airavata.service.profile.tenant.cpi.exception.TenantProfileServiceException; import org.apache.airavata.service.profile.tenant.cpi.profile_tenant_cpiConstants; @@ -60,4 +63,16 @@ public class ProfileServiceClientFactory { throw new TenantProfileServiceException(e.getMessage()); } } + + public static IamAdminServices.Client createIamAdminServiceClient(String serverHost, int serverPort) throws IamAdminServicesException { + try { + TTransport transport = new TSocket(serverHost, serverPort); + transport.open(); + TProtocol protocol = new TBinaryProtocol(transport); + TMultiplexedProtocol multiplexedProtocol = new TMultiplexedProtocol(protocol, iam_admin_services_cpiConstants.IAM_ADMIN_SERVICES_CPI_NAME); + return new IamAdminServices.Client(multiplexedProtocol); + } catch (TTransportException e) { + throw new IamAdminServicesException(e.getMessage()); + } + } } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/airavata/blob/020ecaeb/modules/user-profile-migration/src/main/java/org/apache/airavata/KeycloakTenantCreationManager.java ---------------------------------------------------------------------- diff --git a/modules/user-profile-migration/src/main/java/org/apache/airavata/KeycloakTenantCreationManager.java b/modules/user-profile-migration/src/main/java/org/apache/airavata/KeycloakTenantCreationManager.java new file mode 100644 index 0000000..b166a27 --- /dev/null +++ b/modules/user-profile-migration/src/main/java/org/apache/airavata/KeycloakTenantCreationManager.java @@ -0,0 +1,92 @@ +package org.apache.airavata; + +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ + +import org.apache.airavata.model.credential.store.PasswordCredential; +import org.apache.airavata.model.security.AuthzToken; +import org.apache.airavata.model.workspace.Gateway; +import org.apache.airavata.model.workspace.GatewayApprovalStatus; +import org.apache.airavata.service.profile.client.ProfileServiceClientFactory; +import org.apache.airavata.service.profile.iam.admin.services.cpi.IamAdminServices; +import org.apache.airavata.service.profile.iam.admin.services.cpi.exception.IamAdminServicesException; +import org.apache.thrift.TException; + +public class KeycloakTenantCreationManager { + + private String profileServiceServerHost = "localhost"; + private int profileServiceServerPort = 8962; + private String masterAdminUsername = "admin"; + private String masterAdminPassword = "password"; + + private IamAdminServices.Client iamAdminServiceClient = null; + + public void createTenant(Gateway gateway) { + PasswordCredential passwordCredential = getPasswordCredential(); + try { + // TODO: replace with real authz token? + getIamAdminServiceClient().setUpGateway(new AuthzToken("empty"), gateway, passwordCredential); + } catch (TException e) { + throw new RuntimeException(e); + } + } + + private IamAdminServices.Client getIamAdminServiceClient() { + if (iamAdminServiceClient == null) { + try { + iamAdminServiceClient = ProfileServiceClientFactory.createIamAdminServiceClient(this.profileServiceServerHost, this.profileServiceServerPort); + } catch (IamAdminServicesException e) { + throw new RuntimeException(e); + } + } + return iamAdminServiceClient; + } + + private PasswordCredential getPasswordCredential() { + PasswordCredential passwordCredential = new PasswordCredential(); + passwordCredential.setGatewayId("dummy"); + passwordCredential.setPortalUserName("dummy"); + passwordCredential.setLoginUserName(masterAdminUsername); + passwordCredential.setPassword(masterAdminPassword); + return passwordCredential; + } + + public static void main(String[] args) { + + // Configuration ... + KeycloakTenantCreationManager keycloakTenantCreationManager = new KeycloakTenantCreationManager(); + keycloakTenantCreationManager.masterAdminUsername = ""; + keycloakTenantCreationManager.masterAdminPassword = ""; + keycloakTenantCreationManager.profileServiceServerHost = ""; + + Gateway gateway = new Gateway(); + gateway.setGatewayId(""); + gateway.setGatewayApprovalStatus(GatewayApprovalStatus.CREATED); + gateway.setGatewayName(""); + gateway.setIdentityServerUserName(""); + gateway.setGatewayAdminFirstName(""); + gateway.setGatewayAdminLastName(""); + gateway.setGatewayAdminEmail(""); + gateway.setGatewayURL(""); + + keycloakTenantCreationManager.createTenant(gateway); + } +}
