99
1010import org .elasticsearch .action .ActionListener ;
1111import org .elasticsearch .action .support .ActionTestUtils ;
12+ import org .elasticsearch .action .support .GroupedActionListener ;
1213import org .elasticsearch .action .support .PlainActionFuture ;
1314import org .elasticsearch .client .Request ;
1415import org .elasticsearch .client .Response ;
4041import java .nio .charset .StandardCharsets ;
4142import java .security .cert .CertificateException ;
4243import java .util .Base64 ;
44+ import java .util .Collection ;
4345import java .util .HashMap ;
4446import java .util .List ;
4547import java .util .Map ;
@@ -55,19 +57,23 @@ public class MicrosoftGraphAuthzPluginIT extends ESRestTestCase {
5557 private static final String USERNAME = "Thor" ;
5658 private static final String EXPECTED_GROUP = "test_group" ;
5759
60+ private static final List <TestUser > TEST_USERS = List .of (
61+ new TestUser (
62+ USERNAME ,
63+ "Thor Odinson" ,
64+ 65+ List .of ("unmapped-group-1" , "unmapped-group-2" , "unmapped-group-3" , EXPECTED_GROUP ),
66+ List .of ("microsoft_graph_user" )
67+ ),
68+ new TestUser (
"User2" ,
"User 2" ,
"[email protected] " ,
List .
of (
EXPECTED_GROUP ),
List .
of (
"microsoft_graph_user" )),
69+ new TestUser (
"User3" ,
"User 3" ,
"[email protected] " ,
List .
of (),
List .
of ())
70+ );
71+
5872 private static final MsGraphHttpFixture graphFixture = new MsGraphHttpFixture (
5973 TENANT_ID ,
6074 CLIENT_ID ,
6175 CLIENT_SECRET ,
62- List .of (
63- new TestUser (
64- USERNAME ,
65- "Thor Odinson" ,
66- 67- new String [] { "unmapped-group-1" , "unmapped-group-2" , "unmapped-group-3" , EXPECTED_GROUP },
68- new String [] { "microsoft_graph_user" }
69- )
70- ),
76+ TEST_USERS ,
7177 3
7278 );
7379
@@ -140,11 +146,6 @@ public void setupRoleMapping() throws IOException {
140146 .startArray ("all" )
141147 .startObject ()
142148 .startObject ("field" )
143- .field ("username" , USERNAME )
144- .endObject ()
145- .endObject ()
146- .startObject ()
147- .startObject ("field" )
148149 .field ("realm.name" , "microsoft_graph1" )
149150 .endObject ()
150151 .endObject ()
@@ -183,15 +184,33 @@ protected boolean shouldConfigureProjects() {
183184 }
184185
185186 public void testAuthenticationSuccessful () throws Exception {
186- final var listener = new PlainActionFuture <Response >();
187+ final var listener = new PlainActionFuture <Map < String , Object > >();
187188 samlAuthWithMicrosoftGraphAuthz (USERNAME , getSamlAssertionJsonBodyString (USERNAME ), listener );
188- final var resp = entityAsMap ( listener .get () );
189+ final var resp = listener .get ();
189190 List <String > roles = new XContentTestUtils .JsonMapView (resp ).get ("authentication.roles" );
190191 assertThat (resp .get ("username" ), equalTo (USERNAME ));
191192 assertThat (roles , contains ("microsoft_graph_user" ));
192193 assertThat (ObjectPath .evaluate (resp , "authentication.authentication_realm.name" ), equalTo ("saml1" ));
193194 }
194195
196+ public void testConcurrentAuthentication () throws Exception {
197+ final var resultsListener = new PlainActionFuture <Collection <Map <String , Object >>>();
198+ final var groupedListener = new GroupedActionListener <>(TEST_USERS .size (), resultsListener );
199+ for (var user : TEST_USERS ) {
200+ samlAuthWithMicrosoftGraphAuthz (user .username (), getSamlAssertionJsonBodyString (user .username ()), groupedListener );
201+ }
202+ final var responses = resultsListener .get ();
203+
204+ assertThat (responses .size (), equalTo (TEST_USERS .size ()));
205+ for (var user : TEST_USERS ) {
206+ var response = responses .stream ().filter (r -> r .get ("username" ).equals (user .username ())).findFirst ();
207+ assertTrue (response .isPresent ());
208+ final List <String > roles = new XContentTestUtils .JsonMapView (response .get ()).get ("authentication.roles" );
209+ assertThat (roles , equalTo (user .roles ()));
210+ assertThat (ObjectPath .evaluate (response .get (), "authentication.authentication_realm.name" ), equalTo ("saml1" ));
211+ }
212+ }
213+
195214 private String getSamlAssertionJsonBodyString (String username ) throws Exception {
196215 var message = new SamlResponseBuilder ().spEntityId ("http://sp/default.example.org/" )
197216 .idpEntityId (IDP_ENTITY_ID )
@@ -206,10 +225,10 @@ private String getSamlAssertionJsonBodyString(String username) throws Exception
206225 return Strings .toString (JsonXContent .contentBuilder ().map (body ));
207226 }
208227
209- private void samlAuthWithMicrosoftGraphAuthz (String username , String samlAssertion , ActionListener <Response > listener ) {
228+ private void samlAuthWithMicrosoftGraphAuthz (String username , String samlAssertion , ActionListener <Map < String , Object > > listener ) {
210229 var req = new Request ("POST" , "_security/saml/authenticate" );
211230 req .setJsonEntity (samlAssertion );
212- client ().performRequestAsync (req , ActionTestUtils .wrapAsRestResponseListener (listener ));
231+ client ().performRequestAsync (req , ActionTestUtils .wrapAsRestResponseListener (listener . map ( ESRestTestCase :: entityAsMap ) ));
213232 }
214233
215234}
0 commit comments