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.sigstore;
020
021import javax.inject.Inject;
022import javax.inject.Named;
023import javax.inject.Singleton;
024
025import java.util.Collection;
026
027import org.eclipse.aether.RepositorySystemSession;
028import org.eclipse.aether.artifact.Artifact;
029import org.eclipse.aether.deployment.DeployRequest;
030import org.eclipse.aether.installation.InstallRequest;
031import org.eclipse.aether.spi.artifact.ArtifactPredicateFactory;
032import org.eclipse.aether.spi.artifact.generator.ArtifactGenerator;
033import org.eclipse.aether.spi.artifact.generator.ArtifactGeneratorFactory;
034import org.eclipse.aether.spi.io.PathProcessor;
035import org.eclipse.aether.util.ConfigUtils;
036
037@Singleton
038@Named(SigstoreSignatureArtifactGeneratorFactory.NAME)
039public final class SigstoreSignatureArtifactGeneratorFactory implements ArtifactGeneratorFactory {
040
041    public static final String NAME = "sigstore";
042
043    private final ArtifactPredicateFactory artifactPredicateFactory;
044    private final PathProcessor pathProcessor;
045
046    @Inject
047    public SigstoreSignatureArtifactGeneratorFactory(
048            ArtifactPredicateFactory artifactPredicateFactory, PathProcessor pathProcessor) {
049        this.artifactPredicateFactory = artifactPredicateFactory;
050        this.pathProcessor = pathProcessor;
051    }
052
053    @Override
054    public ArtifactGenerator newInstance(RepositorySystemSession session, InstallRequest request) {
055        return null; // nothing on install
056    }
057
058    @Override
059    public ArtifactGenerator newInstance(RepositorySystemSession session, DeployRequest request) {
060        return newInstance(session, request.getArtifacts()); // generate on deploy
061    }
062
063    private ArtifactGenerator newInstance(RepositorySystemSession session, Collection<Artifact> artifacts) {
064        final boolean enabled = ConfigUtils.getBoolean(
065                session, SigstoreConfigurationKeys.DEFAULT_ENABLED, SigstoreConfigurationKeys.CONFIG_PROP_ENABLED);
066        if (!enabled) {
067            return null;
068        }
069        final boolean publicStaging = ConfigUtils.getBoolean(
070                session,
071                SigstoreConfigurationKeys.DEFAULT_PUBLIC_STAGING,
072                SigstoreConfigurationKeys.CONFIG_PROP_PUBLIC_STAGING);
073
074        return new SigstoreSignatureArtifactGenerator(
075                pathProcessor, artifacts, artifactPredicateFactory.newInstance(session)::hasChecksums, publicStaging);
076    }
077
078    @Override
079    public float getPriority() {
080        return 150;
081    }
082}