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