001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.eclipse.aether.generator.gnupg.loaders; 020 021import javax.inject.Named; 022import javax.inject.Singleton; 023 024import java.nio.charset.StandardCharsets; 025 026import org.bouncycastle.util.encoders.Hex; 027import org.eclipse.aether.RepositorySystemSession; 028import org.eclipse.aether.generator.gnupg.GnupgSignatureArtifactGeneratorFactory; 029import org.eclipse.aether.util.ConfigUtils; 030import org.eclipse.sisu.Priority; 031 032import static org.eclipse.aether.generator.gnupg.GnupgConfigurationKeys.RESOLVER_GPG_KEY; 033import static org.eclipse.aether.generator.gnupg.GnupgConfigurationKeys.RESOLVER_GPG_KEY_FINGERPRINT; 034import static org.eclipse.aether.generator.gnupg.GnupgConfigurationKeys.RESOLVER_GPG_KEY_PASS; 035 036/** 037 * Loader that looks for environment variables. 038 */ 039@Singleton 040@Named(GpgEnvLoader.NAME) 041@Priority(30) 042@SuppressWarnings("checkstyle:magicnumber") 043public final class GpgEnvLoader implements GnupgSignatureArtifactGeneratorFactory.Loader { 044 public static final String NAME = "env"; 045 046 @Override 047 public byte[] loadKeyRingMaterial(RepositorySystemSession session) { 048 String keyMaterial = ConfigUtils.getString(session, null, "env." + RESOLVER_GPG_KEY); 049 if (keyMaterial != null) { 050 return keyMaterial.getBytes(StandardCharsets.UTF_8); 051 } 052 return null; 053 } 054 055 @Override 056 public byte[] loadKeyFingerprint(RepositorySystemSession session) { 057 String keyFingerprint = ConfigUtils.getString(session, null, "env." + RESOLVER_GPG_KEY_FINGERPRINT); 058 if (keyFingerprint != null) { 059 if (keyFingerprint.trim().length() == 40) { 060 return Hex.decode(keyFingerprint); 061 } else { 062 throw new IllegalArgumentException( 063 "Key fingerprint configuration is wrong (hex encoded, 40 characters)"); 064 } 065 } 066 return null; 067 } 068 069 @Override 070 public char[] loadPassword(RepositorySystemSession session, byte[] fingerprint) { 071 String keyPassword = ConfigUtils.getString(session, null, "env." + RESOLVER_GPG_KEY_PASS); 072 if (keyPassword != null) { 073 return keyPassword.toCharArray(); 074 } 075 return null; 076 } 077}