|
| 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.security.authc.apikey; |
| 9 | + |
| 10 | +import org.elasticsearch.ElasticsearchSecurityException; |
| 11 | +import org.elasticsearch.action.admin.indices.create.CreateIndexAction; |
| 12 | +import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; |
| 13 | +import org.elasticsearch.action.get.GetAction; |
| 14 | +import org.elasticsearch.action.get.GetRequest; |
| 15 | +import org.elasticsearch.action.get.GetResponse; |
| 16 | +import org.elasticsearch.action.main.MainAction; |
| 17 | +import org.elasticsearch.action.main.MainRequest; |
| 18 | +import org.elasticsearch.common.Strings; |
| 19 | +import org.elasticsearch.common.settings.SecureString; |
| 20 | +import org.elasticsearch.common.settings.Settings; |
| 21 | +import org.elasticsearch.common.xcontent.XContentType; |
| 22 | +import org.elasticsearch.test.SecuritySingleNodeTestCase; |
| 23 | +import org.elasticsearch.test.XContentTestUtils; |
| 24 | +import org.elasticsearch.xpack.core.XPackSettings; |
| 25 | +import org.elasticsearch.xpack.core.security.action.CreateApiKeyAction; |
| 26 | +import org.elasticsearch.xpack.core.security.action.CreateApiKeyRequest; |
| 27 | +import org.elasticsearch.xpack.core.security.action.CreateApiKeyResponse; |
| 28 | +import org.elasticsearch.xpack.core.security.action.GrantApiKeyAction; |
| 29 | +import org.elasticsearch.xpack.core.security.action.GrantApiKeyRequest; |
| 30 | +import org.elasticsearch.xpack.core.security.action.service.CreateServiceAccountTokenAction; |
| 31 | +import org.elasticsearch.xpack.core.security.action.service.CreateServiceAccountTokenRequest; |
| 32 | +import org.elasticsearch.xpack.core.security.action.service.CreateServiceAccountTokenResponse; |
| 33 | +import org.elasticsearch.xpack.core.security.action.user.PutUserAction; |
| 34 | +import org.elasticsearch.xpack.core.security.action.user.PutUserRequest; |
| 35 | +import org.elasticsearch.xpack.core.security.authc.support.Hasher; |
| 36 | +import org.elasticsearch.xpack.core.security.authz.RoleDescriptor; |
| 37 | +import org.elasticsearch.xpack.security.authc.service.ServiceAccountService; |
| 38 | + |
| 39 | +import java.io.IOException; |
| 40 | +import java.nio.charset.StandardCharsets; |
| 41 | +import java.util.Base64; |
| 42 | +import java.util.Map; |
| 43 | + |
| 44 | +import static org.elasticsearch.xpack.core.security.index.RestrictedIndicesNames.SECURITY_MAIN_ALIAS; |
| 45 | +import static org.hamcrest.Matchers.containsString; |
| 46 | +import static org.hamcrest.Matchers.equalTo; |
| 47 | +import static org.hamcrest.Matchers.hasKey; |
| 48 | + |
| 49 | +public class ApiKeySingleNodeTests extends SecuritySingleNodeTestCase { |
| 50 | + |
| 51 | + @Override |
| 52 | + protected Settings nodeSettings() { |
| 53 | + Settings.Builder builder = Settings.builder().put(super.nodeSettings()); |
| 54 | + builder.put(XPackSettings.API_KEY_SERVICE_ENABLED_SETTING.getKey(), true); |
| 55 | + return builder.build(); |
| 56 | + } |
| 57 | + |
| 58 | + public void testCreatingApiKeyWithNoAccess() { |
| 59 | + final PutUserRequest putUserRequest = new PutUserRequest(); |
| 60 | + final String username = randomAlphaOfLength(8); |
| 61 | + putUserRequest.username(username); |
| 62 | + final SecureString password = new SecureString("super-strong-password".toCharArray()); |
| 63 | + putUserRequest.passwordHash(Hasher.PBKDF2.hash(password)); |
| 64 | + putUserRequest.roles(Strings.EMPTY_ARRAY); |
| 65 | + client().execute(PutUserAction.INSTANCE, putUserRequest).actionGet(); |
| 66 | + |
| 67 | + final GrantApiKeyRequest grantApiKeyRequest = new GrantApiKeyRequest(); |
| 68 | + grantApiKeyRequest.getGrant().setType("password"); |
| 69 | + grantApiKeyRequest.getGrant().setUsername(username); |
| 70 | + grantApiKeyRequest.getGrant().setPassword(password); |
| 71 | + grantApiKeyRequest.getApiKeyRequest().setName(randomAlphaOfLength(8)); |
| 72 | + grantApiKeyRequest.getApiKeyRequest().setRoleDescriptors(org.elasticsearch.core.List.of( |
| 73 | + new RoleDescriptor("x", new String[] { "all" }, |
| 74 | + new RoleDescriptor.IndicesPrivileges[]{ |
| 75 | + RoleDescriptor.IndicesPrivileges.builder().indices("*").privileges("all").allowRestrictedIndices(true).build() |
| 76 | + }, |
| 77 | + null, null, null, null, null))); |
| 78 | + final CreateApiKeyResponse createApiKeyResponse = client().execute(GrantApiKeyAction.INSTANCE, grantApiKeyRequest).actionGet(); |
| 79 | + |
| 80 | + final String base64ApiKeyKeyValue = Base64.getEncoder().encodeToString( |
| 81 | + (createApiKeyResponse.getId() + ":" + createApiKeyResponse.getKey().toString()).getBytes(StandardCharsets.UTF_8)); |
| 82 | + |
| 83 | + // No cluster access |
| 84 | + final ElasticsearchSecurityException e1 = expectThrows( |
| 85 | + ElasticsearchSecurityException.class, |
| 86 | + () -> client().filterWithHeader(org.elasticsearch.core.Map.of("Authorization", "ApiKey " + base64ApiKeyKeyValue)) |
| 87 | + .execute(MainAction.INSTANCE, new MainRequest()) |
| 88 | + .actionGet()); |
| 89 | + assertThat(e1.status().getStatus(), equalTo(403)); |
| 90 | + assertThat(e1.getMessage(), containsString("is unauthorized for API key")); |
| 91 | + |
| 92 | + // No index access |
| 93 | + final ElasticsearchSecurityException e2 = expectThrows( |
| 94 | + ElasticsearchSecurityException.class, |
| 95 | + () -> client().filterWithHeader(org.elasticsearch.core.Map.of("Authorization", "ApiKey " + base64ApiKeyKeyValue)) |
| 96 | + .execute(CreateIndexAction.INSTANCE, new CreateIndexRequest( |
| 97 | + randomFrom(randomAlphaOfLengthBetween(3, 8), SECURITY_MAIN_ALIAS))) |
| 98 | + .actionGet()); |
| 99 | + assertThat(e2.status().getStatus(), equalTo(403)); |
| 100 | + assertThat(e2.getMessage(), containsString("is unauthorized for API key")); |
| 101 | + } |
| 102 | + |
| 103 | + public void testServiceAccountApiKey() throws IOException { |
| 104 | + final CreateServiceAccountTokenRequest createServiceAccountTokenRequest = |
| 105 | + new CreateServiceAccountTokenRequest("elastic", "fleet-server", randomAlphaOfLength(8)); |
| 106 | + final CreateServiceAccountTokenResponse createServiceAccountTokenResponse = |
| 107 | + client().execute(CreateServiceAccountTokenAction.INSTANCE, createServiceAccountTokenRequest).actionGet(); |
| 108 | + |
| 109 | + final CreateApiKeyResponse createApiKeyResponse = |
| 110 | + client() |
| 111 | + .filterWithHeader(org.elasticsearch.core.Map.of("Authorization", "Bearer " + createServiceAccountTokenResponse.getValue())) |
| 112 | + .execute(CreateApiKeyAction.INSTANCE, new CreateApiKeyRequest(randomAlphaOfLength(8), null, null)) |
| 113 | + .actionGet(); |
| 114 | + |
| 115 | + final Map<String, Object> apiKeyDocument = getApiKeyDocument(createApiKeyResponse.getId()); |
| 116 | + |
| 117 | + @SuppressWarnings("unchecked") |
| 118 | + final Map<String, Object> fleetServerRoleDescriptor = |
| 119 | + (Map<String, Object>) apiKeyDocument.get("limited_by_role_descriptors"); |
| 120 | + assertThat(fleetServerRoleDescriptor.size(), equalTo(1)); |
| 121 | + assertThat(fleetServerRoleDescriptor, hasKey("elastic/fleet-server")); |
| 122 | + |
| 123 | + @SuppressWarnings("unchecked") |
| 124 | + final Map<String, ?> descriptor = (Map<String, ?>) fleetServerRoleDescriptor.get("elastic/fleet-server"); |
| 125 | + |
| 126 | + final RoleDescriptor roleDescriptor = RoleDescriptor.parse("elastic/fleet-server", |
| 127 | + XContentTestUtils.convertToXContent(descriptor, XContentType.JSON), |
| 128 | + false, |
| 129 | + XContentType.JSON); |
| 130 | + assertThat(roleDescriptor, equalTo(ServiceAccountService.getServiceAccounts().get("elastic/fleet-server").roleDescriptor())); |
| 131 | + } |
| 132 | + |
| 133 | + private Map<String, Object> getApiKeyDocument(String apiKeyId) { |
| 134 | + final GetResponse getResponse = |
| 135 | + client().execute(GetAction.INSTANCE, new GetRequest(".security-7", apiKeyId)).actionGet(); |
| 136 | + return getResponse.getSource(); |
| 137 | + } |
| 138 | +} |
0 commit comments