View Javadoc

1   /*
2    * Copyright 2012 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.ci.maven.util;
17  
18  import java.io.File;
19  
20  import org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher;
21  import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
22  import org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException;
23  
24  /**
25   * Helper to decrypt passwords from the settings.
26   */
27  public final class SettingsDecrypter
28  {
29    // ********************************* Fields *********************************
30  
31    // --- constants ------------------------------------------------------------
32  
33    // --- members --------------------------------------------------------------
34  
35    /**
36     * The Maven infrastructure to decrypt passwords from the settings.
37     */
38    private final SecDispatcher securityDispatcher;
39  
40    /**
41     * The location of the <code>settings-security.xml</code>.
42     */
43    private final String settingsSecurityLocation;
44  
45    // ****************************** Initializer *******************************
46  
47    // ****************************** Constructors ******************************
48  
49    /**
50     * Default constructor.
51     *
52     * @param securityDispatcher the Maven infrastructure to decrypt passwords
53     *          from the settings.
54     * @param settingsSecurityLocation the location of the
55     *          <code>settings-security</code>.
56     */
57    public SettingsDecrypter(final SecDispatcher securityDispatcher,
58        final String settingsSecurityLocation)
59    {
60      this.securityDispatcher = securityDispatcher;
61      this.settingsSecurityLocation = init(settingsSecurityLocation);
62    }
63  
64    // ****************************** Inner Classes *****************************
65  
66    // ********************************* Methods ********************************
67  
68    // --- init -----------------------------------------------------------------
69  
70    private static String init(final String settingsSecurityLocation)
71    {
72      final File file = new File(settingsSecurityLocation);
73      if (!file.canRead())
74      {
75        return null;
76      }
77  
78      System.setProperty(DefaultSecDispatcher.SYSTEM_PROPERTY_SEC_LOCATION,
79          settingsSecurityLocation);
80      return settingsSecurityLocation;
81    }
82  
83    // --- get&set --------------------------------------------------------------
84  
85    // --- business -------------------------------------------------------------
86  
87    /**
88     * Decrypts the given value if the security dispatcher is initialized with a
89     * valid configuration.
90     *
91     * @param encrypted the value to decrypt.
92     * @return the decrypted value or the unchanged {@code encrypted}.
93     */
94    public String decrypt(final String encrypted)
95    {
96      if (settingsSecurityLocation != null)
97      {
98        try
99        {
100         return securityDispatcher.decrypt(encrypted);
101       }
102       catch (final SecDispatcherException e)
103       {
104         return encrypted;
105       }
106     }
107     return encrypted;
108   }
109 
110   // --- object basics --------------------------------------------------------
111 
112 }