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

Change API token index actions to use action listeners and limit to 100 tokens outstanding #5147

Draft
wants to merge 4 commits into
base: feature/api-tokens
Choose a base branch
from

Conversation

derek-ho
Copy link
Collaborator

Description

Changes index operations on the api token index to be action listeners instead of action get. Also implements 100 api token limit.

Issues Resolved

[List any issues this PR will resolve]

Is this a backport? If so, please add backport PR # and/or commits #, and remove backport-failed label from the original PR.

Do these changes introduce new permission(s) to be displayed in the static dropdown on the front-end? If so, please open a draft PR in the security dashboards plugin and link the draft PR here

Testing

[Please provide details of testing done: unit testing, integration testing and manual testing]

Check List

  • New functionality includes testing
  • New functionality has been documented
  • New Roles/Permissions have a corresponding security dashboards plugin PR
  • API changes companion pull request created
  • Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

Copy link

codecov bot commented Mar 20, 2025

Codecov Report

Attention: Patch coverage is 29.72973% with 78 lines in your changes missing coverage. Please review.

Project coverage is 71.37%. Comparing base (0edba23) to head (cdfc2aa).
Report is 87 commits behind head on feature/api-tokens.

Files with missing lines Patch % Lines
...arch/security/action/apitokens/ApiTokenAction.java 0.00% 67 Missing ⚠️
...ecurity/action/apitokens/ApiTokenIndexHandler.java 73.07% 7 Missing ⚠️
.../security/action/apitokens/ApiTokenRepository.java 76.47% 3 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@                  Coverage Diff                   @@
##           feature/api-tokens    #5147      +/-   ##
======================================================
- Coverage               71.46%   71.37%   -0.09%     
======================================================
  Files                     334      355      +21     
  Lines                   22552    23475     +923     
  Branches                 3590     3686      +96     
======================================================
+ Hits                    16117    16756     +639     
- Misses                   4642     4883     +241     
- Partials                 1793     1836      +43     
Files with missing lines Coverage Δ
...opensearch/security/action/apitokens/ApiToken.java 91.20% <100.00%> (ø)
.../security/action/apitokens/ApiTokenRepository.java 88.63% <76.47%> (ø)
...ecurity/action/apitokens/ApiTokenIndexHandler.java 81.81% <73.07%> (ø)
...arch/security/action/apitokens/ApiTokenAction.java 22.64% <0.00%> (ø)

... and 17 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

for (ApiToken token : tokens.values()) {
builder.startObject();
builder.field(NAME_FIELD, token.getName());
builder.field(CREATION_TIME_FIELD, token.getCreationTime().toEpochMilli());
Copy link
Member

Choose a reason for hiding this comment

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

Can we call this issued_at to be aligned with the rfc? https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.6

try {
XContentBuilder builder = channel.newBuilder();
builder.startObject();
builder.field("Api Token: ", token);
Copy link
Member

Choose a reason for hiding this comment

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

Let's just have the field name be token. No need to include the colon here because this gets serialized to json and will get included.

// If count is ok, create the token
apiTokenRepository.createApiToken(name, clusterPermissions, indexPermissions, expiration, ActionListener.wrap(token -> {
// After successful creation, trigger the update action
ApiTokenUpdateRequest updateRequest = new ApiTokenUpdateRequest();
Copy link
Member

Choose a reason for hiding this comment

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

This logic should be centralized so its applied after any change like issuance of a new token or revocation to make sure each node of the cluster synchronizes with the change as it occurs.

if (deletedDocs == 0) {
listener.onFailure(new ApiTokenException("No token found with name " + name));
} else {
listener.onResponse(null);
Copy link
Member

Choose a reason for hiding this comment

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

No synchronization after delete? Will this ensure that the token is no longer able to be used regardless of which node a request is made against?

Signed-off-by: Derek Ho <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants