20
20
import com .netflix .spinnaker .fiat .model .resources .Role ;
21
21
import com .netflix .spinnaker .fiat .permissions .ExternalUser ;
22
22
import com .netflix .spinnaker .fiat .roles .UserRolesProvider ;
23
- import java .text .MessageFormat ;
24
- import java .text .ParseException ;
25
23
import java .util .*;
26
24
import java .util .stream .Collectors ;
27
25
import javax .naming .InvalidNameException ;
28
26
import javax .naming .Name ;
29
- import javax .naming .NamingEnumeration ;
30
27
import javax .naming .NamingException ;
31
28
import javax .naming .directory .Attributes ;
32
29
import lombok .Setter ;
@@ -100,22 +97,27 @@ public List<Role> loadRoles(ExternalUser user) {
100
97
.collect (Collectors .toList ());
101
98
}
102
99
103
- private class UserGroupMapper implements AttributesMapper <List <Pair <String , Role >>> {
104
- public List <Pair <String , Role >> mapFromAttributes (Attributes attrs ) throws NamingException {
105
- String group = attrs .get (configProps .getGroupRoleAttributes ()).get ().toString ();
106
- Role role = new Role (group ).setSource (Role .Source .LDAP );
107
- List <Pair <String , Role >> member = new ArrayList <>();
108
- for (NamingEnumeration <?> members = attrs .get (configProps .getGroupUserAttributes ()).getAll ();
109
- members .hasMore (); ) {
110
- try {
111
- String user =
112
- String .valueOf (configProps .getUserDnPattern ().parse (members .next ().toString ())[0 ]);
113
- member .add (Pair .of (user , role ));
114
- } catch (ParseException e ) {
115
- e .printStackTrace ();
116
- }
117
- }
118
- return member ;
100
+ private class RoleFullDNtoUserRoleMapper implements AttributesMapper <Pair <String , Role >> {
101
+ @ Override
102
+ public Pair <String , Role > mapFromAttributes (Attributes attrs ) throws NamingException {
103
+ return Pair .of (
104
+ attrs .get ("distinguishedname" ).get ().toString (),
105
+ new Role (attrs .get (configProps .getGroupRoleAttributes ()).get ().toString ())
106
+ .setSource (Role .Source .LDAP ));
107
+ }
108
+ }
109
+
110
+ private class UserGroupMapper implements AttributesMapper <Pair <String , Role >> {
111
+
112
+ private Role role ;
113
+
114
+ public UserGroupMapper (Role role ) {
115
+ this .role = role ;
116
+ }
117
+
118
+ @ Override
119
+ public Pair <String , Role > mapFromAttributes (Attributes attrs ) throws NamingException {
120
+ return Pair .of (attrs .get (configProps .getUserIdAttribute ()).get ().toString (), role );
119
121
}
120
122
}
121
123
@@ -125,30 +127,25 @@ public Map<String, Collection<Role>> multiLoadRoles(Collection<ExternalUser> use
125
127
return new HashMap <>();
126
128
}
127
129
128
- if (users .size () > configProps .getThresholdToUseGroupMembership ()
129
- && StringUtils .isNotEmpty (configProps .getGroupUserAttributes ())) {
130
- Set <String > userIds = users .stream ().map (ExternalUser ::getId ).collect (Collectors .toSet ());
131
- return ldapTemplate
132
- .search (
133
- configProps .getGroupSearchBase (),
134
- MessageFormat .format (
135
- configProps .getGroupSearchFilter (),
136
- "*" ,
137
- "*" ), // Passing two wildcard params like loadRoles
138
- new UserGroupMapper ())
139
- .stream ()
140
- .flatMap (List ::stream )
141
- .filter (p -> userIds .contains (p .getKey ()))
142
- .collect (
143
- Collectors .groupingBy (
144
- Pair ::getKey ,
145
- Collectors .mapping (Pair ::getValue , Collectors .toCollection (ArrayList ::new ))));
146
- }
147
-
148
- // ExternalUser is used here as a simple data type to hold the username/roles combination.
149
- return users .stream ()
150
- .map (u -> new ExternalUser ().setId (u .getId ()).setExternalRoles (loadRoles (u )))
151
- .collect (Collectors .toMap (ExternalUser ::getId , ExternalUser ::getExternalRoles ));
130
+ Set <String > userIds = users .stream ().map (ExternalUser ::getId ).collect (Collectors .toSet ());
131
+ return ldapTemplate
132
+ .search (
133
+ configProps .getGroupSearchBase (),
134
+ configProps .getGroupSearchFilter (),
135
+ new RoleFullDNtoUserRoleMapper ())
136
+ .stream ()
137
+ .map (
138
+ r ->
139
+ ldapTemplate .search (
140
+ configProps .getUserSearchBase (),
141
+ String .format ("(&(objectCategory=user)(memberOf=%s))" , r .getKey ()),
142
+ new UserGroupMapper (r .getValue ())))
143
+ .flatMap (List ::stream )
144
+ .filter (p -> userIds .contains (p .getKey ()))
145
+ .collect (
146
+ Collectors .groupingBy (
147
+ Pair ::getKey ,
148
+ Collectors .mapping (Pair ::getValue , Collectors .toCollection (ArrayList ::new ))));
152
149
}
153
150
154
151
private String getUserFullDn (String userId ) {
0 commit comments