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.*;
33  
34  /**
35   * Loader that looks for environment variables.
36   */
37  @Singleton
38  @Named(GpgEnvLoader.NAME)
39  @Priority(30)
40  @SuppressWarnings("checkstyle:magicnumber")
41  public final class GpgEnvLoader implements GnupgSignatureArtifactGeneratorFactory.Loader {
42      public static final String NAME = "env";
43  
44      @Override
45      public byte[] loadKeyRingMaterial(RepositorySystemSession session) {
46          String keyMaterial = ConfigUtils.getString(session, null, "env." + RESOLVER_GPG_KEY);
47          if (keyMaterial != null) {
48              return keyMaterial.getBytes(StandardCharsets.UTF_8);
49          }
50          return null;
51      }
52  
53      @Override
54      public byte[] loadKeyFingerprint(RepositorySystemSession session) {
55          String keyFingerprint = ConfigUtils.getString(session, null, "env." + RESOLVER_GPG_KEY_FINGERPRINT);
56          if (keyFingerprint != null) {
57              if (keyFingerprint.trim().length() == 40) {
58                  return Hex.decode(keyFingerprint);
59              } else {
60                  throw new IllegalArgumentException(
61                          "Key fingerprint configuration is wrong (hex encoded, 40 characters)");
62              }
63          }
64          return null;
65      }
66  
67      @Override
68      public char[] loadPassword(RepositorySystemSession session, byte[] fingerprint) {
69          String keyPassword = ConfigUtils.getString(session, null, "env." + RESOLVER_GPG_KEY_PASS);
70          if (keyPassword != null) {
71              return keyPassword.toCharArray();
72          }
73          return null;
74      }
75  }