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      @Override
49      public SettingsDecryptionResult decrypt( SettingsDecryptionRequest request )
50      {
51          List<SettingsProblem> problems = new ArrayList<>();
52  
53          List<Server> servers = new ArrayList<>();
54  
55          for ( Server server : request.getServers() )
56          {
57              server = server.clone();
58  
59              servers.add( server );
60  
61              try
62              {
63                  server.setPassword( decrypt( server.getPassword() ) );
64              }
65              catch ( SecDispatcherException e )
66              {
67                  problems.add( new DefaultSettingsProblem( "Failed to decrypt password for server " + server.getId()
68                      + ": " + e.getMessage(), Severity.ERROR, "server: " + server.getId(), -1, -1, e ) );
69              }
70  
71              try
72              {
73                  server.setPassphrase( decrypt( server.getPassphrase() ) );
74              }
75              catch ( SecDispatcherException e )
76              {
77                  problems.add( new DefaultSettingsProblem( "Failed to decrypt passphrase for server " + server.getId()
78                      + ": " + e.getMessage(), Severity.ERROR, "server: " + server.getId(), -1, -1, e ) );
79              }
80          }
81  
82          List<Proxy> proxies = new ArrayList<>();
83  
84          for ( Proxy proxy : request.getProxies() )
85          {
86              proxy = proxy.clone();
87  
88              proxies.add( proxy );
89  
90              try
91              {
92                  proxy.setPassword( decrypt( proxy.getPassword() ) );
93              }
94              catch ( SecDispatcherException e )
95              {
96                  problems.add( new DefaultSettingsProblem( "Failed to decrypt password for proxy " + proxy.getId()
97                      + ": " + e.getMessage(), Severity.ERROR, "proxy: " + proxy.getId(), -1, -1, e ) );
98              }
99          }
100 
101         return new DefaultSettingsDecryptionResult( servers, proxies, problems );
102     }
103 
104     private String decrypt( String str )
105         throws SecDispatcherException
106     {
107         return ( str == null ) ? null : securityDispatcher.decrypt( str );
108     }
109 
110 }