2828import org .elasticsearch .client .security .ClearRolesCacheResponse ;
2929import org .elasticsearch .client .security .ClearSecurityCacheResponse ;
3030import org .elasticsearch .client .security .CreateApiKeyRequest ;
31+ import org .elasticsearch .client .security .CreateApiKeyRequestTests ;
3132import org .elasticsearch .client .security .CreateApiKeyResponse ;
3233import org .elasticsearch .client .security .CreateTokenRequest ;
3334import org .elasticsearch .client .security .CreateTokenResponse ;
@@ -1957,10 +1958,11 @@ public void testCreateApiKey() throws Exception {
19571958 .indicesPrivileges (IndicesPrivileges .builder ().indices ("ind-x" ).privileges (IndexPrivilegeName .ALL ).build ()).build ());
19581959 final TimeValue expiration = TimeValue .timeValueHours (24 );
19591960 final RefreshPolicy refreshPolicy = randomFrom (RefreshPolicy .values ());
1961+ final Map <String , Object > metadata = CreateApiKeyRequestTests .randomMetadata ();
19601962 {
19611963 final String name = randomAlphaOfLength (5 );
19621964 // tag::create-api-key-request
1963- CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest (name , roles , expiration , refreshPolicy );
1965+ CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest (name , roles , expiration , refreshPolicy , metadata );
19641966 // end::create-api-key-request
19651967
19661968 // tag::create-api-key-execute
@@ -1978,7 +1980,7 @@ public void testCreateApiKey() throws Exception {
19781980
19791981 {
19801982 final String name = randomAlphaOfLength (5 );
1981- CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest (name , roles , expiration , refreshPolicy );
1983+ CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest (name , roles , expiration , refreshPolicy , metadata );
19821984
19831985 ActionListener <CreateApiKeyResponse > listener ;
19841986 // tag::create-api-key-execute-listener
@@ -2027,6 +2029,7 @@ public void testGrantApiKey() throws Exception {
20272029
20282030
20292031 final Instant start = Instant .now ();
2032+ final Map <String , Object > metadata = CreateApiKeyRequestTests .randomMetadata ();
20302033 CheckedConsumer <CreateApiKeyResponse , IOException > apiKeyVerifier = (created ) -> {
20312034 final GetApiKeyRequest getApiKeyRequest = GetApiKeyRequest .usingApiKeyId (created .getId (), false );
20322035 final GetApiKeyResponse getApiKeyResponse = client .security ().getApiKey (getApiKeyRequest , RequestOptions .DEFAULT );
@@ -2039,14 +2042,19 @@ public void testGrantApiKey() throws Exception {
20392042 assertThat (apiKeyInfo .isInvalidated (), equalTo (false ));
20402043 assertThat (apiKeyInfo .getCreation (), greaterThanOrEqualTo (start ));
20412044 assertThat (apiKeyInfo .getCreation (), lessThanOrEqualTo (Instant .now ()));
2045+ if (metadata == null ) {
2046+ assertThat (apiKeyInfo .getMetadata (), equalTo (Map .of ()));
2047+ } else {
2048+ assertThat (apiKeyInfo .getMetadata (), equalTo (metadata ));
2049+ }
20422050 };
20432051
20442052 final TimeValue expiration = TimeValue .timeValueHours (24 );
20452053 final RefreshPolicy refreshPolicy = randomFrom (RefreshPolicy .values ());
20462054 {
20472055 final String name = randomAlphaOfLength (5 );
20482056 // tag::grant-api-key-request
2049- CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest (name , roles , expiration , refreshPolicy );
2057+ CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest (name , roles , expiration , refreshPolicy , metadata );
20502058 GrantApiKeyRequest .Grant grant = GrantApiKeyRequest .Grant .passwordGrant (username , password );
20512059 GrantApiKeyRequest grantApiKeyRequest = new GrantApiKeyRequest (grant , createApiKeyRequest );
20522060 // end::grant-api-key-request
@@ -2071,7 +2079,7 @@ public void testGrantApiKey() throws Exception {
20712079 final CreateTokenRequest tokenRequest = CreateTokenRequest .passwordGrant (username , password );
20722080 final CreateTokenResponse token = client .security ().createToken (tokenRequest , RequestOptions .DEFAULT );
20732081
2074- CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest (name , roles , expiration , refreshPolicy );
2082+ CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest (name , roles , expiration , refreshPolicy , metadata );
20752083 GrantApiKeyRequest .Grant grant = GrantApiKeyRequest .Grant .accessTokenGrant (token .getAccessToken ());
20762084 GrantApiKeyRequest grantApiKeyRequest = new GrantApiKeyRequest (grant , createApiKeyRequest );
20772085
@@ -2117,14 +2125,15 @@ public void testGetApiKey() throws Exception {
21172125 .indicesPrivileges (IndicesPrivileges .builder ().indices ("ind-x" ).privileges (IndexPrivilegeName .ALL ).build ()).build ());
21182126 final TimeValue expiration = TimeValue .timeValueHours (24 );
21192127 final RefreshPolicy refreshPolicy = randomFrom (RefreshPolicy .values ());
2128+ final Map <String , Object > metadata = CreateApiKeyRequestTests .randomMetadata ();
21202129 // Create API Keys
2121- CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest ("k1" , roles , expiration , refreshPolicy );
2130+ CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest ("k1" , roles , expiration , refreshPolicy , metadata );
21222131 CreateApiKeyResponse createApiKeyResponse1 = client .security ().createApiKey (createApiKeyRequest , RequestOptions .DEFAULT );
21232132 assertThat (createApiKeyResponse1 .getName (), equalTo ("k1" ));
21242133 assertNotNull (createApiKeyResponse1 .getKey ());
21252134
21262135 final ApiKey expectedApiKeyInfo = new ApiKey (createApiKeyResponse1 .getName (), createApiKeyResponse1 .getId (), Instant .now (),
2127- Instant .now ().plusMillis (expiration .getMillis ()), false , "test_user" , "default_file" );
2136+ Instant .now ().plusMillis (expiration .getMillis ()), false , "test_user" , "default_file" , metadata );
21282137 {
21292138 // tag::get-api-key-id-request
21302139 GetApiKeyRequest getApiKeyRequest = GetApiKeyRequest .usingApiKeyId (createApiKeyResponse1 .getId (), false );
@@ -2258,6 +2267,11 @@ private void verifyApiKey(final ApiKey actual, final ApiKey expected) {
22582267 assertThat (actual .getRealm (), is (expected .getRealm ()));
22592268 assertThat (actual .isInvalidated (), is (expected .isInvalidated ()));
22602269 assertThat (actual .getExpiration (), is (greaterThan (Instant .now ())));
2270+ if (expected .getMetadata () == null ) {
2271+ assertThat (actual .getMetadata (), equalTo (Map .of ()));
2272+ } else {
2273+ assertThat (actual .getMetadata (), equalTo (expected .getMetadata ()));
2274+ }
22612275 }
22622276
22632277 public void testInvalidateApiKey () throws Exception {
@@ -2267,8 +2281,9 @@ public void testInvalidateApiKey() throws Exception {
22672281 .indicesPrivileges (IndicesPrivileges .builder ().indices ("ind-x" ).privileges (IndexPrivilegeName .ALL ).build ()).build ());
22682282 final TimeValue expiration = TimeValue .timeValueHours (24 );
22692283 final RefreshPolicy refreshPolicy = randomFrom (RefreshPolicy .values ());
2284+ final Map <String , Object > metadata = CreateApiKeyRequestTests .randomMetadata ();
22702285 // Create API Keys
2271- CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest ("k1" , roles , expiration , refreshPolicy );
2286+ CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest ("k1" , roles , expiration , refreshPolicy , metadata );
22722287 CreateApiKeyResponse createApiKeyResponse1 = client .security ().createApiKey (createApiKeyRequest , RequestOptions .DEFAULT );
22732288 assertThat (createApiKeyResponse1 .getName (), equalTo ("k1" ));
22742289 assertNotNull (createApiKeyResponse1 .getKey ());
@@ -2312,7 +2327,7 @@ public void testInvalidateApiKey() throws Exception {
23122327 }
23132328
23142329 {
2315- createApiKeyRequest = new CreateApiKeyRequest ("k2" , roles , expiration , refreshPolicy );
2330+ createApiKeyRequest = new CreateApiKeyRequest ("k2" , roles , expiration , refreshPolicy , metadata );
23162331 CreateApiKeyResponse createApiKeyResponse2 = client .security ().createApiKey (createApiKeyRequest , RequestOptions .DEFAULT );
23172332 assertThat (createApiKeyResponse2 .getName (), equalTo ("k2" ));
23182333 assertNotNull (createApiKeyResponse2 .getKey ());
@@ -2336,7 +2351,7 @@ public void testInvalidateApiKey() throws Exception {
23362351 }
23372352
23382353 {
2339- createApiKeyRequest = new CreateApiKeyRequest ("k3" , roles , expiration , refreshPolicy );
2354+ createApiKeyRequest = new CreateApiKeyRequest ("k3" , roles , expiration , refreshPolicy , metadata );
23402355 CreateApiKeyResponse createApiKeyResponse3 = client .security ().createApiKey (createApiKeyRequest , RequestOptions .DEFAULT );
23412356 assertThat (createApiKeyResponse3 .getName (), equalTo ("k3" ));
23422357 assertNotNull (createApiKeyResponse3 .getKey ());
@@ -2359,7 +2374,7 @@ public void testInvalidateApiKey() throws Exception {
23592374 }
23602375
23612376 {
2362- createApiKeyRequest = new CreateApiKeyRequest ("k4" , roles , expiration , refreshPolicy );
2377+ createApiKeyRequest = new CreateApiKeyRequest ("k4" , roles , expiration , refreshPolicy , metadata );
23632378 CreateApiKeyResponse createApiKeyResponse4 = client .security ().createApiKey (createApiKeyRequest , RequestOptions .DEFAULT );
23642379 assertThat (createApiKeyResponse4 .getName (), equalTo ("k4" ));
23652380 assertNotNull (createApiKeyResponse4 .getKey ());
@@ -2382,7 +2397,7 @@ public void testInvalidateApiKey() throws Exception {
23822397 }
23832398
23842399 {
2385- createApiKeyRequest = new CreateApiKeyRequest ("k5" , roles , expiration , refreshPolicy );
2400+ createApiKeyRequest = new CreateApiKeyRequest ("k5" , roles , expiration , refreshPolicy , metadata );
23862401 CreateApiKeyResponse createApiKeyResponse5 = client .security ().createApiKey (createApiKeyRequest , RequestOptions .DEFAULT );
23872402 assertThat (createApiKeyResponse5 .getName (), equalTo ("k5" ));
23882403 assertNotNull (createApiKeyResponse5 .getKey ());
@@ -2407,7 +2422,7 @@ public void testInvalidateApiKey() throws Exception {
24072422 }
24082423
24092424 {
2410- createApiKeyRequest = new CreateApiKeyRequest ("k6" , roles , expiration , refreshPolicy );
2425+ createApiKeyRequest = new CreateApiKeyRequest ("k6" , roles , expiration , refreshPolicy , metadata );
24112426 CreateApiKeyResponse createApiKeyResponse6 = client .security ().createApiKey (createApiKeyRequest , RequestOptions .DEFAULT );
24122427 assertThat (createApiKeyResponse6 .getName (), equalTo ("k6" ));
24132428 assertNotNull (createApiKeyResponse6 .getKey ());
@@ -2450,7 +2465,7 @@ public void onFailure(Exception e) {
24502465 }
24512466
24522467 {
2453- createApiKeyRequest = new CreateApiKeyRequest ("k7" , roles , expiration , refreshPolicy );
2468+ createApiKeyRequest = new CreateApiKeyRequest ("k7" , roles , expiration , refreshPolicy , metadata );
24542469 CreateApiKeyResponse createApiKeyResponse7 = client .security ().createApiKey (createApiKeyRequest , RequestOptions .DEFAULT );
24552470 assertThat (createApiKeyResponse7 .getName (), equalTo ("k7" ));
24562471 assertNotNull (createApiKeyResponse7 .getKey ());
0 commit comments