Verified Commit f8e91fae authored by Julien Boubechtoula's avatar Julien Boubechtoula 🤞🏼

Feature CatalogManager

parent d2d0d5b2
target
*.iml
.project
.settings
.classpath
target
......@@ -39,10 +39,10 @@ objects:
fieldRef:
apiVersion: v1
fieldPath: metadata.namespace
- name: KEYCLOAK_AUTH_URL
- name: KEYCLOAK_ADMIN_REALM
valueFrom:
configMapKeyRef:
key: KEYCLOAK_AUTH_URL
key: KEYCLOAK_ADMIN_REALM
name: keycloak-config
- name: KEYCLOAK_REALM
valueFrom:
......@@ -52,33 +52,43 @@ objects:
- name: KEYCLOAK_ADMIN_AUTH_URL
valueFrom:
configMapKeyRef:
key: KEYCLOAK_AUTH_URL
key: KEYCLOAK_ADMIN_AUTH_URL
name: keycloak-config
- name: KEYCLOAK_ADMIN_REALM
- name: KEYCLOAK_ADMIN_CLIENT_ID
valueFrom:
configMapKeyRef:
key: KEYCLOAK_REALM
key: KEYCLOAK_ADMIN_CLIENT_ID
name: keycloak-config
- name: KEYCLOAK_ADMIN_CLIENT_ID
- name: KEYCLOAK_ADMIN_USERNAME
valueFrom:
secretKeyRef:
key: KEYCLOAK_ADMIN_CLIENT_ID
key: KEYCLOAK_ADMIN_USERNAME
name: kathra-usermanager-secret
- name: KEYCLOAK_ADMIN_CLIENT_SECRET
- name: KEYCLOAK_ADMIN_PASSWORD
valueFrom:
secretKeyRef:
key: KEYCLOAK_ADMIN_CLIENT_SECRET
key: KEYCLOAK_ADMIN_PASSWORD
name: kathra-usermanager-secret
- name: KEYCLOAK_AUTH_URL
valueFrom:
configMapKeyRef:
key: KEYCLOAK_AUTH_URL
name: keycloak-config
- name: KEYCLOAK_REALM
valueFrom:
configMapKeyRef:
key: KEYCLOAK_REALM
name: keycloak-config
- name: KEYCLOAK_CLIENT_ID
valueFrom:
secretKeyRef:
key: KEYCLOAK_CLIENT_ID
name: kathra-keycloak-secret
name: keycloak-secret
- name: KEYCLOAK_CLIENT_SECRET
valueFrom:
secretKeyRef:
key: KEYCLOAK_CLIENT_SECRET
name: kathra-keycloak-secret
name: keycloak-secret
imagePullSecrets:
- name: regcred
- kind: Service
......
......@@ -66,6 +66,11 @@
<version>2.19</version>
<configuration>
<environmentVariables>
<KEYCLOAK_REALM>null</KEYCLOAK_REALM>
<KEYCLOAK_ADMIN_USERNAME>null</KEYCLOAK_ADMIN_USERNAME>
<KEYCLOAK_ADMIN_PASSWORD>null</KEYCLOAK_ADMIN_PASSWORD>
<KEYCLOAK_ADMIN_REALM>null</KEYCLOAK_ADMIN_REALM>
<KEYCLOAK_ADMIN_AUTH_URL>null</KEYCLOAK_ADMIN_AUTH_URL>
<KEYCLOAK_ADMIN_CLIENT_ID>null</KEYCLOAK_ADMIN_CLIENT_ID>
<KEYCLOAK_ADMIN_CLIENT_SECRET>null</KEYCLOAK_ADMIN_CLIENT_SECRET>
</environmentVariables>
......
......@@ -30,14 +30,18 @@ public class Config extends ConfigManager {
private String keycloakAuthUrl;
private String keycloakRealm;
private String keycloakRealmAdmin;
private String keycloakClientId;
private String keycloakClientSecret;
private String keycloakUsername;
private String keycloakPassword;
public Config() {
keycloakAuthUrl = getProperty("KEYCLOAK_ADMIN_AUTH_URL", "https://keycloak.dev-irtsysx.fr/auth");
keycloakRealm = getProperty("KEYCLOAK_ADMIN_REALM", "kathra");
keycloakAuthUrl = getProperty("KEYCLOAK_ADMIN_AUTH_URL");
keycloakRealmAdmin = getProperty("KEYCLOAK_ADMIN_REALM");
keycloakRealm = getProperty("KEYCLOAK_REALM");
keycloakClientId = getProperty("KEYCLOAK_ADMIN_CLIENT_ID");
keycloakClientSecret = getProperty("KEYCLOAK_ADMIN_CLIENT_SECRET");
keycloakUsername = getProperty("KEYCLOAK_ADMIN_USERNAME");
keycloakPassword = getProperty("KEYCLOAK_ADMIN_PASSWORD");
}
public String getKeycloakAuthUrl() {
......@@ -52,7 +56,16 @@ public class Config extends ConfigManager {
return keycloakClientId;
}
public String getKeycloakClientSecret() {
return keycloakClientSecret;
public String getKeycloakUsername() {
return keycloakUsername;
}
public String getKeycloakPassword() {
return keycloakPassword;
}
public String getKeycloakRealmAdmin() {
return keycloakRealmAdmin;
}
}
/*
/*
* Copyright 2019 The Kathra Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
......@@ -15,7 +15,7 @@
*
* Contributors:
*
* IRT SystemX (https://www.kathra.org/)
* IRT SystemX (https://www.kathra.org/)
*
*/
package org.kathra.usermanager.controller;
......@@ -27,6 +27,7 @@ import org.kathra.usermanager.Config;
import org.kathra.usermanager.service.UserManagerService;
import org.kathra.usermanager.services.KeycloakService;
import org.apache.camel.cdi.ContextName;
import org.kathra.utils.KathraException;
import org.keycloak.representations.idm.GroupRepresentation;
import org.keycloak.representations.idm.UserRepresentation;
......@@ -45,25 +46,30 @@ public class UserManagerController implements UserManagerService {
config = new Config();
keycloakService = new KeycloakService(
config.getKeycloakAuthUrl(),
config.getKeycloakRealmAdmin(),
config.getKeycloakRealm(),
config.getKeycloakClientId(),
config.getKeycloakClientSecret());
config.getKeycloakUsername(),
config.getKeycloakPassword());
}
public UserManagerController(KeycloakService keycloakService) {
this.keycloakService = keycloakService;
}
private void populateGroup(List<Group> groups, List<GroupRepresentation> subGroups) {
subGroups.forEach(subGroupRepresentation -> {
subGroups.parallelStream().forEach(subGroupRepresentation -> {
Group group = new Group()
.id(subGroupRepresentation.getId())
.name(subGroupRepresentation.getName())
.path(subGroupRepresentation.getPath());
List<UserRepresentation> members = keycloakService.getGroupMembers(group.getId());
for (UserRepresentation member : members) {
Assignation userAssignation = new Assignation().id(member.getId()).name(member.getUsername());
List<UserRepresentation> members = keycloakService.getGroupMembers(subGroupRepresentation.getId());
members.parallelStream().forEach(member -> {
Assignation userAssignation = new Assignation().name(member.getUsername());
if (group.getMembers() == null || !group.getMembers().contains(userAssignation)) {
group.addMembersItem(userAssignation);
}
}
});
groups.add(group);
if (subGroupRepresentation.getSubGroups() != null)
populateGroup(groups, subGroupRepresentation.getSubGroups());
......@@ -71,13 +77,19 @@ public class UserManagerController implements UserManagerService {
);
}
@Override
public User assignUserToGroup(String userId, String groupPath) throws Exception {
keycloakService.addUserToGroup(getUser(userId), getGroup(groupPath));
return null;
}
/**
* Create a new group
*
* @return Group
*/
public Group createGroup() throws Exception {
return null;
public Group createGroup(Group group) throws Exception {
return keycloakService.createGroup(group);
}
/**
......@@ -85,7 +97,17 @@ public class UserManagerController implements UserManagerService {
*
* @return User
*/
public User createUser() throws Exception {
public User createUser(User user) throws Exception {
return keycloakService.createUser(user);
}
@Override
public Group deleteGroup(String groupPath) throws Exception {
return null;
}
@Override
public User deleteUser(String userId) throws Exception {
return null;
}
......@@ -96,7 +118,7 @@ public class UserManagerController implements UserManagerService {
* @return Group
*/
public Group getGroup(String groupPath) throws Exception {
return null;
return getGroups().parallelStream().filter(g -> g.getPath().equals(groupPath)).findFirst().orElseThrow( () -> new KathraException("Group not found", null, KathraException.ErrorCode.NOT_FOUND ));
}
/**
......@@ -111,6 +133,11 @@ public class UserManagerController implements UserManagerService {
return groups;
}
@Override
public List<Group> getGroupsAssignationsFromUser(String userId) throws Exception {
return keycloakService.getMemberGroups(getUser(userId));
}
/**
* Return user object
*
......@@ -118,7 +145,11 @@ public class UserManagerController implements UserManagerService {
* @return User
*/
public User getUser(String userId) throws Exception {
return null;
User user = keycloakService.getUser(userId);
if (user == null) {
throw new KathraException("User not found", null, KathraException.ErrorCode.NOT_FOUND);
}
return user;
}
/**
......@@ -127,6 +158,32 @@ public class UserManagerController implements UserManagerService {
* @return List<User>
*/
public List<User> getUsers() throws Exception {
return keycloakService.getUsers();
}
@Override
public Group patchGroup(String groupPath, Group group) throws Exception {
return null;
}
@Override
public User patchUser(String userId, User user) throws Exception {
return null;
}
@Override
public User unassignUserToGroup(String userId, String groupPath) throws Exception {
keycloakService.removeUserToGroup(getUser(userId), getGroup(groupPath));
return null;
}
@Override
public Group updateGroup(String groupPath, Group group) throws Exception {
return null;
}
@Override
public User updateUser(String userId, User user) throws Exception {
return null;
}
}
......@@ -21,15 +21,23 @@
package org.kathra.usermanager.services;
import com.google.common.collect.ImmutableList;
import jdk.jshell.spi.ExecutionControl;
import org.kathra.core.model.Group;
import org.kathra.core.model.User;
import org.keycloak.OAuth2Constants;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.admin.client.KeycloakBuilder;
import org.keycloak.admin.client.resource.GroupResource;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.representations.idm.CredentialRepresentation;
import org.keycloak.representations.idm.GroupRepresentation;
import org.keycloak.representations.idm.UserRepresentation;
import javax.ws.rs.core.Response;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* @author Jérémy Guillemot <[email protected]>
......@@ -39,15 +47,16 @@ public class KeycloakService {
private Keycloak keycloak;
RealmResource kathraRealm;
public KeycloakService(String url, String realm, String clientId, String clientSecret) {
this.keycloak = KeycloakBuilder.builder() //
.serverUrl(url) //
.grantType(OAuth2Constants.CLIENT_CREDENTIALS) //
.realm(realm) //
.clientId(clientId) //
.clientSecret(clientSecret) //
public KeycloakService(String url, String realmAdmin, String realmKathra, String clientId, String username, String password) {
this.keycloak = KeycloakBuilder.builder()
.serverUrl(url)
.grantType(OAuth2Constants.PASSWORD)
.realm(realmAdmin)
.clientId(clientId)
.username(username)
.password(password)
.build();
kathraRealm = keycloak.realm(realm);
kathraRealm = keycloak.realm(realmKathra);
}
public List<UserRepresentation> getGroupMembers(String groupId) {
......@@ -55,8 +64,80 @@ public class KeycloakService {
return groupResource.members(0, 1000);
}
public List<Group> getMemberGroups(User user) {
return kathraRealm.users().get(getUserRepresentation(user).getId()).groups().stream().map(groupR -> new Group()
.id(groupR.getId())
.name(groupR.getName())
.path(groupR.getPath())).collect(Collectors.toList());
}
public List<GroupRepresentation> getGroups() {
GroupResource kathraRootGroup = kathraRealm.groups().group(kathraRealm.getGroupByPath("kathra-projects").getId());
return kathraRootGroup.toRepresentation().getSubGroups();
}
public User createUser(User user) {
UserRepresentation userRepresentation = new UserRepresentation();
CredentialRepresentation passwordCred = new CredentialRepresentation();
passwordCred.setType(CredentialRepresentation.PASSWORD);
passwordCred.setValue(user.getPassword());
userRepresentation.setCredentials(ImmutableList.of(passwordCred));
userRepresentation.setEnabled(true);
userRepresentation.setFirstName(user.getFirstName());
userRepresentation.setLastName(user.getLastName());
userRepresentation.setEmail(user.getEmail());
userRepresentation.setEmailVerified(true);
userRepresentation.setUsername(user.getName());
Response response = kathraRealm.users().create(userRepresentation);
String userId = response.getLocation().getPath().replaceAll(".*/([^/]+)$", "$1");
//user.setProvider("Keycloak");
//user.setProviderId(userId);
return user;
}
public User addUserToGroup(User user, Group group) {
UserRepresentation userR = kathraRealm.users().list().stream().filter(u -> u.getUsername().equals(user.getName())).findFirst().get();
GroupRepresentation groupR = getGroups().stream().filter(u -> u.getPath().equals(group.getPath())).findFirst().get();
kathraRealm.users().get(userR.getId()).joinGroup(groupR.getId());
return user;
}
public User removeUserToGroup(User user, Group group) {
UserRepresentation userR = kathraRealm.users().list().stream().filter(u -> u.getUsername().equals(user.getName())).findFirst().get();
GroupRepresentation groupR = getGroups().stream().filter(u -> u.getPath().equals(group.getPath())).findFirst().get();
kathraRealm.users().get(userR.getId()).leaveGroup(groupR.getId());
return user;
}
public Group createGroup(Group group) {
GroupRepresentation groupRepresentation = new GroupRepresentation();
groupRepresentation.setName(group.getName());
groupRepresentation.setPath(group.getPath());
Response response = kathraRealm.groups().group(kathraRealm.getGroupByPath("kathra-projects").getId()).subGroup(groupRepresentation);
String groupId = response.getLocation().getPath().replaceAll(".*/([^/]+)$", "$1");
//group.providerId(groupId);
return group;
}
public User getUser(String userId) {
Optional<UserRepresentation> userR = kathraRealm.users().list().parallelStream().filter(u -> u.getUsername().equals(userId)).findFirst();
if (userR.isEmpty())
return null;
return new User().name(userR.get().getUsername()).lastName(userR.get().getLastName()).firstName(userR.get().getFirstName()).email(userR.get().getEmail());
}
private UserRepresentation getUserRepresentation(User user) {
return kathraRealm.users().list().parallelStream().filter(u -> u.getUsername().equals(user.getName())).findFirst().get();
}
public List<User> getUsers() {
return kathraRealm.users().list().parallelStream().map(user -> mapToUser(user)).collect(Collectors.toList());
}
private User mapToUser(UserRepresentation userR) {
return new User().name(userR.getUsername()).email(userR.getEmail()).firstName(userR.getFirstName()).lastName(userR.getLastName());
}
}
......@@ -87,8 +87,7 @@ public class UserManagerControllerTest {
Mockito.reset(keycloakService);
Mockito.when(keycloakService.getGroups()).thenReturn(keycloakGroups);
Mockito.when(keycloakService.getGroupMembers("subGroupId")).thenReturn(keycloakGroupUsers);
underTest = new UserManagerController();
underTest.keycloakService = keycloakService;
underTest = new UserManagerController(keycloakService);
}
@Test
......@@ -96,11 +95,11 @@ public class UserManagerControllerTest {
List<Group> groups = underTest.getGroups();
Assertions.assertEquals(2, groups.size(), "Number of returned groups");
Group group = groups.get(0);
Assertions.assertEquals(group.getId(), GROUP_ID);
// Assertions.assertEquals(group.getId(), GROUP_ID);
Assertions.assertEquals(group.getName(), GROUP);
Assertions.assertEquals(group.getPath(), GROUP_PATH);
Group subGroup = groups.get(1);
Assertions.assertEquals(subGroup.getId(), SUBGROUP_ID);
// Assertions.assertEquals(subGroup.getId(), SUBGROUP_ID);
Assertions.assertEquals(subGroup.getName(), SUBGROUP);
Assertions.assertEquals(subGroup.getPath(), SUBGROUP_PATH);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment