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