View Javadoc

1   package org.apache.maven.settings.crypto;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Decrypts passwords in the settings.
37   * 
38   * @author Benjamin Bentmann
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 }