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.eclipse.aether.generator.gnupg.loaders;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.nio.charset.StandardCharsets;
25  
26  import org.bouncycastle.util.encoders.Hex;
27  import org.eclipse.aether.RepositorySystemSession;
28  import org.eclipse.aether.generator.gnupg.GnupgSignatureArtifactGeneratorFactory;
29  import org.eclipse.aether.util.ConfigUtils;
30  import org.eclipse.sisu.Priority;
31  
32  import static org.eclipse.aether.generator.gnupg.GnupgConfigurationKeys.RESOLVER_GPG_KEY;
33  import static org.eclipse.aether.generator.gnupg.GnupgConfigurationKeys.RESOLVER_GPG_KEY_FINGERPRINT;
34  import static org.eclipse.aether.generator.gnupg.GnupgConfigurationKeys.RESOLVER_GPG_KEY_PASS;
35  
36  /**
37   * Loader that looks for environment variables.
38   */
39  @Singleton
40  @Named(GpgEnvLoader.NAME)
41  @Priority(30)
42  public final class GpgEnvLoader implements GnupgSignatureArtifactGeneratorFactory.Loader {
43      public static final String NAME = "env";
44  
45      @Override
46      public byte[] loadKeyRingMaterial(RepositorySystemSession session) {
47          String keyMaterial = ConfigUtils.getString(session, null, "env." + RESOLVER_GPG_KEY);
48          if (keyMaterial != null) {
49              return keyMaterial.getBytes(StandardCharsets.UTF_8);
50          }
51          return null;
52      }
53  
54      @Override
55      public byte[] loadKeyFingerprint(RepositorySystemSession session) {
56          String keyFingerprint = ConfigUtils.getString(session, null, "env." + RESOLVER_GPG_KEY_FINGERPRINT);
57          if (keyFingerprint != null) {
58              if (keyFingerprint.trim().length() == 40) {
59                  return Hex.decode(keyFingerprint);
60              } else {
61                  throw new IllegalArgumentException(
62                          "Key fingerprint configuration is wrong (hex encoded, 40 characters)");
63              }
64          }
65          return null;
66      }
67  
68      @Override
69      public char[] loadPassword(RepositorySystemSession session, byte[] fingerprint) {
70          String keyPassword = ConfigUtils.getString(session, null, "env." + RESOLVER_GPG_KEY_PASS);
71          if (keyPassword != null) {
72              return keyPassword.toCharArray();
73          }
74          return null;
75      }
76  }