1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.eclipse.aether.internal.impl;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import org.eclipse.aether.ConfigurationProperties;
26 import org.eclipse.aether.RepositorySystemSession;
27 import org.eclipse.aether.repository.LocalRepository;
28 import org.eclipse.aether.repository.LocalRepositoryManager;
29 import org.eclipse.aether.repository.NoLocalRepositoryManagerException;
30 import org.eclipse.aether.spi.localrepo.LocalRepositoryManagerFactory;
31 import org.eclipse.aether.spi.remoterepo.RepositoryKeyFunctionFactory;
32 import org.eclipse.aether.util.ConfigUtils;
33
34 import static java.util.Objects.requireNonNull;
35
36 /**
37 * Creates enhanced local repository managers for repository types {@code "default"} or {@code "" (automatic)}. Enhanced
38 * local repository manager is built upon the classical Maven 2.0 local repository structure but additionally keeps
39 * track of from what repositories a cached artifact was resolved. Resolution of locally cached artifacts will be
40 * rejected in case the current resolution request does not match the known source repositories of an artifact, thereby
41 * emulating physically separated artifact caches per remote repository.
42 */
43 @Singleton
44 @Named(EnhancedLocalRepositoryManagerFactory.NAME)
45 public class EnhancedLocalRepositoryManagerFactory implements LocalRepositoryManagerFactory {
46 public static final String NAME = "enhanced";
47
48 static final String CONFIG_PROPS_PREFIX = ConfigurationProperties.PREFIX_LRM + NAME + ".";
49
50 /**
51 * Filename of the file in which to track the remote repositories.
52 *
53 * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
54 * @configurationType {@link java.lang.String}
55 * @configurationDefaultValue {@link #DEFAULT_TRACKING_FILENAME}
56 */
57 public static final String CONFIG_PROP_TRACKING_FILENAME = CONFIG_PROPS_PREFIX + "trackingFilename";
58
59 public static final String DEFAULT_TRACKING_FILENAME = "_remote.repositories";
60
61 /**
62 * Whether to verify that the real (on-disk) path of a locally cached artifact matches the requested path
63 * spelling before the artifact is used. On case-insensitive or case/normalization-preserving filesystems (the
64 * macOS and Windows defaults) a file cached for one set of coordinates also answers lookups for coordinates
65 * that differ only in case or Unicode normalization, while the repository tracking data is compared exactly:
66 * such an aliased file is treated as present-but-untracked and accepted with no download and no checksum
67 * verification, letting case-colliding coordinates poison distinct GAVs. When enabled (the default), an
68 * artifact whose on-disk path spelling differs from the requested one is treated as not present, forcing a
69 * proper download. Only the coordinate-derived part of the path is verified, so the base directory and the
70 * local path prefix directories (see {@link DefaultLocalPathPrefixComposerFactory}) may be symbolic links.
71 * Disable only if the local repository intentionally contains symbolic links below those directories.
72 *
73 * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
74 * @configurationType {@link java.lang.Boolean}
75 * @configurationDefaultValue {@link #DEFAULT_VERIFY_REAL_PATH}
76 * @since 2.0.23
77 */
78 public static final String CONFIG_PROP_VERIFY_REAL_PATH = CONFIG_PROPS_PREFIX + "verifyRealPath";
79
80 public static final boolean DEFAULT_VERIFY_REAL_PATH = true;
81
82 /**
83 * Marks whether the local repository is meant to be shared (or was shared) with legacy Maven 3.9 or older
84 * versions. Maven 3.9 and older versions suffer from "impostor" problem, where artifact and metadata origin was
85 * tracked only by the remote repository ID, where two remote repositories may share same ID but different URLs,
86 * in fact they may be completely unrelated to each other (ID clash by mistake), or, it may be due some sort of
87 * "impostor" attempt, where a malicious repository may pretend like some other repository.
88 * Right now, we intentionally default to {@code true} to ease users transitioning, and Resolver 2 will retain
89 * this "old" behavior (will observe legacy tracking entries and will store remote metadata as before). But,
90 * at some point in the future, the default value will be changed to {@code false} (and same change is warmly
91 * recommended for modern Maven users, who do not intend to share local repository with older Maven versions).
92 * When this configuration set to {@code false}, the "repository key" is not ID only anymore, but is changed
93 * to {@code $id-sha1($url)} form, and this key is used in "origin tracking" entries and in caching remote
94 * Maven Repository Metadata XML files as well, guaranteeing they are not mixed in case of same IDs.
95 *
96 * @see ConfigurationProperties#REPOSITORY_SYSTEM_REPOSITORY_KEY_FUNCTION
97 * @see ConfigurationProperties#REPOSITORY_TRACKING_REPOSITORY_KEY_FUNCTION
98 * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
99 * @configurationType {@link java.lang.Boolean}
100 * @configurationDefaultValue {@link #DEFAULT_LEGACY_LOCAL_REPOSITORY}
101 * @since 2.0.23
102 */
103 public static final String CONFIG_PROP_LEGACY_LOCAL_REPOSITORY = CONFIG_PROPS_PREFIX + "legacyLocalRepository";
104
105 public static final boolean DEFAULT_LEGACY_LOCAL_REPOSITORY = true;
106
107 // Legacy support: the property was renamed in Resolver 2.0.x, but we should support the 1.9.x property as well,
108 // like LocalPathPrefixComposerFactorySupport does for its keys. This below is the Resolver 1.9.x property, is
109 // undocumented and shall be removed with Resolver 2.1.x (or later).
110
111 private static final String R1_CONF_PROP_TRACKING_FILENAME = "aether.enhancedLocalRepository.trackingFilename";
112
113 private float priority = 10.0f;
114
115 private final LocalPathComposer localPathComposer;
116
117 private final TrackingFileManager trackingFileManager;
118
119 private final LocalPathPrefixComposerFactory localPathPrefixComposerFactory;
120
121 private final RepositoryKeyFunctionFactory repositoryKeyFunctionFactory;
122
123 @Inject
124 public EnhancedLocalRepositoryManagerFactory(
125 final LocalPathComposer localPathComposer,
126 final TrackingFileManager trackingFileManager,
127 final LocalPathPrefixComposerFactory localPathPrefixComposerFactory,
128 final RepositoryKeyFunctionFactory repositoryKeyFunctionFactory) {
129 this.localPathComposer = requireNonNull(localPathComposer);
130 this.trackingFileManager = requireNonNull(trackingFileManager);
131 this.localPathPrefixComposerFactory = requireNonNull(localPathPrefixComposerFactory);
132 this.repositoryKeyFunctionFactory = requireNonNull(repositoryKeyFunctionFactory);
133 }
134
135 @Override
136 public LocalRepositoryManager newInstance(RepositorySystemSession session, LocalRepository repository)
137 throws NoLocalRepositoryManagerException {
138 requireNonNull(session, "session cannot be null");
139 requireNonNull(repository, "repository cannot be null");
140
141 String trackingFilename =
142 ConfigUtils.getString(session, "", CONFIG_PROP_TRACKING_FILENAME, R1_CONF_PROP_TRACKING_FILENAME);
143 if (trackingFilename.isEmpty()
144 || trackingFilename.contains("/")
145 || trackingFilename.contains("\\")
146 || trackingFilename.contains("..")) {
147 trackingFilename = DEFAULT_TRACKING_FILENAME;
148 }
149 boolean legacyLocalRepository =
150 ConfigUtils.getBoolean(session, DEFAULT_LEGACY_LOCAL_REPOSITORY, CONFIG_PROP_LEGACY_LOCAL_REPOSITORY);
151
152 if ("".equals(repository.getContentType()) || "default".equals(repository.getContentType())) {
153 return new EnhancedLocalRepositoryManager(
154 repository.getBasePath(),
155 localPathComposer,
156 repositoryKeyFunctionFactory.trackingRepositoryKeyFunction(session),
157 trackingFilename,
158 legacyLocalRepository,
159 trackingFileManager,
160 localPathPrefixComposerFactory.createComposer(session));
161 } else {
162 throw new NoLocalRepositoryManagerException(repository);
163 }
164 }
165
166 @Override
167 public float getPriority() {
168 return priority;
169 }
170
171 /**
172 * Sets the priority of this component.
173 *
174 * @param priority The priority.
175 * @return This component for chaining, never {@code null}.
176 */
177 public EnhancedLocalRepositoryManagerFactory setPriority(float priority) {
178 this.priority = priority;
179 return this;
180 }
181 }