@@ -696,43 +696,39 @@ private static Map<String, Object> maybeRewriteMetadataForApiKeyRoleDescriptors(
696696 Map <String , Object > metadata = authentication .getMetadata ();
697697 // If authentication type is API key, regardless whether it has run-as, the metadata must contain API key role descriptors
698698 if (authentication .isAuthenticatedWithApiKey ()) {
699+ assert metadata .containsKey (AuthenticationField .API_KEY_ROLE_DESCRIPTORS_KEY )
700+ : "metadata must contain role descriptor for API key authentication" ;
701+ assert metadata .containsKey (AuthenticationField .API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY )
702+ : "metadata must contain limited role descriptor for API key authentication" ;
699703 if (authentication .getVersion ().onOrAfter (VERSION_API_KEY_ROLES_AS_BYTES )
700704 && streamVersion .before (VERSION_API_KEY_ROLES_AS_BYTES )) {
701705 metadata = new HashMap <>(metadata );
702- if (metadata .containsKey (AuthenticationField .API_KEY_ROLE_DESCRIPTORS_KEY )) {
706+ metadata .put (
707+ AuthenticationField .API_KEY_ROLE_DESCRIPTORS_KEY ,
708+ convertRoleDescriptorsBytesToMap ((BytesReference ) metadata .get (AuthenticationField .API_KEY_ROLE_DESCRIPTORS_KEY ))
709+ );
710+ metadata .put (
711+ AuthenticationField .API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY ,
712+ convertRoleDescriptorsBytesToMap (
713+ (BytesReference ) metadata .get (AuthenticationField .API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY )
714+ )
715+ );
716+ } else if (authentication .getVersion ().before (VERSION_API_KEY_ROLES_AS_BYTES )
717+ && streamVersion .onOrAfter (VERSION_API_KEY_ROLES_AS_BYTES )) {
718+ metadata = new HashMap <>(metadata );
703719 metadata .put (
704720 AuthenticationField .API_KEY_ROLE_DESCRIPTORS_KEY ,
705- convertRoleDescriptorsBytesToMap ((BytesReference ) metadata .get (AuthenticationField .API_KEY_ROLE_DESCRIPTORS_KEY ))
721+ convertRoleDescriptorsMapToBytes (
722+ (Map <String , Object >) metadata .get (AuthenticationField .API_KEY_ROLE_DESCRIPTORS_KEY )
723+ )
706724 );
707- }
708- if (metadata .containsKey (AuthenticationField .API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY )) {
709725 metadata .put (
710726 AuthenticationField .API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY ,
711- convertRoleDescriptorsBytesToMap (
712- (BytesReference ) metadata .get (AuthenticationField .API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY )
727+ convertRoleDescriptorsMapToBytes (
728+ (Map < String , Object > ) metadata .get (AuthenticationField .API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY )
713729 )
714730 );
715731 }
716- } else if (authentication .getVersion ().before (VERSION_API_KEY_ROLES_AS_BYTES )
717- && streamVersion .onOrAfter (VERSION_API_KEY_ROLES_AS_BYTES )) {
718- metadata = new HashMap <>(metadata );
719- if (metadata .containsKey (AuthenticationField .API_KEY_ROLE_DESCRIPTORS_KEY )) {
720- metadata .put (
721- AuthenticationField .API_KEY_ROLE_DESCRIPTORS_KEY ,
722- convertRoleDescriptorsMapToBytes (
723- (Map <String , Object >) metadata .get (AuthenticationField .API_KEY_ROLE_DESCRIPTORS_KEY )
724- )
725- );
726- }
727- if (metadata .containsKey (AuthenticationField .API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY )) {
728- metadata .put (
729- AuthenticationField .API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY ,
730- convertRoleDescriptorsMapToBytes (
731- (Map <String , Object >) metadata .get (AuthenticationField .API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY )
732- )
733- );
734- }
735- }
736732 }
737733 return metadata ;
738734 }
0 commit comments