|
25 | 25 | import java.util.StringTokenizer;
|
26 | 26 | import java.util.stream.Collectors;
|
27 | 27 |
|
28 |
| -import org.codehaus.plexus.components.cipher.PlexusCipher; |
29 |
| -import org.codehaus.plexus.components.cipher.PlexusCipherException; |
30 | 28 | import org.codehaus.plexus.components.secdispatcher.Dispatcher;
|
31 | 29 | import org.codehaus.plexus.components.secdispatcher.DispatcherMeta;
|
32 | 30 | import org.codehaus.plexus.components.secdispatcher.SecDispatcher;
|
|
48 | 46 | * @author Oleg Gusakov
|
49 | 47 | */
|
50 | 48 | public class DefaultSecDispatcher implements SecDispatcher {
|
| 49 | + public static final String SHIELD_BEGIN = "{"; |
| 50 | + public static final String SHIELD_END = "}"; |
51 | 51 | public static final String ATTR_START = "[";
|
52 | 52 | public static final String ATTR_STOP = "]";
|
53 | 53 |
|
54 |
| - protected final PlexusCipher cipher; |
55 | 54 | protected final Map<String, Dispatcher> dispatchers;
|
56 | 55 | protected final Path configurationFile;
|
57 | 56 |
|
58 |
| - public DefaultSecDispatcher(PlexusCipher cipher, Map<String, Dispatcher> dispatchers, Path configurationFile) { |
59 |
| - this.cipher = requireNonNull(cipher); |
| 57 | + public DefaultSecDispatcher(Map<String, Dispatcher> dispatchers, Path configurationFile) { |
60 | 58 | this.dispatchers = requireNonNull(dispatchers);
|
61 | 59 | this.configurationFile = requireNonNull(configurationFile);
|
62 | 60 |
|
@@ -100,66 +98,90 @@ public Collection<Field> fields() {
|
100 | 98 | @Override
|
101 | 99 | public String encrypt(String str, Map<String, String> attr) throws SecDispatcherException, IOException {
|
102 | 100 | if (isEncryptedString(str)) return str;
|
103 |
| - |
104 |
| - try { |
105 |
| - if (attr == null) { |
106 |
| - attr = new HashMap<>(); |
107 |
| - } else { |
108 |
| - attr = new HashMap<>(attr); |
| 101 | + if (attr == null) { |
| 102 | + attr = new HashMap<>(); |
| 103 | + } else { |
| 104 | + attr = new HashMap<>(attr); |
| 105 | + } |
| 106 | + if (attr.get(DISPATCHER_NAME_ATTR) == null) { |
| 107 | + SettingsSecurity conf = readConfiguration(false); |
| 108 | + if (conf == null) { |
| 109 | + throw new SecDispatcherException("No configuration found"); |
109 | 110 | }
|
110 |
| - if (attr.get(DISPATCHER_NAME_ATTR) == null) { |
111 |
| - SettingsSecurity conf = readConfiguration(false); |
112 |
| - if (conf == null) { |
113 |
| - throw new SecDispatcherException("No configuration found"); |
114 |
| - } |
115 |
| - String defaultDispatcher = conf.getDefaultDispatcher(); |
116 |
| - if (defaultDispatcher == null) { |
117 |
| - throw new SecDispatcherException("No defaultDispatcher set in configuration"); |
118 |
| - } |
119 |
| - attr.put(DISPATCHER_NAME_ATTR, defaultDispatcher); |
| 111 | + String defaultDispatcher = conf.getDefaultDispatcher(); |
| 112 | + if (defaultDispatcher == null) { |
| 113 | + throw new SecDispatcherException("No defaultDispatcher set in configuration"); |
120 | 114 | }
|
121 |
| - String name = attr.get(DISPATCHER_NAME_ATTR); |
122 |
| - Dispatcher dispatcher = dispatchers.get(name); |
123 |
| - if (dispatcher == null) throw new SecDispatcherException("No dispatcher exist with name " + name); |
124 |
| - Dispatcher.EncryptPayload payload = dispatcher.encrypt(str, attr, prepareDispatcherConfig(name)); |
125 |
| - HashMap<String, String> resultAttributes = new HashMap<>(payload.getAttributes()); |
126 |
| - resultAttributes.put(SecDispatcher.DISPATCHER_NAME_ATTR, name); |
127 |
| - resultAttributes.put(SecDispatcher.DISPATCHER_VERSION_ATTR, SecUtil.specVersion()); |
128 |
| - String res = ATTR_START |
129 |
| - + resultAttributes.entrySet().stream() |
130 |
| - .map(e -> e.getKey() + "=" + e.getValue()) |
131 |
| - .collect(Collectors.joining(",")) |
132 |
| - + ATTR_STOP; |
133 |
| - res += payload.getEncrypted(); |
134 |
| - return cipher.decorate(res); |
135 |
| - } catch (PlexusCipherException e) { |
136 |
| - throw new SecDispatcherException(e.getMessage(), e); |
| 115 | + attr.put(DISPATCHER_NAME_ATTR, defaultDispatcher); |
137 | 116 | }
|
| 117 | + String name = attr.get(DISPATCHER_NAME_ATTR); |
| 118 | + Dispatcher dispatcher = dispatchers.get(name); |
| 119 | + if (dispatcher == null) throw new SecDispatcherException("No dispatcher exist with name " + name); |
| 120 | + Dispatcher.EncryptPayload payload = dispatcher.encrypt(str, attr, prepareDispatcherConfig(name)); |
| 121 | + HashMap<String, String> resultAttributes = new HashMap<>(payload.getAttributes()); |
| 122 | + resultAttributes.put(SecDispatcher.DISPATCHER_NAME_ATTR, name); |
| 123 | + resultAttributes.put(SecDispatcher.DISPATCHER_VERSION_ATTR, SecUtil.specVersion()); |
| 124 | + return SHIELD_BEGIN |
| 125 | + + ATTR_START |
| 126 | + + resultAttributes.entrySet().stream() |
| 127 | + .map(e -> e.getKey() + "=" + e.getValue()) |
| 128 | + .collect(Collectors.joining(",")) |
| 129 | + + ATTR_STOP |
| 130 | + + payload.getEncrypted() |
| 131 | + + SHIELD_END; |
138 | 132 | }
|
139 | 133 |
|
140 | 134 | @Override
|
141 | 135 | public String decrypt(String str) throws SecDispatcherException, IOException {
|
142 | 136 | if (!isEncryptedString(str)) return str;
|
143 |
| - try { |
144 |
| - String bare = cipher.unDecorate(str); |
145 |
| - Map<String, String> attr = requireNonNull(stripAttributes(bare)); |
146 |
| - if (isLegacyPassword(str)) { |
147 |
| - attr.put(DISPATCHER_NAME_ATTR, LegacyDispatcher.NAME); |
148 |
| - } |
149 |
| - String name = attr.get(DISPATCHER_NAME_ATTR); |
150 |
| - Dispatcher dispatcher = dispatchers.get(name); |
151 |
| - if (dispatcher == null) throw new SecDispatcherException("No dispatcher exist with name " + name); |
152 |
| - return dispatcher.decrypt(strip(bare), attr, prepareDispatcherConfig(name)); |
153 |
| - } catch (PlexusCipherException e) { |
154 |
| - throw new SecDispatcherException(e.getMessage(), e); |
| 137 | + String bare = unDecorate(str); |
| 138 | + Map<String, String> attr = requireNonNull(stripAttributes(bare)); |
| 139 | + if (isLegacyEncryptedString(str)) { |
| 140 | + attr.put(DISPATCHER_NAME_ATTR, LegacyDispatcher.NAME); |
155 | 141 | }
|
| 142 | + String name = attr.get(DISPATCHER_NAME_ATTR); |
| 143 | + Dispatcher dispatcher = dispatchers.get(name); |
| 144 | + if (dispatcher == null) throw new SecDispatcherException("No dispatcher exist with name " + name); |
| 145 | + return dispatcher.decrypt(strip(bare), attr, prepareDispatcherConfig(name)); |
156 | 146 | }
|
157 | 147 |
|
| 148 | + /** |
| 149 | + * <ul> |
| 150 | + * <li>Current: {[name=master,cipher=AES/GCM/NoPadding,version=4.0]vvq66pZ7rkvzSPStGTI9q4QDnsmuDwo+LtjraRel2b0XpcGJFdXcYAHAS75HUA6GLpcVtEkmyQ==}</li> |
| 151 | + * </ul> |
| 152 | + */ |
158 | 153 | @Override
|
159 |
| - public boolean isLegacyPassword(String str) { |
160 |
| - if (!isEncryptedString(str)) return false; |
161 |
| - Map<String, String> attr = requireNonNull(stripAttributes(cipher.unDecorate(str))); |
162 |
| - return !attr.containsKey(DISPATCHER_NAME_ATTR); |
| 154 | + public boolean isEncryptedString(String str) { |
| 155 | + boolean looksLike = str != null |
| 156 | + && !str.isBlank() |
| 157 | + && str.startsWith(SHIELD_BEGIN) |
| 158 | + && str.endsWith(SHIELD_END) |
| 159 | + && !unDecorate(str).contains(SHIELD_BEGIN) |
| 160 | + && !unDecorate(str).contains(SHIELD_END); |
| 161 | + if (looksLike) { |
| 162 | + Map<String, String> attributes = stripAttributes(unDecorate(str)); |
| 163 | + return attributes.containsKey(DISPATCHER_NAME_ATTR) && attributes.containsKey(DISPATCHER_VERSION_ATTR); |
| 164 | + } |
| 165 | + return false; |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * <ul> |
| 170 | + * <li>Legacy: {jSMOWnoPFgsHVpMvz5VrIt5kRbzGpI8u+9EF1iFQyJQ=}</li> |
| 171 | + * </ul> |
| 172 | + */ |
| 173 | + @Override |
| 174 | + public boolean isLegacyEncryptedString(String str) { |
| 175 | + boolean looksLike = str != null |
| 176 | + && !str.isBlank() |
| 177 | + && str.startsWith(SHIELD_BEGIN) |
| 178 | + && str.endsWith(SHIELD_END) |
| 179 | + && !unDecorate(str).contains(SHIELD_BEGIN) |
| 180 | + && !unDecorate(str).contains(SHIELD_END); |
| 181 | + if (looksLike) { |
| 182 | + return stripAttributes(unDecorate(str)).isEmpty(); |
| 183 | + } |
| 184 | + return false; |
163 | 185 | }
|
164 | 186 |
|
165 | 187 | @Override
|
@@ -284,7 +306,7 @@ protected Map<String, String> stripAttributes(String str) {
|
284 | 306 | return result;
|
285 | 307 | }
|
286 | 308 |
|
287 |
| - protected boolean isEncryptedString(String str) { |
288 |
| - return cipher.isEncryptedString(str); |
| 309 | + protected String unDecorate(String str) { |
| 310 | + return str.substring(SHIELD_BEGIN.length(), str.length() - SHIELD_END.length()); |
289 | 311 | }
|
290 | 312 | }
|
0 commit comments