|
| 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 testAuthenticationWithApiKeyAllowsAccessToApiKeyActionsWhenItIsOwner() { |
| 27 | + final ClusterPermission clusterPermission = |
| 28 | + ManageOwnApiKeyClusterPrivilege.INSTANCE.buildPermission(ClusterPermission.builder()).build(); |
| 29 | + |
| 30 | + final String apiKeyId = randomAlphaOfLengthBetween(4, 7); |
| 31 | + final Authentication authentication = createMockAuthentication("joe","_es_api_key", "_es_api_key", |
| 32 | + Map.of("_security_api_key_id", apiKeyId)); |
| 33 | + final TransportRequest getApiKeyRequest = GetApiKeyRequest.usingApiKeyId(apiKeyId, randomBoolean()); |
| 34 | + final TransportRequest invalidateApiKeyRequest = InvalidateApiKeyRequest.usingApiKeyId(apiKeyId, randomBoolean()); |
| 35 | + |
| 36 | + assertTrue(clusterPermission.check("cluster:admin/xpack/security/api_key/get", getApiKeyRequest, authentication)); |
| 37 | + assertTrue(clusterPermission.check("cluster:admin/xpack/security/api_key/invalidate", invalidateApiKeyRequest, authentication)); |
| 38 | + assertFalse(clusterPermission.check("cluster:admin/something", mock(TransportRequest.class), authentication)); |
| 39 | + } |
| 40 | + |
| 41 | + public void testAuthenticationWithApiKeyDeniesAccessToApiKeyActionsWhenItIsNotOwner() { |
| 42 | + final ClusterPermission clusterPermission = |
| 43 | + ManageOwnApiKeyClusterPrivilege.INSTANCE.buildPermission(ClusterPermission.builder()).build(); |
| 44 | + |
| 45 | + final String apiKeyId = randomAlphaOfLengthBetween(4, 7); |
| 46 | + final Authentication authentication = createMockAuthentication("joe","_es_api_key", "_es_api_key", |
| 47 | + Map.of("_security_api_key_id", randomAlphaOfLength(7))); |
| 48 | + final TransportRequest getApiKeyRequest = GetApiKeyRequest.usingApiKeyId(apiKeyId, randomBoolean()); |
| 49 | + final TransportRequest invalidateApiKeyRequest = InvalidateApiKeyRequest.usingApiKeyId(apiKeyId, randomBoolean()); |
| 50 | + |
| 51 | + assertFalse(clusterPermission.check("cluster:admin/xpack/security/api_key/get", getApiKeyRequest, authentication)); |
| 52 | + assertFalse(clusterPermission.check("cluster:admin/xpack/security/api_key/invalidate", invalidateApiKeyRequest, authentication)); |
| 53 | + } |
| 54 | + |
| 55 | + public void testAuthenticationWithUserAllowsAccessToApiKeyActionsWhenItIsOwner() { |
| 56 | + final ClusterPermission clusterPermission = |
| 57 | + ManageOwnApiKeyClusterPrivilege.INSTANCE.buildPermission(ClusterPermission.builder()).build(); |
| 58 | + |
| 59 | + final Authentication authentication = createMockAuthentication("joe","realm1", "native", Map.of()); |
| 60 | + final TransportRequest getApiKeyRequest = GetApiKeyRequest.usingRealmAndUserName("realm1", "joe"); |
| 61 | + final TransportRequest invalidateApiKeyRequest = InvalidateApiKeyRequest.usingRealmAndUserName("realm1", "joe"); |
| 62 | + |
| 63 | + assertTrue(clusterPermission.check("cluster:admin/xpack/security/api_key/get", getApiKeyRequest, authentication)); |
| 64 | + assertTrue(clusterPermission.check("cluster:admin/xpack/security/api_key/invalidate", invalidateApiKeyRequest, authentication)); |
| 65 | + assertFalse(clusterPermission.check("cluster:admin/something", mock(TransportRequest.class), authentication)); |
| 66 | + } |
| 67 | + |
| 68 | + public void testAuthenticationWithUserAllowsAccessToApiKeyActionsWhenItIsOwner_WithOwnerFlagOnly() { |
| 69 | + final ClusterPermission clusterPermission = |
| 70 | + ManageOwnApiKeyClusterPrivilege.INSTANCE.buildPermission(ClusterPermission.builder()).build(); |
| 71 | + |
| 72 | + final Authentication authentication = createMockAuthentication("joe","realm1", "native", Map.of()); |
| 73 | + final TransportRequest getApiKeyRequest = GetApiKeyRequest.forOwnedApiKeys(); |
| 74 | + final TransportRequest invalidateApiKeyRequest = InvalidateApiKeyRequest.forOwnedApiKeys(); |
| 75 | + |
| 76 | + assertTrue(clusterPermission.check("cluster:admin/xpack/security/api_key/get", getApiKeyRequest, authentication)); |
| 77 | + assertTrue(clusterPermission.check("cluster:admin/xpack/security/api_key/invalidate", invalidateApiKeyRequest, authentication)); |
| 78 | + assertFalse(clusterPermission.check("cluster:admin/something", mock(TransportRequest.class), authentication)); |
| 79 | + } |
| 80 | + |
| 81 | + public void testAuthenticationWithUserDeniesAccessToApiKeyActionsWhenItIsNotOwner() { |
| 82 | + final ClusterPermission clusterPermission = |
| 83 | + ManageOwnApiKeyClusterPrivilege.INSTANCE.buildPermission(ClusterPermission.builder()).build(); |
| 84 | + |
| 85 | + final Authentication authentication = createMockAuthentication("joe", "realm1", "native", Map.of()); |
| 86 | + final TransportRequest getApiKeyRequest = randomFrom( |
| 87 | + GetApiKeyRequest.usingRealmAndUserName("realm1", randomAlphaOfLength(7)), |
| 88 | + GetApiKeyRequest.usingRealmAndUserName(randomAlphaOfLength(5), "joe"), |
| 89 | + new GetApiKeyRequest(randomAlphaOfLength(5), randomAlphaOfLength(7), null, null, false)); |
| 90 | + final TransportRequest invalidateApiKeyRequest = randomFrom( |
| 91 | + InvalidateApiKeyRequest.usingRealmAndUserName("realm1", randomAlphaOfLength(7)), |
| 92 | + InvalidateApiKeyRequest.usingRealmAndUserName(randomAlphaOfLength(5), "joe"), |
| 93 | + new InvalidateApiKeyRequest(randomAlphaOfLength(5), randomAlphaOfLength(7), null, null, false)); |
| 94 | + |
| 95 | + assertFalse(clusterPermission.check("cluster:admin/xpack/security/api_key/get", getApiKeyRequest, authentication)); |
| 96 | + assertFalse(clusterPermission.check("cluster:admin/xpack/security/api_key/invalidate", invalidateApiKeyRequest, authentication)); |
| 97 | + } |
| 98 | + |
| 99 | + private Authentication createMockAuthentication(String username, String realmName, String realmType, Map<String, Object> metadata) { |
| 100 | + final User user = new User(username); |
| 101 | + final Authentication authentication = mock(Authentication.class); |
| 102 | + final Authentication.RealmRef authenticatedBy = mock(Authentication.RealmRef.class); |
| 103 | + when(authentication.getUser()).thenReturn(user); |
| 104 | + when(authentication.getAuthenticatedBy()).thenReturn(authenticatedBy); |
| 105 | + when(authenticatedBy.getName()).thenReturn(realmName); |
| 106 | + when(authenticatedBy.getType()).thenReturn(realmType); |
| 107 | + when(authentication.getMetadata()).thenReturn(metadata); |
| 108 | + return authentication; |
| 109 | + } |
| 110 | +} |
0 commit comments