|
| 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.test.ESTestCase; |
| 11 | +import org.elasticsearch.xpack.core.security.authc.RealmConfig; |
| 12 | +import org.mockito.Mockito; |
| 13 | +import org.opensaml.saml.saml2.core.Attribute; |
| 14 | + |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +import static org.elasticsearch.xpack.core.security.authc.saml.SamlRealmSettings.PRIVATE_ATTRIBUTES; |
| 18 | +import static org.hamcrest.Matchers.is; |
| 19 | +import static org.mockito.Mockito.doReturn; |
| 20 | +import static org.mockito.Mockito.mock; |
| 21 | +import static org.mockito.Mockito.when; |
| 22 | + |
| 23 | +public class SamlSecureAttributePredicateTests extends ESTestCase { |
| 24 | + |
| 25 | + public void testPredicateWithSettingConfigured() { |
| 26 | + |
| 27 | + final List<String> privateAttributes = List.of("private", "http://elastic.co/confidential"); |
| 28 | + final RealmConfig config = realmConfig(privateAttributes); |
| 29 | + final SamlPrivateAttributePredicate predicate = SamlPrivateAttributePredicate.create(config); |
| 30 | + |
| 31 | + final String privateAttribute = randomFrom(privateAttributes); |
| 32 | + final String nonPrivateAttribute = randomFrom(new String[] { null, " ", randomAlphaOfLengthBetween(0, 3) }); |
| 33 | + |
| 34 | + assertThat(predicate.test(attribute("private", "http://elastic.co/confidential")), is(true)); |
| 35 | + assertThat(predicate.test(attribute(privateAttribute, nonPrivateAttribute)), is(true)); |
| 36 | + assertThat(predicate.test(attribute(nonPrivateAttribute, privateAttribute)), is(true)); |
| 37 | + |
| 38 | + assertThat(predicate.test(attribute(privateAttribute, null)), is(true)); |
| 39 | + assertThat(predicate.test(attribute(null, privateAttribute)), is(true)); |
| 40 | + |
| 41 | + assertThat(predicate.test(attribute(nonPrivateAttribute, null)), is(false)); |
| 42 | + assertThat(predicate.test(attribute(null, nonPrivateAttribute)), is(false)); |
| 43 | + assertThat(predicate.test(attribute(null, null)), is(false)); |
| 44 | + |
| 45 | + assertThat(predicate.test(attribute("something", "else")), is(false)); |
| 46 | + assertThat(predicate.test(attribute("", "")), is(false)); |
| 47 | + |
| 48 | + } |
| 49 | + |
| 50 | + public void testPredicateWhenSettingIsNotConfigured() { |
| 51 | + |
| 52 | + List<String> privateAttributes = randomBoolean() ? List.of() : null; |
| 53 | + RealmConfig config = realmConfig(privateAttributes); |
| 54 | + SamlPrivateAttributePredicate predicate = SamlPrivateAttributePredicate.create(config); |
| 55 | + |
| 56 | + String name = randomFrom(randomAlphaOfLengthBetween(0, 5), null); |
| 57 | + String friendlyName = randomFrom(randomAlphaOfLengthBetween(0, 5), null); |
| 58 | + |
| 59 | + assertThat(predicate.test(attribute(name, friendlyName)), is(false)); |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + private static Attribute attribute(String name, String friendlyName) { |
| 64 | + Attribute attribute = mock(Attribute.class); |
| 65 | + when(attribute.getName()).thenReturn(name); |
| 66 | + when(attribute.getFriendlyName()).thenReturn(friendlyName); |
| 67 | + return attribute; |
| 68 | + } |
| 69 | + |
| 70 | + private static RealmConfig realmConfig(List<String> privateAttributeNames) { |
| 71 | + RealmConfig config = Mockito.mock(RealmConfig.class); |
| 72 | + when(config.hasSetting(PRIVATE_ATTRIBUTES)).thenReturn(privateAttributeNames != null); |
| 73 | + doReturn(privateAttributeNames).when(config).getSetting(PRIVATE_ATTRIBUTES); |
| 74 | + return config; |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments