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.internal.impl.secdispatcher;
20  
21  import java.nio.file.Path;
22  import java.nio.file.Paths;
23  import java.util.Map;
24  
25  import org.apache.maven.api.Constants;
26  import org.apache.maven.api.di.Named;
27  import org.apache.maven.api.di.Provides;
28  import org.codehaus.plexus.components.secdispatcher.Cipher;
29  import org.codehaus.plexus.components.secdispatcher.Dispatcher;
30  import org.codehaus.plexus.components.secdispatcher.MasterSource;
31  import org.codehaus.plexus.components.secdispatcher.SecDispatcher;
32  import org.codehaus.plexus.components.secdispatcher.internal.DefaultSecDispatcher;
33  import org.codehaus.plexus.components.secdispatcher.internal.cipher.AESGCMNoPadding;
34  import org.codehaus.plexus.components.secdispatcher.internal.dispatchers.LegacyDispatcher;
35  import org.codehaus.plexus.components.secdispatcher.internal.dispatchers.MasterDispatcher;
36  import org.codehaus.plexus.components.secdispatcher.internal.sources.EnvMasterSource;
37  import org.codehaus.plexus.components.secdispatcher.internal.sources.GpgAgentMasterSource;
38  import org.codehaus.plexus.components.secdispatcher.internal.sources.PinEntryMasterSource;
39  import org.codehaus.plexus.components.secdispatcher.internal.sources.SystemPropertyMasterSource;
40  
41  /**
42   * Delegate that offers just the minimal surface needed to decrypt settings.
43   */
44  @SuppressWarnings("unused")
45  @Named
46  public class SecDispatcherProvider {
47  
48      private static final String FILE_NAME = "settings-security4.xml";
49  
50      @Provides
51      public static SecDispatcher secDispatcher(Map<String, Dispatcher> dispatchers) {
52          return new DefaultSecDispatcher(dispatchers, configurationFile());
53      }
54  
55      @Provides
56      @Named(LegacyDispatcher.NAME)
57      public static Dispatcher legacyDispatcher() {
58          return new LegacyDispatcher();
59      }
60  
61      @Provides
62      @Named(MasterDispatcher.NAME)
63      public static Dispatcher masterDispatcher(
64              Map<String, Cipher> masterCiphers, Map<String, MasterSource> masterSources) {
65          return new MasterDispatcher(masterCiphers, masterSources);
66      }
67  
68      @Provides
69      @Named(AESGCMNoPadding.CIPHER_ALG)
70      public static Cipher aesGcmNoPaddingCipher() {
71          return new AESGCMNoPadding();
72      }
73  
74      @Provides
75      @Named(EnvMasterSource.NAME)
76      public static MasterSource envMasterSource() {
77          return new EnvMasterSource();
78      }
79  
80      @Provides
81      @Named(GpgAgentMasterSource.NAME)
82      public static MasterSource gpgAgentMasterSource() {
83          return new GpgAgentMasterSource();
84      }
85  
86      @Provides
87      @Named(PinEntryMasterSource.NAME)
88      public static MasterSource pinEntryMasterSource() {
89          return new PinEntryMasterSource();
90      }
91  
92      @Provides
93      @Named(SystemPropertyMasterSource.NAME)
94      public static MasterSource systemPropertyMasterSource() {
95          return new SystemPropertyMasterSource();
96      }
97  
98      private static Path configurationFile() {
99          String mavenUserConf = System.getProperty(Constants.MAVEN_USER_CONF);
100         if (mavenUserConf != null) {
101             return Paths.get(mavenUserConf, FILE_NAME);
102         }
103         // this means we are in UT or alike
104         return Paths.get(System.getProperty("user.home"), ".m2", FILE_NAME);
105     }
106 }