|
23 | 23 |
|
24 | 24 | public class ManageOwnApiKeyClusterPrivilegeTests extends ESTestCase { |
25 | 25 |
|
26 | | - public void testActionRequestAuthenticationBasedPredicateWhenAuthenticatingWithApiKey() { |
| 26 | + public void testAuthenticationWithApiKeyAllowsAccessToApiKeyActionsWhenItIsOwner() { |
27 | 27 | final ClusterPermission clusterPermission = |
28 | 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 | | - } |
| 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 | + 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("_es_api_key", "_es_api_key", |
| 47 | + Map.of("_security_api_key_id", randomAlphaOfLength(7))); |
| 48 | + final TransportRequest request = randomFrom(GetApiKeyRequest.usingApiKeyId(apiKeyId, randomBoolean()), |
| 49 | + InvalidateApiKeyRequest.usingApiKeyId(apiKeyId, randomBoolean())); |
| 50 | + |
| 51 | + assertFalse(clusterPermission.check("cluster:admin/xpack/security/api_key/get", request, authentication)); |
| 52 | + assertFalse(clusterPermission.check("cluster:admin/xpack/security/api_key/invalidate", request, authentication)); |
50 | 53 | } |
51 | 54 |
|
52 | | - public void testActionRequestAuthenticationBasedPredicateWhenRequestContainsUsernameAndRealmName() { |
| 55 | + public void testAuthenticationWithUserAllowsAccessToApiKeyActionsWhenItIsOwner() { |
53 | 56 | final ClusterPermission clusterPermission = |
54 | 57 | 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 | | - } |
| 58 | + |
| 59 | + final Authentication authentication = createMockAuthentication("realm1", "native", Map.of()); |
| 60 | + final TransportRequest request = randomFrom(GetApiKeyRequest.usingRealmAndUserName("realm1", "joe"), |
| 61 | + InvalidateApiKeyRequest.usingRealmAndUserName("realm1", "joe")); |
| 62 | + |
| 63 | + assertTrue(clusterPermission.check("cluster:admin/xpack/security/api_key/get", request, authentication)); |
| 64 | + assertTrue(clusterPermission.check("cluster:admin/xpack/security/api_key/invalidate", request, authentication)); |
| 65 | + assertFalse(clusterPermission.check("cluster:admin/something", request, authentication)); |
| 66 | + } |
| 67 | + |
| 68 | + public void testAuthenticationWithUserAllowsAccessToApiKeyActionsWhenItIsNotOwner() { |
| 69 | + final ClusterPermission clusterPermission = |
| 70 | + ManageOwnApiKeyClusterPrivilege.INSTANCE.buildPermission(ClusterPermission.builder()).build(); |
| 71 | + |
| 72 | + final Authentication authentication = createMockAuthentication("realm1", "native", Map.of()); |
| 73 | + final TransportRequest request = randomFrom( |
| 74 | + GetApiKeyRequest.usingRealmAndUserName("realm1", randomAlphaOfLength(7)), |
| 75 | + GetApiKeyRequest.usingRealmAndUserName(randomAlphaOfLength(5), "joe"), |
| 76 | + InvalidateApiKeyRequest.usingRealmAndUserName("realm1", randomAlphaOfLength(7)), |
| 77 | + InvalidateApiKeyRequest.usingRealmAndUserName(randomAlphaOfLength(5), "joe")); |
| 78 | + |
| 79 | + assertFalse(clusterPermission.check("cluster:admin/xpack/security/api_key/get", request, authentication)); |
| 80 | + assertFalse(clusterPermission.check("cluster:admin/xpack/security/api_key/invalidate", request, authentication)); |
75 | 81 | } |
76 | 82 |
|
77 | 83 | private Authentication createMockAuthentication(String realmName, String realmType, Map<String, Object> metadata) { |
|
0 commit comments