|
| 1 | +/* |
| 2 | + * Copyright 2021 - 2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.sbm.project.parser; |
| 17 | + |
| 18 | +import lombok.RequiredArgsConstructor; |
| 19 | +import org.apache.maven.settings.Server; |
| 20 | +import org.apache.maven.settings.crypto.*; |
| 21 | +import org.openrewrite.maven.MavenSettings; |
| 22 | +import org.sonatype.plexus.components.cipher.DefaultPlexusCipher; |
| 23 | +import org.sonatype.plexus.components.cipher.PlexusCipherException; |
| 24 | +import org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher; |
| 25 | +import org.springframework.sbm.testhelper.ResourceUtil; |
| 26 | +import org.springframework.stereotype.Component; |
| 27 | +import org.springframework.util.ReflectionUtils; |
| 28 | + |
| 29 | +import java.lang.reflect.Field; |
| 30 | +import java.nio.file.Path; |
| 31 | +import java.util.List; |
| 32 | + |
| 33 | +/** |
| 34 | + * Decrypt Maven Server passwords. |
| 35 | + * |
| 36 | + * Requires access to Maven security settings file (~/.settings-security.xml) to decrypt the server passwords provided in |
| 37 | + * the server settings in Maven settings file (~/.m2/settings.xml) which are provided with {@link MavenSettings}. |
| 38 | + * |
| 39 | + * @author Fabian Krüger |
| 40 | + */ |
| 41 | +@Component |
| 42 | +@RequiredArgsConstructor |
| 43 | +// TODO: should be package private |
| 44 | +public class MavenPasswordDecrypter { |
| 45 | + private DefaultSecDispatcher securityDispatcher = new DefaultSecDispatcher(); |
| 46 | + private final SettingsDecrypter settingsDecrypter = new DefaultSettingsDecrypter(securityDispatcher); |
| 47 | + |
| 48 | + public void decryptMavenServerPasswords(MavenSettings mavenSettings, Path mavenSecuritySettingsFile) { |
| 49 | + try { |
| 50 | + // DefaultSecDispatcher reads file with security settings |
| 51 | + // hack, the cipher is required but can't be set from outside |
| 52 | + Field cipher1 = ReflectionUtils.findField(DefaultSecDispatcher.class, "_cipher"); |
| 53 | + //Field cipher = ReflectionUtils.getField(cipher1, securityDispatcher); |
| 54 | + ReflectionUtils.makeAccessible(cipher1); |
| 55 | + ReflectionUtils.setField(cipher1, securityDispatcher, new DefaultPlexusCipher()); |
| 56 | + // The file location is retrieved from env, default is ~/${user.home}/.settings-security.xml |
| 57 | + System.setProperty("settings.security", mavenSecuritySettingsFile.toString()); |
| 58 | + decryptMavenServerPasswords(mavenSettings); |
| 59 | + } catch (PlexusCipherException e) { |
| 60 | + throw new RuntimeException(e); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + private void decryptMavenServerPasswords(MavenSettings mavenSettings) { |
| 66 | + if(mavenSettings.getServers() != null && mavenSettings.getServers().getServers() != null) { |
| 67 | + List<MavenSettings.Server> servers = mavenSettings.getServers().getServers(); |
| 68 | + for(int i = 0; i< servers.size(); i++){ |
| 69 | + MavenSettings.Server server = servers.get(i); |
| 70 | + if(server.getPassword() != null) { |
| 71 | + MavenSettings.Server serverWithDecodedPw = decryptPassword(server); |
| 72 | + servers.set(i, serverWithDecodedPw); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private MavenSettings.Server decryptPassword(MavenSettings.Server server) { |
| 79 | + Server mavenServer = new Server(); |
| 80 | + mavenServer.setPassword(server.getPassword()); |
| 81 | + SettingsDecryptionRequest decryptionRequest = new DefaultSettingsDecryptionRequest(mavenServer); |
| 82 | + SettingsDecryptionResult decryptionResult = settingsDecrypter.decrypt(decryptionRequest); |
| 83 | + String decryptedPassword = decryptionResult.getServer().getPassword(); |
| 84 | + return server.withPassword(decryptedPassword); |
| 85 | + } |
| 86 | +} |
0 commit comments