001package org.apache.maven.settings.crypto;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.ArrayList;
023import java.util.List;
024
025import org.apache.maven.settings.Proxy;
026import org.apache.maven.settings.Server;
027import org.apache.maven.settings.building.DefaultSettingsProblem;
028import org.apache.maven.settings.building.SettingsProblem;
029import org.apache.maven.settings.building.SettingsProblem.Severity;
030import org.codehaus.plexus.component.annotations.Component;
031import org.codehaus.plexus.component.annotations.Requirement;
032import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
033import org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException;
034
035/**
036 * Decrypts passwords in the settings.
037 *
038 * @author Benjamin Bentmann
039 */
040@Component( role = SettingsDecrypter.class )
041public class DefaultSettingsDecrypter
042    implements SettingsDecrypter
043{
044
045    @Requirement( hint = "maven" )
046    private SecDispatcher securityDispatcher;
047
048    @Override
049    public SettingsDecryptionResult decrypt( SettingsDecryptionRequest request )
050    {
051        List<SettingsProblem> problems = new ArrayList<>();
052
053        List<Server> servers = new ArrayList<>();
054
055        for ( Server server : request.getServers() )
056        {
057            server = server.clone();
058
059            servers.add( server );
060
061            try
062            {
063                server.setPassword( decrypt( server.getPassword() ) );
064            }
065            catch ( SecDispatcherException e )
066            {
067                problems.add( new DefaultSettingsProblem( "Failed to decrypt password for server " + server.getId()
068                    + ": " + e.getMessage(), Severity.ERROR, "server: " + server.getId(), -1, -1, e ) );
069            }
070
071            try
072            {
073                server.setPassphrase( decrypt( server.getPassphrase() ) );
074            }
075            catch ( SecDispatcherException e )
076            {
077                problems.add( new DefaultSettingsProblem( "Failed to decrypt passphrase for server " + server.getId()
078                    + ": " + e.getMessage(), Severity.ERROR, "server: " + server.getId(), -1, -1, e ) );
079            }
080        }
081
082        List<Proxy> proxies = new ArrayList<>();
083
084        for ( Proxy proxy : request.getProxies() )
085        {
086            proxy = proxy.clone();
087
088            proxies.add( proxy );
089
090            try
091            {
092                proxy.setPassword( decrypt( proxy.getPassword() ) );
093            }
094            catch ( SecDispatcherException e )
095            {
096                problems.add( new DefaultSettingsProblem( "Failed to decrypt password for proxy " + proxy.getId()
097                    + ": " + e.getMessage(), Severity.ERROR, "proxy: " + proxy.getId(), -1, -1, e ) );
098            }
099        }
100
101        return new DefaultSettingsDecryptionResult( servers, proxies, problems );
102    }
103
104    private String decrypt( String str )
105        throws SecDispatcherException
106    {
107        return ( str == null ) ? null : securityDispatcher.decrypt( str );
108    }
109
110}