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

Always write empty role descriptor fields to index #110424

Merged
merged 8 commits into from
Jul 5, 2024

Conversation

jfreden
Copy link
Contributor

@jfreden jfreden commented Jul 3, 2024

Resolves: #110416

A RoleDescriptor's toXContent function doesn't serialize some fields that are not set. This currently works because when we do an update, we will always overwrite the full RoleDescriptor in the index to make sure fields that are not set, will be updated properly.

In the bulk role update, we use an upsert, that combined with not knowing what fields are set leads to us not clearing fields that are set to nothing in requests. To fix that we have some alternatives:

  • Change to do an index request instead of update request and always overwrite the whole doc.
  • Serialize the full RoleDescriptor when we write to the index to make sure all fields are as expected.

If we go with the first alternative, we can't leverage the noop functionality of the upsert anymore and for large volumes of roles being written, we would do a lot of unnecessary IO to write to the Lucene index. Since this is the expected behaviour of the Connectors project, I'm hesitant to going down that path.

This PR explores the second alternative to write empty fields for:

  • runAs
  • remoteIndices
  • remoteCluster
  • description

@jfreden jfreden added :Security/Security Security issues without another label >non-issue labels Jul 3, 2024
@jfreden jfreden marked this pull request as ready for review July 3, 2024 12:20
@elasticsearchmachine elasticsearchmachine added the Team:Security Meta label for security team label Jul 3, 2024
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-security (Team:Security)

@jfreden jfreden marked this pull request as draft July 4, 2024 09:52
@jfreden jfreden added test-full-bwc Trigger full BWC version matrix tests test-update-serverless labels Jul 4, 2024
true,
featureService.clusterHasFeature(clusterService.state(), SECURITY_ROLES_METADATA_FLATTENED)
);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I went back and forth on this but landed on that the cleanest approach is to handle the special case for the security index defaults and metadata_flattened logic here.

Simply using the docCreation is true is not enough since it's also used in the ApiKeyService in a couple of places, to create role descriptors. Since we don't do the same upsert operations on api keys, the default values are not needed and the metadata_falttened field is not needed. This could be fixed by providing a couple of extra parameters to toXContent or a context object of some sort, that would need to be populated by all callers. In the end this turned in to many booleans, instead of actually doing the logic where we have all the context.

The downside with this approach is that the role descriptor logic is now spread out.

* Make sure all top level fields for a RoleDescriptor have default values to make sure they can be set to empty in an upsert
* call to the roles API
*/
public void testAllTopFieldsHaveEmptyDefaultsForUpsert() throws IOException, IllegalAccessException {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The purpose of this test is to catch any new top level role descriptor fields that do not provide defaults since it's easy to forget to add that and there is no guarantee that a regular unit test would catch that.

{"cluster": ["all"],"indices": [{"names": ["*"],"privileges": ["read"]}]}, "test3":
{"cluster": ["all"],"indices": [{"names": ["*"],"privileges": ["write"]}]}}}""";

{"cluster": ["all"],"indices": [{"names": ["*"],"privileges": ["read"]}], "description": "something"}, "test3":
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added some more explicit validation that we can reset some top level fields. These things are already implicitly caught by other tests but I think it's nice to do some more validation.

@jfreden jfreden marked this pull request as ready for review July 5, 2024 06:50
@jfreden
Copy link
Contributor Author

jfreden commented Jul 5, 2024

Serverless CI Failure is unrelated:

java.lang.AssertionError: java.lang.IllegalStateException: Blob store is null

Copy link
Contributor

@albertzaharovits albertzaharovits left a comment

Choose a reason for hiding this comment

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

LGTM

@albertzaharovits
Copy link
Contributor

In the description you mention runAs but that already implements a default of [] when serialized by RoleDescriptor#toXContent right?

@jfreden
Copy link
Contributor Author

jfreden commented Jul 5, 2024

Yes, there is a check if runAs != null and exclude it if it is null, but runAs is never null as far as I can see.

@jfreden jfreden merged commit 1274a39 into elastic:main Jul 5, 2024
20 of 21 checks passed
jfreden added a commit to jfreden/elasticsearch that referenced this pull request Jul 5, 2024
* Always write empty role descriptor fields to index
elasticsearchmachine pushed a commit that referenced this pull request Jul 5, 2024
* Always write empty role descriptor fields to index
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>non-issue :Security/Security Security issues without another label Team:Security Meta label for security team test-full-bwc Trigger full BWC version matrix tests test-update-serverless v8.15.0 v8.16.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[CI] RoleWithDescriptionRestIT testCreateOrUpdateRoleWithDescription failing
3 participants