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