|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.security.authc.saml; |
| 9 | + |
| 10 | +import org.elasticsearch.client.Request; |
| 11 | +import org.elasticsearch.common.Strings; |
| 12 | +import org.elasticsearch.common.settings.SecureString; |
| 13 | +import org.elasticsearch.common.settings.Settings; |
| 14 | +import org.elasticsearch.common.util.concurrent.ThreadContext; |
| 15 | +import org.elasticsearch.core.PathUtils; |
| 16 | +import org.elasticsearch.test.XContentTestUtils; |
| 17 | +import org.elasticsearch.test.cluster.ElasticsearchCluster; |
| 18 | +import org.elasticsearch.test.cluster.local.model.User; |
| 19 | +import org.elasticsearch.test.cluster.util.resource.Resource; |
| 20 | +import org.elasticsearch.test.rest.ESRestTestCase; |
| 21 | +import org.elasticsearch.test.rest.ObjectPath; |
| 22 | +import org.elasticsearch.xcontent.json.JsonXContent; |
| 23 | +import org.junit.BeforeClass; |
| 24 | +import org.junit.ClassRule; |
| 25 | +import org.junit.rules.RuleChain; |
| 26 | +import org.junit.rules.TestRule; |
| 27 | + |
| 28 | +import java.io.FileNotFoundException; |
| 29 | +import java.io.IOException; |
| 30 | +import java.net.URISyntaxException; |
| 31 | +import java.net.URL; |
| 32 | +import java.nio.charset.StandardCharsets; |
| 33 | +import java.nio.file.Path; |
| 34 | +import java.security.cert.CertificateException; |
| 35 | +import java.util.Base64; |
| 36 | +import java.util.HashMap; |
| 37 | +import java.util.List; |
| 38 | +import java.util.Map; |
| 39 | + |
| 40 | +import static org.hamcrest.Matchers.empty; |
| 41 | +import static org.hamcrest.Matchers.equalTo; |
| 42 | + |
| 43 | +public class MicrosoftGraphAuthzPluginIT extends ESRestTestCase { |
| 44 | + public static ElasticsearchCluster cluster = initTestCluster(); |
| 45 | + private static Path caPath; |
| 46 | + |
| 47 | + @ClassRule |
| 48 | + public static TestRule ruleChain = RuleChain.outerRule(cluster); |
| 49 | + |
| 50 | + private static final String IDP_ENTITY_ID = "https://idp.example.org/"; |
| 51 | + |
| 52 | + private static ElasticsearchCluster initTestCluster() { |
| 53 | + return ElasticsearchCluster.local() |
| 54 | + .setting("xpack.security.enabled", "true") |
| 55 | + .setting("xpack.license.self_generated.type", "trial") |
| 56 | + .setting("xpack.security.authc.token.enabled", "true") |
| 57 | + .setting("xpack.security.authc.api_key.enabled", "true") |
| 58 | + .setting("xpack.security.http.ssl.enabled", "true") |
| 59 | + .setting("xpack.security.http.ssl.certificate", "node.crt") |
| 60 | + .setting("xpack.security.http.ssl.key", "node.key") |
| 61 | + .setting("xpack.security.http.ssl.certificate_authorities", "ca.crt") |
| 62 | + .setting("xpack.security.transport.ssl.enabled", "true") |
| 63 | + .setting("xpack.security.transport.ssl.certificate", "node.crt") |
| 64 | + .setting("xpack.security.transport.ssl.key", "node.key") |
| 65 | + .setting("xpack.security.transport.ssl.certificate_authorities", "ca.crt") |
| 66 | + .setting("xpack.security.transport.ssl.verification_mode", "certificate") |
| 67 | + .plugin("microsoft-graph-authz") |
| 68 | + .keystore("bootstrap.password", "x-pack-test-password") |
| 69 | + .user("test_admin", "x-pack-test-password", User.ROOT_USER_ROLE, true) |
| 70 | + .user("rest_test", "rest_password", User.ROOT_USER_ROLE, true) |
| 71 | + .configFile("node.key", Resource.fromClasspath("ssl/node.key")) |
| 72 | + .configFile("node.crt", Resource.fromClasspath("ssl/node.crt")) |
| 73 | + .configFile("ca.crt", Resource.fromClasspath("ssl/ca.crt")) |
| 74 | + .configFile("metadata.xml", Resource.fromString(getIDPMetadata())) |
| 75 | + .setting("xpack.security.authc.realms.saml.saml1.order", "1") |
| 76 | + .setting("xpack.security.authc.realms.saml.saml1.idp.entity_id", IDP_ENTITY_ID) |
| 77 | + .setting("xpack.security.authc.realms.saml.saml1.idp.metadata.path", "metadata.xml") |
| 78 | + .setting("xpack.security.authc.realms.saml.saml1.attributes.principal", "urn:oid:2.5.4.3") |
| 79 | + .setting("xpack.security.authc.realms.saml.saml1.ssl.certificate_authorities", "ca.crt") |
| 80 | + .setting("xpack.security.authc.realms.saml.saml1.sp.entity_id", "https://sp/default.example.org/") |
| 81 | + .setting("xpack.security.authc.realms.saml.saml1.sp.acs", "https://acs/default") |
| 82 | + .setting("xpack.security.authc.realms.saml.saml1.sp.logout", "https://logout/default") |
| 83 | + .setting("xpack.security.authc.realms.saml.saml1.authorization_realms", "microsoft_graph1") |
| 84 | + .setting("xpack.security.authc.realms.microsoft_graph.microsoft_graph1.order", "2") |
| 85 | + .build(); |
| 86 | + } |
| 87 | + |
| 88 | + private static String getIDPMetadata() { |
| 89 | + try { |
| 90 | + var signingCert = PathUtils.get(MicrosoftGraphAuthzPluginIT.class.getResource("/saml/signing.crt").toURI()); |
| 91 | + return new SamlIdpMetadataBuilder().entityId(IDP_ENTITY_ID).idpUrl(IDP_ENTITY_ID).sign(signingCert).asString(); |
| 92 | + } catch (URISyntaxException | CertificateException | IOException exception) { |
| 93 | + fail(exception); |
| 94 | + } |
| 95 | + return null; |
| 96 | + } |
| 97 | + |
| 98 | + @BeforeClass |
| 99 | + public static void loadCertificateAuthority() throws Exception { |
| 100 | + URL resource = MicrosoftGraphAuthzPluginIT.class.getResource("/ssl/ca.crt"); |
| 101 | + if (resource == null) { |
| 102 | + throw new FileNotFoundException("Cannot find classpath resource /ssl/ca.crt"); |
| 103 | + } |
| 104 | + caPath = PathUtils.get(resource.toURI()); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + protected String getTestRestCluster() { |
| 109 | + return cluster.getHttpAddresses(); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + protected String getProtocol() { |
| 114 | + return "https"; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + protected Settings restClientSettings() { |
| 119 | + final String token = basicAuthHeaderValue("rest_test", new SecureString("rest_password".toCharArray())); |
| 120 | + return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", token).put(CERTIFICATE_AUTHORITIES, caPath).build(); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + protected boolean shouldConfigureProjects() { |
| 125 | + return false; |
| 126 | + } |
| 127 | + |
| 128 | + public void testAuthenticationSuccessful() throws Exception { |
| 129 | + final String username = randomAlphaOfLengthBetween(4, 12); |
| 130 | + samlAuthWithMicrosoftGraphAuthz(username, getSamlAssertionJsonBodyString(username)); |
| 131 | + } |
| 132 | + |
| 133 | + private String getSamlAssertionJsonBodyString(String username) throws Exception { |
| 134 | + var message = new SamlResponseBuilder().spEntityId("https://sp/default.example.org/") |
| 135 | + .idpEntityId(IDP_ENTITY_ID) |
| 136 | + .acs(new URL("https://acs/default")) |
| 137 | + .attribute("urn:oid:2.5.4.3", username) |
| 138 | + .sign(getDataPath("/saml/signing.crt"), getDataPath("/saml/signing.key"), new char[0]) |
| 139 | + .asString(); |
| 140 | + |
| 141 | + final Map<String, Object> body = new HashMap<>(); |
| 142 | + body.put("content", Base64.getEncoder().encodeToString(message.getBytes(StandardCharsets.UTF_8))); |
| 143 | + body.put("realm", "saml1"); |
| 144 | + return Strings.toString(JsonXContent.contentBuilder().map(body)); |
| 145 | + } |
| 146 | + |
| 147 | + private void samlAuthWithMicrosoftGraphAuthz(String username, String samlAssertion) throws Exception { |
| 148 | + var req = new Request("POST", "_security/saml/authenticate"); |
| 149 | + req.setJsonEntity(samlAssertion); |
| 150 | + var resp = entityAsMap(client().performRequest(req)); |
| 151 | + List<String> roles = new XContentTestUtils.JsonMapView(entityAsMap(client().performRequest(req))).get("authentication.roles"); |
| 152 | + assertThat(resp.get("username"), equalTo(username)); |
| 153 | + // TODO add check for mapped groups and roles when available |
| 154 | + assertThat(roles, empty()); |
| 155 | + assertThat(ObjectPath.evaluate(resp, "authentication.authentication_realm.name"), equalTo("saml1")); |
| 156 | + } |
| 157 | + |
| 158 | +} |
0 commit comments