-
Notifications
You must be signed in to change notification settings - Fork 663
KARAF-5014: consider first group role in users.properties and ignore empty roles #1863
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
stataru8
wants to merge
6
commits into
apache:main
Choose a base branch
from
Talend:stataru/fix/KARAF-5014
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2e57742
KARAF-5014: consider first group role in users.properties and ignore …
stataru8 ea7e507
KARAF-5014: consider first group role in users.properties and ignore …
stataru8 ced63fe
KARAF-5014: consider first group role in users.properties and ignore …
stataru8 93ecc45
KARAF-5014: consider empty groups in the output of jaas:user-list
stataru8 67f8200
KARAF-5014: align properties login modules logic for retrieving group…
stataru8 27b9946
KARAF-5014: make nested groups discoverable
stataru8 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
314 changes: 314 additions & 0 deletions
314
.../modules/src/main/java/org/apache/karaf/jaas/modules/AbstractPropertiesBackingEngine.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,314 @@ | ||
| /* | ||
| * 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.karaf.jaas.modules; | ||
|
|
||
| import org.apache.felix.utils.properties.Properties; | ||
| import org.apache.karaf.jaas.boot.principal.GroupPrincipal; | ||
| import org.apache.karaf.jaas.boot.principal.RolePrincipal; | ||
| import org.apache.karaf.jaas.boot.principal.UserPrincipal; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import java.security.Principal; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public abstract class AbstractPropertiesBackingEngine implements BackingEngine { | ||
|
|
||
| private static final Logger LOGGER = LoggerFactory.getLogger(AbstractPropertiesBackingEngine.class); | ||
|
|
||
| private Properties users; | ||
|
|
||
| public AbstractPropertiesBackingEngine(Properties users) { | ||
| this.users = users; | ||
| } | ||
|
|
||
| protected void addUserInternal(String username, String password) { | ||
| String[] infos = null; | ||
| StringBuilder userInfoBuffer = new StringBuilder(); | ||
|
|
||
| String userInfos = users.get(username); | ||
|
|
||
| // if user already exists, update password | ||
| if (userInfos != null && userInfos.length() > 0) { | ||
| infos = userInfos.split(","); | ||
| userInfoBuffer.append(password); | ||
|
|
||
| for (int i = 1; i < infos.length; i++) { | ||
| userInfoBuffer.append(","); | ||
| userInfoBuffer.append(infos[i]); | ||
| } | ||
| String newUserInfo = userInfoBuffer.toString(); | ||
| users.put(username, newUserInfo); | ||
| } else { | ||
| users.put(username, password); | ||
| } | ||
|
|
||
| try { | ||
| users.save(); | ||
| } catch (Exception ex) { | ||
| LOGGER.error("Cannot update users file,", ex); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteUser(String username) { | ||
| // delete all its groups first, for garbage collection of the groups | ||
| for (GroupPrincipal gp : listGroups(users, username)) { | ||
| deleteGroup(username, gp.getName()); | ||
| } | ||
|
|
||
| users.remove(username); | ||
|
|
||
| try { | ||
| users.save(); | ||
| } catch (Exception ex) { | ||
| LOGGER.error("Cannot remove users file,", ex); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public List<UserPrincipal> listUsers() { | ||
| List<UserPrincipal> result = new ArrayList<>(); | ||
|
|
||
| for (Object user : users.keySet()) { | ||
| String userName = (String) user; | ||
| if (userName.startsWith(GROUP_PREFIX)) | ||
| continue; | ||
|
|
||
| UserPrincipal userPrincipal = new UserPrincipal(userName); | ||
| result.add(userPrincipal); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| @Override | ||
| public UserPrincipal lookupUser(String username) { | ||
| for (UserPrincipal userPrincipal : listUsers()) { | ||
| if (userPrincipal.getName().equals(username)) { | ||
| return userPrincipal; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public List<RolePrincipal> listRoles(Principal principal) { | ||
| String userName = principal.getName(); | ||
| if (principal instanceof GroupPrincipal) { | ||
| userName = GROUP_PREFIX + userName; | ||
| } | ||
| return listRoles(users, userName); | ||
| } | ||
|
|
||
| public static List<RolePrincipal> listRoles(Properties users, String name) { | ||
| List<RolePrincipal> result = new ArrayList<>(); | ||
|
|
||
| String userInfo = users.get(name); | ||
| if (userInfo != null) { | ||
| String[] infos = userInfo.split(","); | ||
| for (int i = getFirstRoleIndex(name); i < infos.length; i++) { | ||
| String roleName = infos[i].trim(); | ||
| if (roleName.isEmpty()) { | ||
| // ignore | ||
| } else if (roleName.startsWith(GROUP_PREFIX)) { | ||
| for (RolePrincipal rp : listRoles(users, roleName)) { | ||
| if (!result.contains(rp)) { | ||
| result.add(rp); | ||
| } | ||
| } | ||
| } else { | ||
| RolePrincipal rp = new RolePrincipal(roleName); | ||
| if (!result.contains(rp)) { | ||
| result.add(rp); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Determines the starting index of role definitions for a given property name. | ||
| * @param name the property to evaluate, can represent either a group or a user | ||
| * @return 0 if the property starts with the group prefix, otherwise 1 | ||
| */ | ||
| private static int getFirstRoleIndex(String name) { | ||
| if (name.trim().startsWith(GROUP_PREFIX)) | ||
| return 0; | ||
| return 1; | ||
| } | ||
|
|
||
| @Override | ||
| public void addRole(String username, String role) { | ||
| String userInfos = users.get(username); | ||
| if (userInfos != null) { | ||
| // for groups, empty info should be replaced with role | ||
| // for users, empty info means empty password and role should be appended | ||
| if (userInfos.trim().isEmpty() && username.trim().startsWith(GROUP_PREFIX)) { | ||
| users.put(username, role); | ||
| } else { | ||
| for (RolePrincipal rp : listRoles(users, username)) { | ||
| if (role.equals(rp.getName())) { | ||
| return; | ||
| } | ||
| } | ||
| for (GroupPrincipal gp : listGroups(users, username)) { | ||
| if (role.equals(GROUP_PREFIX + gp.getName())) { | ||
| return; | ||
| } | ||
| } | ||
| String newUserInfos = userInfos + "," + role; | ||
| users.put(username, newUserInfos); | ||
| } | ||
| try { | ||
| users.save(); | ||
| } catch (Exception ex) { | ||
| LOGGER.error("Cannot update users file,", ex); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteRole(String username, String role) { | ||
| String userInfos = users.get(username); | ||
|
|
||
| // if user already exists, remove the role | ||
| if (userInfos != null && userInfos.length() > 0) { | ||
| StringBuilder userInfoBuffer = new StringBuilder(); | ||
| String[] infos = userInfos.split(","); | ||
|
|
||
| int firstRoleIndex = getFirstRoleIndex(username); | ||
| if (firstRoleIndex == 1) { // index 0 is password | ||
| String password = infos[0]; | ||
| userInfoBuffer.append(password); | ||
| } | ||
| for (int i = firstRoleIndex; i < infos.length; i++) { | ||
| if (infos[i] != null && !infos[i].equals(role)) { | ||
| if(firstRoleIndex == 1 || userInfoBuffer.length() > 0) { | ||
| userInfoBuffer.append(","); | ||
| } | ||
| userInfoBuffer.append(infos[i]); | ||
| } | ||
| } | ||
| String newUserInfo = userInfoBuffer.toString(); | ||
| users.put(username, newUserInfo); | ||
| try { | ||
| users.save(); | ||
| } catch (Exception ex) { | ||
| LOGGER.error("Cannot update users file,", ex); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public List<GroupPrincipal> listGroups(UserPrincipal user) { | ||
| String userName = user.getName(); | ||
| return listGroups(users, userName); | ||
| } | ||
|
|
||
| public static List<GroupPrincipal> listGroups(Properties users, String userName) { | ||
| List<GroupPrincipal> result = new ArrayList<>(); | ||
| String userInfo = users.get(userName); | ||
| if (userInfo != null) { | ||
| String[] infos = userInfo.split(","); | ||
| for (int i = getFirstRoleIndex(userName); i < infos.length; i++) { | ||
| String name = infos[i].trim(); | ||
| if (name.startsWith(GROUP_PREFIX)) { | ||
| GroupPrincipal group = new GroupPrincipal(name.substring(GROUP_PREFIX.length())); | ||
| if (!result.contains(group)) { | ||
| result.add(group); | ||
| for (GroupPrincipal g : listGroups(users, name)) { | ||
| if (!result.contains(g)) { | ||
| result.add(g); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| @Override | ||
| public void addGroup(String username, String group) { | ||
| String groupName = GROUP_PREFIX + group; | ||
| if (users.get(groupName) == null) { | ||
| addUserInternal(groupName, ""); // groups don't have password | ||
| } | ||
| addRole(username, groupName); | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteGroup(String username, String group) { | ||
| deleteRole(username, GROUP_PREFIX + group); | ||
|
|
||
| // garbage collection, clean up the groups if needed | ||
| for (UserPrincipal user : listUsers()) { | ||
| for (GroupPrincipal g : listGroups(user)) { | ||
| if (group.equals(g.getName())) { | ||
| // there is another user of this group, nothing to clean up | ||
| return; | ||
| } | ||
| } | ||
| } | ||
| for (GroupPrincipal g : listGroups().keySet()) { | ||
| if (!group.equals(g.getName())) { | ||
| for (GroupPrincipal nestedGroup : listGroups(users, GROUP_PREFIX + g.getName())) { | ||
| if (group.equals(nestedGroup.getName())) { | ||
| // there is another group referencing this group, nothing to clean up | ||
| return; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // nobody is using this group any more, remove it | ||
| deleteUser(GROUP_PREFIX + group); | ||
| } | ||
|
|
||
| @Override | ||
| public void addGroupRole(String group, String role) { | ||
| addRole(GROUP_PREFIX + group, role); | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteGroupRole(String group, String role) { | ||
| deleteRole(GROUP_PREFIX + group, role); | ||
| } | ||
|
|
||
| @Override | ||
| public Map<GroupPrincipal, String> listGroups() { | ||
| Map<GroupPrincipal, String> result = new HashMap<>(); | ||
| for (String name : users.keySet()) { | ||
| if (name.startsWith(GROUP_PREFIX)) { | ||
| result.put(new GroupPrincipal(name.substring(GROUP_PREFIX.length())), users.get(name)); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| @Override | ||
| public void createGroup(String group) { | ||
| String groupName = GROUP_PREFIX + group; | ||
| if (users.get(groupName) == null) { | ||
| addUserInternal(groupName, ""); // groups don't have password | ||
| } else { | ||
| throw new IllegalArgumentException("Group: " + group + " already exist"); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if this proposal makes sense. I can revert.
Setup: user
foowithout any groups or roles declared.Current behaviour
Proposal
Proposal - more tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it makes sense for the last output to be