|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + * |
| 8 | + * Modifications Copyright OpenSearch Contributors. See |
| 9 | + * GitHub history for details. |
| 10 | + */ |
| 11 | +package org.opensearch.security.user; |
| 12 | + |
| 13 | +import java.util.Arrays; |
| 14 | +import java.util.Map; |
| 15 | + |
| 16 | +import com.google.common.collect.ImmutableMap; |
| 17 | +import com.google.common.collect.ImmutableSet; |
| 18 | +import org.junit.Test; |
| 19 | + |
| 20 | +import org.opensearch.security.support.Base64Helper; |
| 21 | + |
| 22 | +import static org.junit.Assert.assertEquals; |
| 23 | +import static org.junit.Assert.assertSame; |
| 24 | + |
| 25 | +public class UserTest { |
| 26 | + @Test |
| 27 | + public void serialization() { |
| 28 | + User user = new User("serialization_test_user").withRoles(Arrays.asList("br1", "br2", "br3")) |
| 29 | + .withSecurityRoles(Arrays.asList("sr1", "sr2")) |
| 30 | + .withAttributes(ImmutableMap.of("a", "v_a", "b", "v_b")); |
| 31 | + |
| 32 | + String serialized = Base64Helper.serializeObject(user); |
| 33 | + User user2 = User.fromSerializedBase64(serialized); |
| 34 | + assertEquals(user, user2); |
| 35 | + |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void deserializationFrom2_19() { |
| 40 | + // The following base64 string was produced by the following code on OpenSearch 2.19 |
| 41 | + // User user = new User("serialization_test_user"); |
| 42 | + // user.addRoles(Arrays.asList("br1", "br2", "br3")); |
| 43 | + // user.addSecurityRoles(Arrays.asList("sr1", "sr2")); |
| 44 | + // user.addAttributes(ImmutableMap.of("a", "v_a", "b", "v_b")); |
| 45 | + // println(Base64JDKHelper.serializeObject(user)); |
| 46 | + String serialized = |
| 47 | + "rO0ABXNyACFvcmcub3BlbnNlYXJjaC5zZWN1cml0eS51c2VyLlVzZXKzqL2T65dH3AIABloACmlzSW5qZWN0ZWRMAAphdHRyaWJ1dGVzdAAPTGphdmEvdXRpbC9NYXA7TAAEbmFtZXQAEkxqYXZhL2xhbmcvU3RyaW5nO0wAD3JlcXVlc3RlZFRlbmFudHEAfgACTAAFcm9sZXN0AA9MamF2YS91dGlsL1NldDtMAA1zZWN1cml0eVJvbGVzcQB+AAN4cABzcgAlamF2YS51dGlsLkNvbGxlY3Rpb25zJFN5bmNocm9uaXplZE1hcBtz+QlLSzl7AwACTAABbXEAfgABTAAFbXV0ZXh0ABJMamF2YS9sYW5nL09iamVjdDt4cHNyABFqYXZhLnV0aWwuSGFzaE1hcAUH2sHDFmDRAwACRgAKbG9hZEZhY3RvckkACXRocmVzaG9sZHhwP0AAAAAAAAN3CAAAAAQAAAACdAABYXQAA3ZfYXQAAWJ0AAN2X2J4cQB+AAd4dAAXc2VyaWFsaXphdGlvbl90ZXN0X3VzZXJwc3IAJWphdmEudXRpbC5Db2xsZWN0aW9ucyRTeW5jaHJvbml6ZWRTZXQGw8J5Au7fPAIAAHhyACxqYXZhLnV0aWwuQ29sbGVjdGlvbnMkU3luY2hyb25pemVkQ29sbGVjdGlvbiph+E0JnJm1AwACTAABY3QAFkxqYXZhL3V0aWwvQ29sbGVjdGlvbjtMAAVtdXRleHEAfgAGeHBzcgARamF2YS51dGlsLkhhc2hTZXS6RIWVlri3NAMAAHhwdwwAAAAQP0AAAAAAAAN0AANicjF0AANicjN0AANicjJ4cQB+ABJ4c3EAfgAPc3EAfgATdwwAAAAQP0AAAAAAAAJ0AANzcjJ0AANzcjF4cQB+ABh4"; |
| 48 | + |
| 49 | + User user = User.fromSerializedBase64(serialized); |
| 50 | + assertEquals( |
| 51 | + new User("serialization_test_user").withRoles(Arrays.asList("br1", "br2", "br3")) |
| 52 | + .withSecurityRoles(Arrays.asList("sr1", "sr2")) |
| 53 | + .withAttributes(ImmutableMap.of("a", "v_a", "b", "v_b")), |
| 54 | + user |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void deserializationLdapUserFrom2_19() { |
| 60 | + // The following base64 string was produced by the following code on OpenSearch 2.19 |
| 61 | + // LdapUser user = new LdapUser("serialization_test_user", |
| 62 | + // "original_user_name", |
| 63 | + // new LdapEntry("cn=test,ou=people,o=TEST", new LdapAttribute("test_ldap_attr", "test_ldap_attr_value")), |
| 64 | + // new AuthCredentials("test_user", "secret".getBytes(StandardCharsets.UTF_8)), |
| 65 | + // 100, |
| 66 | + // WildcardMatcher.ANY); |
| 67 | + // user.addRoles(Arrays.asList("br1", "br2", "br3")); |
| 68 | + // user.addSecurityRoles(Arrays.asList("sr1", "sr2")); |
| 69 | + // user.addAttributes(ImmutableMap.of("a", "v_a", "b", "v_b")); |
| 70 | + // println(Base64JDKHelper.serializeObject(user)); |
| 71 | + String serialized = |
| 72 | + "rO0ABXNyACJjb20uYW1hem9uLmRsaWMuYXV0aC5sZGFwLkxkYXBVc2VyAAAAAAAAAAECAAFMABBvcmlnaW5hbFVzZXJuYW1ldAASTGphdmEvbGFuZy9TdHJpbmc7eHIAIW9yZy5vcGVuc2VhcmNoLnNlY3VyaXR5LnVzZXIuVXNlcrOovZPrl0fcAgAGWgAKaXNJbmplY3RlZEwACmF0dHJpYnV0ZXN0AA9MamF2YS91dGlsL01hcDtMAARuYW1lcQB+AAFMAA9yZXF1ZXN0ZWRUZW5hbnRxAH4AAUwABXJvbGVzdAAPTGphdmEvdXRpbC9TZXQ7TAANc2VjdXJpdHlSb2xlc3EAfgAEeHAAc3IAJWphdmEudXRpbC5Db2xsZWN0aW9ucyRTeW5jaHJvbml6ZWRNYXAbc/kJS0s5ewMAAkwAAW1xAH4AA0wABW11dGV4dAASTGphdmEvbGFuZy9PYmplY3Q7eHBzcgARamF2YS51dGlsLkhhc2hNYXAFB9rBwxZg0QMAAkYACmxvYWRGYWN0b3JJAAl0aHJlc2hvbGR4cD9AAAAAAAAGdwgAAAAIAAAABXQAB2xkYXAuZG50ABhjbj10ZXN0LG91PXBlb3BsZSxvPVRFU1R0AAFhdAADdl9hdAAYYXR0ci5sZGFwLnRlc3RfbGRhcF9hdHRydAAUdGVzdF9sZGFwX2F0dHJfdmFsdWV0AAFidAADdl9idAAWbGRhcC5vcmlnaW5hbC51c2VybmFtZXQAEm9yaWdpbmFsX3VzZXJfbmFtZXhxAH4ACHh0ABdzZXJpYWxpemF0aW9uX3Rlc3RfdXNlcnBzcgAlamF2YS51dGlsLkNvbGxlY3Rpb25zJFN5bmNocm9uaXplZFNldAbDwnkC7t88AgAAeHIALGphdmEudXRpbC5Db2xsZWN0aW9ucyRTeW5jaHJvbml6ZWRDb2xsZWN0aW9uKmH4TQmcmbUDAAJMAAFjdAAWTGphdmEvdXRpbC9Db2xsZWN0aW9uO0wABW11dGV4cQB+AAd4cHNyABFqYXZhLnV0aWwuSGFzaFNldLpEhZWWuLc0AwAAeHB3DAAAABA/QAAAAAAAA3QAA2JyMXQAA2JyM3QAA2JyMnhxAH4AGXhzcQB+ABZzcQB+ABp3DAAAABA/QAAAAAAAAnQAA3NyMnQAA3NyMXhxAH4AH3hxAH4AFA=="; |
| 73 | + |
| 74 | + User user = User.fromSerializedBase64(serialized); |
| 75 | + assertEquals( |
| 76 | + new User("serialization_test_user").withRoles(Arrays.asList("br1", "br2", "br3")) |
| 77 | + .withSecurityRoles(Arrays.asList("sr1", "sr2")) |
| 78 | + .withAttributes(ImmutableMap.of("a", "v_a", "b", "v_b")), |
| 79 | + user |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void withRoles() { |
| 85 | + User original = new User("test_user").withRoles("a"); |
| 86 | + User modified = original.withRoles("b"); |
| 87 | + |
| 88 | + assertEquals(ImmutableSet.of("a"), original.getRoles()); |
| 89 | + assertEquals(ImmutableSet.of("a", "b"), modified.getRoles()); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void withRoles_unmodified() { |
| 94 | + User original = new User("test_user").withRoles("a"); |
| 95 | + User unmodified = original.withRoles(ImmutableSet.of()); |
| 96 | + |
| 97 | + assertSame(original, unmodified); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void withAttributes() { |
| 102 | + User original = new User("test_user").withAttributes(Map.of("a", "1")); |
| 103 | + User modified = original.withAttributes(Map.of("b", "2")); |
| 104 | + |
| 105 | + assertEquals(ImmutableMap.of("a", "1"), original.getCustomAttributesMap()); |
| 106 | + assertEquals(ImmutableMap.of("a", "1", "b", "2"), modified.getCustomAttributesMap()); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void withAttributes_unmodified() { |
| 111 | + User original = new User("test_user").withAttributes(Map.of("a", "1")); |
| 112 | + User unmodified = original.withAttributes(Map.of()); |
| 113 | + |
| 114 | + assertSame(original, unmodified); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void withRequestedTenant() { |
| 119 | + User original = new User("test_user").withRequestedTenant("a"); |
| 120 | + User modified = original.withRequestedTenant("b"); |
| 121 | + |
| 122 | + assertEquals("a", original.getRequestedTenant()); |
| 123 | + assertEquals("b", modified.getRequestedTenant()); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void withRequestedTenant_unmodified() { |
| 128 | + User original = new User("test_user").withRequestedTenant("a"); |
| 129 | + User unmodified = original.withRequestedTenant("a"); |
| 130 | + |
| 131 | + assertSame(original, unmodified); |
| 132 | + } |
| 133 | + |
| 134 | + @Test(expected = IllegalArgumentException.class) |
| 135 | + public void illegalName() { |
| 136 | + new User(""); |
| 137 | + } |
| 138 | +} |
0 commit comments