1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.shared.release.scm;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.util.concurrent.atomic.AtomicReference;
26
27 import org.apache.maven.scm.manager.NoSuchScmProviderException;
28 import org.apache.maven.scm.manager.ScmManager;
29 import org.apache.maven.scm.provider.ScmProvider;
30 import org.apache.maven.scm.provider.ScmProviderRepository;
31 import org.apache.maven.scm.provider.ScmProviderRepositoryWithHost;
32 import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
33 import org.apache.maven.scm.repository.ScmRepository;
34 import org.apache.maven.scm.repository.ScmRepositoryException;
35 import org.apache.maven.settings.Server;
36 import org.apache.maven.settings.Settings;
37 import org.apache.maven.shared.release.config.ReleaseDescriptor;
38 import org.apache.maven.shared.release.util.MavenCrypto;
39 import org.apache.maven.shared.release.util.MavenCrypto.MavenCryptoException;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 import static java.util.Objects.requireNonNull;
44
45
46
47
48
49
50 @Singleton
51 @Named
52 public class DefaultScmRepositoryConfigurator implements ScmRepositoryConfigurator {
53 private final Logger logger = LoggerFactory.getLogger(getClass());
54
55 private final AtomicReference<ScmManager> scmManager;
56
57 private final MavenCrypto mavenCrypto;
58
59 @Inject
60 public DefaultScmRepositoryConfigurator(ScmManager scmManager, MavenCrypto mavenCrypto) {
61 this.scmManager = new AtomicReference<>(requireNonNull(scmManager));
62 this.mavenCrypto = requireNonNull(mavenCrypto);
63 }
64
65
66
67
68 public void setScmManager(ScmManager scmManager) {
69 this.scmManager.set(scmManager);
70 }
71
72 @Override
73 public ScmRepository getConfiguredRepository(ReleaseDescriptor releaseDescriptor, Settings settings)
74 throws ScmRepositoryException, NoSuchScmProviderException {
75 String url = releaseDescriptor.getScmSourceUrl();
76 return getConfiguredRepository(url, releaseDescriptor, settings);
77 }
78
79 @Override
80 public ScmRepository getConfiguredRepository(String url, ReleaseDescriptor releaseDescriptor, Settings settings)
81 throws ScmRepositoryException, NoSuchScmProviderException {
82 String username = releaseDescriptor.getScmUsername();
83 String password = releaseDescriptor.getScmPassword();
84 String privateKey = releaseDescriptor.getScmPrivateKey();
85 String passphrase = releaseDescriptor.getScmPrivateKeyPassPhrase();
86
87 ScmRepository repository = scmManager.get().makeScmRepository(url);
88
89 ScmProviderRepository scmRepo = repository.getProviderRepository();
90
91
92 scmRepo.setPersistCheckout(false);
93
94 if (settings != null) {
95 Server server = null;
96
97 if (releaseDescriptor.getScmId() != null) {
98 server = settings.getServer(releaseDescriptor.getScmId());
99 if (server == null) {
100 logger.warn("No server with id '{}' found in Maven settings", releaseDescriptor.getScmId());
101 }
102 }
103
104 if (server == null && repository.getProviderRepository() instanceof ScmProviderRepositoryWithHost) {
105 ScmProviderRepositoryWithHost repositoryWithHost =
106 (ScmProviderRepositoryWithHost) repository.getProviderRepository();
107 String host = repositoryWithHost.getHost();
108
109 int port = repositoryWithHost.getPort();
110
111 if (port > 0) {
112 host += ":" + port;
113 }
114
115
116
117 server = settings.getServer(host);
118 }
119
120 if (server != null) {
121 if (username == null && server.getUsername() != null) {
122 logger.debug(
123 "Using username from server id '{}' found in Maven settings", releaseDescriptor.getScmId());
124 username = server.getUsername();
125 }
126
127 if (password == null && server.getPassword() != null) {
128 logger.debug(
129 "Using password from server id '{}' found in Maven settings", releaseDescriptor.getScmId());
130 password = decrypt(server.getPassword(), server.getId());
131 }
132
133 if (privateKey == null && server.getPrivateKey() != null) {
134 logger.debug(
135 "Using private key from server id '{}' found in Maven settings",
136 releaseDescriptor.getScmId());
137 privateKey = server.getPrivateKey();
138 }
139
140 if (passphrase == null && server.getPassphrase() != null) {
141 logger.debug(
142 "Using passphrase from server id '{}' found in Maven settings",
143 releaseDescriptor.getScmId());
144 passphrase = decrypt(server.getPassphrase(), server.getId());
145 }
146 }
147 }
148
149 if (!(username == null || username.isEmpty())) {
150 scmRepo.setUser(username);
151 } else {
152 logger.debug("No explicit username configured");
153 }
154 if (!(password == null || password.isEmpty())) {
155 scmRepo.setPassword(password);
156 } else {
157 logger.debug("No explicit password configured");
158 }
159
160 if (scmRepo instanceof ScmProviderRepositoryWithHost) {
161 ScmProviderRepositoryWithHost repositoryWithHost = (ScmProviderRepositoryWithHost) scmRepo;
162 if (!(privateKey == null || privateKey.isEmpty())) {
163 repositoryWithHost.setPrivateKey(privateKey);
164 } else {
165 logger.debug("No explicit private key configured");
166 }
167
168 if (!(passphrase == null || passphrase.isEmpty())) {
169 repositoryWithHost.setPassphrase(passphrase);
170 } else {
171 logger.debug("No explicit passphrase configured");
172 }
173 }
174
175 if ("svn".equals(repository.getProvider())) {
176 SvnScmProviderRepository svnRepo = (SvnScmProviderRepository) repository.getProviderRepository();
177
178 String tagBase = releaseDescriptor.getScmTagBase();
179 if (!(tagBase == null || tagBase.isEmpty())) {
180 svnRepo.setTagBase(tagBase);
181 }
182
183 String branchBase = releaseDescriptor.getScmBranchBase();
184 if (!(branchBase == null || branchBase.isEmpty())) {
185 svnRepo.setBranchBase(branchBase);
186 }
187 }
188 if (!releaseDescriptor.isInteractive()) {
189 scmManager.get().getProviderByRepository(repository).setInteractive(releaseDescriptor.isInteractive());
190 }
191 return repository;
192 }
193
194 private String decrypt(String str, String serverId) {
195 try {
196 return mavenCrypto.decrypt(str);
197 } catch (MavenCryptoException e) {
198 String msg = "Failed to decrypt password/passphrase for server with id '" + serverId
199 + "', using auth token as is: " + e.getMessage();
200 if (logger.isDebugEnabled()) {
201 logger.warn(msg, e);
202 } else {
203 logger.warn(msg);
204 }
205 return str;
206 }
207 }
208
209 @Override
210 public ScmProvider getRepositoryProvider(ScmRepository repository) throws NoSuchScmProviderException {
211 return scmManager.get().getProviderByRepository(repository);
212 }
213 }