View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.settings.crypto;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import org.apache.maven.settings.Proxy;
29  import org.apache.maven.settings.Server;
30  import org.apache.maven.settings.building.DefaultSettingsProblem;
31  import org.apache.maven.settings.building.SettingsProblem;
32  import org.apache.maven.settings.building.SettingsProblem.Severity;
33  import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
34  import org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException;
35  
36  /**
37   * Decrypts passwords in the settings.
38   *
39   * @author Benjamin Bentmann
40   */
41  @Named
42  @Singleton
43  public class DefaultSettingsDecrypter implements SettingsDecrypter {
44      private final SecDispatcher securityDispatcher;
45  
46      @Inject
47      public DefaultSettingsDecrypter(@Named("maven") SecDispatcher securityDispatcher) {
48          this.securityDispatcher = securityDispatcher;
49      }
50  
51      @Override
52      public SettingsDecryptionResult decrypt(SettingsDecryptionRequest request) {
53          List<SettingsProblem> problems = new ArrayList<>();
54  
55          List<Server> servers = new ArrayList<>();
56  
57          for (Server server : request.getServers()) {
58              server = server.clone();
59  
60              servers.add(server);
61  
62              try {
63                  server.setPassword(decrypt(server.getPassword()));
64              } catch (SecDispatcherException e) {
65                  problems.add(new DefaultSettingsProblem(
66                          "Failed to decrypt password for server " + server.getId() + ": " + e.getMessage(),
67                          Severity.ERROR,
68                          "server: " + server.getId(),
69                          -1,
70                          -1,
71                          e));
72              }
73  
74              try {
75                  server.setPassphrase(decrypt(server.getPassphrase()));
76              } catch (SecDispatcherException e) {
77                  problems.add(new DefaultSettingsProblem(
78                          "Failed to decrypt passphrase for server " + server.getId() + ": " + e.getMessage(),
79                          Severity.ERROR,
80                          "server: " + server.getId(),
81                          -1,
82                          -1,
83                          e));
84              }
85          }
86  
87          List<Proxy> proxies = new ArrayList<>();
88  
89          for (Proxy proxy : request.getProxies()) {
90              proxy = proxy.clone();
91  
92              proxies.add(proxy);
93  
94              try {
95                  proxy.setPassword(decrypt(proxy.getPassword()));
96              } catch (SecDispatcherException e) {
97                  problems.add(new DefaultSettingsProblem(
98                          "Failed to decrypt password for proxy " + proxy.getId() + ": " + e.getMessage(),
99                          Severity.ERROR,
100                         "proxy: " + proxy.getId(),
101                         -1,
102                         -1,
103                         e));
104             }
105         }
106 
107         return new DefaultSettingsDecryptionResult(servers, proxies, problems);
108     }
109 
110     private String decrypt(String str) throws SecDispatcherException {
111         return (str == null) ? null : securityDispatcher.decrypt(str);
112     }
113 }