Skip to content

Commit bd23248

Browse files
committed
Some more updates
1 parent 1a25a48 commit bd23248

File tree

18 files changed

+666
-175
lines changed

18 files changed

+666
-175
lines changed

.github/workflows/maven.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ jobs:
2323
build:
2424
name: Build it
2525
uses: codehaus-plexus/.github/.github/workflows/maven.yml@master
26+
with:
27+
jdk-matrix: '[ "23", "21", "17" ]'
2628

2729
deploy:
2830
name: Deploy

pom.xml

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,17 @@
3333
</distributionManagement>
3434

3535
<properties>
36+
<javaVersion>17</javaVersion>
3637
<project.build.outputTimestamp>2023-05-22T22:22:22Z</project.build.outputTimestamp>
3738
</properties>
3839

3940
<dependencies>
41+
<dependency>
42+
<groupId>org.codehaus.plexus</groupId>
43+
<artifactId>plexus-cipher</artifactId>
44+
<version>3.0.0-SNAPSHOT</version>
45+
</dependency>
46+
4047
<dependency>
4148
<groupId>org.codehaus.plexus</groupId>
4249
<artifactId>plexus-xml</artifactId>
@@ -47,17 +54,20 @@
4754
<artifactId>plexus-utils</artifactId>
4855
<version>4.0.2</version>
4956
</dependency>
50-
<dependency>
51-
<groupId>org.codehaus.plexus</groupId>
52-
<artifactId>plexus-cipher</artifactId>
53-
<version>3.0.0-SNAPSHOT</version>
54-
</dependency>
5557

5658
<dependency>
5759
<groupId>javax.inject</groupId>
5860
<artifactId>javax.inject</artifactId>
5961
<version>1</version>
62+
<scope>provided</scope>
6063
</dependency>
64+
<dependency>
65+
<groupId>org.eclipse.sisu</groupId>
66+
<artifactId>org.eclipse.sisu.inject</artifactId>
67+
<version>${sisuMavenPluginVersion}</version>
68+
<scope>provided</scope>
69+
</dependency>
70+
6171
<dependency>
6272
<groupId>org.junit.jupiter</groupId>
6373
<artifactId>junit-jupiter</artifactId>

src/main/java/org/sonatype/plexus/components/sec/dispatcher/SecDispatcher.java renamed to src/main/java/org/codehaus/plexus/components/secdispatcher/SecDispatcher.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,14 @@
1111
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
1212
*/
1313

14-
package org.sonatype.plexus.components.sec.dispatcher;
14+
package org.codehaus.plexus.components.secdispatcher;
1515

1616
/**
1717
* This component decrypts a string, passed to it
1818
*
1919
* @author Oleg Gusakov
2020
*/
2121
public interface SecDispatcher {
22-
String[] SYSTEM_PROPERTY_MASTER_PASSWORD = new String[] {"settings.master.password", "settings-master-password"};
23-
24-
String[] SYSTEM_PROPERTY_SERVER_PASSWORD = new String[] {"settings.server.password", "settings-server-password"};
25-
2622
/**
2723
* decrypt given encrypted string
2824
*
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
1212
*/
1313

14-
package org.sonatype.plexus.components.sec.dispatcher;
14+
package org.codehaus.plexus.components.secdispatcher;
1515

1616
public class SecDispatcherException extends RuntimeException {
1717
public SecDispatcherException(String message) {
1818
super(message);
1919
}
20+
2021
public SecDispatcherException(String message, Throwable cause) {
2122
super(message, cause);
2223
}

src/main/java/org/sonatype/plexus/components/sec/dispatcher/DefaultSecDispatcher.java renamed to src/main/java/org/codehaus/plexus/components/secdispatcher/internal/DefaultSecDispatcher.java

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,69 +11,59 @@
1111
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
1212
*/
1313

14-
package org.sonatype.plexus.components.sec.dispatcher;
14+
package org.codehaus.plexus.components.secdispatcher.internal;
1515

1616
import javax.inject.Inject;
1717
import javax.inject.Named;
1818
import javax.inject.Singleton;
1919

2020
import java.io.BufferedReader;
2121
import java.io.InputStreamReader;
22+
import java.net.URI;
23+
import java.net.URISyntaxException;
24+
import java.util.Collections;
2225
import java.util.HashMap;
2326
import java.util.Map;
2427
import java.util.StringTokenizer;
2528

26-
import org.sonatype.plexus.components.cipher.DefaultPlexusCipher;
27-
import org.sonatype.plexus.components.cipher.PlexusCipher;
28-
import org.sonatype.plexus.components.cipher.PlexusCipherException;
29-
import org.sonatype.plexus.components.sec.dispatcher.model.SettingsSecurity;
29+
import org.codehaus.plexus.components.cipher.PlexusCipher;
30+
import org.codehaus.plexus.components.cipher.PlexusCipherException;
31+
import org.codehaus.plexus.components.cipher.internal.DefaultPlexusCipher;
32+
import org.codehaus.plexus.components.secdispatcher.SecDispatcher;
33+
import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
34+
import org.codehaus.plexus.components.secdispatcher.model.SettingsSecurity;
35+
36+
import static java.util.Objects.requireNonNull;
3037

3138
/**
3239
* @author Oleg Gusakov
3340
*/
3441
@Singleton
3542
@Named
3643
public class DefaultSecDispatcher implements SecDispatcher {
37-
private static final String DEFAULT_CONFIGURATION = "~/.settings-security.xml";
44+
public static final String DEFAULT_CONFIGURATION = "~/.m2/settings-security.xml";
3845

3946
public static final String SYSTEM_PROPERTY_SEC_LOCATION = "settings.security";
4047

4148
public static final String TYPE_ATTR = "type";
42-
4349
public static final char ATTR_START = '[';
44-
4550
public static final char ATTR_STOP = ']';
4651

47-
/**
48-
* DefaultHandler
49-
*/
50-
protected final PlexusCipher _cipher;
51-
52-
/**
53-
* All available dispatchers
54-
*/
55-
protected final Map<String, PasswordDecryptor> _decryptors;
56-
57-
/**
58-
* Configuration file
59-
*/
60-
protected String _configurationFile;
52+
protected final PlexusCipher cipher;
53+
protected final Map<String, MasterPasswordSource> masterPasswordSources;
54+
protected final Map<String, PasswordDecryptor> decryptors;
55+
protected String configurationFile;
6156

6257
@Inject
6358
public DefaultSecDispatcher(
64-
final PlexusCipher _cipher,
65-
final Map<String, PasswordDecryptor> _decryptors,
66-
@Named("${_configurationFile:-" + DEFAULT_CONFIGURATION + "}") final String _configurationFile) {
67-
this._cipher = _cipher;
68-
this._decryptors = _decryptors;
69-
this._configurationFile = _configurationFile;
70-
}
71-
72-
/**
73-
* Ctor to be used in tests and other simplified cases (no decryptors and config).
74-
*/
75-
public DefaultSecDispatcher(final PlexusCipher _cipher) {
76-
this(_cipher, new HashMap<>(), DEFAULT_CONFIGURATION);
59+
PlexusCipher cipher,
60+
Map<String, MasterPasswordSource> masterPasswordSources,
61+
Map<String, PasswordDecryptor> decryptors,
62+
@Named("${configurationFile:-" + DEFAULT_CONFIGURATION + "}") final String configurationFile) {
63+
this.cipher = cipher;
64+
this.masterPasswordSources = masterPasswordSources;
65+
this.decryptors = decryptors;
66+
this.configurationFile = configurationFile;
7767
}
7868

7969
// ---------------------------------------------------------------
@@ -85,7 +75,7 @@ public String decrypt(String str) throws SecDispatcherException {
8575
String bare;
8676

8777
try {
88-
bare = _cipher.unDecorate(str);
78+
bare = cipher.unDecorate(str);
8979

9080
Map<String, String> attr = stripAttributes(bare);
9181

@@ -96,17 +86,17 @@ public String decrypt(String str) throws SecDispatcherException {
9686
if (attr == null || attr.get("type") == null) {
9787
String master = getMaster(sec);
9888

99-
res = _cipher.decrypt(bare, master);
89+
res = cipher.decrypt(bare, master);
10090
} else {
10191
String type = attr.get(TYPE_ATTR);
10292

103-
if (_decryptors == null)
93+
if (decryptors == null)
10494
throw new SecDispatcherException(
10595
"plexus container did not supply any required dispatchers - cannot lookup " + type);
10696

10797
Map<String, String> conf = SecUtil.getConfig(sec, type);
10898

109-
PasswordDecryptor dispatcher = _decryptors.get(type);
99+
PasswordDecryptor dispatcher = decryptors.get(type);
110100

111101
if (dispatcher == null) throw new SecDispatcherException("no dispatcher for hint " + type);
112102

@@ -141,7 +131,7 @@ private Map<String, String> stripAttributes(String str) {
141131

142132
Map<String, String> res = null;
143133

144-
StringTokenizer st = new StringTokenizer(attrs, ", ");
134+
StringTokenizer st = new StringTokenizer(attrs, ",");
145135

146136
while (st.hasMoreTokens()) {
147137
if (res == null) res = new HashMap<>(st.countTokens());
@@ -170,7 +160,7 @@ private Map<String, String> stripAttributes(String str) {
170160
private boolean isEncryptedString(String str) {
171161
if (str == null) return false;
172162

173-
return _cipher.isEncryptedString(str);
163+
return cipher.isEncryptedString(str);
174164
}
175165

176166
// ----------------------------------------------------------------------------
@@ -192,23 +182,25 @@ private SettingsSecurity getSec() throws SecDispatcherException {
192182
// ----------------------------------------------------------------------------
193183

194184
private String getMaster(SettingsSecurity sec) throws SecDispatcherException {
195-
String master = sec.getMaster();
196-
197-
if (master == null) throw new SecDispatcherException("master password is not set");
198-
185+
String masterSource = requireNonNull(sec.getMasterSource(), "masterSource is null");
199186
try {
200-
return _cipher.decryptDecorated(master, SYSTEM_PROPERTY_SEC_LOCATION);
201-
} catch (PlexusCipherException e) {
202-
throw new SecDispatcherException(e.getMessage(), e);
187+
URI masterSourceUri = new URI(masterSource);
188+
for (MasterPasswordSource masterPasswordSource : masterPasswordSources.values()) {
189+
String master = masterPasswordSource.handle(masterSourceUri);
190+
if (master != null) return master;
191+
}
192+
} catch (URISyntaxException e) {
193+
throw new SecDispatcherException("Invalid master source URI", e);
203194
}
195+
throw new SecDispatcherException("master password could not be fetched");
204196
}
205197
// ---------------------------------------------------------------
206198
public String getConfigurationFile() {
207-
return _configurationFile;
199+
return configurationFile;
208200
}
209201

210202
public void setConfigurationFile(String file) {
211-
_configurationFile = file;
203+
configurationFile = file;
212204
}
213205

214206
// ---------------------------------------------------------------
@@ -241,6 +233,12 @@ private static void usage() {
241233

242234
// ---------------------------------------------------------------
243235

236+
private static final String[] SYSTEM_PROPERTY_MASTER_PASSWORD =
237+
new String[] {"settings.master.password", "settings-master-password"};
238+
239+
private static final String[] SYSTEM_PROPERTY_SERVER_PASSWORD =
240+
new String[] {"settings.server.password", "settings-server-password"};
241+
244242
public static void main(String[] args) throws Exception {
245243
if (args == null || args.length < 1) {
246244
usage();
@@ -267,7 +265,8 @@ private static void show(boolean showMaster) throws Exception {
267265
System.out.println("\n");
268266

269267
DefaultPlexusCipher dc = new DefaultPlexusCipher();
270-
DefaultSecDispatcher dd = new DefaultSecDispatcher(dc);
268+
DefaultSecDispatcher dd =
269+
new DefaultSecDispatcher(dc, Collections.emptyMap(), Collections.emptyMap(), DEFAULT_CONFIGURATION);
271270

272271
if (showMaster)
273272
System.out.println(dc.encryptAndDecorate(pass, DefaultSecDispatcher.SYSTEM_PROPERTY_SEC_LOCATION));
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.net.URI;
17+
18+
import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
19+
20+
/**
21+
* Source of master password.
22+
*/
23+
public interface MasterPasswordSource {
24+
/**
25+
* Handles the URI to get master password. Implementation may do one of the following things:
26+
* <ul>
27+
* <li>if the URI cannot be handled by given source, return {@code null}</li>
28+
* <li>if master password retrieval was attempted, but failed throw {@link SecDispatcherException}</li>
29+
* <li>happy path: return the master password.</li>
30+
* </ul>
31+
*/
32+
String handle(URI uri) throws SecDispatcherException;
33+
}

src/main/java/org/sonatype/plexus/components/sec/dispatcher/PasswordDecryptor.java renamed to src/main/java/org/codehaus/plexus/components/secdispatcher/internal/PasswordDecryptor.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
1212
*/
1313

14-
package org.sonatype.plexus.components.sec.dispatcher;
14+
package org.codehaus.plexus.components.secdispatcher.internal;
1515

1616
import java.util.Map;
1717

18+
import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
19+
1820
/**
1921
*
2022
*
@@ -33,5 +35,6 @@ public interface PasswordDecryptor {
3335
*
3436
* @throws SecDispatcherException
3537
*/
36-
String decrypt(String str, Map<String, String> attributes, Map<String, String> config) throws SecDispatcherException;
38+
String decrypt(String str, Map<String, String> attributes, Map<String, String> config)
39+
throws SecDispatcherException;
3740
}

src/main/java/org/sonatype/plexus/components/sec/dispatcher/SecUtil.java renamed to src/main/java/org/codehaus/plexus/components/secdispatcher/internal/SecUtil.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
1212
*/
1313

14-
package org.sonatype.plexus.components.sec.dispatcher;
14+
package org.codehaus.plexus.components.secdispatcher.internal;
1515

1616
import java.io.IOException;
1717
import java.io.InputStream;
@@ -22,10 +22,11 @@
2222
import java.util.List;
2323
import java.util.Map;
2424

25-
import org.sonatype.plexus.components.sec.dispatcher.model.Config;
26-
import org.sonatype.plexus.components.sec.dispatcher.model.ConfigProperty;
27-
import org.sonatype.plexus.components.sec.dispatcher.model.SettingsSecurity;
28-
import org.sonatype.plexus.components.sec.dispatcher.model.io.xpp3.SecurityConfigurationXpp3Reader;
25+
import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
26+
import org.codehaus.plexus.components.secdispatcher.model.Config;
27+
import org.codehaus.plexus.components.secdispatcher.model.ConfigProperty;
28+
import org.codehaus.plexus.components.secdispatcher.model.SettingsSecurity;
29+
import org.codehaus.plexus.components.secdispatcher.model.io.xpp3.SecurityConfigurationXpp3Reader;
2930

3031
/**
3132
*

0 commit comments

Comments
 (0)