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)
042public final class GpgEnvLoader implements GnupgSignatureArtifactGeneratorFactory.Loader {
043    public static final String NAME = "env";
044
045    @Override
046    public byte[] loadKeyRingMaterial(RepositorySystemSession session) {
047        String keyMaterial = ConfigUtils.getString(session, null, "env." + RESOLVER_GPG_KEY);
048        if (keyMaterial != null) {
049            return keyMaterial.getBytes(StandardCharsets.UTF_8);
050        }
051        return null;
052    }
053
054    @Override
055    public byte[] loadKeyFingerprint(RepositorySystemSession session) {
056        String keyFingerprint = ConfigUtils.getString(session, null, "env." + RESOLVER_GPG_KEY_FINGERPRINT);
057        if (keyFingerprint != null) {
058            if (keyFingerprint.trim().length() == 40) {
059                return Hex.decode(keyFingerprint);
060            } else {
061                throw new IllegalArgumentException(
062                        "Key fingerprint configuration is wrong (hex encoded, 40 characters)");
063            }
064        }
065        return null;
066    }
067
068    @Override
069    public char[] loadPassword(RepositorySystemSession session, byte[] fingerprint) {
070        String keyPassword = ConfigUtils.getString(session, null, "env." + RESOLVER_GPG_KEY_PASS);
071        if (keyPassword != null) {
072            return keyPassword.toCharArray();
073        }
074        return null;
075    }
076}