4848import static org .elasticsearch .xpack .core .security .support .Exceptions .authenticationError ;
4949import static org .elasticsearch .xpack .core .security .support .Exceptions .authorizationError ;
5050import static org .elasticsearch .xpack .security .authc .CrossClusterAccessHeaders .CROSS_CLUSTER_ACCESS_CREDENTIALS_HEADER_KEY ;
51- import static org .elasticsearch .xpack .security .transport .CrossClusterAccessServerTransportFilter .CROSS_CLUSTER_ACCESS_ACTION_ALLOWLIST ;
5251import static org .hamcrest .Matchers .arrayWithSize ;
5352import static org .hamcrest .Matchers .containsString ;
5453import static org .hamcrest .Matchers .equalTo ;
@@ -109,66 +108,42 @@ public void testInbound() {
109108 public void testCrossClusterAccessInbound () {
110109 TransportRequest request = mock (TransportRequest .class );
111110 Authentication authentication = AuthenticationTestHelper .builder ().build ();
112- boolean allowlisted = randomBoolean ();
113- String action = allowlisted ? randomFrom (CROSS_CLUSTER_ACCESS_ACTION_ALLOWLIST ) : "_action" ;
111+ String action = randomAlphaOfLengthBetween (10 , 20 );
114112 doAnswer (getAnswer (authentication )).when (authcService ).authenticate (eq (action ), eq (request ), eq (true ), anyActionListener ());
115113 doAnswer (getAnswer (authentication , true )).when (crossClusterAccessAuthcService )
116114 .authenticate (eq (action ), eq (request ), anyActionListener ());
117115 ServerTransportFilter filter = getNodeCrossClusterAccessFilter ();
118116 PlainActionFuture <Void > listener = spy (new PlainActionFuture <>());
119117 filter .inbound (action , request , channel , listener );
120- if (allowlisted ) {
121- verify (authzService ).authorize (eq (authentication ), eq (action ), eq (request ), anyActionListener ());
122- verify (crossClusterAccessAuthcService ).authenticate (anyString (), any (), anyActionListener ());
123- verify (authcService , never ()).authenticate (anyString (), any (), anyBoolean (), anyActionListener ());
124- } else {
125- var actual = expectThrows (IllegalArgumentException .class , listener ::actionGet );
126- assertThat (
127- actual .getMessage (),
128- equalTo ("action [" + action + "] is not allowed as a cross cluster operation on the dedicated remote cluster server port" )
129- );
130- verify (authcService , never ()).authenticate (anyString (), any (), anyBoolean (), anyActionListener ());
131- verify (crossClusterAccessAuthcService , never ()).authenticate (anyString (), any (), anyActionListener ());
132- verifyNoMoreInteractions (authzService );
133- }
118+ verify (authzService ).authorize (eq (authentication ), eq (action ), eq (request ), anyActionListener ());
119+ verify (crossClusterAccessAuthcService ).authenticate (anyString (), any (), anyActionListener ());
120+ verify (authcService , never ()).authenticate (anyString (), any (), anyBoolean (), anyActionListener ());
134121 }
135122
136123 public void testCrossClusterAccessInboundInvalidHeadersFail () {
137124 TransportRequest request = mock (TransportRequest .class );
138125 Authentication authentication = AuthenticationTestHelper .builder ().build ();
139- boolean allowlisted = randomBoolean ();
140- String action = allowlisted ? randomFrom (CROSS_CLUSTER_ACCESS_ACTION_ALLOWLIST ) : "_action" ;
126+ String action = randomAlphaOfLengthBetween (10 , 20 );
141127 doAnswer (getAnswer (authentication )).when (authcService ).authenticate (eq (action ), eq (request ), eq (true ), anyActionListener ());
142128 doAnswer (getAnswer (authentication , true )).when (crossClusterAccessAuthcService )
143129 .authenticate (eq (action ), eq (request ), anyActionListener ());
144130 ServerTransportFilter filter = getNodeCrossClusterAccessFilter (Set .copyOf (randomNonEmptySubsetOf (SECURITY_HEADER_FILTERS )));
145131 PlainActionFuture <Void > listener = new PlainActionFuture <>();
146132 filter .inbound (action , request , channel , listener );
147133 var actual = expectThrows (IllegalArgumentException .class , listener ::actionGet );
148- if (allowlisted ) {
149- verifyNoMoreInteractions (authcService );
150- verifyNoMoreInteractions (authzService );
151- assertThat (
152- actual .getMessage (),
153- containsString ("is not allowed for cross cluster requests through the dedicated remote cluster server port" )
154- );
155- } else {
156- verify (authcService , never ()).authenticate (anyString (), any (), anyBoolean (), anyActionListener ());
157- verify (crossClusterAccessAuthcService , never ()).authenticate (anyString (), any (), anyActionListener ());
158- verifyNoMoreInteractions (authzService );
159- assertThat (
160- actual .getMessage (),
161- equalTo ("action [" + action + "] is not allowed as a cross cluster operation on the dedicated remote cluster server port" )
162- );
163- }
134+ verifyNoMoreInteractions (authcService );
135+ verifyNoMoreInteractions (authzService );
136+ assertThat (
137+ actual .getMessage (),
138+ containsString ("is not allowed for cross cluster requests through the dedicated remote cluster server port" )
139+ );
164140 verify (crossClusterAccessAuthcService , never ()).authenticate (anyString (), any (), anyActionListener ());
165141 }
166142
167143 public void testCrossClusterAccessInboundMissingHeadersFail () {
168144 TransportRequest request = mock (TransportRequest .class );
169145 Authentication authentication = AuthenticationTestHelper .builder ().build ();
170- boolean allowlisted = randomBoolean ();
171- String action = allowlisted ? randomFrom (CROSS_CLUSTER_ACCESS_ACTION_ALLOWLIST ) : "_action" ;
146+ String action = randomAlphaOfLengthBetween (10 , 20 );
172147 doAnswer (getAnswer (authentication )).when (authcService ).authenticate (eq (action ), eq (request ), eq (true ), anyActionListener ());
173148 doAnswer (getAnswer (authentication , true )).when (crossClusterAccessAuthcService )
174149 .authenticate (eq (action ), eq (request ), anyActionListener ());
@@ -198,27 +173,17 @@ public void testCrossClusterAccessInboundMissingHeadersFail() {
198173 filter .inbound (action , request , channel , listener );
199174 var actual = expectThrows (IllegalArgumentException .class , listener ::actionGet );
200175
201- if (allowlisted ) {
202- verifyNoMoreInteractions (authcService );
203- verifyNoMoreInteractions (authzService );
204- assertThat (
205- actual .getMessage (),
206- equalTo (
207- "Cross cluster requests through the dedicated remote cluster server port require transport header ["
208- + firstMissingHeader
209- + "] but none found. "
210- + "Please ensure you have configured remote cluster credentials on the cluster originating the request."
211- )
212- );
213- } else {
214- verify (authcService , never ()).authenticate (anyString (), any (), anyBoolean (), anyActionListener ());
215- verify (crossClusterAccessAuthcService , never ()).authenticate (anyString (), any (), anyActionListener ());
216- verifyNoMoreInteractions (authzService );
217- assertThat (
218- actual .getMessage (),
219- equalTo ("action [" + action + "] is not allowed as a cross cluster operation on the dedicated remote cluster server port" )
220- );
221- }
176+ verifyNoMoreInteractions (authcService );
177+ verifyNoMoreInteractions (authzService );
178+ assertThat (
179+ actual .getMessage (),
180+ equalTo (
181+ "Cross cluster requests through the dedicated remote cluster server port require transport header ["
182+ + firstMissingHeader
183+ + "] but none found. "
184+ + "Please ensure you have configured remote cluster credentials on the cluster originating the request."
185+ )
186+ );
222187 verify (crossClusterAccessAuthcService , never ()).authenticate (anyString (), any (), anyActionListener ());
223188 }
224189
@@ -230,26 +195,14 @@ public void testInboundDestructiveOperations() {
230195 );
231196 Authentication authentication = AuthenticationTestHelper .builder ().build ();
232197 doAnswer (getAnswer (authentication )).when (authcService ).authenticate (eq (action ), eq (request ), eq (true ), anyActionListener ());
233- boolean crossClusterAccess = randomBoolean ();
234- ServerTransportFilter filter = crossClusterAccess ? getNodeCrossClusterAccessFilter () : getNodeFilter ();
198+ ServerTransportFilter filter = getNodeFilter ();
235199 PlainActionFuture <Void > listener = spy (new PlainActionFuture <>());
236200 filter .inbound (action , request , channel , listener );
237201 if (failDestructiveOperations ) {
238202 expectThrows (IllegalArgumentException .class , listener ::actionGet );
239203 verifyNoMoreInteractions (authzService );
240204 } else {
241- if (crossClusterAccess ) {
242- var actual = expectThrows (IllegalArgumentException .class , listener ::actionGet );
243- assertThat (
244- actual .getMessage (),
245- equalTo (
246- "action [" + action + "] is not allowed as a cross cluster operation on the dedicated remote cluster server port"
247- )
248- );
249- verifyNoMoreInteractions (authzService );
250- } else {
251- verify (authzService ).authorize (eq (authentication ), eq (action ), eq (request ), anyActionListener ());
252- }
205+ verify (authzService ).authorize (eq (authentication ), eq (action ), eq (request ), anyActionListener ());
253206 }
254207 }
255208
@@ -279,9 +232,7 @@ public void testInboundAuthenticationException() {
279232 public void testCrossClusterAccessInboundAuthenticationException () {
280233 TransportRequest request = mock (TransportRequest .class );
281234 Exception authE = authenticationError ("authc failed" );
282- // Only pick allowlisted action -- it does not make sense to pick one that isn't because we will never get to authenticate in that
283- // case
284- String action = randomFrom (CROSS_CLUSTER_ACCESS_ACTION_ALLOWLIST );
235+ String action = randomAlphaOfLengthBetween (10 , 20 );
285236 doAnswer (i -> {
286237 final Object [] args = i .getArguments ();
287238 assertThat (args , arrayWithSize (3 ));
@@ -344,7 +295,7 @@ public void testCrossClusterAccessInboundFailsWithUnsupportedLicense() {
344295
345296 ServerTransportFilter crossClusterAccessFilter = getNodeCrossClusterAccessFilter (unsupportedLicenseState );
346297 PlainActionFuture <Void > listener = new PlainActionFuture <>();
347- String action = randomBoolean () ? randomFrom ( CROSS_CLUSTER_ACCESS_ACTION_ALLOWLIST ) : "_action" ;
298+ String action = randomAlphaOfLengthBetween ( 10 , 20 ) ;
348299 crossClusterAccessFilter .inbound (action , mock (TransportRequest .class ), channel , listener );
349300
350301 ElasticsearchSecurityException actualException = expectThrows (ElasticsearchSecurityException .class , listener ::actionGet );
0 commit comments