|  | 
| 8 | 8 | package org.elasticsearch.xpack.core.security.authc; | 
| 9 | 9 | 
 | 
| 10 | 10 | import org.elasticsearch.TransportVersion; | 
|  | 11 | +import org.elasticsearch.TransportVersions; | 
| 11 | 12 | import org.elasticsearch.common.Strings; | 
| 12 | 13 | import org.elasticsearch.common.bytes.BytesArray; | 
| 13 | 14 | import org.elasticsearch.common.bytes.BytesReference; | 
| 14 | 15 | import org.elasticsearch.common.settings.Settings; | 
| 15 | 16 | import org.elasticsearch.common.util.ArrayUtils; | 
| 16 | 17 | import org.elasticsearch.test.ESTestCase; | 
|  | 18 | +import org.elasticsearch.test.TransportVersionUtils; | 
| 17 | 19 | import org.elasticsearch.xpack.core.security.action.apikey.ApiKey; | 
| 18 | 20 | import org.elasticsearch.xpack.core.security.authc.service.ServiceAccountSettings; | 
| 19 | 21 | import org.elasticsearch.xpack.core.security.authz.RoleDescriptorsIntersection; | 
| 20 | 22 | import org.elasticsearch.xpack.core.security.authz.store.RoleKey; | 
| 21 | 23 | import org.elasticsearch.xpack.core.security.authz.store.RoleReference; | 
| 22 | 24 | import org.elasticsearch.xpack.core.security.authz.store.RoleReference.ApiKeyRoleReference; | 
|  | 25 | +import org.elasticsearch.xpack.core.security.authz.store.RoleReference.BwcApiKeyRoleReference; | 
| 23 | 26 | import org.elasticsearch.xpack.core.security.authz.store.RoleReference.FixedRoleReference; | 
| 24 | 27 | import org.elasticsearch.xpack.core.security.authz.store.RoleReference.NamedRoleReference; | 
| 25 | 28 | import org.elasticsearch.xpack.core.security.authz.store.RoleReference.ServiceAccountRoleReference; | 
|  | 
| 29 | 32 | import org.elasticsearch.xpack.core.security.user.User; | 
| 30 | 33 | 
 | 
| 31 | 34 | import java.util.Arrays; | 
|  | 35 | +import java.util.Collections; | 
| 32 | 36 | import java.util.HashMap; | 
| 33 | 37 | import java.util.List; | 
| 34 | 38 | import java.util.Map; | 
| @@ -283,6 +287,50 @@ private static void expectFixedReferenceAtIndex(int index, List<RoleReference> r | 
| 283 | 287 |         assertThat(fixedRoleReference.id(), equalTo(expectedKey)); | 
| 284 | 288 |     } | 
| 285 | 289 | 
 | 
|  | 290 | +    public void testGetRoleReferencesForApiKeyBwc() { | 
|  | 291 | +        Map<String, Object> authMetadata = new HashMap<>(); | 
|  | 292 | +        final String apiKeyId = randomAlphaOfLength(12); | 
|  | 293 | +        authMetadata.put(AuthenticationField.API_KEY_ID_KEY, apiKeyId); | 
|  | 294 | +        authMetadata.put(AuthenticationField.API_KEY_NAME_KEY, randomBoolean() ? null : randomAlphaOfLength(12)); | 
|  | 295 | +        boolean emptyApiKeyRoleDescriptor = randomBoolean(); | 
|  | 296 | +        Map<String, Object> roleARDMap = Map.of("cluster", List.of("monitor")); | 
|  | 297 | +        authMetadata.put( | 
|  | 298 | +            API_KEY_ROLE_DESCRIPTORS_KEY, | 
|  | 299 | +            (emptyApiKeyRoleDescriptor) | 
|  | 300 | +                ? randomFrom(Arrays.asList(null, Collections.emptyMap())) | 
|  | 301 | +                : Collections.singletonMap("a role", roleARDMap) | 
|  | 302 | +        ); | 
|  | 303 | + | 
|  | 304 | +        Map<String, Object> limitedRdMap = Map.of("cluster", List.of("all")); | 
|  | 305 | +        authMetadata.put(API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY, Collections.singletonMap("limited role", limitedRdMap)); | 
|  | 306 | + | 
|  | 307 | +        final Subject subject = new Subject( | 
|  | 308 | +            new User("joe"), | 
|  | 309 | +            new Authentication.RealmRef(API_KEY_REALM_NAME, API_KEY_REALM_TYPE, "node"), | 
|  | 310 | +            TransportVersionUtils.randomVersionBetween(random(), TransportVersions.V_7_0_0, TransportVersions.V_7_8_1), | 
|  | 311 | +            authMetadata | 
|  | 312 | +        ); | 
|  | 313 | + | 
|  | 314 | +        final RoleReferenceIntersection roleReferenceIntersection = subject.getRoleReferenceIntersection(getAnonymousUser()); | 
|  | 315 | +        final List<RoleReference> roleReferences = roleReferenceIntersection.getRoleReferences(); | 
|  | 316 | + | 
|  | 317 | +        if (emptyApiKeyRoleDescriptor) { | 
|  | 318 | +            assertThat(roleReferences, contains(isA(BwcApiKeyRoleReference.class))); | 
|  | 319 | +            final BwcApiKeyRoleReference limitedByRoleReference = (BwcApiKeyRoleReference) roleReferences.get(0); | 
|  | 320 | +            assertThat(limitedByRoleReference.getApiKeyId(), equalTo(apiKeyId)); | 
|  | 321 | +            assertThat(limitedByRoleReference.getRoleDescriptorsMap(), equalTo(authMetadata.get(API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY))); | 
|  | 322 | +        } else { | 
|  | 323 | +            assertThat(roleReferences, contains(isA(BwcApiKeyRoleReference.class), isA(BwcApiKeyRoleReference.class))); | 
|  | 324 | +            final BwcApiKeyRoleReference roleReference = (BwcApiKeyRoleReference) roleReferences.get(0); | 
|  | 325 | +            assertThat(roleReference.getApiKeyId(), equalTo(apiKeyId)); | 
|  | 326 | +            assertThat(roleReference.getRoleDescriptorsMap(), equalTo(authMetadata.get(API_KEY_ROLE_DESCRIPTORS_KEY))); | 
|  | 327 | + | 
|  | 328 | +            final BwcApiKeyRoleReference limitedByRoleReference = (BwcApiKeyRoleReference) roleReferences.get(1); | 
|  | 329 | +            assertThat(limitedByRoleReference.getApiKeyId(), equalTo(apiKeyId)); | 
|  | 330 | +            assertThat(limitedByRoleReference.getRoleDescriptorsMap(), equalTo(authMetadata.get(API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY))); | 
|  | 331 | +        } | 
|  | 332 | +    } | 
|  | 333 | + | 
| 286 | 334 |     public void testGetFleetApiKeyRoleReferenceBwcBugFix() { | 
| 287 | 335 |         final BytesReference roleBytes = new BytesArray("{\"a role\": {\"cluster\": [\"all\"]}}"); | 
| 288 | 336 |         final BytesReference limitedByRoleBytes = new BytesArray("{}"); | 
|  | 
0 commit comments