-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Refactor building role from single role descriptor #91107
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
Changes from 8 commits
61c20c8
7417d3d
d22e0df
7408109
7e57b5f
15898c4
bf37d35
20af8a5
f7679f4
e9e83ab
f91e5c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,6 +34,7 @@ | |
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
| import java.util.function.Predicate; | ||
|
|
||
|
|
@@ -177,10 +178,6 @@ static Builder builder(RestrictedIndices restrictedIndices, String... names) { | |
| return new Builder(restrictedIndices, names); | ||
| } | ||
|
|
||
| static Builder builder(RoleDescriptor rd, FieldPermissionsCache fieldPermissionsCache, RestrictedIndices restrictedIndices) { | ||
| return new Builder(rd, fieldPermissionsCache, restrictedIndices); | ||
| } | ||
|
|
||
| class Builder { | ||
|
|
||
| private final String[] names; | ||
|
|
@@ -196,26 +193,6 @@ private Builder(RestrictedIndices restrictedIndices, String[] names) { | |
| this.names = names; | ||
| } | ||
|
|
||
| private Builder(RoleDescriptor rd, @Nullable FieldPermissionsCache fieldPermissionsCache, RestrictedIndices restrictedIndices) { | ||
| // TODO handle this when we introduce remote index privileges for built-in users and roles. That's the only production code | ||
| // using this builder | ||
| assert false == rd.hasRemoteIndicesPrivileges(); | ||
| this.names = new String[] { rd.getName() }; | ||
| cluster(Sets.newHashSet(rd.getClusterPrivileges()), Arrays.asList(rd.getConditionalClusterPrivileges())); | ||
| groups.addAll(convertFromIndicesPrivileges(rd.getIndicesPrivileges(), fieldPermissionsCache)); | ||
|
|
||
| final RoleDescriptor.ApplicationResourcePrivileges[] applicationPrivileges = rd.getApplicationPrivileges(); | ||
| for (RoleDescriptor.ApplicationResourcePrivileges applicationPrivilege : applicationPrivileges) { | ||
| applicationPrivs.add(convertApplicationPrivilege(applicationPrivilege)); | ||
| } | ||
|
|
||
| String[] rdRunAs = rd.getRunAs(); | ||
| if (rdRunAs != null && rdRunAs.length > 0) { | ||
| this.runAs(new Privilege(Sets.newHashSet(rdRunAs), rdRunAs)); | ||
| } | ||
| this.restrictedIndices = restrictedIndices; | ||
| } | ||
|
|
||
| public Builder cluster(Set<String> privilegeNames, Iterable<ConfigurableClusterPrivilege> configurableClusterPrivileges) { | ||
| ClusterPermission.Builder builder = ClusterPermission.builder(); | ||
| if (privilegeNames.isEmpty() == false) { | ||
|
|
@@ -314,41 +291,6 @@ public SimpleRole build() { | |
| return new SimpleRole(names, cluster, indices, applicationPermission, runAs, remoteIndices); | ||
| } | ||
|
|
||
| static List<IndicesPermissionGroupDefinition> convertFromIndicesPrivileges( | ||
| RoleDescriptor.IndicesPrivileges[] indicesPrivileges, | ||
| @Nullable FieldPermissionsCache fieldPermissionsCache | ||
| ) { | ||
| List<IndicesPermissionGroupDefinition> list = new ArrayList<>(indicesPrivileges.length); | ||
| for (RoleDescriptor.IndicesPrivileges privilege : indicesPrivileges) { | ||
| final FieldPermissions fieldPermissions; | ||
| if (fieldPermissionsCache != null) { | ||
| fieldPermissions = fieldPermissionsCache.getFieldPermissions(privilege.getGrantedFields(), privilege.getDeniedFields()); | ||
| } else { | ||
| fieldPermissions = new FieldPermissions( | ||
| new FieldPermissionsDefinition(privilege.getGrantedFields(), privilege.getDeniedFields()) | ||
| ); | ||
| } | ||
| final Set<BytesReference> query = privilege.getQuery() == null ? null : Collections.singleton(privilege.getQuery()); | ||
| list.add( | ||
| new IndicesPermissionGroupDefinition( | ||
| IndexPrivilege.get(Sets.newHashSet(privilege.getPrivileges())), | ||
| fieldPermissions, | ||
| query, | ||
| privilege.allowRestrictedIndices(), | ||
| privilege.getIndices() | ||
| ) | ||
| ); | ||
| } | ||
| return list; | ||
| } | ||
|
|
||
| static Tuple<ApplicationPrivilege, Set<String>> convertApplicationPrivilege(RoleDescriptor.ApplicationResourcePrivileges arp) { | ||
| return new Tuple<>( | ||
| new ApplicationPrivilege(arp.getApplication(), Sets.newHashSet(arp.getPrivileges()), arp.getPrivileges()), | ||
| Sets.newHashSet(arp.getResources()) | ||
| ); | ||
| } | ||
|
|
||
| private static class IndicesPermissionGroupDefinition { | ||
| private final IndexPrivilege privilege; | ||
| private final FieldPermissions fieldPermissions; | ||
|
|
@@ -371,4 +313,52 @@ private IndicesPermissionGroupDefinition( | |
| } | ||
| } | ||
| } | ||
|
|
||
| static SimpleRole buildFromRoleDescriptor( | ||
| final RoleDescriptor roleDescriptor, | ||
| final FieldPermissionsCache fieldPermissionsCache, | ||
| final RestrictedIndices restrictedIndices | ||
| ) { | ||
| // TODO handle this when we introduce remote index privileges for built-in users and roles. That's the only production code | ||
| // using this builder | ||
| assert false == roleDescriptor.hasRemoteIndicesPrivileges(); | ||
| Objects.requireNonNull(fieldPermissionsCache); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| final Builder builder = builder(restrictedIndices, roleDescriptor.getName()); | ||
|
|
||
| builder.cluster( | ||
| Sets.newHashSet(roleDescriptor.getClusterPrivileges()), | ||
| Arrays.asList(roleDescriptor.getConditionalClusterPrivileges()) | ||
| ); | ||
|
|
||
| for (RoleDescriptor.IndicesPrivileges indexPrivilege : roleDescriptor.getIndicesPrivileges()) { | ||
| builder.add( | ||
| fieldPermissionsCache.getFieldPermissions( | ||
| new FieldPermissionsDefinition(indexPrivilege.getGrantedFields(), indexPrivilege.getDeniedFields()) | ||
| ), | ||
| indexPrivilege.getQuery() == null ? null : Collections.singleton(indexPrivilege.getQuery()), | ||
| IndexPrivilege.get(Sets.newHashSet(indexPrivilege.getPrivileges())), | ||
| indexPrivilege.allowRestrictedIndices(), | ||
| indexPrivilege.getIndices() | ||
| ); | ||
| } | ||
|
|
||
| for (RoleDescriptor.ApplicationResourcePrivileges applicationPrivilege : roleDescriptor.getApplicationPrivileges()) { | ||
| builder.addApplicationPrivilege( | ||
| new ApplicationPrivilege( | ||
| applicationPrivilege.getApplication(), | ||
| Sets.newHashSet(applicationPrivilege.getPrivileges()), | ||
| applicationPrivilege.getPrivileges() | ||
| ), | ||
| Sets.newHashSet(applicationPrivilege.getResources()) | ||
| ); | ||
| } | ||
|
Comment on lines
+346
to
+355
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not part of the current code. But I think it is worthwhile to assert that all privilege names are patterns (e.g. Concretely, what I am suggesting is something like similar to the check in NativePrivilegeStore.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense. Added the assertion 👍
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's quite a few tests that fail this assertion, e.g.,
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was wondering whether it would catch something like this. Yeah, separate PR is fine. Should just be a test issue, but it might need some work to get around it. Thanks! |
||
|
|
||
| final String[] rdRunAs = roleDescriptor.getRunAs(); | ||
| if (rdRunAs != null && rdRunAs.length > 0) { | ||
| builder.runAs(new Privilege(Sets.newHashSet(rdRunAs), rdRunAs)); | ||
| } | ||
|
|
||
| return builder.build(); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.