|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.core.security.action.service; |
| 9 | + |
| 10 | +import org.elasticsearch.common.bytes.BytesReference; |
| 11 | +import org.elasticsearch.common.io.stream.Writeable; |
| 12 | +import org.elasticsearch.common.xcontent.ToXContent; |
| 13 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 14 | +import org.elasticsearch.common.xcontent.XContentFactory; |
| 15 | +import org.elasticsearch.common.xcontent.XContentHelper; |
| 16 | +import org.elasticsearch.common.xcontent.XContentType; |
| 17 | +import org.elasticsearch.test.AbstractWireSerializingTestCase; |
| 18 | +import org.elasticsearch.test.XContentTestUtils; |
| 19 | +import org.elasticsearch.xpack.core.security.authz.RoleDescriptor; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import static org.hamcrest.Matchers.anEmptyMap; |
| 25 | +import static org.hamcrest.Matchers.equalTo; |
| 26 | + |
| 27 | +public class GetServiceAccountResponseTests extends AbstractWireSerializingTestCase<GetServiceAccountResponse> { |
| 28 | + |
| 29 | + @Override |
| 30 | + protected Writeable.Reader<GetServiceAccountResponse> instanceReader() { |
| 31 | + return GetServiceAccountResponse::new; |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + protected GetServiceAccountResponse createTestInstance() { |
| 36 | + final String principal = randomPrincipal(); |
| 37 | + return new GetServiceAccountResponse(randomBoolean() |
| 38 | + ? new ServiceAccountInfo[]{new ServiceAccountInfo(principal, getRoleDescriptorFor(principal))} |
| 39 | + : new ServiceAccountInfo[0]); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + protected GetServiceAccountResponse mutateInstance(GetServiceAccountResponse instance) throws IOException { |
| 44 | + if (instance.getServiceAccountInfos().length == 0) { |
| 45 | + final String principal = randomPrincipal(); |
| 46 | + return new GetServiceAccountResponse(new ServiceAccountInfo[]{ |
| 47 | + new ServiceAccountInfo(principal, getRoleDescriptorFor(principal))}); |
| 48 | + } else { |
| 49 | + return new GetServiceAccountResponse(new ServiceAccountInfo[0]); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @SuppressWarnings("unchecked") |
| 54 | + public void testToXContent() throws IOException { |
| 55 | + final GetServiceAccountResponse response = createTestInstance(); |
| 56 | + XContentBuilder builder = XContentFactory.jsonBuilder(); |
| 57 | + response.toXContent(builder, ToXContent.EMPTY_PARAMS); |
| 58 | + final Map<String, Object> responseMap = XContentHelper.convertToMap( |
| 59 | + BytesReference.bytes(builder), |
| 60 | + false, builder.contentType()).v2(); |
| 61 | + final ServiceAccountInfo[] serviceAccountInfos = response.getServiceAccountInfos(); |
| 62 | + if (serviceAccountInfos.length == 0) { |
| 63 | + assertThat(responseMap, anEmptyMap()); |
| 64 | + } else { |
| 65 | + assertThat(responseMap.size(), equalTo(serviceAccountInfos.length)); |
| 66 | + for (int i = 0; i < serviceAccountInfos.length - 1; i++) { |
| 67 | + final String key = serviceAccountInfos[i].getPrincipal(); |
| 68 | + assertRoleDescriptorEquals((Map<String, Object>) responseMap.get(key), serviceAccountInfos[i].getRoleDescriptor()); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private String randomPrincipal() { |
| 74 | + return randomAlphaOfLengthBetween(3, 8) + "/" + randomAlphaOfLengthBetween(3, 8); |
| 75 | + } |
| 76 | + |
| 77 | + private RoleDescriptor getRoleDescriptorFor(String name) { |
| 78 | + return new RoleDescriptor(name, |
| 79 | + new String[] { "monitor", "manage_own_api_key" }, |
| 80 | + new RoleDescriptor.IndicesPrivileges[] { |
| 81 | + RoleDescriptor.IndicesPrivileges.builder() |
| 82 | + .indices("logs-*", "metrics-*", "traces-*") |
| 83 | + .privileges("write", "create_index", "auto_configure").build() }, |
| 84 | + null, |
| 85 | + null, |
| 86 | + null, |
| 87 | + null, |
| 88 | + null); |
| 89 | + } |
| 90 | + |
| 91 | + private void assertRoleDescriptorEquals(Map<String, Object> responseFragment, RoleDescriptor roleDescriptor) throws IOException { |
| 92 | + @SuppressWarnings("unchecked") |
| 93 | + final Map<String, Object> descriptorMap = (Map<String, Object>) responseFragment.get("role_descriptor"); |
| 94 | + assertThat(RoleDescriptor.parse(roleDescriptor.getName(), |
| 95 | + XContentTestUtils.convertToXContent(descriptorMap, XContentType.JSON), false, XContentType.JSON), |
| 96 | + equalTo(roleDescriptor)); |
| 97 | + } |
| 98 | +} |
0 commit comments