Github user anujbhan commented on a diff in the pull request:

    https://github.com/apache/airavata/pull/108#discussion_r114189806
  
    --- 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<RoleRepresentation> defaultRoles = new 
ArrayList<RoleRepresentation>();
    +        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<String> 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<UserRepresentation> 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<ClientRepresentation> 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<String> redirectUris = new ArrayList<>();
    +            redirectUris.add("http://accord.scigap.org/callback-url";);
    --- End diff --
    
    @marcus,
    The Gateway profile already has this GatewayUrl field,
    [Gateway 
Profile](https://github.com/apache/airavata/blob/master/thrift-interface-descriptions/data-models/experiment-catalog-models/workspace_model.thrift)
    ```
    struct Gateway {
        1: required string gatewayId,
        2: required GatewayApprovalStatus gatewayApprovalStatus,
        3: optional string gatewayName,
        4: optional string domain,
        5: optional string emailAddress
        6: optional string gatewayAcronym,
        7: optional string gatewayURL,
        8: optional string gatewayPublicAbstract,
        9: optional string reviewProposalDescription,
        10: optional string gatewayAdminFirstName,
        11: optional string gatewayAdminLastName,
        12: optional string gatewayAdminEmail,
        13: optional string identityServerUserName,
        14: optional string identityServerPasswordToken,
        15: optional string declinedReason,
        16: optional string oauthClientId,
        17: optional string oauthClientSecret,
        18: optional i64 requestCreationTime,
        19: optional string requesterUsername
    }
    ```
    
    Can i use it to construct callback url. i.e, append callback-url to that 
field ?


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to