-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Limited-by role descriptors in Get/QueryApiKey response #89273
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
Merged
elasticsearchmachine
merged 3 commits into
elastic:main
from
ywangd:view-limited-by-role-descriptors
Aug 12, 2022
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pr: 89273 | ||
summary: Limited-by role descriptors in Get/QueryApiKey response | ||
area: Security | ||
type: enhancement | ||
issues: [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,10 +30,7 @@ public final class GetApiKeyRequest extends ActionRequest { | |
private final String apiKeyId; | ||
private final String apiKeyName; | ||
private final boolean ownedByAuthenticatedUser; | ||
|
||
public GetApiKeyRequest() { | ||
this(null, null, null, null, false); | ||
} | ||
private final boolean withLimitedBy; | ||
|
||
public GetApiKeyRequest(StreamInput in) throws IOException { | ||
super(in); | ||
|
@@ -46,20 +43,27 @@ public GetApiKeyRequest(StreamInput in) throws IOException { | |
} else { | ||
ownedByAuthenticatedUser = false; | ||
} | ||
if (in.getVersion().onOrAfter(Version.V_8_5_0)) { | ||
withLimitedBy = in.readBoolean(); | ||
} else { | ||
withLimitedBy = false; | ||
} | ||
} | ||
|
||
public GetApiKeyRequest( | ||
private GetApiKeyRequest( | ||
@Nullable String realmName, | ||
@Nullable String userName, | ||
@Nullable String apiKeyId, | ||
@Nullable String apiKeyName, | ||
boolean ownedByAuthenticatedUser | ||
boolean ownedByAuthenticatedUser, | ||
boolean withLimitedBy | ||
) { | ||
this.realmName = textOrNull(realmName); | ||
this.userName = textOrNull(userName); | ||
this.apiKeyId = textOrNull(apiKeyId); | ||
this.apiKeyName = textOrNull(apiKeyName); | ||
this.ownedByAuthenticatedUser = ownedByAuthenticatedUser; | ||
this.withLimitedBy = withLimitedBy; | ||
} | ||
|
||
private static String textOrNull(@Nullable String arg) { | ||
|
@@ -86,22 +90,28 @@ public boolean ownedByAuthenticatedUser() { | |
return ownedByAuthenticatedUser; | ||
} | ||
|
||
public boolean withLimitedBy() { | ||
return withLimitedBy; | ||
} | ||
|
||
/** | ||
* Creates get API key request for given realm name | ||
* @param realmName realm name | ||
* @return {@link GetApiKeyRequest} | ||
*/ | ||
@Deprecated | ||
public static GetApiKeyRequest usingRealmName(String realmName) { | ||
return new GetApiKeyRequest(realmName, null, null, null, false); | ||
return new GetApiKeyRequest(realmName, null, null, null, false, false); | ||
} | ||
|
||
/** | ||
* Creates get API key request for given user name | ||
* @param userName user name | ||
* @return {@link GetApiKeyRequest} | ||
*/ | ||
@Deprecated | ||
public static GetApiKeyRequest usingUserName(String userName) { | ||
return new GetApiKeyRequest(null, userName, null, null, false); | ||
return new GetApiKeyRequest(null, userName, null, null, false, false); | ||
} | ||
|
||
/** | ||
|
@@ -110,8 +120,9 @@ public static GetApiKeyRequest usingUserName(String userName) { | |
* @param userName user name | ||
* @return {@link GetApiKeyRequest} | ||
*/ | ||
@Deprecated | ||
public static GetApiKeyRequest usingRealmAndUserName(String realmName, String userName) { | ||
return new GetApiKeyRequest(realmName, userName, null, null, false); | ||
return new GetApiKeyRequest(realmName, userName, null, null, false, false); | ||
} | ||
|
||
/** | ||
|
@@ -121,8 +132,9 @@ public static GetApiKeyRequest usingRealmAndUserName(String realmName, String us | |
* {@code false} | ||
* @return {@link GetApiKeyRequest} | ||
*/ | ||
@Deprecated | ||
public static GetApiKeyRequest usingApiKeyId(String apiKeyId, boolean ownedByAuthenticatedUser) { | ||
return new GetApiKeyRequest(null, null, apiKeyId, null, ownedByAuthenticatedUser); | ||
return new GetApiKeyRequest(null, null, apiKeyId, null, ownedByAuthenticatedUser, false); | ||
} | ||
|
||
/** | ||
|
@@ -133,21 +145,23 @@ public static GetApiKeyRequest usingApiKeyId(String apiKeyId, boolean ownedByAut | |
* @return {@link GetApiKeyRequest} | ||
*/ | ||
public static GetApiKeyRequest usingApiKeyName(String apiKeyName, boolean ownedByAuthenticatedUser) { | ||
return new GetApiKeyRequest(null, null, null, apiKeyName, ownedByAuthenticatedUser); | ||
return new GetApiKeyRequest(null, null, null, apiKeyName, ownedByAuthenticatedUser, false); | ||
} | ||
|
||
/** | ||
* Creates get api key request to retrieve api key information for the api keys owned by the current authenticated user. | ||
*/ | ||
@Deprecated | ||
public static GetApiKeyRequest forOwnedApiKeys() { | ||
return new GetApiKeyRequest(null, null, null, null, true); | ||
return new GetApiKeyRequest(null, null, null, null, true, false); | ||
} | ||
|
||
/** | ||
* Creates get api key request to retrieve api key information for all api keys if the authenticated user is authorized to do so. | ||
*/ | ||
@Deprecated | ||
public static GetApiKeyRequest forAllApiKeys() { | ||
return new GetApiKeyRequest(); | ||
return GetApiKeyRequest.builder().build(); | ||
} | ||
|
||
@Override | ||
|
@@ -185,6 +199,9 @@ public void writeTo(StreamOutput out) throws IOException { | |
if (out.getVersion().onOrAfter(Version.V_7_4_0)) { | ||
out.writeOptionalBoolean(ownedByAuthenticatedUser); | ||
} | ||
if (out.getVersion().onOrAfter(Version.V_8_5_0)) { | ||
out.writeBoolean(withLimitedBy); | ||
} | ||
} | ||
|
||
@Override | ||
|
@@ -200,11 +217,67 @@ public boolean equals(Object o) { | |
&& Objects.equals(realmName, that.realmName) | ||
&& Objects.equals(userName, that.userName) | ||
&& Objects.equals(apiKeyId, that.apiKeyId) | ||
&& Objects.equals(apiKeyName, that.apiKeyName); | ||
&& Objects.equals(apiKeyName, that.apiKeyName) | ||
&& withLimitedBy == that.withLimitedBy; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(realmName, userName, apiKeyId, apiKeyName, ownedByAuthenticatedUser); | ||
return Objects.hash(realmName, userName, apiKeyId, apiKeyName, ownedByAuthenticatedUser, withLimitedBy); | ||
} | ||
|
||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
public static class Builder { | ||
private String realmName = null; | ||
private String userName = null; | ||
private String apiKeyId = null; | ||
private String apiKeyName = null; | ||
private boolean ownedByAuthenticatedUser = false; | ||
private boolean withLimitedBy = false; | ||
Comment on lines
+233
to
+239
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a builder class and deprecated the various convenient methods. I think this request class has come to a point that a Builder class is justified. I will have a follow-up PR to remove those deprecated methods. |
||
|
||
public Builder realmName(String realmName) { | ||
this.realmName = realmName; | ||
return this; | ||
} | ||
|
||
public Builder userName(String userName) { | ||
this.userName = userName; | ||
return this; | ||
} | ||
|
||
public Builder apiKeyId(String apiKeyId) { | ||
this.apiKeyId = apiKeyId; | ||
return this; | ||
} | ||
|
||
public Builder apiKeyName(String apiKeyName) { | ||
this.apiKeyName = apiKeyName; | ||
return this; | ||
} | ||
|
||
public Builder ownedByAuthenticatedUser() { | ||
return ownedByAuthenticatedUser(true); | ||
} | ||
|
||
public Builder ownedByAuthenticatedUser(boolean ownedByAuthenticatedUser) { | ||
this.ownedByAuthenticatedUser = ownedByAuthenticatedUser; | ||
return this; | ||
} | ||
|
||
public Builder withLimitedBy() { | ||
return withLimitedBy(true); | ||
} | ||
|
||
public Builder withLimitedBy(boolean withLimitedBy) { | ||
this.withLimitedBy = withLimitedBy; | ||
return this; | ||
} | ||
|
||
public GetApiKeyRequest build() { | ||
return new GetApiKeyRequest(realmName, userName, apiKeyId, apiKeyName, ownedByAuthenticatedUser, withLimitedBy); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this new
RoleDescriptorsIntersection
mainly for future proof (in case we support derived keys properly). It also mirrors what we did with RoleReferenceIntesection and LimitedRole.It does also introduce some code level overhead. Let me know if you dislike it and prefer a simpler
List<RoleDescriptor>
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missed this comment.
RoleDescriptorsIntersection
makes sense to me. We know we will need something like this and all the work is already there now 👍