Skip to content

Commit be296be

Browse files
add test for concurrent login
1 parent 668c441 commit be296be

File tree

5 files changed

+47
-23
lines changed

5 files changed

+47
-23
lines changed

plugins/microsoft-graph-authz/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,5 @@ tasks.named("javadoc").configure { enabled = false }
9595

9696
tasks.named("dependencyLicenses").configure {
9797
mapping from: "microsoft-graph-core", to: "microsoft-graph"
98+
mapping from: "azure-identity", to: "azure"
9899
}

plugins/microsoft-graph-authz/licenses/azure-NOTICE.txt

Whitespace-only changes.

x-pack/plugin/security/qa/microsoft-graph-authz-tests/src/javaRestTest/java/org/elasticsearch/xpack/security/authz/microsoft/MicrosoftGraphAuthzPluginIT.java

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import org.elasticsearch.action.ActionListener;
1111
import org.elasticsearch.action.support.ActionTestUtils;
12+
import org.elasticsearch.action.support.GroupedActionListener;
1213
import org.elasticsearch.action.support.PlainActionFuture;
1314
import org.elasticsearch.client.Request;
1415
import org.elasticsearch.client.Response;
@@ -40,6 +41,7 @@
4041
import java.nio.charset.StandardCharsets;
4142
import java.security.cert.CertificateException;
4243
import java.util.Base64;
44+
import java.util.Collection;
4345
import java.util.HashMap;
4446
import java.util.List;
4547
import 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
}

x-pack/plugin/security/qa/microsoft-graph-authz-tests/src/javaRestTest/java/org/elasticsearch/xpack/security/authz/microsoft/MsGraphHttpFixture.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,17 @@ private void registerGetUserMembershipHandler(TestUser user) {
234234
return;
235235
}
236236

237-
var nextLink = getBaseUrl() + exchange.getRequestURI().toString() + "&$skiptoken=" + skipToken;
237+
String nextLink = null;
238238
Object[] groups;
239239

240240
// return multiple pages of results, to ensure client correctly supports paging
241241
if (exchange.getRequestURI().getQuery().contains("$skiptoken")) {
242-
groups = Arrays.stream(user.groups()).skip(groupsPageSize).map(id -> Map.of("id", id)).toArray();
243-
nextLink = null;
242+
groups = user.groups().stream().skip(groupsPageSize).map(id -> Map.of("id", id)).toArray();
244243
} else {
245-
groups = Arrays.stream(user.groups()).limit(groupsPageSize).map(id -> Map.of("id", id)).toArray();
244+
groups = user.groups().stream().limit(groupsPageSize).map(id -> Map.of("id", id)).toArray();
245+
if (user.groups().size() > groupsPageSize) {
246+
nextLink = getBaseUrl() + exchange.getRequestURI().toString() + "&$skiptoken=" + skipToken;
247+
}
246248
}
247249

248250
final var groupMembership = XContentBuilder.builder(XContentType.JSON.xContent());

x-pack/plugin/security/qa/microsoft-graph-authz-tests/src/javaRestTest/java/org/elasticsearch/xpack/security/authz/microsoft/TestUser.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@
77

88
package org.elasticsearch.xpack.security.authz.microsoft;
99

10-
public record TestUser(String username, String displayName, String email, String[] groups, String[] roles) {}
10+
import java.util.List;
11+
12+
public record TestUser(String username, String displayName, String email, List<String> groups, List<String> roles) {}

0 commit comments

Comments
 (0)