1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.settings.crypto;
20
21 import java.util.ArrayList;
22 import java.util.List;
23 import javax.inject.Inject;
24 import javax.inject.Named;
25 import javax.inject.Singleton;
26 import org.apache.maven.settings.Proxy;
27 import org.apache.maven.settings.Server;
28 import org.apache.maven.settings.building.DefaultSettingsProblem;
29 import org.apache.maven.settings.building.SettingsProblem;
30 import org.apache.maven.settings.building.SettingsProblem.Severity;
31 import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
32 import org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException;
33
34
35
36
37
38
39 @Named
40 @Singleton
41 public class DefaultSettingsDecrypter implements SettingsDecrypter {
42 private final SecDispatcher securityDispatcher;
43
44 @Inject
45 public DefaultSettingsDecrypter(@Named("maven") SecDispatcher securityDispatcher) {
46 this.securityDispatcher = securityDispatcher;
47 }
48
49 @Override
50 public SettingsDecryptionResult decrypt(SettingsDecryptionRequest request) {
51 List<SettingsProblem> problems = new ArrayList<>();
52
53 List<Server> servers = new ArrayList<>();
54
55 for (Server server : request.getServers()) {
56 server = server.clone();
57
58 try {
59 server.setPassword(decrypt(server.getPassword()));
60 } catch (SecDispatcherException e) {
61 problems.add(new DefaultSettingsProblem(
62 "Failed to decrypt password for server " + server.getId() + ": " + e.getMessage(),
63 Severity.ERROR,
64 "server: " + server.getId(),
65 -1,
66 -1,
67 e));
68 }
69
70 try {
71 server.setPassphrase(decrypt(server.getPassphrase()));
72 } catch (SecDispatcherException e) {
73 problems.add(new DefaultSettingsProblem(
74 "Failed to decrypt passphrase for server " + server.getId() + ": " + e.getMessage(),
75 Severity.ERROR,
76 "server: " + server.getId(),
77 -1,
78 -1,
79 e));
80 }
81
82 servers.add(server);
83 }
84
85 List<Proxy> proxies = new ArrayList<>();
86
87 for (Proxy proxy : request.getProxies()) {
88 try {
89 proxy.setPassword(decrypt(proxy.getPassword()));
90 } catch (SecDispatcherException e) {
91 problems.add(new DefaultSettingsProblem(
92 "Failed to decrypt password for proxy " + proxy.getId() + ": " + e.getMessage(),
93 Severity.ERROR,
94 "proxy: " + proxy.getId(),
95 -1,
96 -1,
97 e));
98 }
99
100 proxies.add(proxy);
101 }
102
103 return new DefaultSettingsDecryptionResult(servers, proxies, problems);
104 }
105
106 private String decrypt(String str) throws SecDispatcherException {
107 return (str == null) ? null : securityDispatcher.decrypt(str);
108 }
109 }