3434import java .util .HashMap ;
3535import java .util .List ;
3636import java .util .Map ;
37+ import java .util .Objects ;
3738import java .util .Set ;
3839import java .util .function .Predicate ;
3940
@@ -177,10 +178,6 @@ static Builder builder(RestrictedIndices restrictedIndices, String... names) {
177178 return new Builder (restrictedIndices , names );
178179 }
179180
180- static Builder builder (RoleDescriptor rd , FieldPermissionsCache fieldPermissionsCache , RestrictedIndices restrictedIndices ) {
181- return new Builder (rd , fieldPermissionsCache , restrictedIndices );
182- }
183-
184181 class Builder {
185182
186183 private final String [] names ;
@@ -196,26 +193,6 @@ private Builder(RestrictedIndices restrictedIndices, String[] names) {
196193 this .names = names ;
197194 }
198195
199- private Builder (RoleDescriptor rd , @ Nullable FieldPermissionsCache fieldPermissionsCache , RestrictedIndices restrictedIndices ) {
200- // TODO handle this when we introduce remote index privileges for built-in users and roles. That's the only production code
201- // using this builder
202- assert false == rd .hasRemoteIndicesPrivileges ();
203- this .names = new String [] { rd .getName () };
204- cluster (Sets .newHashSet (rd .getClusterPrivileges ()), Arrays .asList (rd .getConditionalClusterPrivileges ()));
205- groups .addAll (convertFromIndicesPrivileges (rd .getIndicesPrivileges (), fieldPermissionsCache ));
206-
207- final RoleDescriptor .ApplicationResourcePrivileges [] applicationPrivileges = rd .getApplicationPrivileges ();
208- for (RoleDescriptor .ApplicationResourcePrivileges applicationPrivilege : applicationPrivileges ) {
209- applicationPrivs .add (convertApplicationPrivilege (applicationPrivilege ));
210- }
211-
212- String [] rdRunAs = rd .getRunAs ();
213- if (rdRunAs != null && rdRunAs .length > 0 ) {
214- this .runAs (new Privilege (Sets .newHashSet (rdRunAs ), rdRunAs ));
215- }
216- this .restrictedIndices = restrictedIndices ;
217- }
218-
219196 public Builder cluster (Set <String > privilegeNames , Iterable <ConfigurableClusterPrivilege > configurableClusterPrivileges ) {
220197 ClusterPermission .Builder builder = ClusterPermission .builder ();
221198 if (privilegeNames .isEmpty () == false ) {
@@ -314,41 +291,6 @@ public SimpleRole build() {
314291 return new SimpleRole (names , cluster , indices , applicationPermission , runAs , remoteIndices );
315292 }
316293
317- static List <IndicesPermissionGroupDefinition > convertFromIndicesPrivileges (
318- RoleDescriptor .IndicesPrivileges [] indicesPrivileges ,
319- @ Nullable FieldPermissionsCache fieldPermissionsCache
320- ) {
321- List <IndicesPermissionGroupDefinition > list = new ArrayList <>(indicesPrivileges .length );
322- for (RoleDescriptor .IndicesPrivileges privilege : indicesPrivileges ) {
323- final FieldPermissions fieldPermissions ;
324- if (fieldPermissionsCache != null ) {
325- fieldPermissions = fieldPermissionsCache .getFieldPermissions (privilege .getGrantedFields (), privilege .getDeniedFields ());
326- } else {
327- fieldPermissions = new FieldPermissions (
328- new FieldPermissionsDefinition (privilege .getGrantedFields (), privilege .getDeniedFields ())
329- );
330- }
331- final Set <BytesReference > query = privilege .getQuery () == null ? null : Collections .singleton (privilege .getQuery ());
332- list .add (
333- new IndicesPermissionGroupDefinition (
334- IndexPrivilege .get (Sets .newHashSet (privilege .getPrivileges ())),
335- fieldPermissions ,
336- query ,
337- privilege .allowRestrictedIndices (),
338- privilege .getIndices ()
339- )
340- );
341- }
342- return list ;
343- }
344-
345- static Tuple <ApplicationPrivilege , Set <String >> convertApplicationPrivilege (RoleDescriptor .ApplicationResourcePrivileges arp ) {
346- return new Tuple <>(
347- new ApplicationPrivilege (arp .getApplication (), Sets .newHashSet (arp .getPrivileges ()), arp .getPrivileges ()),
348- Sets .newHashSet (arp .getResources ())
349- );
350- }
351-
352294 private static class IndicesPermissionGroupDefinition {
353295 private final IndexPrivilege privilege ;
354296 private final FieldPermissions fieldPermissions ;
@@ -371,4 +313,52 @@ private IndicesPermissionGroupDefinition(
371313 }
372314 }
373315 }
316+
317+ static SimpleRole buildFromRoleDescriptor (
318+ final RoleDescriptor roleDescriptor ,
319+ final FieldPermissionsCache fieldPermissionsCache ,
320+ final RestrictedIndices restrictedIndices
321+ ) {
322+ // TODO handle this when we introduce remote index privileges for built-in users and roles. That's the only production code
323+ // using this builder
324+ assert false == roleDescriptor .hasRemoteIndicesPrivileges ();
325+ Objects .requireNonNull (fieldPermissionsCache );
326+
327+ final Builder builder = builder (restrictedIndices , roleDescriptor .getName ());
328+
329+ builder .cluster (
330+ Sets .newHashSet (roleDescriptor .getClusterPrivileges ()),
331+ Arrays .asList (roleDescriptor .getConditionalClusterPrivileges ())
332+ );
333+
334+ for (RoleDescriptor .IndicesPrivileges indexPrivilege : roleDescriptor .getIndicesPrivileges ()) {
335+ builder .add (
336+ fieldPermissionsCache .getFieldPermissions (
337+ new FieldPermissionsDefinition (indexPrivilege .getGrantedFields (), indexPrivilege .getDeniedFields ())
338+ ),
339+ indexPrivilege .getQuery () == null ? null : Collections .singleton (indexPrivilege .getQuery ()),
340+ IndexPrivilege .get (Sets .newHashSet (indexPrivilege .getPrivileges ())),
341+ indexPrivilege .allowRestrictedIndices (),
342+ indexPrivilege .getIndices ()
343+ );
344+ }
345+
346+ for (RoleDescriptor .ApplicationResourcePrivileges applicationPrivilege : roleDescriptor .getApplicationPrivileges ()) {
347+ builder .addApplicationPrivilege (
348+ new ApplicationPrivilege (
349+ applicationPrivilege .getApplication (),
350+ Sets .newHashSet (applicationPrivilege .getPrivileges ()),
351+ applicationPrivilege .getPrivileges ()
352+ ),
353+ Sets .newHashSet (applicationPrivilege .getResources ())
354+ );
355+ }
356+
357+ final String [] rdRunAs = roleDescriptor .getRunAs ();
358+ if (rdRunAs != null && rdRunAs .length > 0 ) {
359+ builder .runAs (new Privilege (Sets .newHashSet (rdRunAs ), rdRunAs ));
360+ }
361+
362+ return builder .build ();
363+ }
374364}
0 commit comments