Skip to content

Commit e0d3d20

Browse files
committed
Cleanup and add support classes
1 parent d012053 commit e0d3d20

File tree

12 files changed

+149
-65
lines changed

12 files changed

+149
-65
lines changed

src/main/java/org/codehaus/plexus/components/secdispatcher/SecDispatcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
public interface SecDispatcher {
2424
String DEFAULT_CONFIGURATION = "~/.m2/settings-security.xml";
2525
String SYSTEM_PROPERTY_CONFIGURATION_LOCATION = "settings.security";
26-
String TYPE_ATTR = "type";
26+
String DISPATCHER_NAME_ATTR = "dispatcher.name";
2727

2828
/**
2929
* encrypt given plaintext string

src/main/java/org/codehaus/plexus/components/secdispatcher/internal/DefaultSecDispatcher.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
import javax.inject.Named;
1818
import javax.inject.Singleton;
1919

20-
import java.net.URI;
21-
import java.net.URISyntaxException;
2220
import java.util.HashMap;
2321
import java.util.Map;
2422
import java.util.StringTokenizer;
@@ -67,11 +65,11 @@ public String encrypt(String str, Map<String, String> attr) throws SecDispatcher
6765
try {
6866
String res;
6967
SettingsSecurity sec = getSec();
70-
if (attr == null || attr.get(TYPE_ATTR) == null) {
68+
if (attr == null || attr.get(DISPATCHER_NAME_ATTR) == null) {
7169
String master = getMaster(sec);
7270
res = cipher.encrypt(str, master);
7371
} else {
74-
String type = attr.get(TYPE_ATTR);
72+
String type = attr.get(DISPATCHER_NAME_ATTR);
7573
Map<String, String> conf = SecUtil.getConfig(sec, type);
7674
Dispatcher dispatcher = dispatchers.get(type);
7775
if (dispatcher == null) throw new SecDispatcherException("no dispatcher for type " + type);
@@ -95,11 +93,11 @@ public String decrypt(String str) throws SecDispatcherException {
9593
String bare = cipher.unDecorate(str);
9694
Map<String, String> attr = stripAttributes(bare);
9795
SettingsSecurity sec = getSec();
98-
if (attr == null || attr.get(TYPE_ATTR) == null) {
96+
if (attr == null || attr.get(DISPATCHER_NAME_ATTR) == null) {
9997
String master = getMaster(sec);
10098
return cipher.decrypt(bare, master);
10199
} else {
102-
String type = attr.get(TYPE_ATTR);
100+
String type = attr.get(DISPATCHER_NAME_ATTR);
103101
Map<String, String> conf = SecUtil.getConfig(sec, type);
104102
Dispatcher dispatcher = dispatchers.get(type);
105103
if (dispatcher == null) throw new SecDispatcherException("no dispatcher for type " + type);
@@ -178,14 +176,9 @@ private SettingsSecurity getSec() throws SecDispatcherException {
178176

179177
private String getMaster(SettingsSecurity sec) throws SecDispatcherException {
180178
String masterSource = requireNonNull(sec.getMasterSource(), "masterSource is null");
181-
try {
182-
URI masterSourceUri = new URI(masterSource);
183-
for (MasterPasswordSource masterPasswordSource : masterPasswordSources.values()) {
184-
String master = masterPasswordSource.handle(masterSourceUri);
185-
if (master != null) return master;
186-
}
187-
} catch (URISyntaxException e) {
188-
throw new SecDispatcherException("Invalid master source URI", e);
179+
for (MasterPasswordSource masterPasswordSource : masterPasswordSources.values()) {
180+
String master = masterPasswordSource.handle(masterSource);
181+
if (master != null) return master;
189182
}
190183
throw new SecDispatcherException("master password could not be fetched");
191184
}

src/main/java/org/codehaus/plexus/components/secdispatcher/internal/MasterPasswordSource.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
package org.codehaus.plexus.components.secdispatcher.internal;
1515

16-
import java.net.URI;
17-
1816
import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
1917

2018
/**
@@ -29,5 +27,5 @@ public interface MasterPasswordSource {
2927
* <li>happy path: return the master password.</li>
3028
* </ul>
3129
*/
32-
String handle(URI uri) throws SecDispatcherException;
30+
String handle(String uri) throws SecDispatcherException;
3331
}

src/main/java/org/codehaus/plexus/components/secdispatcher/internal/sources/EnvMasterPasswordSource.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,27 +21,25 @@
2121
import javax.inject.Named;
2222
import javax.inject.Singleton;
2323

24-
import java.net.URI;
25-
2624
import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
27-
import org.codehaus.plexus.components.secdispatcher.internal.MasterPasswordSource;
2825

2926
/**
3027
* Password source that uses env.
3128
*/
3229
@Singleton
3330
@Named(EnvMasterPasswordSource.NAME)
34-
public final class EnvMasterPasswordSource implements MasterPasswordSource {
31+
public final class EnvMasterPasswordSource extends PrefixMasterPasswordSourceSupport {
3532
public static final String NAME = "env";
3633

34+
public EnvMasterPasswordSource() {
35+
super(NAME + ":");
36+
}
37+
3738
@Override
38-
public String handle(URI uri) throws SecDispatcherException {
39-
if (!NAME.equals(uri.getScheme())) {
40-
return null;
41-
}
42-
String value = System.getenv(uri.getPath().substring(1));
39+
protected String doHandle(String transformed) throws SecDispatcherException {
40+
String value = System.getenv(transformed);
4341
if (value == null) {
44-
throw new SecDispatcherException("Environment variable '" + uri.getPath() + "' not found");
42+
throw new SecDispatcherException("Environment variable '" + transformed + "' not found");
4543
}
4644
return value;
4745
}

src/main/java/org/codehaus/plexus/components/secdispatcher/internal/sources/GpgAgentMasterPasswordSource.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.io.InputStreamReader;
2727
import java.io.OutputStream;
2828
import java.net.StandardProtocolFamily;
29-
import java.net.URI;
3029
import java.net.UnixDomainSocketAddress;
3130
import java.nio.channels.Channels;
3231
import java.nio.channels.SocketChannel;
@@ -35,23 +34,28 @@
3534
import java.util.HexFormat;
3635

3736
import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
38-
import org.codehaus.plexus.components.secdispatcher.internal.MasterPasswordSource;
3937

4038
/**
4139
* Password source that uses GnuPG Agent.
4240
*/
4341
@Singleton
4442
@Named(GpgAgentMasterPasswordSource.NAME)
45-
public final class GpgAgentMasterPasswordSource implements MasterPasswordSource {
43+
public final class GpgAgentMasterPasswordSource extends PrefixMasterPasswordSourceSupport {
4644
public static final String NAME = "gpg-agent";
4745

46+
public GpgAgentMasterPasswordSource() {
47+
super(NAME + ":");
48+
}
49+
4850
@Override
49-
public String handle(URI uri) throws SecDispatcherException {
50-
if (!NAME.equals(uri.getScheme())) {
51-
return null;
51+
protected String doHandle(String transformed) throws SecDispatcherException {
52+
String extra = "";
53+
if (transformed.contains("?")) {
54+
extra = transformed.substring(transformed.indexOf("?"));
55+
transformed = transformed.substring(0, transformed.indexOf("?"));
5256
}
53-
String socketLocation = uri.getPath();
54-
boolean interactive = uri.getQuery() == null || !uri.getQuery().contains("non-interactive");
57+
String socketLocation = transformed;
58+
boolean interactive = !extra.contains("non-interactive");
5559
try {
5660
Path socketLocationPath = Paths.get(socketLocation);
5761
if (!socketLocationPath.isAbsolute()) {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.codehaus.plexus.components.secdispatcher.internal.sources;
20+
21+
import java.util.function.Function;
22+
import java.util.function.Predicate;
23+
24+
import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
25+
import org.codehaus.plexus.components.secdispatcher.internal.MasterPasswordSource;
26+
27+
import static java.util.Objects.requireNonNull;
28+
29+
/**
30+
* Master password source support class.
31+
*/
32+
public abstract class MasterPasswordSourceSupport implements MasterPasswordSource {
33+
private final Predicate<String> matcher;
34+
private final Function<String, String> transformer;
35+
36+
public MasterPasswordSourceSupport(Predicate<String> matcher, Function<String, String> transformer) {
37+
this.matcher = requireNonNull(matcher);
38+
this.transformer = requireNonNull(transformer);
39+
}
40+
41+
@Override
42+
public String handle(String masterSource) throws SecDispatcherException {
43+
if (matcher.test(masterSource)) {
44+
return doHandle(transformer.apply(masterSource));
45+
}
46+
return null;
47+
}
48+
49+
protected abstract String doHandle(String transformed) throws SecDispatcherException;
50+
}

src/main/java/org/codehaus/plexus/components/secdispatcher/internal/sources/MatchingMasterPasswordSource.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
package org.codehaus.plexus.components.secdispatcher.internal.sources;
1515

16-
import java.net.URI;
1716
import java.util.function.Predicate;
1817

1918
import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
@@ -22,18 +21,18 @@
2221
import static java.util.Objects.requireNonNull;
2322

2423
public class MatchingMasterPasswordSource implements MasterPasswordSource {
25-
private final Predicate<URI> matcher;
24+
private final Predicate<String> matcher;
2625
private final MasterPasswordSource masterPasswordSource;
2726

28-
public MatchingMasterPasswordSource(Predicate<URI> matcher, MasterPasswordSource masterPasswordSource) {
27+
public MatchingMasterPasswordSource(Predicate<String> matcher, MasterPasswordSource masterPasswordSource) {
2928
this.matcher = requireNonNull(matcher);
3029
this.masterPasswordSource = requireNonNull(masterPasswordSource);
3130
}
3231

3332
@Override
34-
public String handle(URI uri) throws SecDispatcherException {
35-
if (matcher.test(uri)) {
36-
return masterPasswordSource.handle(uri);
33+
public String handle(String masterSource) throws SecDispatcherException {
34+
if (matcher.test(masterSource)) {
35+
return masterPasswordSource.handle(masterSource);
3736
}
3837
return null;
3938
}

src/main/java/org/codehaus/plexus/components/secdispatcher/internal/sources/MemoizingMasterPasswordSource.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
package org.codehaus.plexus.components.secdispatcher.internal.sources;
1515

16-
import java.net.URI;
1716
import java.util.concurrent.ConcurrentHashMap;
1817

1918
import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
@@ -23,15 +22,15 @@
2322

2423
public class MemoizingMasterPasswordSource implements MasterPasswordSource {
2524
private final MasterPasswordSource masterPasswordSource;
26-
private final ConcurrentHashMap<URI, String> memo;
25+
private final ConcurrentHashMap<String, String> memo;
2726

2827
public MemoizingMasterPasswordSource(MasterPasswordSource masterPasswordSource) {
2928
this.masterPasswordSource = requireNonNull(masterPasswordSource);
3029
this.memo = new ConcurrentHashMap<>();
3130
}
3231

3332
@Override
34-
public String handle(URI uri) throws SecDispatcherException {
35-
return memo.computeIfAbsent(uri, k -> masterPasswordSource.handle(uri));
33+
public String handle(String masterSource) throws SecDispatcherException {
34+
return memo.computeIfAbsent(masterSource, k -> masterPasswordSource.handle(masterSource));
3635
}
3736
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.codehaus.plexus.components.secdispatcher.internal.sources;
20+
21+
import java.util.function.Function;
22+
import java.util.function.Predicate;
23+
24+
import static java.util.Objects.requireNonNull;
25+
26+
/**
27+
* Master password source support class for simple "prefix" use case.
28+
*/
29+
public abstract class PrefixMasterPasswordSourceSupport extends MasterPasswordSourceSupport {
30+
public PrefixMasterPasswordSourceSupport(String prefix) {
31+
super(prefixMatcher(prefix), prefixRemover(prefix));
32+
}
33+
34+
private static Predicate<String> prefixMatcher(String prefix) {
35+
requireNonNull(prefix, "prefix cannot be null");
36+
return s -> s != null && s.startsWith(prefix);
37+
}
38+
39+
private static Function<String, String> prefixRemover(String prefix) {
40+
requireNonNull(prefix, "prefix cannot be null");
41+
return s -> s.substring(prefix.length());
42+
}
43+
}

src/main/java/org/codehaus/plexus/components/secdispatcher/internal/sources/StaticMasterPasswordSource.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
package org.codehaus.plexus.components.secdispatcher.internal.sources;
1515

16-
import java.net.URI;
17-
1816
import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
1917
import org.codehaus.plexus.components.secdispatcher.internal.MasterPasswordSource;
2018

@@ -28,7 +26,7 @@ public StaticMasterPasswordSource(String masterPassword) {
2826
}
2927

3028
@Override
31-
public String handle(URI uri) throws SecDispatcherException {
29+
public String handle(String masterSource) throws SecDispatcherException {
3230
return masterPassword;
3331
}
3432
}

0 commit comments

Comments
 (0)