7
7
8
8
import org .elasticsearch .Version ;
9
9
import org .elasticsearch .common .bytes .BytesArray ;
10
+ import org .elasticsearch .common .bytes .BytesReference ;
10
11
import org .elasticsearch .common .settings .Settings ;
11
12
import org .elasticsearch .common .util .concurrent .ThreadContext ;
12
13
import org .elasticsearch .common .util .concurrent .ThreadContext .StoredContext ;
28
29
import java .util .Map ;
29
30
import java .util .concurrent .atomic .AtomicReference ;
30
31
32
+ import static org .elasticsearch .xpack .core .security .authc .Authentication .VERSION_API_KEY_ROLES_AS_BYTES ;
31
33
import static org .elasticsearch .xpack .core .security .authc .AuthenticationField .API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY ;
32
34
import static org .elasticsearch .xpack .core .security .authc .AuthenticationField .API_KEY_ROLE_DESCRIPTORS_KEY ;
33
35
import static org .hamcrest .Matchers .instanceOf ;
@@ -136,7 +138,7 @@ public void testExecuteAfterRewritingAuthentication() throws IOException {
136
138
assertEquals (original , securityContext .getAuthentication ());
137
139
}
138
140
139
- public void testExecuteAfterRewritingAuthenticationShouldRewriteApiKeyMetadataForBwc () throws IOException {
141
+ public void testExecuteAfterRewritingAuthenticationWillConditionallyRewriteNewApiKeyMetadata () throws IOException {
140
142
User user = new User ("test" , null , new User ("authUser" ));
141
143
RealmRef authBy = new RealmRef ("_es_api_key" , "_es_api_key" , "node1" );
142
144
final Map <String , Object > metadata = Map .of (
@@ -147,16 +149,23 @@ API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY, new BytesArray("{\"limitedBy role\": {\"cl
147
149
AuthenticationType .API_KEY , metadata );
148
150
original .writeToContext (threadContext );
149
151
152
+ // If target is old node, rewrite new style API key metadata to old format
150
153
securityContext .executeAfterRewritingAuthentication (originalCtx -> {
151
154
Authentication authentication = securityContext .getAuthentication ();
152
155
assertEquals (Map .of ("a role" , Map .of ("cluster" , List .of ("all" ))),
153
156
authentication .getMetadata ().get (API_KEY_ROLE_DESCRIPTORS_KEY ));
154
157
assertEquals (Map .of ("limitedBy role" , Map .of ("cluster" , List .of ("all" ))),
155
158
authentication .getMetadata ().get (API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY ));
156
159
}, Version .V_7_8_0 );
160
+
161
+ // If target is new node, no need to rewrite the new style API key metadata
162
+ securityContext .executeAfterRewritingAuthentication (originalCtx -> {
163
+ Authentication authentication = securityContext .getAuthentication ();
164
+ assertSame (metadata , authentication .getMetadata ());
165
+ }, VersionUtils .randomVersionBetween (random (), VERSION_API_KEY_ROLES_AS_BYTES , Version .CURRENT ));
157
166
}
158
167
159
- public void testExecuteAfterRewritingAuthenticationShouldNotRewriteApiKeyMetadataForOldAuthenticationObject () throws IOException {
168
+ public void testExecuteAfterRewritingAuthenticationWillConditionallyRewriteOldApiKeyMetadata () throws IOException {
160
169
User user = new User ("test" , null , new User ("authUser" ));
161
170
RealmRef authBy = new RealmRef ("_es_api_key" , "_es_api_key" , "node1" );
162
171
final Map <String , Object > metadata = Map .of (
@@ -166,9 +175,19 @@ public void testExecuteAfterRewritingAuthenticationShouldNotRewriteApiKeyMetadat
166
175
final Authentication original = new Authentication (user , authBy , authBy , Version .V_7_8_0 , AuthenticationType .API_KEY , metadata );
167
176
original .writeToContext (threadContext );
168
177
178
+ // If target is old node, no need to rewrite old style API key metadata
169
179
securityContext .executeAfterRewritingAuthentication (originalCtx -> {
170
180
Authentication authentication = securityContext .getAuthentication ();
171
181
assertSame (metadata , authentication .getMetadata ());
172
- }, randomFrom (Version .V_8_0_0 , Version .V_7_8_0 ));
182
+ }, Version .V_7_8_0 );
183
+
184
+ // If target is new old, ensure old map style API key metadata is rewritten to bytesreference
185
+ securityContext .executeAfterRewritingAuthentication (originalCtx -> {
186
+ Authentication authentication = securityContext .getAuthentication ();
187
+ assertEquals ("{\" a role\" :{\" cluster\" :[\" all\" ]}}" ,
188
+ ((BytesReference )authentication .getMetadata ().get (API_KEY_ROLE_DESCRIPTORS_KEY )).utf8ToString ());
189
+ assertEquals ("{\" limitedBy role\" :{\" cluster\" :[\" all\" ]}}" ,
190
+ ((BytesReference )authentication .getMetadata ().get (API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY )).utf8ToString ());
191
+ }, VersionUtils .randomVersionBetween (random (), VERSION_API_KEY_ROLES_AS_BYTES , Version .CURRENT ));
173
192
}
174
193
}
0 commit comments