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