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.transport.minio;
020
021import javax.inject.Inject;
022import javax.inject.Named;
023
024import java.util.Map;
025
026import org.eclipse.aether.RepositorySystemSession;
027import org.eclipse.aether.repository.RemoteRepository;
028import org.eclipse.aether.spi.connector.transport.Transporter;
029import org.eclipse.aether.spi.connector.transport.TransporterFactory;
030import org.eclipse.aether.spi.io.PathProcessor;
031import org.eclipse.aether.transfer.NoTransporterException;
032import org.eclipse.aether.util.ConfigUtils;
033
034import static java.util.Objects.requireNonNull;
035
036/**
037 * A transporter factory for repositories using the S3 API object storage using Minio.
038 *
039 * @since 2.0.2
040 */
041@Named(MinioTransporterFactory.NAME)
042public final class MinioTransporterFactory implements TransporterFactory {
043    public static final String NAME = "minio";
044
045    private static final float DEFAULT_PRIORITY = 0.0f;
046
047    private float priority = DEFAULT_PRIORITY;
048
049    private final Map<String, ObjectNameMapperFactory> objectNameMapperFactories;
050
051    private final PathProcessor pathProcessor;
052
053    @Inject
054    public MinioTransporterFactory(
055            Map<String, ObjectNameMapperFactory> objectNameMapperFactories, PathProcessor pathProcessor) {
056        this.objectNameMapperFactories = requireNonNull(objectNameMapperFactories, "objectNameMapperFactories");
057        this.pathProcessor = requireNonNull(pathProcessor, "pathProcessor");
058    }
059
060    @Override
061    public float getPriority() {
062        return priority;
063    }
064
065    public MinioTransporterFactory setPriority(float priority) {
066        this.priority = priority;
067        return this;
068    }
069
070    @Override
071    public Transporter newInstance(RepositorySystemSession session, RemoteRepository repository)
072            throws NoTransporterException {
073        requireNonNull(session, "session cannot be null");
074        requireNonNull(repository, "repository cannot be null");
075
076        // this check is here only to support "minio+http" and "s3+http" protocols by default. But also when
077        // raised priorities by user, allow to "overtake" plain HTTP repositories, if needed.
078        RemoteRepository adjusted = repository;
079        boolean prefixStripped = false;
080        if ("minio+http".equalsIgnoreCase(repository.getProtocol())
081                || "minio+https".equalsIgnoreCase(repository.getProtocol())) {
082            adjusted = new RemoteRepository.Builder(repository)
083                    .setUrl(repository.getUrl().substring("minio+".length()))
084                    .build();
085            prefixStripped = true;
086        } else if ("s3+http".equalsIgnoreCase(repository.getProtocol())
087                || "s3+https".equalsIgnoreCase(repository.getProtocol())) {
088            adjusted = new RemoteRepository.Builder(repository)
089                    .setUrl(repository.getUrl().substring("s3+".length()))
090                    .build();
091            prefixStripped = true;
092        } else if (priority == DEFAULT_PRIORITY) {
093            throw new NoTransporterException(
094                    repository,
095                    "To use Minio transport with plain HTTP/HTTPS repositories, increase the Minio transport priority");
096        }
097        // Plaintext endpoints from "minio+http"/"s3+http" URLs are not matched by "external:http:*" mirror
098        // blocking (the repository protocol string is not "http") and offer no transport integrity: artifacts
099        // and their checksum objects travel over the same tamperable channel. Refuse them unless the operator
100        // explicitly opted in.
101        if (prefixStripped && "http".equalsIgnoreCase(adjusted.getProtocol())) {
102            boolean allowInsecureProtocol = ConfigUtils.getBoolean(
103                    session,
104                    MinioTransporterConfigurationKeys.DEFAULT_ALLOW_INSECURE_PROTOCOL,
105                    MinioTransporterConfigurationKeys.CONFIG_PROP_ALLOW_INSECURE_PROTOCOL + "." + repository.getId(),
106                    MinioTransporterConfigurationKeys.CONFIG_PROP_ALLOW_INSECURE_PROTOCOL);
107            if (!allowInsecureProtocol) {
108                throw new NoTransporterException(
109                        repository,
110                        "Plaintext HTTP object storage endpoints are insecure (no transport integrity; not subject"
111                                + " to 'external:http:*' mirror blocking) and are refused by default: use the"
112                                + " 'minio+https'/'s3+https' URL form, or explicitly opt in by setting the"
113                                + " configuration property '"
114                                + MinioTransporterConfigurationKeys.CONFIG_PROP_ALLOW_INSECURE_PROTOCOL
115                                + "' (optionally suffixed with '." + repository.getId() + "') to 'true'");
116            }
117        }
118        String objectNameMapperConf = ConfigUtils.getString(
119                session,
120                MinioTransporterConfigurationKeys.DEFAULT_OBJECT_NAME_MAPPER,
121                MinioTransporterConfigurationKeys.CONFIG_PROP_OBJECT_NAME_MAPPER + "." + repository.getId(),
122                MinioTransporterConfigurationKeys.CONFIG_PROP_OBJECT_NAME_MAPPER);
123        ObjectNameMapperFactory objectNameMapperFactory = objectNameMapperFactories.get(objectNameMapperConf);
124        if (objectNameMapperFactory == null) {
125            throw new IllegalArgumentException("Unknown object name mapper configured '" + objectNameMapperConf
126                    + "' for repository " + repository.getId());
127        }
128        return new MinioTransporter(session, adjusted, objectNameMapperFactory, pathProcessor);
129    }
130}