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