13
13
import org .elasticsearch .xpack .core .security .action .GetApiKeyRequest ;
14
14
import org .elasticsearch .xpack .core .security .action .InvalidateApiKeyRequest ;
15
15
import org .elasticsearch .xpack .core .security .authc .Authentication ;
16
+ import org .elasticsearch .xpack .core .security .authc .Authentication .AuthenticationType ;
16
17
import org .elasticsearch .xpack .core .security .authz .permission .ClusterPermission ;
17
18
import org .elasticsearch .xpack .core .security .user .User ;
18
19
@@ -28,8 +29,8 @@ public void testAuthenticationWithApiKeyAllowsAccessToApiKeyActionsWhenItIsOwner
28
29
ManageOwnApiKeyClusterPrivilege .INSTANCE .buildPermission (ClusterPermission .builder ()).build ();
29
30
30
31
final String apiKeyId = randomAlphaOfLengthBetween (4 , 7 );
31
- final Authentication authentication = createMockAuthentication ("joe" ,"_es_api_key" , "_es_api_key" ,
32
- Map .of ("_security_api_key_id" , apiKeyId ));
32
+ final Authentication authentication = createMockAuthentication ("joe" ,"_es_api_key" ,
33
+ AuthenticationType . API_KEY , Map .of ("_security_api_key_id" , apiKeyId ));
33
34
final TransportRequest getApiKeyRequest = GetApiKeyRequest .usingApiKeyId (apiKeyId , randomBoolean ());
34
35
final TransportRequest invalidateApiKeyRequest = InvalidateApiKeyRequest .usingApiKeyId (apiKeyId , randomBoolean ());
35
36
@@ -43,8 +44,8 @@ public void testAuthenticationWithApiKeyDeniesAccessToApiKeyActionsWhenItIsNotOw
43
44
ManageOwnApiKeyClusterPrivilege .INSTANCE .buildPermission (ClusterPermission .builder ()).build ();
44
45
45
46
final String apiKeyId = randomAlphaOfLengthBetween (4 , 7 );
46
- final Authentication authentication = createMockAuthentication ("joe" ,"_es_api_key" , "_es_api_key" ,
47
- Map .of ("_security_api_key_id" , randomAlphaOfLength (7 )));
47
+ final Authentication authentication = createMockAuthentication ("joe" ,"_es_api_key" ,
48
+ AuthenticationType . API_KEY , Map .of ("_security_api_key_id" , randomAlphaOfLength (7 )));
48
49
final TransportRequest getApiKeyRequest = GetApiKeyRequest .usingApiKeyId (apiKeyId , randomBoolean ());
49
50
final TransportRequest invalidateApiKeyRequest = InvalidateApiKeyRequest .usingApiKeyId (apiKeyId , randomBoolean ());
50
51
@@ -56,7 +57,8 @@ public void testAuthenticationWithUserAllowsAccessToApiKeyActionsWhenItIsOwner()
56
57
final ClusterPermission clusterPermission =
57
58
ManageOwnApiKeyClusterPrivilege .INSTANCE .buildPermission (ClusterPermission .builder ()).build ();
58
59
59
- final Authentication authentication = createMockAuthentication ("joe" ,"realm1" , "native" , Map .of ());
60
+ final Authentication authentication = createMockAuthentication ("joe" ,"realm1" ,
61
+ AuthenticationType .REALM , Map .of ());
60
62
final TransportRequest getApiKeyRequest = GetApiKeyRequest .usingRealmAndUserName ("realm1" , "joe" );
61
63
final TransportRequest invalidateApiKeyRequest = InvalidateApiKeyRequest .usingRealmAndUserName ("realm1" , "joe" );
62
64
@@ -69,7 +71,8 @@ public void testAuthenticationWithUserAllowsAccessToApiKeyActionsWhenItIsOwner_W
69
71
final ClusterPermission clusterPermission =
70
72
ManageOwnApiKeyClusterPrivilege .INSTANCE .buildPermission (ClusterPermission .builder ()).build ();
71
73
72
- final Authentication authentication = createMockAuthentication ("joe" ,"realm1" , "native" , Map .of ());
74
+ final Authentication authentication = createMockAuthentication ("joe" ,"realm1" ,
75
+ AuthenticationType .REALM , Map .of ());
73
76
final TransportRequest getApiKeyRequest = GetApiKeyRequest .forOwnedApiKeys ();
74
77
final TransportRequest invalidateApiKeyRequest = InvalidateApiKeyRequest .forOwnedApiKeys ();
75
78
@@ -82,7 +85,8 @@ public void testAuthenticationWithUserDeniesAccessToApiKeyActionsWhenItIsNotOwne
82
85
final ClusterPermission clusterPermission =
83
86
ManageOwnApiKeyClusterPrivilege .INSTANCE .buildPermission (ClusterPermission .builder ()).build ();
84
87
85
- final Authentication authentication = createMockAuthentication ("joe" , "realm1" , "native" , Map .of ());
88
+ final Authentication authentication = createMockAuthentication ("joe" , "realm1" ,
89
+ AuthenticationType .REALM , Map .of ());
86
90
final TransportRequest getApiKeyRequest = randomFrom (
87
91
GetApiKeyRequest .usingRealmAndUserName ("realm1" , randomAlphaOfLength (7 )),
88
92
GetApiKeyRequest .usingRealmAndUserName (randomAlphaOfLength (5 ), "joe" ),
@@ -110,14 +114,15 @@ public void testGetAndInvalidateApiKeyWillRespectRunAsUser() {
110
114
InvalidateApiKeyRequest .usingRealmAndUserName ("realm_b" , "user_b" ), authentication ));
111
115
}
112
116
113
- private Authentication createMockAuthentication (String username , String realmName , String realmType , Map <String , Object > metadata ) {
117
+ private Authentication createMockAuthentication (String username , String realmName ,
118
+ AuthenticationType authenticationType , Map <String , Object > metadata ) {
114
119
final User user = new User (username );
115
120
final Authentication authentication = mock (Authentication .class );
116
121
final Authentication .RealmRef authenticatedBy = mock (Authentication .RealmRef .class );
117
122
when (authentication .getUser ()).thenReturn (user );
118
123
when (authentication .getSourceRealm ()).thenReturn (authenticatedBy );
124
+ when (authentication .getAuthenticationType ()).thenReturn (authenticationType );
119
125
when (authenticatedBy .getName ()).thenReturn (realmName );
120
- when (authenticatedBy .getType ()).thenReturn (realmType );
121
126
when (authentication .getMetadata ()).thenReturn (metadata );
122
127
return authentication ;
123
128
}
@@ -136,6 +141,7 @@ private Authentication createMockRunAsAuthentication(String username, String rea
136
141
when (authentication .getAuthenticatedBy ()).thenReturn (authenticatedBy );
137
142
when (authentication .getSourceRealm ()).thenReturn (lookedUpBy );
138
143
when (authentication .getMetadata ()).thenReturn (Map .of ());
144
+ when (authentication .getAuthenticationType ()).thenReturn (AuthenticationType .REALM );
139
145
return authentication ;
140
146
}
141
147
}
0 commit comments