|
| 1 | +/* |
| 2 | + * Copyright (c) 2008 Sonatype, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * This program is licensed to you under the Apache License Version 2.0, |
| 5 | + * and you may not use this file except in compliance with the Apache License Version 2.0. |
| 6 | + * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0. |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, |
| 9 | + * software distributed under the Apache License Version 2.0 is distributed on an |
| 10 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under. |
| 12 | + */ |
| 13 | + |
| 14 | +package org.codehaus.plexus.components.secdispatcher.internal; |
| 15 | + |
| 16 | +import java.io.FileWriter; |
| 17 | +import java.nio.charset.StandardCharsets; |
| 18 | +import java.util.Base64; |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import org.codehaus.plexus.components.cipher.internal.DefaultPlexusCipher; |
| 22 | +import org.codehaus.plexus.components.secdispatcher.SecDispatcher; |
| 23 | +import org.codehaus.plexus.components.secdispatcher.internal.dispatcher.StaticDispatcher; |
| 24 | +import org.codehaus.plexus.components.secdispatcher.internal.sources.EnvMasterPasswordSource; |
| 25 | +import org.codehaus.plexus.components.secdispatcher.internal.sources.GpgAgentMasterPasswordSource; |
| 26 | +import org.codehaus.plexus.components.secdispatcher.internal.sources.StaticMasterPasswordSource; |
| 27 | +import org.codehaus.plexus.components.secdispatcher.internal.sources.SystemPropertyMasterPasswordSource; |
| 28 | +import org.codehaus.plexus.components.secdispatcher.model.SettingsSecurity; |
| 29 | +import org.codehaus.plexus.components.secdispatcher.model.io.stax.SecurityConfigurationStaxWriter; |
| 30 | +import org.junit.jupiter.api.BeforeEach; |
| 31 | +import org.junit.jupiter.api.Disabled; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | + |
| 34 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 35 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 36 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 37 | + |
| 38 | +public class DefaultSecDispatcherTest { |
| 39 | + String masterPassword = "masterPw"; |
| 40 | + String password = "somePassword"; |
| 41 | + String passwordEncrypted = "{TT2NQZ4iAdoHqsSfYUab3s6X2IHl5qaf4vx/F8DvtSI=}"; |
| 42 | + |
| 43 | + private void saveSec(String masterSource) throws Exception { |
| 44 | + SettingsSecurity sec = new SettingsSecurity(); |
| 45 | + sec.setMasterSource(masterSource); |
| 46 | + |
| 47 | + try (FileWriter fw = new FileWriter("./target/sec.xml")) { |
| 48 | + new SecurityConfigurationStaxWriter().write(fw, sec); |
| 49 | + fw.flush(); |
| 50 | + } |
| 51 | + System.setProperty(DefaultSecDispatcher.SYSTEM_PROPERTY_CONFIGURATION_LOCATION, "./target/sec.xml"); |
| 52 | + } |
| 53 | + |
| 54 | + @BeforeEach |
| 55 | + public void prepare() throws Exception { |
| 56 | + saveSec("magic:might"); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + void testEncrypt() throws Exception { |
| 61 | + DefaultSecDispatcher sd = new DefaultSecDispatcher( |
| 62 | + new DefaultPlexusCipher(), |
| 63 | + Map.of("static", new StaticMasterPasswordSource(masterPassword)), |
| 64 | + Map.of(), |
| 65 | + DefaultSecDispatcher.DEFAULT_CONFIGURATION); |
| 66 | + String enc = sd.encrypt(password, null); |
| 67 | + assertNotNull(enc); |
| 68 | + String password1 = sd.decrypt(enc); |
| 69 | + assertEquals(password, password1); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void testDecrypt() throws Exception { |
| 74 | + DefaultSecDispatcher sd = new DefaultSecDispatcher( |
| 75 | + new DefaultPlexusCipher(), |
| 76 | + Map.of("static", new StaticMasterPasswordSource(masterPassword)), |
| 77 | + Map.of(), |
| 78 | + DefaultSecDispatcher.DEFAULT_CONFIGURATION); |
| 79 | + String pass = sd.decrypt(passwordEncrypted); |
| 80 | + assertNotNull(pass); |
| 81 | + assertEquals(password, pass); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + void testDecryptSystemProperty() throws Exception { |
| 86 | + System.setProperty("foobar", masterPassword); |
| 87 | + saveSec("prop:foobar"); |
| 88 | + DefaultSecDispatcher sd = new DefaultSecDispatcher( |
| 89 | + new DefaultPlexusCipher(), |
| 90 | + Map.of( |
| 91 | + "prop", |
| 92 | + new SystemPropertyMasterPasswordSource(), |
| 93 | + "env", |
| 94 | + new EnvMasterPasswordSource(), |
| 95 | + "gpg", |
| 96 | + new GpgAgentMasterPasswordSource()), |
| 97 | + Map.of(), |
| 98 | + DefaultSecDispatcher.DEFAULT_CONFIGURATION); |
| 99 | + String pass = sd.decrypt(passwordEncrypted); |
| 100 | + assertNotNull(pass); |
| 101 | + assertEquals(password, pass); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void testDecryptEnv() throws Exception { |
| 106 | + saveSec("env:MASTER_PASSWORD"); |
| 107 | + DefaultSecDispatcher sd = new DefaultSecDispatcher( |
| 108 | + new DefaultPlexusCipher(), |
| 109 | + Map.of( |
| 110 | + "prop", |
| 111 | + new SystemPropertyMasterPasswordSource(), |
| 112 | + "env", |
| 113 | + new EnvMasterPasswordSource(), |
| 114 | + "gpg", |
| 115 | + new GpgAgentMasterPasswordSource()), |
| 116 | + Map.of(), |
| 117 | + DefaultSecDispatcher.DEFAULT_CONFIGURATION); |
| 118 | + String pass = sd.decrypt(passwordEncrypted); |
| 119 | + assertNotNull(pass); |
| 120 | + assertEquals(password, pass); |
| 121 | + } |
| 122 | + |
| 123 | + @Disabled("triggers GPG agent: remove this and type in 'masterPw'") |
| 124 | + @Test |
| 125 | + void testDecryptGpg() throws Exception { |
| 126 | + saveSec("gpg-agent:/run/user/1000/gnupg/S.gpg-agent"); |
| 127 | + DefaultSecDispatcher sd = new DefaultSecDispatcher( |
| 128 | + new DefaultPlexusCipher(), |
| 129 | + Map.of( |
| 130 | + "prop", |
| 131 | + new SystemPropertyMasterPasswordSource(), |
| 132 | + "env", |
| 133 | + new EnvMasterPasswordSource(), |
| 134 | + "gpg", |
| 135 | + new GpgAgentMasterPasswordSource()), |
| 136 | + Map.of(), |
| 137 | + DefaultSecDispatcher.DEFAULT_CONFIGURATION); |
| 138 | + String pass = sd.decrypt(passwordEncrypted); |
| 139 | + assertNotNull(pass); |
| 140 | + assertEquals(password, pass); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + void testEncryptWithDispatcher() throws Exception { |
| 145 | + DefaultSecDispatcher sd = new DefaultSecDispatcher( |
| 146 | + new DefaultPlexusCipher(), |
| 147 | + Map.of("static", new StaticMasterPasswordSource(masterPassword)), |
| 148 | + Map.of("magic", new StaticDispatcher("decrypted", "encrypted")), |
| 149 | + DefaultSecDispatcher.DEFAULT_CONFIGURATION); |
| 150 | + |
| 151 | + String enc = sd.encrypt("whatever", Map.of(SecDispatcher.DISPATCHER_NAME_ATTR, "magic", "a", "b")); |
| 152 | + assertNotNull(enc); |
| 153 | + assertTrue(enc.contains("encrypted")); |
| 154 | + assertTrue(enc.contains(SecDispatcher.DISPATCHER_NAME_ATTR + "=magic")); |
| 155 | + String password1 = sd.decrypt(enc); |
| 156 | + assertEquals("decrypted", password1); |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + void testDecryptWithDispatcher() throws Exception { |
| 161 | + DefaultSecDispatcher sd = new DefaultSecDispatcher( |
| 162 | + new DefaultPlexusCipher(), |
| 163 | + Map.of("static", new StaticMasterPasswordSource(masterPassword)), |
| 164 | + Map.of("magic", new StaticDispatcher("decrypted", "encrypted")), |
| 165 | + DefaultSecDispatcher.DEFAULT_CONFIGURATION); |
| 166 | + |
| 167 | + String pass = sd.decrypt("{" + Base64.getEncoder().encodeToString("whatever".getBytes(StandardCharsets.UTF_8)) |
| 168 | + + "[a=b," + SecDispatcher.DISPATCHER_NAME_ATTR + "=magic]}"); |
| 169 | + assertNotNull(pass); |
| 170 | + assertEquals("decrypted", pass); |
| 171 | + } |
| 172 | +} |
0 commit comments