Skip to content
Closed
Show file tree
Hide file tree
Changes from 6 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 @@ -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_MIGRATION = def(8_517_00_0, Version.LUCENE_9_12_0);
Copy link
Contributor Author

@jfreden jfreden Oct 15, 2024

Choose a reason for hiding this comment

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

This is needed to allow us to check if an index was created on the same version as the migration. This means that the index was created with all the settings/mappings/docs needed, so we don't need to run the migration (it can be short-circuit and skipped since there is nothing to migrate).


/*
* STOP! READ THIS FIRST! No, really,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,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 @@ -189,7 +189,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,6 +47,7 @@
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 java.time.Instant;
Expand Down Expand Up @@ -74,7 +76,8 @@
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 String HANDLER_ROLE_MAPPINGS_NAME = "role_mappings";
private static final Logger logger = LogManager.getLogger(SecurityIndexManager.class);

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

private static boolean isReservedRoleMappingsSynced(ClusterState clusterState) {
Copy link
Contributor

@n1v0lg n1v0lg Oct 22, 2024

Choose a reason for hiding this comment

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

WDYT about calling this readyForRoleMappingCleanupMigration? It feels more relevant as a security-index concept, and better reflects the fact that true can also mean "run migration even if there are no role mappings to sync"

Alternatively, I think we could entirely remove this from the index manager if we expose a client instance (or the ClusterService) to the checkPreConditions method. That would give us cluster state instead of relying on the index manager.

Copy link
Contributor Author

@jfreden jfreden Oct 23, 2024

Choose a reason for hiding this comment

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

WDYT about calling this readyForRoleMappingCleanupMigration

My intention was to decouple it from the migration but I guess that's pretty silly, because it's only ever going to be used for that. I'll update it to be more explicit.

Alternatively, I think we could entirely remove this from the index manager if we expose a client instance (or the ClusterService) to the checkPreConditions method. That would give us cluster state instead of relying on the index manager.

The migrations are only triggered on security index manager state changes (it's a listener). So if the migration is not ready to be executed, we need a way to make sure that the state changes when it is and that's why this needs to be part of the state.

That's what I'm referring to here: #114830 (comment) ideally I think there should be a separate state that tracks the migrations, that includes state changes that are not necessarily specific to the security index.

ReservedStateMetadata fileSettingsMetadata = clusterState.metadata().reservedStateMetadata().get(FILE_SETTINGS_METADATA_NAMESPACE);
if (fileSettingsMetadata != null && fileSettingsMetadata.handlers().containsKey(HANDLER_ROLE_MAPPINGS_NAME)) {
int fileSettingsMetadataSize = fileSettingsMetadata.handlers().get(HANDLER_ROLE_MAPPINGS_NAME).keys().size();
if (fileSettingsMetadataSize > 0) {
return fileSettingsMetadataSize == RoleMappingMetadata.getFromClusterState(clusterState).getRoleMappings().size();
}
}
return true;
}

@Override
public void clusterChanged(ClusterChangedEvent event) {
if (event.state().blocks().hasGlobalBlock(GatewayService.STATE_NOT_RECOVERED_BLOCK)) {
Expand All @@ -284,6 +298,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 reservedRoleMappingsSynced = isReservedRoleMappingsSynced(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 +329,7 @@ public void clusterChanged(ClusterChangedEvent event) {
indexAvailableForWrite,
mappingIsUpToDate,
createdOnLatestVersion,
reservedRoleMappingsSynced,
migrationsVersion,
minClusterMappingVersion,
indexMappingVersion,
Expand Down Expand Up @@ -438,7 +454,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 +688,7 @@ public static class State {
false,
false,
false,
false,
null,
null,
null,
Expand All @@ -686,6 +704,7 @@ public static class State {
public final boolean indexAvailableForWrite;
public final boolean mappingUpToDate;
public final boolean createdOnLatestVersion;
public final boolean reservedRoleMappingsSynced;
public final Integer migrationsVersion;
// Min mapping version supported by the descriptors in the cluster
public final SystemIndexDescriptor.MappingsVersion minClusterMappingVersion;
Expand All @@ -704,6 +723,7 @@ public State(
boolean indexAvailableForWrite,
boolean mappingUpToDate,
boolean createdOnLatestVersion,
boolean reservedRoleMappingsSynced,
Integer migrationsVersion,
SystemIndexDescriptor.MappingsVersion minClusterMappingVersion,
Integer indexMappingVersion,
Expand All @@ -720,6 +740,7 @@ public State(
this.mappingUpToDate = mappingUpToDate;
this.migrationsVersion = migrationsVersion;
this.createdOnLatestVersion = createdOnLatestVersion;
this.reservedRoleMappingsSynced = reservedRoleMappingsSynced;
this.minClusterMappingVersion = minClusterMappingVersion;
this.indexMappingVersion = indexMappingVersion;
this.concreteIndexName = concreteIndexName;
Expand All @@ -740,6 +761,7 @@ public boolean equals(Object o) {
&& indexAvailableForWrite == state.indexAvailableForWrite
&& mappingUpToDate == state.mappingUpToDate
&& createdOnLatestVersion == state.createdOnLatestVersion
&& reservedRoleMappingsSynced == state.reservedRoleMappingsSynced
&& Objects.equals(indexMappingVersion, state.indexMappingVersion)
&& Objects.equals(migrationsVersion, state.migrationsVersion)
&& Objects.equals(minClusterMappingVersion, state.minClusterMappingVersion)
Expand All @@ -762,6 +784,7 @@ public int hashCode() {
indexAvailableForWrite,
mappingUpToDate,
createdOnLatestVersion,
reservedRoleMappingsSynced,
migrationsVersion,
minClusterMappingVersion,
indexMappingVersion,
Expand All @@ -786,6 +809,8 @@ public String toString() {
+ mappingUpToDate
+ ", createdOnLatestVersion="
+ createdOnLatestVersion
+ ", reservedRoleMappingsSynced="
+ reservedRoleMappingsSynced
+ ", migrationsVersion="
+ migrationsVersion
+ ", minClusterMappingVersion="
Expand Down
Loading