88package org .elasticsearch .xpack .security .profile ;
99
1010import org .elasticsearch .ElasticsearchException ;
11+ import org .elasticsearch .Version ;
1112import org .elasticsearch .action .ActionListener ;
1213import org .elasticsearch .action .bulk .BulkAction ;
1314import org .elasticsearch .action .bulk .BulkRequest ;
2526import org .elasticsearch .action .update .UpdateRequest ;
2627import org .elasticsearch .action .update .UpdateResponse ;
2728import org .elasticsearch .client .internal .Client ;
29+ import org .elasticsearch .cluster .ClusterState ;
30+ import org .elasticsearch .cluster .node .DiscoveryNodes ;
31+ import org .elasticsearch .cluster .service .ClusterService ;
2832import org .elasticsearch .common .Strings ;
2933import org .elasticsearch .common .bytes .BytesArray ;
3034import org .elasticsearch .common .settings .Settings ;
3943import org .elasticsearch .search .sort .ScoreSortBuilder ;
4044import org .elasticsearch .search .sort .SortOrder ;
4145import org .elasticsearch .test .ESTestCase ;
46+ import org .elasticsearch .test .VersionUtils ;
4247import org .elasticsearch .threadpool .FixedExecutorBuilder ;
4348import org .elasticsearch .threadpool .TestThreadPool ;
4449import org .elasticsearch .threadpool .ThreadPool ;
6873
6974import static org .elasticsearch .common .util .concurrent .ThreadContext .ACTION_ORIGIN_TRANSIENT_NAME ;
7075import static org .elasticsearch .test .ActionListenerUtils .anyActionListener ;
76+ import static org .elasticsearch .xpack .core .ClientHelper .SECURITY_ORIGIN ;
7177import static org .elasticsearch .xpack .core .ClientHelper .SECURITY_PROFILE_ORIGIN ;
7278import static org .elasticsearch .xpack .security .Security .SECURITY_CRYPTO_THREAD_POOL_NAME ;
7379import static org .elasticsearch .xpack .security .support .SecuritySystemIndices .SECURITY_PROFILE_ALIAS ;
@@ -124,6 +130,7 @@ public class ProfileServiceTests extends ESTestCase {
124130 private Client client ;
125131 private SecurityIndexManager profileIndex ;
126132 private ProfileService profileService ;
133+ private Version minNodeVersion ;
127134
128135 @ Before
129136 public void prepare () {
@@ -146,7 +153,14 @@ public void prepare() {
146153 new SearchRequestBuilder (client , SearchAction .INSTANCE ).setIndices (SECURITY_PROFILE_ALIAS )
147154 );
148155 this .profileIndex = SecurityMocks .mockSecurityIndexManager (SECURITY_PROFILE_ALIAS );
149- this .profileService = new ProfileService (Settings .EMPTY , Clock .systemUTC (), client , profileIndex , threadPool );
156+ final ClusterService clusterService = mock (ClusterService .class );
157+ final ClusterState clusterState = mock (ClusterState .class );
158+ when (clusterService .state ()).thenReturn (clusterState );
159+ final DiscoveryNodes discoveryNodes = mock (DiscoveryNodes .class );
160+ when (clusterState .nodes ()).thenReturn (discoveryNodes );
161+ minNodeVersion = VersionUtils .randomVersionBetween (random (), Version .V_7_17_0 , Version .CURRENT );
162+ when (discoveryNodes .getMinNodeVersion ()).thenReturn (minNodeVersion );
163+ this .profileService = new ProfileService (Settings .EMPTY , Clock .systemUTC (), client , profileIndex , clusterService , threadPool );
150164 }
151165
152166 @ After
@@ -157,7 +171,10 @@ public void stopThreadPool() {
157171 public void testGetProfileByUid () {
158172 final String uid = randomAlphaOfLength (20 );
159173 doAnswer (invocation -> {
160- assertThat (threadPool .getThreadContext ().getTransient (ACTION_ORIGIN_TRANSIENT_NAME ), equalTo (SECURITY_PROFILE_ORIGIN ));
174+ assertThat (
175+ threadPool .getThreadContext ().getTransient (ACTION_ORIGIN_TRANSIENT_NAME ),
176+ equalTo (minNodeVersion .onOrAfter (Version .V_8_3_0 ) ? SECURITY_PROFILE_ORIGIN : SECURITY_ORIGIN )
177+ );
161178 final GetRequest getRequest = (GetRequest ) invocation .getArguments ()[1 ];
162179 @ SuppressWarnings ("unchecked" )
163180 final ActionListener <GetResponse > listener = (ActionListener <GetResponse >) invocation .getArguments ()[2 ];
@@ -316,7 +333,10 @@ public void testBuildSearchRequest() {
316333 public void testSecurityProfileOrigin () {
317334 // Activate profile
318335 doAnswer (invocation -> {
319- assertThat (threadPool .getThreadContext ().getTransient (ACTION_ORIGIN_TRANSIENT_NAME ), equalTo (SECURITY_PROFILE_ORIGIN ));
336+ assertThat (
337+ threadPool .getThreadContext ().getTransient (ACTION_ORIGIN_TRANSIENT_NAME ),
338+ equalTo (minNodeVersion .onOrAfter (Version .V_8_3_0 ) ? SECURITY_PROFILE_ORIGIN : SECURITY_ORIGIN )
339+ );
320340 @ SuppressWarnings ("unchecked" )
321341 final ActionListener <SearchResponse > listener = (ActionListener <SearchResponse >) invocation .getArguments ()[2 ];
322342 listener .onResponse (SearchResponse .empty (() -> 1L , SearchResponse .Clusters .EMPTY ));
@@ -329,7 +349,10 @@ public void testSecurityProfileOrigin() {
329349
330350 final RuntimeException expectedException = new RuntimeException ("expected" );
331351 doAnswer (invocation -> {
332- assertThat (threadPool .getThreadContext ().getTransient (ACTION_ORIGIN_TRANSIENT_NAME ), equalTo (SECURITY_PROFILE_ORIGIN ));
352+ assertThat (
353+ threadPool .getThreadContext ().getTransient (ACTION_ORIGIN_TRANSIENT_NAME ),
354+ equalTo (minNodeVersion .onOrAfter (Version .V_8_3_0 ) ? SECURITY_PROFILE_ORIGIN : SECURITY_ORIGIN )
355+ );
333356 final ActionListener <?> listener = (ActionListener <?>) invocation .getArguments ()[2 ];
334357 listener .onFailure (expectedException );
335358 return null ;
@@ -342,7 +365,10 @@ public void testSecurityProfileOrigin() {
342365
343366 // Update
344367 doAnswer (invocation -> {
345- assertThat (threadPool .getThreadContext ().getTransient (ACTION_ORIGIN_TRANSIENT_NAME ), equalTo (SECURITY_PROFILE_ORIGIN ));
368+ assertThat (
369+ threadPool .getThreadContext ().getTransient (ACTION_ORIGIN_TRANSIENT_NAME ),
370+ equalTo (minNodeVersion .onOrAfter (Version .V_8_3_0 ) ? SECURITY_PROFILE_ORIGIN : SECURITY_ORIGIN )
371+ );
346372 final ActionListener <?> listener = (ActionListener <?>) invocation .getArguments ()[2 ];
347373 listener .onFailure (expectedException );
348374 return null ;
@@ -354,7 +380,10 @@ public void testSecurityProfileOrigin() {
354380
355381 // Suggest
356382 doAnswer (invocation -> {
357- assertThat (threadPool .getThreadContext ().getTransient (ACTION_ORIGIN_TRANSIENT_NAME ), equalTo (SECURITY_PROFILE_ORIGIN ));
383+ assertThat (
384+ threadPool .getThreadContext ().getTransient (ACTION_ORIGIN_TRANSIENT_NAME ),
385+ equalTo (minNodeVersion .onOrAfter (Version .V_8_3_0 ) ? SECURITY_PROFILE_ORIGIN : SECURITY_ORIGIN )
386+ );
358387 final ActionListener <?> listener = (ActionListener <?>) invocation .getArguments ()[2 ];
359388 listener .onFailure (expectedException );
360389 return null ;
0 commit comments