use of org.apache.airavata.service.profile.iam.admin.services.cpi.exception.IamAdminServicesException in project airavata by apache.
the class TenantManagementKeycloakImpl method addTenant.
@Override
public Gateway addTenant(PasswordCredential isSuperAdminPasswordCreds, Gateway gatewayDetails) throws IamAdminServicesException {
Keycloak client = null;
try {
// get client
client = TenantManagementKeycloakImpl.getClient(ServerSettings.getIamServerUrl(), this.superAdminRealmId, isSuperAdminPasswordCreds);
// create realm
RealmRepresentation newRealmDetails = new RealmRepresentation();
newRealmDetails.setEnabled(true);
newRealmDetails.setId(gatewayDetails.getGatewayId());
newRealmDetails.setDisplayName(gatewayDetails.getGatewayName());
newRealmDetails.setRealm(gatewayDetails.getGatewayId());
// Following two settings allow duplicate email addresses
newRealmDetails.setLoginWithEmailAllowed(false);
newRealmDetails.setDuplicateEmailsAllowed(true);
// Default access token lifespan to 30 minutes, SSO session idle to 60 minutes
newRealmDetails.setAccessTokenLifespan(1800);
newRealmDetails.setSsoSessionIdleTimeout(3600);
RealmRepresentation realmWithRoles = TenantManagementKeycloakImpl.createDefaultRoles(newRealmDetails);
client.realms().create(realmWithRoles);
return gatewayDetails;
} catch (ApplicationSettingsException ex) {
logger.error("Error getting values from property file, reason: " + ex.getMessage(), 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.getMessage(), ex);
IamAdminServicesException exception = new IamAdminServicesException();
exception.setMessage("Error creating Realm in Keycloak Server, reason: " + ex.getMessage());
throw exception;
} finally {
if (client != null) {
client.close();
}
}
}
use of org.apache.airavata.service.profile.iam.admin.services.cpi.exception.IamAdminServicesException in project airavata by apache.
the class TenantManagementKeycloakImpl method getUsersWithRole.
@Override
public List<UserProfile> getUsersWithRole(PasswordCredential realmAdminCreds, String tenantId, String roleName) throws IamAdminServicesException {
Keycloak client = null;
try {
client = TenantManagementKeycloakImpl.getClient(ServerSettings.getIamServerUrl(), tenantId, realmAdminCreds);
// FIXME: this only searches through the most recent 100 users for the given role (assuming there are no more than 10,000 users in the gateway)
int totalUserCount = client.realm(tenantId).users().count();
logger.debug("getUsersWithRole: totalUserCount=" + totalUserCount);
// Load all users in batches
List<UserRepresentation> allUsers = new ArrayList<>();
int userBatchSize = 100;
for (int start = 0; start < totalUserCount; start = start + userBatchSize) {
logger.debug("getUsersWithRole: fetching " + userBatchSize + " users...");
allUsers.addAll(client.realm(tenantId).users().search(null, null, null, null, start, userBatchSize));
}
logger.debug("getUsersWithRole: all users count=" + allUsers.size());
allUsers.sort((a, b) -> a.getCreatedTimestamp() - b.getCreatedTimestamp() > 0 ? -1 : 1);
// The 100 most recently created users
List<UserRepresentation> mostRecentUsers = allUsers.subList(0, Math.min(allUsers.size(), 100));
logger.debug("getUsersWithRole: most recent users count=" + mostRecentUsers.size());
List<UserProfile> usersWithRole = new ArrayList<>();
for (UserRepresentation user : mostRecentUsers) {
UserResource userResource = client.realm(tenantId).users().get(user.getId());
List<RoleRepresentation> roleRepresentations = userResource.roles().realmLevel().listAll();
for (RoleRepresentation roleRepresentation : roleRepresentations) {
if (roleRepresentation.getName().equals(roleName)) {
usersWithRole.add(convertUserRepresentationToUserProfile(user, tenantId));
break;
}
}
}
logger.debug("getUsersWithRole: most recent users with role count=" + usersWithRole.size());
return usersWithRole;
} catch (ApplicationSettingsException ex) {
logger.error("Error getting values from property file, reason: " + ex.getMessage(), ex);
IamAdminServicesException exception = new IamAdminServicesException();
exception.setMessage("Error getting values from property file, reason " + ex.getMessage());
throw exception;
} finally {
if (client != null) {
logger.debug("getUsersWithRole: closing client...");
client.close();
logger.debug("getUsersWithRole: client closed");
}
}
}
use of org.apache.airavata.service.profile.iam.admin.services.cpi.exception.IamAdminServicesException in project airavata by apache.
the class IamAdminServicesHandler method getUsersWithRole.
@Override
@SecurityCheck
public List<UserProfile> getUsersWithRole(AuthzToken authzToken, String roleName) throws IamAdminServicesException, AuthorizationException, TException {
TenantManagementKeycloakImpl keycloakclient = new TenantManagementKeycloakImpl();
String gatewayId = authzToken.getClaimsMap().get(Constants.GATEWAY_ID);
try {
PasswordCredential isRealmAdminCredentials = getTenantAdminPasswordCredential(gatewayId);
return keycloakclient.getUsersWithRole(isRealmAdminCredentials, gatewayId, roleName);
} catch (Exception ex) {
String msg = "Error while retrieving users with role, reason: " + ex.getMessage();
logger.error(msg, ex);
throw new IamAdminServicesException(msg);
}
}
use of org.apache.airavata.service.profile.iam.admin.services.cpi.exception.IamAdminServicesException in project airavata by apache.
the class IamAdminServicesHandler method enableUser.
@Override
@SecurityCheck
public boolean enableUser(AuthzToken authzToken, String username) throws IamAdminServicesException, AuthorizationException {
TenantManagementKeycloakImpl keycloakclient = new TenantManagementKeycloakImpl();
String gatewayId = authzToken.getClaimsMap().get(Constants.GATEWAY_ID);
try {
PasswordCredential isRealmAdminCredentials = getTenantAdminPasswordCredential(gatewayId);
if (keycloakclient.enableUserAccount(isRealmAdminCredentials, gatewayId, username))
return true;
else
return false;
} catch (TException | ApplicationSettingsException ex) {
String msg = "Error while enabling user account, reason: " + ex.getMessage();
logger.error(msg, ex);
throw new IamAdminServicesException(msg);
}
}
use of org.apache.airavata.service.profile.iam.admin.services.cpi.exception.IamAdminServicesException in project airavata by apache.
the class IamAdminServicesHandler method addRoleToUser.
@Override
@SecurityCheck
public boolean addRoleToUser(AuthzToken authzToken, String username, String roleName) throws IamAdminServicesException, AuthorizationException, TException {
TenantManagementKeycloakImpl keycloakclient = new TenantManagementKeycloakImpl();
String gatewayId = authzToken.getClaimsMap().get(Constants.GATEWAY_ID);
try {
PasswordCredential isRealmAdminCredentials = getTenantAdminPasswordCredential(gatewayId);
return keycloakclient.addRoleToUser(isRealmAdminCredentials, gatewayId, username, roleName);
} catch (TException | ApplicationSettingsException ex) {
String msg = "Error while adding role to user, reason: " + ex.getMessage();
logger.error(msg, ex);
throw new IamAdminServicesException(msg);
}
}
Aggregations