Skip to content
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

Add Role Mapping Cleanup Security Migration #114830

Closed
wants to merge 44 commits into from
Closed
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
c23b783
Add security migration for cleaning up ECK role mappings
jfreden Oct 4, 2024
157be76
Update docs/changelog/114830.yaml
jfreden Oct 16, 2024
c4ad317
fixup! Use new API for role mapping cleanup
jfreden Oct 17, 2024
dda3c77
fixup! Test
jfreden Oct 17, 2024
c6e54e6
fixup! Test
jfreden Oct 17, 2024
e018eaf
fixup! Refactor and add tests
jfreden Oct 17, 2024
3a7e61f
fixup! Fix cleanup test
jfreden Oct 17, 2024
8b50d79
fixup! Add comments
jfreden Oct 17, 2024
d3259d5
fixup! merge build.gradle
jfreden Oct 17, 2024
7299d19
fixup! another build.gradle
jfreden Oct 17, 2024
11f775a
fixup! Formatting
jfreden Oct 17, 2024
97abe3c
fixup! comments
jfreden Oct 17, 2024
4d4f412
Merge remote-tracking branch 'upstream/8.16' into add_eck_migration_8_16
jfreden Oct 21, 2024
3974dce
fixup! Code review comments
jfreden Oct 23, 2024
52146ed
fixup! Code review comment
jfreden Oct 23, 2024
e16279b
fixup! Spotless :(
jfreden Oct 23, 2024
9c8efe3
fixup! Keep deleting even if something fails
jfreden Oct 23, 2024
1db4315
Merge remote-tracking branch 'upstream/8.16' into add_eck_migration_8_16
jfreden Oct 23, 2024
07db034
fixup! Use new constants
jfreden Oct 23, 2024
d23974f
fixup! Code review comments and bug fixes
jfreden Oct 23, 2024
08c7d95
fixup! typo
jfreden Oct 23, 2024
22ef3b7
fixup! test
jfreden Oct 23, 2024
8762d9b
fixup! lint
jfreden Oct 23, 2024
698b554
fixup! Tests
jfreden Oct 24, 2024
3430c81
Use an enum to track status
jfreden Oct 24, 2024
5a96adf
fixup! checkstyle issue
jfreden Oct 24, 2024
2563993
Merge remote-tracking branch 'upstream/8.16' into add_eck_migration_8_16
jfreden Oct 24, 2024
1709df1
Always include operator file
jfreden Oct 24, 2024
1e01e08
Add another migration test suite
jfreden Oct 24, 2024
90eb45f
fixup! Spotless
jfreden Oct 24, 2024
b86c0a8
fixup! Add assertion on migration status
jfreden Oct 24, 2024
ac9e6da
fixup! Skip calc + test
jfreden Oct 24, 2024
a91f37b
fixup! Review comments and test code
jfreden Oct 25, 2024
ce405e2
Merge remote-tracking branch 'upstream/8.16' into add_eck_migration_8_16
jfreden Oct 25, 2024
304e105
fixup! Test
jfreden Oct 25, 2024
1e65bf7
fixup! Bug
jfreden Oct 25, 2024
9533f6f
Use index setting to track migration version index created on
jfreden Oct 25, 2024
d6e466b
fixup! line
jfreden Oct 28, 2024
9112aba
fixup! Update name
jfreden Oct 28, 2024
a568a31
fixup! Add tests and review comments
jfreden Oct 28, 2024
2ed011b
Revert "Use index setting to track migration version index created on"
jfreden Oct 28, 2024
e1bb9d0
fixup! name
jfreden Oct 28, 2024
3160bd4
fixup! Too much revert
jfreden Oct 28, 2024
9eb0dbe
fixup! Failing tests
jfreden Oct 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/114830.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 114830
summary: Add Role Mapping Cleanup Security Migration
area: Security
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,21 @@ public Set<String> conflicts(String handlerName, Set<String> modified) {
return Collections.unmodifiableSet(intersect);
}

/**
* Get the reserved keys for the handler name
*
* @param handlerName handler name to get keys for
* @return set of keys for that handler
*/
public Set<String> keys(String handlerName) {
ReservedStateHandlerMetadata handlerMetadata = handlers.get(handlerName);
if (handlerMetadata == null || handlerMetadata.keys().isEmpty()) {
return Collections.emptySet();
}

return Collections.unmodifiableSet(handlerMetadata.keys());
}

/**
* Reads an {@link ReservedStateMetadata} from a {@link StreamInput}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ private static IndexVersion def(int id, Version luceneVersion) {
public static final IndexVersion ENABLE_IGNORE_MALFORMED_LOGSDB = def(8_514_00_0, Version.LUCENE_9_11_1);
public static final IndexVersion MERGE_ON_RECOVERY_VERSION = def(8_515_00_0, Version.LUCENE_9_11_1);
public static final IndexVersion UPGRADE_TO_LUCENE_9_12 = def(8_516_00_0, Version.LUCENE_9_12_0);
public static final IndexVersion ADD_ROLE_MAPPING_CLEANUP_MIGRATION = def(8_517_00_0, Version.LUCENE_9_12_0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've not yet backported anything with a version like this before... how does it work? I'm assuming 8.16.0, 8.17.0 and 9 will all have diverging constants (with 9 having all, 8.17 fewer, etc)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! We have to be careful when creating the backport PRs.

Copy link
Contributor Author

@jfreden jfreden Oct 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After discussing with core infra, I opted to go for an index setting, see this commit: 9533f6f

Did a small POC in this PR to test this before: #115666

The benefit with this a approach is that we no longer need to manually bump index versions when adding new migrations and that it's not as heavy weight (we no longer need to bump the index version for all indices).


/*
* STOP! READ THIS FIRST! No, really,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,15 @@ public RoleMapperExpression getExpression() {
* that match the {@link #getExpression() expression} in this mapping.
*/
public List<String> getRoles() {
return Collections.unmodifiableList(roles);
return roles != null ? Collections.unmodifiableList(roles) : Collections.emptyList();
}

/**
* The list of {@link RoleDescriptor roles} (specified by a {@link TemplateRoleName template} that evaluates to one or more names)
* that should be assigned to users that match the {@link #getExpression() expression} in this mapping.
*/
public List<TemplateRoleName> getRoleTemplates() {
return Collections.unmodifiableList(roleTemplates);
return roleTemplates != null ? Collections.unmodifiableList(roleTemplates) : Collections.emptyList();
}

/**
Expand All @@ -223,7 +223,7 @@ public List<TemplateRoleName> getRoleTemplates() {
* This is not used within the mapping process, and does not affect whether the expression matches, nor which roles are assigned.
*/
public Map<String, Object> getMetadata() {
return Collections.unmodifiableMap(metadata);
return metadata != null ? Collections.unmodifiableMap(metadata) : Collections.emptyMap();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are not set to null anywhere in production code, but could be in the future (since constructor is public) and can in tests (as I found out).

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
import static org.elasticsearch.xpack.security.support.SecuritySystemIndices.SECURITY_MIGRATION_FRAMEWORK;
import static org.elasticsearch.xpack.security.support.SecuritySystemIndices.SECURITY_PROFILE_ORIGIN_FEATURE;
import static org.elasticsearch.xpack.security.support.SecuritySystemIndices.SECURITY_ROLES_METADATA_FLATTENED;
import static org.elasticsearch.xpack.security.support.SecuritySystemIndices.SECURITY_ROLE_MAPPING_CLEANUP;
import static org.elasticsearch.xpack.security.support.SecuritySystemIndices.VERSION_SECURITY_PROFILE_ORIGIN;

public class SecurityFeatures implements FeatureSpecification {

@Override
public Set<NodeFeature> getFeatures() {
return Set.of(SECURITY_ROLES_METADATA_FLATTENED, SECURITY_MIGRATION_FRAMEWORK);
return Set.of(SECURITY_ROLE_MAPPING_CLEANUP, SECURITY_ROLES_METADATA_FLATTENED, SECURITY_MIGRATION_FRAMEWORK);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.elasticsearch.cluster.metadata.IndexMetadata;
import org.elasticsearch.cluster.metadata.MappingMetadata;
import org.elasticsearch.cluster.metadata.Metadata;
import org.elasticsearch.cluster.metadata.ReservedStateMetadata;
import org.elasticsearch.cluster.routing.IndexRoutingTable;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.core.TimeValue;
Expand All @@ -46,7 +47,9 @@
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.threadpool.Scheduler;
import org.elasticsearch.xcontent.XContentType;
import org.elasticsearch.xpack.core.security.authz.RoleMappingMetadata;
import org.elasticsearch.xpack.security.SecurityFeatures;
import org.elasticsearch.xpack.security.action.rolemapping.ReservedRoleMappingAction;

import java.time.Instant;
import java.util.List;
Expand Down Expand Up @@ -74,7 +77,7 @@
public class SecurityIndexManager implements ClusterStateListener {

public static final String SECURITY_VERSION_STRING = "security-version";

private static final String FILE_SETTINGS_METADATA_NAMESPACE = "file_settings";
private static final Logger logger = LogManager.getLogger(SecurityIndexManager.class);

/**
Expand Down Expand Up @@ -267,6 +270,21 @@ private static boolean isCreatedOnLatestVersion(IndexMetadata indexMetadata) {
return indexVersionCreated != null && indexVersionCreated.onOrAfter(IndexVersion.current());
}

/**
* Check to see if there are any file based role mappings and if they have been loaded into cluster state. If the
* {@link ReservedStateMetadata} file_settings namespace contains role mapping names, it means that there should be the same number of
* cluster state role mappings available. If they're not available yet using {@code RoleMappingMetadata.getFromClusterState()}, they
* have not yet been synchronized.
*/
private static boolean isReadyForRoleMappingCleanupMigration(ClusterState clusterState) {
ReservedStateMetadata fileSettingsMetadata = clusterState.metadata().reservedStateMetadata().get(FILE_SETTINGS_METADATA_NAMESPACE);
assert fileSettingsMetadata != null || clusterState.metadata().reservedStateMetadata().isEmpty();
return fileSettingsMetadata == null
|| RoleMappingMetadata.getFromClusterState(clusterState).getRoleMappings().size() == fileSettingsMetadata.keys(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we also need to check that RoleMappingMetadata.getFromClusterState(clusterState).getRoleMappings() returns mappings with non-fallback names. Otherwise, there is a small window where customers coming from 8.15.3 might get mappings here that don't have the right names yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, added that. Good catch!

ReservedRoleMappingAction.NAME
).size();
}

@Override
public void clusterChanged(ClusterChangedEvent event) {
if (event.state().blocks().hasGlobalBlock(GatewayService.STATE_NOT_RECOVERED_BLOCK)) {
Expand All @@ -284,6 +302,7 @@ public void clusterChanged(ClusterChangedEvent event) {
Tuple<Boolean, Boolean> available = checkIndexAvailable(event.state());
final boolean indexAvailableForWrite = available.v1();
final boolean indexAvailableForSearch = available.v2();
final boolean readyForRoleMappingCleanupMigration = isReadyForRoleMappingCleanupMigration(event.state());
final boolean mappingIsUpToDate = indexMetadata == null || checkIndexMappingUpToDate(event.state());
final int migrationsVersion = getMigrationVersionFromIndexMetadata(indexMetadata);
final SystemIndexDescriptor.MappingsVersion minClusterMappingVersion = getMinSecurityIndexMappingVersion(event.state());
Expand Down Expand Up @@ -314,6 +333,7 @@ public void clusterChanged(ClusterChangedEvent event) {
indexAvailableForWrite,
mappingIsUpToDate,
createdOnLatestVersion,
readyForRoleMappingCleanupMigration,
migrationsVersion,
minClusterMappingVersion,
indexMappingVersion,
Expand Down Expand Up @@ -438,7 +458,8 @@ private Tuple<Boolean, Boolean> checkIndexAvailable(ClusterState state) {

public boolean isEligibleSecurityMigration(SecurityMigrations.SecurityMigration securityMigration) {
return state.securityFeatures.containsAll(securityMigration.nodeFeaturesRequired())
&& state.indexMappingVersion >= securityMigration.minMappingVersion();
&& state.indexMappingVersion >= securityMigration.minMappingVersion()
&& securityMigration.checkPreConditions(state);
}

public boolean isReadyForSecurityMigration(SecurityMigrations.SecurityMigration securityMigration) {
Expand Down Expand Up @@ -671,6 +692,7 @@ public static class State {
false,
false,
false,
false,
null,
null,
null,
Expand All @@ -686,6 +708,7 @@ public static class State {
public final boolean indexAvailableForWrite;
public final boolean mappingUpToDate;
public final boolean createdOnLatestVersion;
public final boolean readyForRoleMappingCleanupMigration;
public final Integer migrationsVersion;
// Min mapping version supported by the descriptors in the cluster
public final SystemIndexDescriptor.MappingsVersion minClusterMappingVersion;
Expand All @@ -704,6 +727,7 @@ public State(
boolean indexAvailableForWrite,
boolean mappingUpToDate,
boolean createdOnLatestVersion,
boolean readyForRoleMappingCleanupMigration,
Integer migrationsVersion,
SystemIndexDescriptor.MappingsVersion minClusterMappingVersion,
Integer indexMappingVersion,
Expand All @@ -720,6 +744,7 @@ public State(
this.mappingUpToDate = mappingUpToDate;
this.migrationsVersion = migrationsVersion;
this.createdOnLatestVersion = createdOnLatestVersion;
this.readyForRoleMappingCleanupMigration = readyForRoleMappingCleanupMigration;
this.minClusterMappingVersion = minClusterMappingVersion;
this.indexMappingVersion = indexMappingVersion;
this.concreteIndexName = concreteIndexName;
Expand All @@ -740,6 +765,7 @@ public boolean equals(Object o) {
&& indexAvailableForWrite == state.indexAvailableForWrite
&& mappingUpToDate == state.mappingUpToDate
&& createdOnLatestVersion == state.createdOnLatestVersion
&& readyForRoleMappingCleanupMigration == state.readyForRoleMappingCleanupMigration
&& Objects.equals(indexMappingVersion, state.indexMappingVersion)
&& Objects.equals(migrationsVersion, state.migrationsVersion)
&& Objects.equals(minClusterMappingVersion, state.minClusterMappingVersion)
Expand All @@ -762,6 +788,7 @@ public int hashCode() {
indexAvailableForWrite,
mappingUpToDate,
createdOnLatestVersion,
readyForRoleMappingCleanupMigration,
migrationsVersion,
minClusterMappingVersion,
indexMappingVersion,
Expand All @@ -786,6 +813,8 @@ public String toString() {
+ mappingUpToDate
+ ", createdOnLatestVersion="
+ createdOnLatestVersion
+ ", readyForRoleMappingCleanupMigration="
+ readyForRoleMappingCleanupMigration
+ ", migrationsVersion="
+ migrationsVersion
+ ", minClusterMappingVersion="
Expand Down
Loading