-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Create API Key on behalf of other user #52886
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
tvernum
merged 9 commits into
elastic:master
from
tvernum:feature/48716-api-key-other-user
Mar 12, 2020
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f4cbe14
Create API Key on behalf of other user
tvernum 21c16ff
Revert formatting change
tvernum 17d292f
Revert unnecessary changes
tvernum 24e0fcc
Merge branch 'master' into feature/48716-api-key-other-user
tvernum f580825
Move API key test to new QA/security-trial project
tvernum dbd7726
Merge branch 'master' into feature/48716-api-key-other-user
tvernum 036c402
Address feedback
tvernum 8215d25
Merge branch 'master' into feature/48716-api-key-other-user
tvernum 04339c6
Address more feedback
tvernum 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
24 changes: 24 additions & 0 deletions
24
...in/core/src/main/java/org/elasticsearch/xpack/core/security/action/GrantApiKeyAction.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,24 @@ | ||
/* | ||
* 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.ActionType; | ||
|
||
/** | ||
* ActionType for the creation of an API key on behalf of another user | ||
* This returns the {@link CreateApiKeyResponse} because the REST output is intended to be identical to the {@link CreateApiKeyAction}. | ||
*/ | ||
public final class GrantApiKeyAction extends ActionType<CreateApiKeyResponse> { | ||
|
||
public static final String NAME = "cluster:admin/xpack/security/api_key/grant"; | ||
public static final GrantApiKeyAction INSTANCE = new GrantApiKeyAction(); | ||
|
||
private GrantApiKeyAction() { | ||
super(NAME, CreateApiKeyResponse::new); | ||
} | ||
|
||
} |
171 changes: 171 additions & 0 deletions
171
...n/core/src/main/java/org/elasticsearch/xpack/core/security/action/GrantApiKeyRequest.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,171 @@ | ||
/* | ||
* 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.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.io.stream.Writeable; | ||
import org.elasticsearch.common.settings.SecureString; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
import static org.elasticsearch.action.ValidateActions.addValidationError; | ||
|
||
/** | ||
* Request class used for the creation of an API key on behalf of another user. | ||
* Logically this is similar to {@link CreateApiKeyRequest}, but is for cases when the user that has permission to call this action | ||
* is different to the user for whom the API key should be created | ||
*/ | ||
public final class GrantApiKeyRequest extends ActionRequest { | ||
|
||
public static final WriteRequest.RefreshPolicy DEFAULT_REFRESH_POLICY = WriteRequest.RefreshPolicy.WAIT_UNTIL; | ||
public static final String PASSWORD_GRANT_TYPE = "password"; | ||
public static final String ACCESS_TOKEN_GRANT_TYPE = "access_token"; | ||
|
||
/** | ||
* Fields related to the end user authentication | ||
*/ | ||
public static class Grant implements Writeable { | ||
private String type; | ||
private String username; | ||
private SecureString password; | ||
private SecureString accessToken; | ||
|
||
public Grant() { | ||
} | ||
|
||
public Grant(StreamInput in) throws IOException { | ||
this.type = in.readString(); | ||
this.username = in.readOptionalString(); | ||
this.password = in.readOptionalSecureString(); | ||
this.accessToken = in.readOptionalSecureString(); | ||
} | ||
|
||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeString(type); | ||
out.writeOptionalString(username); | ||
out.writeOptionalSecureString(password); | ||
out.writeOptionalSecureString(accessToken); | ||
} | ||
|
||
public String getType() { | ||
return type; | ||
} | ||
|
||
public String getUsername() { | ||
return username; | ||
} | ||
|
||
public SecureString getPassword() { | ||
return password; | ||
} | ||
|
||
public SecureString getAccessToken() { | ||
return accessToken; | ||
} | ||
|
||
public void setType(String type) { | ||
this.type = type; | ||
} | ||
|
||
public void setUsername(String username) { | ||
this.username = username; | ||
} | ||
|
||
public void setPassword(SecureString password) { | ||
this.password = password; | ||
} | ||
|
||
public void setAccessToken(SecureString accessToken) { | ||
this.accessToken = accessToken; | ||
} | ||
} | ||
|
||
private final Grant grant; | ||
private CreateApiKeyRequest apiKey; | ||
private WriteRequest.RefreshPolicy refreshPolicy; | ||
|
||
public GrantApiKeyRequest() { | ||
this.grant = new Grant(); | ||
this.apiKey = new CreateApiKeyRequest(); | ||
this.refreshPolicy = DEFAULT_REFRESH_POLICY; | ||
} | ||
|
||
public GrantApiKeyRequest(StreamInput in) throws IOException { | ||
super(in); | ||
this.grant = new Grant(in); | ||
this.apiKey = new CreateApiKeyRequest(in); | ||
this.refreshPolicy = WriteRequest.RefreshPolicy.readFrom(in); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
grant.writeTo(out); | ||
apiKey.writeTo(out); | ||
refreshPolicy.writeTo(out); | ||
} | ||
|
||
public WriteRequest.RefreshPolicy getRefreshPolicy() { | ||
return refreshPolicy; | ||
} | ||
|
||
public void setRefreshPolicy(WriteRequest.RefreshPolicy refreshPolicy) { | ||
this.refreshPolicy = Objects.requireNonNull(refreshPolicy, "refresh policy may not be null"); | ||
} | ||
|
||
public Grant getGrant() { | ||
return grant; | ||
} | ||
|
||
public CreateApiKeyRequest getApiKeyRequest() { | ||
return apiKey; | ||
} | ||
|
||
public void setApiKeyRequest(CreateApiKeyRequest apiKeyRequest) { | ||
this.apiKey = Objects.requireNonNull(apiKeyRequest, "Cannot set a null api_key"); | ||
} | ||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
ActionRequestValidationException validationException = apiKey.validate(); | ||
if (grant.type == null) { | ||
validationException = addValidationError("[grant_type] is required", validationException); | ||
} else if (grant.type.equals(PASSWORD_GRANT_TYPE)) { | ||
validationException = validateRequiredField("username", grant.username, validationException); | ||
validationException = validateRequiredField("password", grant.password, validationException); | ||
validationException = validateUnsupportedField("access_token", grant.accessToken, validationException); | ||
} else if (grant.type.equals(ACCESS_TOKEN_GRANT_TYPE)) { | ||
validationException = validateRequiredField("access_token", grant.accessToken, validationException); | ||
validationException = validateUnsupportedField("username", grant.username, validationException); | ||
validationException = validateUnsupportedField("password", grant.password, validationException); | ||
} else { | ||
validationException = addValidationError("grant_type [" + grant.type + "] is not supported", validationException); | ||
} | ||
return validationException; | ||
} | ||
|
||
private ActionRequestValidationException validateRequiredField(String fieldName, CharSequence fieldValue, | ||
ActionRequestValidationException validationException) { | ||
if (fieldValue == null || fieldValue.length() == 0) { | ||
return addValidationError("[" + fieldName + "] is required for grant_type [" + grant.type + "]", validationException); | ||
} | ||
return validationException; | ||
} | ||
|
||
private ActionRequestValidationException validateUnsupportedField(String fieldName, CharSequence fieldValue, | ||
ActionRequestValidationException validationException) { | ||
if (fieldValue != null && fieldValue.length() > 0) { | ||
return addValidationError("[" + fieldName + "] is not supported for grant_type [" + grant.type + "]", validationException); | ||
} | ||
return validationException; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...ity-basic/src/test/java/org/elasticsearch/xpack/security/SecurityInBasicRestTestCase.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,49 @@ | ||
/* | ||
* 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.security; | ||
|
||
import org.elasticsearch.client.RestHighLevelClient; | ||
import org.elasticsearch.common.settings.SecureString; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.common.util.concurrent.ThreadContext; | ||
import org.elasticsearch.test.rest.ESRestTestCase; | ||
|
||
import java.util.List; | ||
|
||
import static org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken.basicAuthHeaderValue; | ||
|
||
public abstract class SecurityInBasicRestTestCase extends ESRestTestCase { | ||
private RestHighLevelClient highLevelAdminClient; | ||
|
||
@Override | ||
protected Settings restAdminSettings() { | ||
String token = basicAuthHeaderValue("admin_user", new SecureString("admin-password".toCharArray())); | ||
return Settings.builder() | ||
.put(ThreadContext.PREFIX + ".Authorization", token) | ||
.build(); | ||
} | ||
|
||
@Override | ||
protected Settings restClientSettings() { | ||
String token = basicAuthHeaderValue("security_test_user", new SecureString("security-test-password".toCharArray())); | ||
return Settings.builder() | ||
.put(ThreadContext.PREFIX + ".Authorization", token) | ||
.build(); | ||
} | ||
|
||
private RestHighLevelClient getHighLevelAdminClient() { | ||
if (highLevelAdminClient == null) { | ||
highLevelAdminClient = new RestHighLevelClient( | ||
adminClient(), | ||
ignore -> { | ||
}, | ||
List.of()) { | ||
}; | ||
} | ||
return highLevelAdminClient; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
apply plugin: 'elasticsearch.testclusters' | ||
apply plugin: 'elasticsearch.standalone-rest-test' | ||
apply plugin: 'elasticsearch.rest-test' | ||
|
||
dependencies { | ||
testCompile project(path: xpackModule('core'), configuration: 'default') | ||
testCompile project(path: xpackModule('security'), configuration: 'testArtifacts') | ||
testCompile project(path: xpackModule('core'), configuration: 'testArtifacts') | ||
} | ||
|
||
testClusters.integTest { | ||
testDistribution = 'DEFAULT' | ||
numberOfNodes = 2 | ||
|
||
setting 'xpack.ilm.enabled', 'false' | ||
setting 'xpack.ml.enabled', 'false' | ||
setting 'xpack.license.self_generated.type', 'trial' | ||
setting 'xpack.security.enabled', 'true' | ||
setting 'xpack.security.ssl.diagnose.trust', 'true' | ||
setting 'xpack.security.http.ssl.enabled', 'false' | ||
setting 'xpack.security.transport.ssl.enabled', 'false' | ||
setting 'xpack.security.authc.token.enabled', 'true' | ||
setting 'xpack.security.authc.api_key.enabled', 'true' | ||
|
||
extraConfigFile 'roles.yml', file('src/test/resources/roles.yml') | ||
user username: "admin_user", password: "admin-password" | ||
user username: "security_test_user", password: "security-test-password", role: "security_test_role" | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.