-
Notifications
You must be signed in to change notification settings - Fork 25.9k
Security: add create api key transport action #34572
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
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
ff5d47d
Security: add create api key transport action
jaymode 7446f3d
Merge branch 'master' into create_api_key
jaymode aac38fe
Merge branch 'master' into create_api_key
jaymode 4dfb7ed
add streaminput super constructors
jaymode 710ab24
check name when setting
jaymode eacd0da
fix class name
jaymode 28d2e72
fail if role retrieval was not a success
jaymode 758abac
add api key service
jaymode a4c2db8
fix test
jaymode a8bccfa
add setting
jaymode 02bd86d
add client action
jaymode 9a0260e
Merge branch 'master' into create_api_key
jaymode 6e74f5a
add super calls for reading
jaymode 60a06fd
delete unimplemented rest action
jaymode 68cce7c
use unmodifiable list
jaymode fddba0c
use securestring in response
jaymode 447fb30
fix securestring to xcontent
jaymode 6a44c9b
Merge branch 'security_api_keys' into create_api_key
jaymode e6e82c7
provide id with key, only store hash of key
jaymode 54d753f
Merge branch 'security_api_keys' into create_api_key
jaymode 03a45ea
Merge branch 'security_api_keys' into create_api_key
jaymode a9e9c61
use writeable.reader for response
jaymode a9b7e3f
s/LOGGER/logger
jaymode a4d163d
no key in id
jaymode 2a06c0f
assert no key in id
jaymode a818b45
allow configurable hash
jaymode 40aea03
rename setting
jaymode 75ae8d4
more name restrictions
jaymode 68104e2
Merge branch 'security_api_keys' into create_api_key
jaymode 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
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
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
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
33 changes: 33 additions & 0 deletions
33
...n/core/src/main/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyAction.java
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,33 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.core.security.action; | ||
|
|
||
| import org.elasticsearch.action.Action; | ||
| import org.elasticsearch.common.io.stream.Writeable; | ||
|
|
||
| /** | ||
| * Action for the creation of an API key | ||
| */ | ||
| public final class CreateApiKeyAction extends Action<CreateApiKeyResponse> { | ||
|
|
||
| public static final String NAME = "cluster:admin/xpack/security/api_key/create"; | ||
| public static final CreateApiKeyAction INSTANCE = new CreateApiKeyAction(); | ||
|
|
||
| private CreateApiKeyAction() { | ||
| super(NAME); | ||
| } | ||
|
|
||
| @Override | ||
| public CreateApiKeyResponse newResponse() { | ||
| throw new UnsupportedOperationException("usage of Streamable is to be replaced by Writeable"); | ||
| } | ||
|
|
||
| @Override | ||
| public Writeable.Reader<CreateApiKeyResponse> getResponseReader() { | ||
| return CreateApiKeyResponse::new; | ||
| } | ||
| } |
114 changes: 114 additions & 0 deletions
114
.../core/src/main/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyRequest.java
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,114 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License; | ||
| * you may not use this file except in compliance with the Elastic License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.core.security.action; | ||
|
|
||
| import org.elasticsearch.action.ActionRequest; | ||
| import org.elasticsearch.action.ActionRequestValidationException; | ||
| import org.elasticsearch.action.support.WriteRequest; | ||
| import org.elasticsearch.common.Strings; | ||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
| import org.elasticsearch.common.io.stream.StreamOutput; | ||
| import org.elasticsearch.common.unit.TimeValue; | ||
| import org.elasticsearch.xpack.core.security.authz.RoleDescriptor; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| import static org.elasticsearch.action.ValidateActions.addValidationError; | ||
|
|
||
| /** | ||
| * Request class used for the creation of an API key. The request requires a name to be provided | ||
| * and optionally an expiration time and permission limitation can be provided. | ||
| */ | ||
| public final class CreateApiKeyRequest extends ActionRequest { | ||
|
|
||
| private String name; | ||
| private TimeValue expiration; | ||
| private List<RoleDescriptor> roleDescriptors = Collections.emptyList(); | ||
| private WriteRequest.RefreshPolicy refreshPolicy = WriteRequest.RefreshPolicy.WAIT_UNTIL; | ||
|
|
||
| public CreateApiKeyRequest() {} | ||
|
|
||
| public CreateApiKeyRequest(StreamInput in) throws IOException { | ||
| super(in); | ||
| this.name = in.readString(); | ||
| this.expiration = in.readOptionalTimeValue(); | ||
| this.roleDescriptors = Collections.unmodifiableList(in.readList(RoleDescriptor::new)); | ||
| this.refreshPolicy = WriteRequest.RefreshPolicy.readFrom(in); | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| if (Strings.hasText(name)) { | ||
| this.name = name; | ||
| } else { | ||
| throw new IllegalArgumentException("name must not be null or empty"); | ||
| } | ||
| } | ||
|
|
||
| public TimeValue getExpiration() { | ||
| return expiration; | ||
| } | ||
|
|
||
| public void setExpiration(TimeValue expiration) { | ||
| this.expiration = expiration; | ||
| } | ||
|
|
||
| public List<RoleDescriptor> getRoleDescriptors() { | ||
| return roleDescriptors; | ||
| } | ||
|
|
||
| public void setRoleDescriptors(List<RoleDescriptor> roleDescriptors) { | ||
| this.roleDescriptors = Collections.unmodifiableList(Objects.requireNonNull(roleDescriptors, "role descriptors may not be null")); | ||
| } | ||
|
|
||
| public WriteRequest.RefreshPolicy getRefreshPolicy() { | ||
| return refreshPolicy; | ||
| } | ||
|
|
||
| public void setRefreshPolicy(WriteRequest.RefreshPolicy refreshPolicy) { | ||
| this.refreshPolicy = Objects.requireNonNull(refreshPolicy, "refresh policy may not be null"); | ||
| } | ||
|
|
||
| @Override | ||
| public ActionRequestValidationException validate() { | ||
| ActionRequestValidationException validationException = null; | ||
| if (Strings.isNullOrEmpty(name)) { | ||
| validationException = addValidationError("name is required", validationException); | ||
| } else { | ||
| if (name.length() > 256) { | ||
| validationException = addValidationError("name may not be more than 256 characters long", validationException); | ||
| } | ||
| if (name.equals(name.trim()) == false) { | ||
| validationException = addValidationError("name may not begin or end with whitespace", validationException); | ||
| } | ||
| if (name.startsWith("_")) { | ||
| validationException = addValidationError("name may not begin with an underscore", validationException); | ||
| } | ||
| } | ||
| return validationException; | ||
| } | ||
|
|
||
| @Override | ||
| public void writeTo(StreamOutput out) throws IOException { | ||
| super.writeTo(out); | ||
| out.writeString(name); | ||
| out.writeOptionalTimeValue(expiration); | ||
| out.writeList(roleDescriptors); | ||
| refreshPolicy.writeTo(out); | ||
| } | ||
|
|
||
| @Override | ||
| public void readFrom(StreamInput in) { | ||
| throw new UnsupportedOperationException("usage of Streamable is to be replaced by Writeable"); | ||
| } | ||
| } | ||
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.
Should we have any other restrictions on names?
I feel like starting or ending in whitespace is likely to cause confusion, and perhaps we should reserve names start with
_just in case we need them in the future?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.
Added those restrictions