|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 4 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 5 | + * you may not use this file except in compliance with the Elastic License. |
| 6 | + * |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.xpack.core.security.authz.privilege; |
| 10 | + |
| 11 | +import org.elasticsearch.test.ESTestCase; |
| 12 | +import org.elasticsearch.transport.TransportRequest; |
| 13 | +import org.elasticsearch.xpack.core.security.action.GetApiKeyRequest; |
| 14 | +import org.elasticsearch.xpack.core.security.action.InvalidateApiKeyRequest; |
| 15 | +import org.elasticsearch.xpack.core.security.authc.Authentication; |
| 16 | +import org.elasticsearch.xpack.core.security.authz.permission.ClusterPermission; |
| 17 | +import org.elasticsearch.xpack.core.security.user.User; |
| 18 | + |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import static org.mockito.Mockito.mock; |
| 22 | +import static org.mockito.Mockito.when; |
| 23 | + |
| 24 | +public class ManageOwnApiKeyClusterPrivilegeTests extends ESTestCase { |
| 25 | + |
| 26 | + public void testActionRequestAuthenticationBasedPredicateWhenAuthenticatingWithApiKey() { |
| 27 | + final ClusterPermission clusterPermission = |
| 28 | + ManageOwnApiKeyClusterPrivilege.INSTANCE.buildPermission(ClusterPermission.builder()).build(); |
| 29 | + { |
| 30 | + final String apiKeyId = randomAlphaOfLengthBetween(4, 7); |
| 31 | + final Authentication authentication = createMockAuthentication("_es_api_key", "_es_api_key", |
| 32 | + Map.of("_security_api_key_id", apiKeyId)); |
| 33 | + final TransportRequest request = randomFrom(GetApiKeyRequest.usingApiKeyId(apiKeyId, randomBoolean()), |
| 34 | + InvalidateApiKeyRequest.usingApiKeyId(apiKeyId, randomBoolean())); |
| 35 | + |
| 36 | + assertTrue(clusterPermission.check("cluster:admin/xpack/security/api_key/get", request, authentication)); |
| 37 | + assertTrue(clusterPermission.check("cluster:admin/xpack/security/api_key/invalidate", request, authentication)); |
| 38 | + assertFalse(clusterPermission.check("cluster:admin/something", request, authentication)); |
| 39 | + } |
| 40 | + { |
| 41 | + final String apiKeyId = randomAlphaOfLengthBetween(4, 7); |
| 42 | + final Authentication authentication = createMockAuthentication("_es_api_key", "_es_api_key", |
| 43 | + Map.of("_security_api_key_id", randomAlphaOfLength(7))); |
| 44 | + final TransportRequest request = randomFrom(GetApiKeyRequest.usingApiKeyId(apiKeyId, randomBoolean()), |
| 45 | + InvalidateApiKeyRequest.usingApiKeyId(apiKeyId, randomBoolean())); |
| 46 | + |
| 47 | + assertFalse(clusterPermission.check("cluster:admin/xpack/security/api_key/get", request, authentication)); |
| 48 | + assertFalse(clusterPermission.check("cluster:admin/xpack/security/api_key/invalidate", request, authentication)); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public void testActionRequestAuthenticationBasedPredicateWhenRequestContainsUsernameAndRealmName() { |
| 53 | + final ClusterPermission clusterPermission = |
| 54 | + ManageOwnApiKeyClusterPrivilege.INSTANCE.buildPermission(ClusterPermission.builder()).build(); |
| 55 | + { |
| 56 | + final Authentication authentication = createMockAuthentication("realm1", "native", Map.of()); |
| 57 | + final TransportRequest request = randomFrom(GetApiKeyRequest.usingRealmAndUserName("realm1", "joe"), |
| 58 | + InvalidateApiKeyRequest.usingRealmAndUserName("realm1", "joe")); |
| 59 | + |
| 60 | + assertTrue(clusterPermission.check("cluster:admin/xpack/security/api_key/get", request, authentication)); |
| 61 | + assertTrue(clusterPermission.check("cluster:admin/xpack/security/api_key/invalidate", request, authentication)); |
| 62 | + assertFalse(clusterPermission.check("cluster:admin/something", request, authentication)); |
| 63 | + } |
| 64 | + { |
| 65 | + final Authentication authentication = createMockAuthentication("realm1", "native", Map.of()); |
| 66 | + final TransportRequest request = randomFrom( |
| 67 | + GetApiKeyRequest.usingRealmAndUserName("realm1", randomAlphaOfLength(7)), |
| 68 | + GetApiKeyRequest.usingRealmAndUserName(randomAlphaOfLength(5), "joe"), |
| 69 | + InvalidateApiKeyRequest.usingRealmAndUserName("realm1", randomAlphaOfLength(7)), |
| 70 | + InvalidateApiKeyRequest.usingRealmAndUserName(randomAlphaOfLength(5), "joe")); |
| 71 | + |
| 72 | + assertFalse(clusterPermission.check("cluster:admin/xpack/security/api_key/get", request, authentication)); |
| 73 | + assertFalse(clusterPermission.check("cluster:admin/xpack/security/api_key/invalidate", request, authentication)); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private Authentication createMockAuthentication(String realmName, String realmType, Map<String, Object> metadata) { |
| 78 | + final User user = new User("joe"); |
| 79 | + final Authentication authentication = mock(Authentication.class); |
| 80 | + final Authentication.RealmRef authenticatedBy = mock(Authentication.RealmRef.class); |
| 81 | + when(authentication.getUser()).thenReturn(user); |
| 82 | + when(authentication.getAuthenticatedBy()).thenReturn(authenticatedBy); |
| 83 | + when(authenticatedBy.getName()).thenReturn(realmName); |
| 84 | + when(authenticatedBy.getType()).thenReturn(realmType); |
| 85 | + when(authentication.getMetadata()).thenReturn(metadata); |
| 86 | + return authentication; |
| 87 | + } |
| 88 | +} |
0 commit comments