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