Skip to content

Commit 3baaab5

Browse files
authored
Refactor building role from single role descriptor (#91107)
This PR refactors functionality for building a role from a single role descriptor. This used to be handled by a specialized constructor of the Role.Builder class that accessed internal fields. This PR consolidates this logic into a static method that uses the builder's canonical methods instead. The method is used to construct the roles of internal users, from static role descriptors, as well as in test code. Relates: #90614 (comment)
1 parent 0ac81ce commit 3baaab5

File tree

8 files changed

+168
-120
lines changed

8 files changed

+168
-120
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authz/permission/Role.java

Lines changed: 49 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.HashMap;
3535
import java.util.List;
3636
import java.util.Map;
37+
import java.util.Objects;
3738
import java.util.Set;
3839
import 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
}

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/security/authz/permission/SimpleRoleTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ public void testEmptyRoleHasNoEmptyListOfNames() {
2828
}
2929

3030
public void testHasPrivilegesCache() throws ExecutionException {
31-
final SimpleRole role = Role.builder(
31+
final SimpleRole role = Role.buildFromRoleDescriptor(
3232
new RoleDescriptor(randomAlphaOfLengthBetween(3, 8), new String[] { "monitor" }, null, null),
33-
null,
33+
new FieldPermissionsCache(Settings.EMPTY),
3434
RESTRICTED_INDICES
35-
).build();
35+
);
3636

3737
// cache is null to begin with
3838
assertThat(role.getHasPrivilegesCache(), nullValue());

0 commit comments

Comments
 (0)