@@ -545,9 +545,37 @@ public void testApiKeyAuthorizationApiKeyMustBeAbleToRetrieveItsOwnInformation()
545545
546546 final PlainActionFuture <GetApiKeyResponse > failureListener = new PlainActionFuture <>();
547547 // for any other API key id, it must deny access
548- client .execute (GetApiKeyAction .INSTANCE , GetApiKeyRequest .usingApiKeyId (responses .get (1 ).getId (), false ), failureListener );
548+ client .execute (GetApiKeyAction .INSTANCE , GetApiKeyRequest .usingApiKeyId (responses .get (1 ).getId (), false ),
549+ failureListener );
549550 ElasticsearchSecurityException ese = expectThrows (ElasticsearchSecurityException .class , () -> failureListener .actionGet ());
550- assertErrorMessage (ese , "cluster:admin/xpack/security/api_key/get" , SecuritySettingsSource .TEST_SUPERUSER );
551+ assertErrorMessage (ese , "cluster:admin/xpack/security/api_key/get" , SecuritySettingsSource .TEST_SUPERUSER ,
552+ responses .get (0 ).getId ());
553+ }
554+
555+ public void testApiKeyWithManageOwnPrivilegeIsAbleToInvalidateItselfButNotAnyOtherKeysCreatedBySameOwner () throws InterruptedException
556+ , ExecutionException {
557+ List <CreateApiKeyResponse > responses = createApiKeys (SecuritySettingsSource .TEST_SUPERUSER , 2 , null , "manage_own_api_key" );
558+ final String base64ApiKeyKeyValue = Base64 .getEncoder ().encodeToString (
559+ (responses .get (0 ).getId () + ":" + responses .get (0 ).getKey ().toString ()).getBytes (StandardCharsets .UTF_8 ));
560+ Client client = client ().filterWithHeader (Map .of ("Authorization" , "ApiKey " + base64ApiKeyKeyValue ));
561+ PlainActionFuture <InvalidateApiKeyResponse > listener = new PlainActionFuture <>();
562+
563+ final PlainActionFuture <InvalidateApiKeyResponse > failureListener = new PlainActionFuture <>();
564+ // for any other API key id, it must deny access
565+ client .execute (InvalidateApiKeyAction .INSTANCE , InvalidateApiKeyRequest .usingApiKeyId (responses .get (1 ).getId (), false ),
566+ failureListener );
567+ ElasticsearchSecurityException ese = expectThrows (ElasticsearchSecurityException .class , () -> failureListener .actionGet ());
568+ assertErrorMessage (ese , "cluster:admin/xpack/security/api_key/invalidate" , SecuritySettingsSource .TEST_SUPERUSER ,
569+ responses .get (0 ).getId ());
570+
571+ client .execute (InvalidateApiKeyAction .INSTANCE , InvalidateApiKeyRequest .usingApiKeyId (responses .get (0 ).getId (), false ),
572+ listener );
573+ InvalidateApiKeyResponse invalidateResponse = listener .get ();
574+
575+ assertThat (invalidateResponse .getInvalidatedApiKeys ().size (), equalTo (1 ));
576+ assertThat (invalidateResponse .getInvalidatedApiKeys (), containsInAnyOrder (responses .get (0 ).getId ()));
577+ assertThat (invalidateResponse .getPreviouslyInvalidatedApiKeys ().size (), equalTo (0 ));
578+ assertThat (invalidateResponse .getErrors ().size (), equalTo (0 ));
551579 }
552580
553581 private void verifyGetResponse (int expectedNumberOfApiKeys , List <CreateApiKeyResponse > responses ,
@@ -582,13 +610,17 @@ private void verifyGetResponse(String user, int expectedNumberOfApiKeys, List<Cr
582610 }
583611
584612 private List <CreateApiKeyResponse > createApiKeys (int noOfApiKeys , TimeValue expiration ) {
585- return createApiKeys (SecuritySettingsSource .TEST_SUPERUSER , noOfApiKeys , expiration );
613+ return createApiKeys (SecuritySettingsSource .TEST_SUPERUSER , noOfApiKeys , expiration , "monitor" );
586614 }
587615
588616 private List <CreateApiKeyResponse > createApiKeys (String user , int noOfApiKeys , TimeValue expiration ) {
617+ return createApiKeys (user , noOfApiKeys , expiration , "monitor" );
618+ }
619+
620+ private List <CreateApiKeyResponse > createApiKeys (String user , int noOfApiKeys , TimeValue expiration , String role ) {
589621 List <CreateApiKeyResponse > responses = new ArrayList <>();
590622 for (int i = 0 ; i < noOfApiKeys ; i ++) {
591- final RoleDescriptor descriptor = new RoleDescriptor ("role" , new String [] { "monitor" }, null , null );
623+ final RoleDescriptor descriptor = new RoleDescriptor ("role" , new String [] { role }, null , null );
592624 Client client = client ().filterWithHeader (Collections .singletonMap ("Authorization" , UsernamePasswordToken
593625 .basicAuthHeaderValue (user , SecuritySettingsSourceField .TEST_PASSWORD_SECURE_STRING )));
594626 final CreateApiKeyResponse response = new CreateApiKeyRequestBuilder (client )
@@ -602,6 +634,11 @@ private List<CreateApiKeyResponse> createApiKeys(String user, int noOfApiKeys, T
602634 return responses ;
603635 }
604636
637+ private void assertErrorMessage (final ElasticsearchSecurityException ese , String action , String userName , String apiKeyId ) {
638+ assertThat (ese .getMessage (),
639+ is ("action [" + action + "] is unauthorized for API key id [" + apiKeyId + "] of user [" + userName + "]" ));
640+ }
641+
605642 private void assertErrorMessage (final ElasticsearchSecurityException ese , String action , String userName ) {
606643 assertThat (ese .getMessage (), is ("action [" + action + "] is unauthorized for user [" + userName + "]" ));
607644 }
0 commit comments