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