Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id D8493200C77 for ; Mon, 1 May 2017 22:13:05 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id D6D4A160BAE; Mon, 1 May 2017 20:13:05 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 02468160BA0 for ; Mon, 1 May 2017 22:13:04 +0200 (CEST) Received: (qmail 95711 invoked by uid 500); 1 May 2017 20:13:04 -0000 Mailing-List: contact issues-help@airavata.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Delivered-To: mailing list issues@airavata.apache.org Received: (qmail 95699 invoked by uid 99); 1 May 2017 20:13:03 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 01 May 2017 20:13:03 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id D7C45DFB8A; Mon, 1 May 2017 20:13:03 +0000 (UTC) From: machristie To: issues@airavata.apache.org Reply-To: issues@airavata.apache.org References: In-Reply-To: Subject: [GitHub] airavata pull request #108: Identity Server Admin Services Content-Type: text/plain Message-Id: <20170501201303.D7C45DFB8A@git1-us-west.apache.org> Date: Mon, 1 May 2017 20:13:03 +0000 (UTC) archived-at: Mon, 01 May 2017 20:13:06 -0000 Github user machristie commented on a diff in the pull request: https://github.com/apache/airavata/pull/108#discussion_r114191738 --- Diff: airavata-services/profile-service/iam-admin-services-core/src/main/java/org/apache/airavata/service/profile/iam/admin/services/core/impl/TenantManagementKeycloakImpl.java --- @@ -0,0 +1,254 @@ +/* + * + * 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. + * + */ + +package org.apache.airavata.service.profile.iam.admin.services.core.impl; + +import org.apache.airavata.common.exception.ApplicationSettingsException; +import org.apache.airavata.common.utils.ServerSettings; +import org.apache.airavata.model.credential.store.PasswordCredential; +import org.apache.airavata.model.user.UserProfile; +import org.apache.airavata.model.workspace.Gateway; +import org.apache.airavata.service.profile.iam.admin.services.core.interfaces.TenantManagementInterface; +import org.apache.airavata.service.profile.iam.admin.services.cpi.exception.IamAdminServicesException; +import org.keycloak.admin.client.Keycloak; +import org.keycloak.admin.client.resource.UserResource; +import org.keycloak.representations.idm.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import javax.ws.rs.core.Response; +import java.util.ArrayList; +import java.util.List; + +public class TenantManagementKeycloakImpl implements TenantManagementInterface { + + private final static Logger logger = LoggerFactory.getLogger(TenantManagementKeycloakImpl.class); + + private static Keycloak getClient(String adminUrl, String realm, PasswordCredential AdminPasswordCreds) { + + return Keycloak.getInstance( + adminUrl, + realm, // the realm to log in to + AdminPasswordCreds.getLoginUserName(), AdminPasswordCreds.getPassword(), // the user + "admin-cli"); // admin-cli is the client ID used for keycloak admin operations. + } + + @Override + public Gateway addTenant(PasswordCredential isSuperAdminPasswordCreds, Gateway gatewayDetails) throws IamAdminServicesException { + try { + // get client + Keycloak client = TenantManagementKeycloakImpl.getClient(ServerSettings.getIamServerUrl(), "master", isSuperAdminPasswordCreds); + // create realm + RealmRepresentation newRealmDetails = new RealmRepresentation(); + newRealmDetails.setEnabled(true); + newRealmDetails.setId(gatewayDetails.getGatewayId()); + newRealmDetails.setDisplayName(gatewayDetails.getGatewayName()); + newRealmDetails.setRealm(gatewayDetails.getGatewayId()); + RealmRepresentation realmWithRoles = TenantManagementKeycloakImpl.createDefaultRoles(newRealmDetails); + client.realms().create(realmWithRoles); + return gatewayDetails; + } catch (ApplicationSettingsException ex) { + logger.error("Error getting values from property file, reason: " + ex.getCause(), ex); + IamAdminServicesException exception = new IamAdminServicesException(); + exception.setMessage("Error getting Iam server Url from property file, reason: " + ex.getMessage()); + throw exception; + } catch (Exception ex){ + logger.error("Error creating Realm in Keycloak Server, reason: " + ex.getCause(), ex); + IamAdminServicesException exception = new IamAdminServicesException(); + exception.setMessage("Error creating Realm in Keycloak Server, reason: " + ex.getMessage()); + throw exception; + } + } + + public static RealmRepresentation createDefaultRoles(RealmRepresentation realmDetails){ + List defaultRoles = new ArrayList(); + RoleRepresentation adminRole = new RoleRepresentation(); + adminRole.setName("admin"); + adminRole.setDescription("Admin role for PGA users"); + defaultRoles.add(adminRole); + RoleRepresentation adminReadOnlyRole = new RoleRepresentation(); + adminReadOnlyRole.setName("admin-read-only"); + adminReadOnlyRole.setDescription("Read only role for PGA Admin users"); + defaultRoles.add(adminReadOnlyRole); + RoleRepresentation gatewayUserRole = new RoleRepresentation(); + gatewayUserRole.setName("gateway-user"); + gatewayUserRole.setDescription("default role for PGA users"); + defaultRoles.add(gatewayUserRole); + RolesRepresentation rolesRepresentation = new RolesRepresentation(); + rolesRepresentation.setRealm(defaultRoles); + realmDetails.setRoles(rolesRepresentation); + return realmDetails; + } + + @Override + public boolean createTenantAdminAccount(PasswordCredential isSuperAdminPasswordCreds, Gateway gatewayDetails) throws IamAdminServicesException{ + try{ + Keycloak client = TenantManagementKeycloakImpl.getClient(ServerSettings.getIamServerUrl(), "master", isSuperAdminPasswordCreds); + UserRepresentation user = new UserRepresentation(); + user.setUsername(gatewayDetails.getIdentityServerUserName()); + user.setFirstName(gatewayDetails.getGatewayAdminFirstName()); + user.setLastName(gatewayDetails.getGatewayAdminLastName()); + user.setEmail(gatewayDetails.getGatewayAdminEmail()); + user.setEnabled(true); + List requiredActionList = new ArrayList<>(); + requiredActionList.add("UPDATE_PASSWORD"); + user.setRequiredActions(requiredActionList); + Response httpResponse = client.realm(gatewayDetails.getGatewayId()).users().create(user); + logger.info("Tenant Admin account creation exited with code : " + httpResponse.getStatus()+" : " +httpResponse.getStatusInfo()); + if (httpResponse.getStatus() == 201) { //HTTP code for record creation: HTTP 201 + List retrieveCreatedUserList = client.realm(gatewayDetails.getGatewayId()).users().search(user.getUsername(), + user.getFirstName(), + user.getLastName(), + user.getEmail(), + 0, 1); + UserResource retrievedUser = client.realm(gatewayDetails.getGatewayId()).users().get(retrieveCreatedUserList.get(0).getId()); + CredentialRepresentation credential = new CredentialRepresentation(); + credential.setType(CredentialRepresentation.PASSWORD); + credential.setValue(ServerSettings.getGatewayAdminTempPwd()); + credential.setTemporary(true); + retrievedUser.resetPassword(credential); + List realmClients = client.realm(gatewayDetails.getGatewayId()).clients().findAll(); + String realmManagementClientId=null; + for(ClientRepresentation realmClient : realmClients){ + if(realmClient.getClientId().equals("realm-management")){ + realmManagementClientId = realmClient.getId(); + } + } + retrievedUser.roles().clientLevel(realmManagementClientId).add(retrievedUser.roles().clientLevel(realmManagementClientId).listAvailable()); + return true; + } else { + logger.error("Request for Tenant Admin Account Creation failed with HTTP code : " + httpResponse.getStatus()); + logger.error("Reason for Tenant Admin account creation failure : " + httpResponse.getStatusInfo()); + return false; + } + }catch (ApplicationSettingsException ex) { + logger.error("Error getting values from property file, reason: " + ex.getCause(), ex); + IamAdminServicesException exception = new IamAdminServicesException(); + exception.setMessage("Error getting values from property file, reason " + ex.getMessage()); + throw exception; + }catch (Exception ex){ + logger.error("Error creating Realm Admin Account in keycloak server, reason: " + ex.getCause(), ex); + IamAdminServicesException exception = new IamAdminServicesException(); + exception.setMessage("Error creating Realm Admin Account in keycloak server, reason: " + ex.getMessage()); + throw exception; + } + } + + @Override + public Gateway configureClient(PasswordCredential isSuperAdminPasswordCreds, Gateway gatewayDetails) throws IamAdminServicesException{ + try{ + Keycloak client = TenantManagementKeycloakImpl.getClient(ServerSettings.getIamServerUrl(), "master", isSuperAdminPasswordCreds); + ClientRepresentation pgaClient = new ClientRepresentation(); + pgaClient.setName("pga"); + pgaClient.setClientId("pga"); + pgaClient.setProtocol("openid-connect"); + pgaClient.setStandardFlowEnabled(true); + pgaClient.setEnabled(true); + pgaClient.setAuthorizationServicesEnabled(true); + pgaClient.setDirectAccessGrantsEnabled(true); + pgaClient.setServiceAccountsEnabled(true); + pgaClient.setFullScopeAllowed(true); + pgaClient.setClientAuthenticatorType("client-secret"); + String[] defaultRoles = {"gateway-user"}; + pgaClient.setDefaultRoles(defaultRoles); + List redirectUris = new ArrayList<>(); + redirectUris.add("http://accord.scigap.org/callback-url"); --- End diff -- I'm not familiar with how this is used, so I can't say. I think we can assume so for now and we can adjust it later as necessary. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---