You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@airavata.apache.org by anujbhan <gi...@git.apache.org> on 2017/05/01 19:17:23 UTC

[GitHub] airavata pull request #108: Identity Server Admin Services

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

    https://github.com/apache/airavata/pull/108#discussion_r114181208
  
    --- 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");
    --- End diff --
    
    @marcus, 
    This admin account is created passively, i.e, it is created automatically when a new tenant is created and a temporary password is assigned to admin (from airavata property file). Upon first login, he has to update it.


---
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.
---