Skip to content

Commit fa2ab89

Browse files
committed
KARAF-5014: align properties login modules logic for retrieving principals with the underlying baking engine
1 parent 93ecc45 commit fa2ab89

5 files changed

Lines changed: 23 additions & 31 deletions

File tree

jaas/modules/src/main/java/org/apache/karaf/jaas/modules/AbstractPropertiesBackingEngine.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,20 @@ public List<RolePrincipal> listRoles(Principal principal) {
116116
}
117117

118118
protected List<RolePrincipal> listRoles(String name) {
119+
return listRoles(users, name);
120+
}
121+
122+
public static List<RolePrincipal> listRoles(Properties users, String name) {
119123
List<RolePrincipal> result = new ArrayList<>();
120124

121125
String userInfo = users.get(name);
122126
String[] infos = userInfo.split(",");
123127
for (int i = getFirstRoleIndex(name); i < infos.length; i++) {
124-
String roleName = infos[i];
125-
if (roleName.trim().isEmpty()) {
128+
String roleName = infos[i].trim();
129+
if (roleName.isEmpty()) {
126130
// ignore
127131
} else if (roleName.startsWith(GROUP_PREFIX)) {
128-
for (RolePrincipal rp : listRoles(roleName)) {
132+
for (RolePrincipal rp : listRoles(users, roleName)) {
129133
if (!result.contains(rp)) {
130134
result.add(rp);
131135
}
@@ -145,7 +149,7 @@ protected List<RolePrincipal> listRoles(String name) {
145149
* @param name the property to evaluate, can represent either a group or a user
146150
* @return 0 if the property starts with the group prefix, otherwise 1
147151
*/
148-
private int getFirstRoleIndex(String name) {
152+
private static int getFirstRoleIndex(String name) {
149153
if (name.trim().startsWith(GROUP_PREFIX))
150154
return 0;
151155
return 1;
@@ -223,12 +227,16 @@ public List<GroupPrincipal> listGroups(UserPrincipal user) {
223227
}
224228

225229
private List<GroupPrincipal> listGroups(String userName) {
230+
return listGroups(users, userName);
231+
}
232+
233+
public static List<GroupPrincipal> listGroups(Properties users, String userName) {
226234
List<GroupPrincipal> result = new ArrayList<>();
227235
String userInfo = users.get(userName);
228236
if (userInfo != null) {
229237
String[] infos = userInfo.split(",");
230238
for (int i = getFirstRoleIndex(userName); i < infos.length; i++) {
231-
String name = infos[i];
239+
String name = infos[i].trim();
232240
if (name.startsWith(GROUP_PREFIX)) {
233241
result.add(new GroupPrincipal(name.substring(GROUP_PREFIX.length())));
234242
}

jaas/modules/src/main/java/org/apache/karaf/jaas/modules/JAASUtils.java

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,32 +37,16 @@ public static String getString(Map<String, ?> options, String key) {
3737
return (String)val;
3838
}
3939

40-
public static Set<Principal> getPrincipals(String user, Properties users, String[] infos) {
40+
public static Set<Principal> getPrincipals(String user, Properties users) {
4141
Set<Principal> principals = new HashSet<>();
4242
principals.add(new UserPrincipal(user));
4343

44-
for (int i = 1; i < infos.length; i++) {
45-
if (infos[i].trim().startsWith(BackingEngine.GROUP_PREFIX)) {
46-
// it's a group reference
47-
principals.add(new GroupPrincipal(infos[i].trim().substring(BackingEngine.GROUP_PREFIX.length())));
48-
String groupInfo = users.get(infos[i].trim());
49-
if (groupInfo != null) {
50-
String[] roles = groupInfo.split(",");
51-
for (int j = 0; j < roles.length; j++) {
52-
addRole(principals, roles[j]);
53-
}
54-
}
55-
} else {
56-
// it's an user reference
57-
addRole(principals, infos[i]);
58-
}
59-
}
60-
return principals;
61-
}
44+
AbstractPropertiesBackingEngine.listGroups(users, user)
45+
.forEach(group -> principals.add(new GroupPrincipal(group.getName())));
6246

63-
static void addRole(Set<Principal> principals, String role) {
64-
role = role.trim();
65-
if (!role.isEmpty())
66-
principals.add(new RolePrincipal(role));
47+
AbstractPropertiesBackingEngine.listRoles(users, user)
48+
.forEach(role -> principals.add(new RolePrincipal(role.getName())));
49+
50+
return principals;
6751
}
6852
}

jaas/modules/src/main/java/org/apache/karaf/jaas/modules/properties/DigestPasswordLoginModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public boolean login() throws LoginException {
202202
}
203203
}
204204

205-
principals = JAASUtils.getPrincipals(user, users, infos);
205+
principals = JAASUtils.getPrincipals(user, users);
206206

207207
users.clear();
208208

jaas/modules/src/main/java/org/apache/karaf/jaas/modules/properties/PropertiesLoginModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public boolean login() throws LoginException {
129129
}
130130
}
131131

132-
principals = JAASUtils.getPrincipals(user, users, infos);
132+
principals = JAASUtils.getPrincipals(user, users);
133133

134134
users.clear();
135135

jaas/modules/src/main/java/org/apache/karaf/jaas/modules/publickey/PublickeyLoginModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public boolean login() throws LoginException {
147147
}
148148
}
149149

150-
principals = JAASUtils.getPrincipals(user, users, infos);
150+
principals = JAASUtils.getPrincipals(user, users);
151151

152152
users.clear();
153153

0 commit comments

Comments
 (0)