-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Service Accounts - Get service account API #71315
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 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
282fa9e
Service Accounts - Get service account API
ywangd af10ba6
suppress casting warning
ywangd 2aefa89
fix operator constants
ywangd 5fd3029
Update x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/s…
ywangd 1285c91
address feedback
ywangd 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
20 changes: 20 additions & 0 deletions
20
...in/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountAction.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,20 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.core.security.action.service; | ||
|
||
import org.elasticsearch.action.ActionType; | ||
|
||
public class GetServiceAccountAction extends ActionType<GetServiceAccountResponse> { | ||
|
||
public static final String NAME = "cluster:admin/xpack/security/service_account/get"; | ||
public static final GetServiceAccountAction INSTANCE = new GetServiceAccountAction(); | ||
|
||
public GetServiceAccountAction() { | ||
super(NAME, GetServiceAccountResponse::new); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...n/java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountRequest.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,71 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.core.security.action.service; | ||
|
||
import org.elasticsearch.action.ActionRequest; | ||
import org.elasticsearch.action.ActionRequestValidationException; | ||
import org.elasticsearch.common.Nullable; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
public class GetServiceAccountRequest extends ActionRequest { | ||
|
||
@Nullable | ||
private final String namespace; | ||
@Nullable | ||
private final String serviceName; | ||
|
||
public GetServiceAccountRequest(@Nullable String namespace, @Nullable String serviceName) { | ||
this.namespace = namespace; | ||
this.serviceName = serviceName; | ||
} | ||
|
||
public GetServiceAccountRequest(StreamInput in) throws IOException { | ||
super(in); | ||
this.namespace = in.readOptionalString(); | ||
this.serviceName = in.readOptionalString(); | ||
} | ||
|
||
public String getNamespace() { | ||
return namespace; | ||
} | ||
|
||
public String getServiceName() { | ||
return serviceName; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) | ||
return true; | ||
if (o == null || getClass() != o.getClass()) | ||
return false; | ||
GetServiceAccountRequest that = (GetServiceAccountRequest) o; | ||
return Objects.equals(namespace, that.namespace) && Objects.equals(serviceName, that.serviceName); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(namespace, serviceName); | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
super.writeTo(out); | ||
out.writeOptionalString(namespace); | ||
out.writeOptionalString(serviceName); | ||
} | ||
|
||
@Override | ||
public ActionRequestValidationException validate() { | ||
return null; | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
.../java/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountResponse.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,70 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.core.security.action.service; | ||
|
||
import org.elasticsearch.action.ActionResponse; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.xcontent.ToXContentObject; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.xpack.core.security.authz.RoleDescriptor; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
public class GetServiceAccountResponse extends ActionResponse implements ToXContentObject { | ||
|
||
private final Map<String, RoleDescriptor> serviceAccounts; | ||
|
||
public GetServiceAccountResponse(Map<String, RoleDescriptor> serviceAccounts) { | ||
this.serviceAccounts = Objects.requireNonNull(serviceAccounts); | ||
} | ||
|
||
public GetServiceAccountResponse(StreamInput in) throws IOException { | ||
super(in); | ||
this.serviceAccounts = in.readMap(StreamInput::readString, RoleDescriptor::new); | ||
} | ||
|
||
public Map<String, RoleDescriptor> getServiceAccounts() { | ||
return serviceAccounts; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) throws IOException { | ||
out.writeMap(serviceAccounts, StreamOutput::writeString, (o, v) -> v.writeTo(o)); | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.startObject(); | ||
for (Map.Entry<String, RoleDescriptor> entry: serviceAccounts.entrySet()) { | ||
builder.startObject(entry.getKey()); | ||
builder.field("role_descriptor"); | ||
entry.getValue().toXContent(builder, params); | ||
builder.endObject(); | ||
} | ||
builder.endObject(); | ||
return builder; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) | ||
return true; | ||
if (o == null || getClass() != o.getClass()) | ||
return false; | ||
GetServiceAccountResponse that = (GetServiceAccountResponse) o; | ||
return serviceAccounts.equals(that.serviceAccounts); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(serviceAccounts); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...a/org/elasticsearch/xpack/core/security/action/service/GetServiceAccountRequestTests.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,40 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.core.security.action.service; | ||
|
||
import org.elasticsearch.common.io.stream.Writeable; | ||
import org.elasticsearch.test.AbstractWireSerializingTestCase; | ||
|
||
import java.io.IOException; | ||
|
||
public class GetServiceAccountRequestTests extends AbstractWireSerializingTestCase<GetServiceAccountRequest> { | ||
|
||
@Override | ||
protected Writeable.Reader<GetServiceAccountRequest> instanceReader() { | ||
return GetServiceAccountRequest::new; | ||
} | ||
|
||
@Override | ||
protected GetServiceAccountRequest createTestInstance() { | ||
return new GetServiceAccountRequest(randomFrom(randomAlphaOfLengthBetween(3, 8), null), | ||
randomFrom(randomAlphaOfLengthBetween(3, 8), null)); | ||
} | ||
|
||
@Override | ||
protected GetServiceAccountRequest mutateInstance(GetServiceAccountRequest instance) throws IOException { | ||
if (randomBoolean()) { | ||
return new GetServiceAccountRequest( | ||
randomValueOtherThan(instance.getNamespace(), () -> randomFrom(randomAlphaOfLengthBetween(3, 8), null)), | ||
instance.getServiceName()); | ||
} else { | ||
return new GetServiceAccountRequest( | ||
instance.getNamespace(), | ||
randomValueOtherThan(instance.getServiceName(), () -> randomFrom(randomAlphaOfLengthBetween(3, 8), null))); | ||
} | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
.../org/elasticsearch/xpack/core/security/action/service/GetServiceAccountResponseTests.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,95 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.core.security.action.service; | ||
|
||
import org.elasticsearch.common.bytes.BytesReference; | ||
import org.elasticsearch.common.io.stream.Writeable; | ||
import org.elasticsearch.common.xcontent.ToXContent; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentFactory; | ||
import org.elasticsearch.common.xcontent.XContentHelper; | ||
import org.elasticsearch.common.xcontent.XContentType; | ||
import org.elasticsearch.test.AbstractWireSerializingTestCase; | ||
import org.elasticsearch.test.XContentTestUtils; | ||
import org.elasticsearch.xpack.core.security.authz.RoleDescriptor; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import static org.hamcrest.Matchers.anEmptyMap; | ||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
public class GetServiceAccountResponseTests extends AbstractWireSerializingTestCase<GetServiceAccountResponse> { | ||
|
||
@Override | ||
protected Writeable.Reader<GetServiceAccountResponse> instanceReader() { | ||
return GetServiceAccountResponse::new; | ||
} | ||
|
||
@Override | ||
protected GetServiceAccountResponse createTestInstance() { | ||
final String principal = randomPrincipal(); | ||
return new GetServiceAccountResponse(randomBoolean() ? Map.of(principal, getRoleDescriptorFor(principal)) : Map.of()); | ||
} | ||
|
||
@Override | ||
protected GetServiceAccountResponse mutateInstance(GetServiceAccountResponse instance) throws IOException { | ||
if (instance.getServiceAccounts().isEmpty()) { | ||
final String principal = randomPrincipal(); | ||
return new GetServiceAccountResponse(Map.of(principal, getRoleDescriptorFor(principal))); | ||
} else { | ||
return new GetServiceAccountResponse(Map.of()); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public void testToXContent() throws IOException { | ||
final GetServiceAccountResponse response = createTestInstance(); | ||
XContentBuilder builder = XContentFactory.jsonBuilder(); | ||
response.toXContent(builder, ToXContent.EMPTY_PARAMS); | ||
final Map<String, Object> responseMap = XContentHelper.convertToMap( | ||
BytesReference.bytes(builder), | ||
false, builder.contentType()).v2(); | ||
final Map<String, RoleDescriptor> serviceAccounts = response.getServiceAccounts(); | ||
if (serviceAccounts.isEmpty()) { | ||
assertThat(responseMap, anEmptyMap()); | ||
} else { | ||
assertThat(responseMap.size(), equalTo(serviceAccounts.size())); | ||
assertThat(responseMap.keySet(), equalTo(serviceAccounts.keySet())); | ||
for (String key : responseMap.keySet()) { | ||
assertRoleDescriptorEquals((Map<String, Object>) responseMap.get(key), serviceAccounts.get(key)); | ||
} | ||
} | ||
} | ||
|
||
private String randomPrincipal() { | ||
return randomAlphaOfLengthBetween(3, 8) + "/" + randomAlphaOfLengthBetween(3, 8); | ||
} | ||
|
||
private RoleDescriptor getRoleDescriptorFor(String name) { | ||
return new RoleDescriptor(name, | ||
new String[] { "monitor", "manage_own_api_key" }, | ||
new RoleDescriptor.IndicesPrivileges[] { | ||
RoleDescriptor.IndicesPrivileges.builder() | ||
.indices("logs-*", "metrics-*", "traces-*") | ||
.privileges("write", "create_index", "auto_configure").build() }, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null); | ||
} | ||
|
||
private void assertRoleDescriptorEquals(Map<String, Object> responseFragment, RoleDescriptor roleDescriptor) throws IOException { | ||
@SuppressWarnings("unchecked") | ||
final Map<String, Object> descriptorMap = (Map<String, Object>) responseFragment.get("role_descriptor"); | ||
assertThat(RoleDescriptor.parse(roleDescriptor.getName(), | ||
XContentTestUtils.convertToXContent(descriptorMap, XContentType.JSON), false, XContentType.JSON), | ||
equalTo(roleDescriptor)); | ||
} | ||
} |
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
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 think this will be easier to expand in the future if we introduce a
ServiceAccountInfo
object here.If we do, then the serialization code here stays mostly the same, even as the response evolves.
However, with the code as it is, we would have to introduce some ugly version compat code.
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
ServiceAccountInfo
as suggested.