Skip to content

Commit 8904363

Browse files
committed
Split meta and the thing
1 parent eae95a3 commit 8904363

File tree

9 files changed

+111
-73
lines changed

9 files changed

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

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

1616
import java.util.Map;
1717

18-
import org.codehaus.plexus.components.secdispatcher.DispatcherMeta;
19-
import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
20-
2118
import static java.util.Objects.requireNonNull;
2219

2320
/**
@@ -49,11 +46,6 @@ public String getEncrypted() {
4946
}
5047
}
5148

52-
/**
53-
* The metadata of this dispatcher.
54-
*/
55-
DispatcherMeta meta();
56-
5749
/**
5850
* Encrypt given plaintext string. Implementation must return at least same attributes it got, but may add more
5951
* attributes to returned payload.
Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +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.codehaus.plexus.components.secdispatcher.internal;
15-
16-
import java.util.Optional;
17-
18-
import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
14+
package org.codehaus.plexus.components.secdispatcher;
1915

2016
/**
2117
* Source of master password.
2218
*/
2319
public interface MasterSource {
24-
/**
25-
* String describing what this source does.
26-
*/
27-
String description();
28-
29-
/**
30-
* Optional "config template" that may serve as basis to configure this master source. The template cannot be
31-
* "reused" as is as configuration.
32-
*/
33-
Optional<String> configTemplate();
34-
3520
/**
3621
* Handles the config to get master password. Implementation may do one of the following things:
3722
* <ul>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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;
15+
16+
import java.util.Optional;
17+
18+
/**
19+
* Source of master password.
20+
*/
21+
public interface MasterSourceMeta {
22+
/**
23+
* String describing what this source does.
24+
*/
25+
String description();
26+
27+
/**
28+
* Optional "config template" that may serve as basis to configure this master source. The template cannot be
29+
* "reused" as is as configuration.
30+
*/
31+
Optional<String> configTemplate();
32+
}

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import java.io.IOException;
2121
import java.nio.file.Path;
2222
import java.nio.file.Paths;
23+
import java.util.Collection;
2324
import java.util.HashMap;
25+
import java.util.List;
2426
import java.util.Map;
2527
import java.util.Objects;
2628
import java.util.Set;
@@ -29,6 +31,7 @@
2931

3032
import org.codehaus.plexus.components.cipher.PlexusCipher;
3133
import org.codehaus.plexus.components.cipher.PlexusCipherException;
34+
import org.codehaus.plexus.components.secdispatcher.Dispatcher;
3235
import org.codehaus.plexus.components.secdispatcher.DispatcherMeta;
3336
import org.codehaus.plexus.components.secdispatcher.SecDispatcher;
3437
import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
@@ -61,7 +64,31 @@ public DefaultSecDispatcher(
6164

6265
@Override
6366
public Set<DispatcherMeta> availableDispatchers() {
64-
return Set.copyOf(dispatchers.values().stream().map(Dispatcher::meta).collect(Collectors.toSet()));
67+
return Set.copyOf(
68+
dispatchers.entrySet().stream().map(this::dispatcherMeta).collect(Collectors.toSet()));
69+
}
70+
71+
private DispatcherMeta dispatcherMeta(Map.Entry<String, Dispatcher> dispatcher) {
72+
if (dispatcher instanceof DispatcherMeta) {
73+
return (DispatcherMeta) dispatcher;
74+
} else {
75+
return new DispatcherMeta() {
76+
@Override
77+
public String name() {
78+
return dispatcher.getKey();
79+
}
80+
81+
@Override
82+
public String displayName() {
83+
return dispatcher.getKey() + " (needs manual configuration)";
84+
}
85+
86+
@Override
87+
public Collection<Field> fields() {
88+
return List.of();
89+
}
90+
};
91+
}
6592
}
6693

6794
@Override

src/main/java/org/codehaus/plexus/components/secdispatcher/internal/dispatchers/MasterDispatcher.java

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,19 @@
2424

2525
import org.codehaus.plexus.components.cipher.PlexusCipher;
2626
import org.codehaus.plexus.components.cipher.PlexusCipherException;
27+
import org.codehaus.plexus.components.secdispatcher.Dispatcher;
2728
import org.codehaus.plexus.components.secdispatcher.DispatcherMeta;
29+
import org.codehaus.plexus.components.secdispatcher.MasterSource;
30+
import org.codehaus.plexus.components.secdispatcher.MasterSourceMeta;
2831
import org.codehaus.plexus.components.secdispatcher.SecDispatcher;
2932
import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
30-
import org.codehaus.plexus.components.secdispatcher.internal.Dispatcher;
31-
import org.codehaus.plexus.components.secdispatcher.internal.MasterSource;
3233

3334
/**
3435
* This dispatcher is logically equivalent (but much more secure) that Maven3 "master password" encryption.
3536
*/
3637
@Singleton
3738
@Named(MasterDispatcher.NAME)
38-
public class MasterDispatcher implements Dispatcher {
39+
public class MasterDispatcher implements Dispatcher, DispatcherMeta {
3940
public static final String NAME = "master";
4041

4142
private static final String MASTER_CIPHER = "cipher";
@@ -51,48 +52,46 @@ public MasterDispatcher(PlexusCipher cipher, Map<String, MasterSource> masterSou
5152
}
5253

5354
@Override
54-
public DispatcherMeta meta() {
55-
return new DispatcherMeta() {
56-
@Override
57-
public String name() {
58-
return NAME;
59-
}
55+
public String name() {
56+
return NAME;
57+
}
6058

61-
@Override
62-
public String displayName() {
63-
return "Master Password Dispatcher";
64-
}
59+
@Override
60+
public String displayName() {
61+
return "Master Password Dispatcher";
62+
}
6563

66-
@Override
67-
public Collection<Field> fields() {
68-
return List.of(
69-
Field.builder(MASTER_SOURCE)
70-
.optional(false)
71-
.description("The source of master password")
72-
.options(masterSources.entrySet().stream()
73-
.map(e -> {
74-
Field.Builder b = Field.builder(e.getKey())
75-
.description(e.getValue().description());
76-
if (e.getValue().configTemplate().isPresent()) {
77-
b = b.defaultValue(e.getValue()
78-
.configTemplate()
79-
.get());
80-
}
81-
return b.build();
82-
})
83-
.toList())
84-
.build(),
85-
Field.builder(MASTER_CIPHER)
86-
.optional(false)
87-
.description("The cipher to use with master password")
88-
.options(cipher.availableCiphers().stream()
89-
.map(c -> Field.builder(c)
90-
.description("Cipher implementation " + c)
91-
.build())
92-
.toList())
93-
.build());
94-
}
95-
};
64+
@Override
65+
public Collection<Field> fields() {
66+
return List.of(
67+
Field.builder(MASTER_SOURCE)
68+
.optional(false)
69+
.description("The source of master password")
70+
.options(masterSources.entrySet().stream()
71+
.map(e -> {
72+
if (e instanceof MasterSourceMeta m) {
73+
Field.Builder b =
74+
Field.builder(e.getKey()).description(m.description());
75+
if (m.configTemplate().isPresent()) {
76+
b = b.defaultValue(
77+
m.configTemplate().get());
78+
}
79+
return b.build();
80+
} else {
81+
return Field.builder(e.getKey())
82+
.description("Field not described (needs manual configuration)")
83+
.build();
84+
}
85+
})
86+
.toList())
87+
.build(),
88+
Field.builder(MASTER_CIPHER)
89+
.optional(false)
90+
.description("The cipher to use with master password")
91+
.options(cipher.availableCiphers().stream()
92+
.map(c -> Field.builder(c).description(c).build())
93+
.toList())
94+
.build());
9695
}
9796

9897
@Override

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import java.util.Optional;
2525

26+
import org.codehaus.plexus.components.secdispatcher.MasterSourceMeta;
2627
import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
2728

2829
/**
@@ -32,7 +33,7 @@
3233
*/
3334
@Singleton
3435
@Named(EnvMasterSource.NAME)
35-
public final class EnvMasterSource extends PrefixMasterSourceSupport {
36+
public final class EnvMasterSource extends PrefixMasterSourceSupport implements MasterSourceMeta {
3637
public static final String NAME = "env";
3738

3839
public EnvMasterSource() {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.HexFormat;
3535
import java.util.Optional;
3636

37+
import org.codehaus.plexus.components.secdispatcher.MasterSourceMeta;
3738
import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
3839

3940
/**
@@ -43,7 +44,7 @@
4344
*/
4445
@Singleton
4546
@Named(GpgAgentMasterSource.NAME)
46-
public final class GpgAgentMasterSource extends PrefixMasterSourceSupport {
47+
public final class GpgAgentMasterSource extends PrefixMasterSourceSupport implements MasterSourceMeta {
4748
public static final String NAME = "gpg-agent";
4849

4950
public GpgAgentMasterSource() {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import java.util.function.Function;
2222
import java.util.function.Predicate;
2323

24+
import org.codehaus.plexus.components.secdispatcher.MasterSource;
2425
import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
25-
import org.codehaus.plexus.components.secdispatcher.internal.MasterSource;
2626

2727
import static java.util.Objects.requireNonNull;
2828

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import java.util.Optional;
2525

26+
import org.codehaus.plexus.components.secdispatcher.MasterSourceMeta;
2627
import org.codehaus.plexus.components.secdispatcher.SecDispatcherException;
2728

2829
/**
@@ -32,7 +33,7 @@
3233
*/
3334
@Singleton
3435
@Named(SystemPropertyMasterSource.NAME)
35-
public final class SystemPropertyMasterSource extends PrefixMasterSourceSupport {
36+
public final class SystemPropertyMasterSource extends PrefixMasterSourceSupport implements MasterSourceMeta {
3637
public static final String NAME = "system-property";
3738

3839
public SystemPropertyMasterSource() {

0 commit comments

Comments
 (0)