001    package 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    
022    import java.util.ArrayList;
023    import java.util.List;
024    
025    import org.apache.maven.settings.Proxy;
026    import org.apache.maven.settings.Server;
027    import org.apache.maven.settings.building.DefaultSettingsProblem;
028    import org.apache.maven.settings.building.SettingsProblem;
029    import org.apache.maven.settings.building.SettingsProblem.Severity;
030    import org.codehaus.plexus.component.annotations.Component;
031    import org.codehaus.plexus.component.annotations.Requirement;
032    import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
033    import 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 )
041    public class DefaultSettingsDecrypter
042        implements SettingsDecrypter
043    {
044    
045        @Requirement( hint = "maven" )
046        private SecDispatcher securityDispatcher;
047    
048        public SettingsDecryptionResult decrypt( SettingsDecryptionRequest request )
049        {
050            List<SettingsProblem> problems = new ArrayList<SettingsProblem>();
051    
052            List<Server> servers = new ArrayList<Server>();
053    
054            for ( Server server : request.getServers() )
055            {
056                server = server.clone();
057    
058                servers.add( server );
059    
060                try
061                {
062                    server.setPassword( decrypt( server.getPassword() ) );
063                }
064                catch ( SecDispatcherException e )
065                {
066                    problems.add( new DefaultSettingsProblem( "Failed to decrypt password for server " + server.getId()
067                        + ": " + e.getMessage(), Severity.ERROR, "server: " + server.getId(), -1, -1, e ) );
068                }
069    
070                try
071                {
072                    server.setPassphrase( decrypt( server.getPassphrase() ) );
073                }
074                catch ( SecDispatcherException e )
075                {
076                    problems.add( new DefaultSettingsProblem( "Failed to decrypt passphrase for server " + server.getId()
077                        + ": " + e.getMessage(), Severity.ERROR, "server: " + server.getId(), -1, -1, e ) );
078                }
079            }
080    
081            List<Proxy> proxies = new ArrayList<Proxy>();
082    
083            for ( Proxy proxy : request.getProxies() )
084            {
085                proxy = proxy.clone();
086    
087                proxies.add( proxy );
088    
089                try
090                {
091                    proxy.setPassword( decrypt( proxy.getPassword() ) );
092                }
093                catch ( SecDispatcherException e )
094                {
095                    problems.add( new DefaultSettingsProblem( "Failed to decrypt password for proxy " + proxy.getId()
096                        + ": " + e.getMessage(), Severity.ERROR, "proxy: " + proxy.getId(), -1, -1, e ) );
097                }
098            }
099    
100            return new DefaultSettingsDecryptionResult( servers, proxies, problems );
101        }
102    
103        private String decrypt( String str )
104            throws SecDispatcherException
105        {
106            return ( str == null ) ? null : securityDispatcher.decrypt( str );
107        }
108    
109    }