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

Expose the invalidation_time field in Get/Query ApiKey APIs #102472

Merged

Conversation

jfreden
Copy link
Contributor

@jfreden jfreden commented Nov 22, 2023

This PR exposes invalidation_time in responses from GetApiKey and QueryApiKey API
To address the first half of #92404.

As a follow up to this PR invalidation_time needs to be supported as a valid query value for the QueryApiKey API. There will also be a separate PR to add it to the API docs for GetApiKey and QueryApiKey.

@jfreden jfreden added >enhancement :Security/Client Security in clients (Transport, Rest) labels Nov 22, 2023
@elasticsearchmachine elasticsearchmachine added v8.12.0 external-contributor Pull request authored by a developer outside the Elasticsearch team labels Nov 22, 2023
@elasticsearchmachine
Copy link
Collaborator

Hi @jfreden, I've created a changelog YAML for you.

@n1v0lg n1v0lg removed the external-contributor Pull request authored by a developer outside the Elasticsearch team label Nov 22, 2023
@n1v0lg n1v0lg self-requested a review November 22, 2023 13:32
Copy link
Contributor

@n1v0lg n1v0lg left a comment

Choose a reason for hiding this comment

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

Looking great 👍 discussed a few tweaks on Zoom, will re-review afterwards

@jfreden jfreden marked this pull request as ready for review November 23, 2023 13:29
@jfreden jfreden force-pushed the expose-invalidation_time-apikey-apis branch from f7127ba to d23dc60 Compare November 23, 2023 13:29
@elasticsearchmachine elasticsearchmachine added the Team:Security Meta label for security team label Nov 23, 2023
@elasticsearchmachine
Copy link
Collaborator

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

@jfreden
Copy link
Contributor Author

jfreden commented Nov 23, 2023

@elasticmachine update branch

Copy link
Contributor

@n1v0lg n1v0lg left a comment

Choose a reason for hiding this comment

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

LGTM 🚀 Great first PR!

A couple of (not strongly held) nits and smaller tweaks but nothing that needs another round of review.

One easy test coverage boost suggestion:

I'd add an assertion on expiration to this IT here -- that way we also cover the Query API with an integration test.

@@ -0,0 +1,5 @@
pr: 102472
summary: Expose the `invalidation_time` field in Get/Query `ApiKey` APIs
area: Client
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 Security/Security or Security/Authentication are more appropriate labels since API keys are primarily authentication primitives. If you update the label on the PR, I believe the changelog should get updated automatically.

@@ -0,0 +1,5 @@
pr: 102472
summary: Expose the `invalidation_time` field in Get/Query `ApiKey` APIs
Copy link
Contributor

@n1v0lg n1v0lg Nov 23, 2023

Choose a reason for hiding this comment

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

Nit: I'd go with invalidation here as well as in ApiKey::toXContent since that's the user-facing part. I'm added in-line suggestions where I've come across it but may have missed a few parts.

@@ -107,6 +108,7 @@ public ApiKey(
Instant creation,
Instant expiration,
boolean invalidated,
Instant invalidation,
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: would mark @Nullable

.field("metadata", (metadata == null ? Map.of() : metadata));
builder.field("invalidated", invalidated);
if (invalidation != null) {
builder.field("invalidation_time", invalidation.toEpochMilli());
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
builder.field("invalidation_time", invalidation.toEpochMilli());
builder.field("invalidation", invalidation.toEpochMilli());

@@ -385,6 +419,7 @@ public boolean equals(Object obj) {
PARSER.declareLong(constructorArg(), new ParseField("creation"));
PARSER.declareLong(optionalConstructorArg(), new ParseField("expiration"));
PARSER.declareBoolean(constructorArg(), new ParseField("invalidated"));
PARSER.declareLong(optionalConstructorArg(), new ParseField("invalidation_time"));
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
PARSER.declareLong(optionalConstructorArg(), new ParseField("invalidation_time"));
PARSER.declareLong(optionalConstructorArg(), new ParseField("invalidation"));

import static org.elasticsearch.xpack.core.security.action.apikey.ApiKeyTests.randomApiKeyInstance;
import static org.hamcrest.Matchers.nullValue;

public class ApiKeySerializationTests extends AbstractWireSerializingTestCase<ApiKey> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Neat!

@@ -192,6 +197,7 @@ public void testToXContent() throws IOException {
"creation": 100000,
"expiration": 10000000,
"invalidated": true,
"invalidation_time": 100000000,
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"invalidation_time": 100000000,
"invalidation": 100000000,

private static final String MANAGE_SECURITY_USER = "manage_security_user";

@Before
public void createUsers() throws IOException {
createUser(MANAGE_OWN_API_KEY_USER, END_USER_PASSWORD, List.of("manage_own_api_key_role"));
createRole("manage_own_api_key_role", Set.of("manage_own_api_key"));
createUser(MANAGE_API_KEY_USER, END_USER_PASSWORD, List.of("manage_api_key_role"));
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: I don't think we need the new role and user -- either of the existing ones should work here (for sure MANAGE_SECURITY_USER).

@@ -1677,10 +1677,10 @@ private void indexInvalidation(Collection<String> apiKeyIds, ActionListener<Inva
listener.onFailure(new ElasticsearchSecurityException("No api key ids provided for invalidation"));
} else {
BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
final long invalidationTime = clock.instant().toEpochMilli();
final long invalidation = clock.instant().toEpochMilli();
Copy link
Contributor

@n1v0lg n1v0lg Nov 23, 2023

Choose a reason for hiding this comment

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

Nit: sorry, I forgot to mention this bit: I'd stick with invalidationTime here since it's a different context. Here (i.e., in ApiKeyService), we do refer to the time values with a time suffix, e.g., creationTime, expirationTime.

@@ -1024,7 +1020,7 @@ public void testValidateApiKey() throws Exception {
assertFalse(result.isAuthenticated());

// key is invalidated
apiKeyDoc = buildApiKeyDoc(hash, -1, true);
apiKeyDoc = buildApiKeyDoc(hash, -1, true, -1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Could randomize a timestamp here since the key is invalidated (it's not the primary purpose of the test but might as well add that coverage).

@jfreden jfreden added :Security/Security Security issues without another label and removed :Security/Client Security in clients (Transport, Rest) labels Nov 23, 2023
@jfreden jfreden added auto-merge Automatically merge pull request when CI checks pass (NB doesn't wait for reviews!) test-update-serverless labels Nov 24, 2023
@jfreden jfreden force-pushed the expose-invalidation_time-apikey-apis branch from 51c88a8 to 760cade Compare November 24, 2023 09:20
@elasticsearchmachine elasticsearchmachine merged commit f3d5a65 into elastic:main Nov 24, 2023
19 checks passed
@jfreden jfreden deleted the expose-invalidation_time-apikey-apis branch November 24, 2023 11:31
elasticsearchmachine pushed a commit that referenced this pull request Nov 27, 2023
timgrein pushed a commit to timgrein/elasticsearch that referenced this pull request Nov 30, 2023
timgrein pushed a commit to timgrein/elasticsearch that referenced this pull request Nov 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-merge Automatically merge pull request when CI checks pass (NB doesn't wait for reviews!) >enhancement :Security/Security Security issues without another label Team:Security Meta label for security team test-update-serverless v8.12.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants