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 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   * Decrypts passwords in the settings.
39   *
40   * @author Benjamin Bentmann
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 }