diff --git a/pom.xml b/pom.xml
index 24f3e09..6403900 100644
--- a/pom.xml
+++ b/pom.xml
@@ -10,7 +10,7 @@
plexus-sec-dispatcher
- 1.5-SNAPSHOT
+ 2.0-SNAPSHOT
Plexus Security Dispatcher Component
@@ -33,14 +33,46 @@
7
+ 0.3.4
+
+
+ org.codehaus.plexus
+ plexus-utils
+ 3.4.1
+
+
+ org.codehaus.plexus
+ plexus-cipher
+ 2.0
+
+
+
+ javax.inject
+ javax.inject
+ 1
+
+
+ org.eclipse.sisu
+ org.eclipse.sisu.inject
+ ${sisuVersion}
+ provided
+
+
+ junit
+ junit
+ 4.13.2
+ test
+
+
+
org.eclipse.sisu
sisu-maven-plugin
- 0.3.4
+ ${sisuVersion}
@@ -59,7 +91,7 @@
src/main/mdo/settings-security.mdo
- false
+ true
@@ -74,28 +106,4 @@
-
-
-
- org.codehaus.plexus
- plexus-utils
- 3.4.1
-
-
- org.codehaus.plexus
- plexus-cipher
- 1.8
-
-
- org.codehaus.plexus
- plexus-container-default
- 1.0-alpha-9-stable-1
- provided
-
-
- junit
- junit
- 4.13.2
-
-
diff --git a/src/main/java/org/sonatype/plexus/components/sec/dispatcher/DefaultSecDispatcher.java b/src/main/java/org/sonatype/plexus/components/sec/dispatcher/DefaultSecDispatcher.java
index 7ee0486..d62f301 100644
--- a/src/main/java/org/sonatype/plexus/components/sec/dispatcher/DefaultSecDispatcher.java
+++ b/src/main/java/org/sonatype/plexus/components/sec/dispatcher/DefaultSecDispatcher.java
@@ -1,4 +1,4 @@
-/**
+/*
* Copyright (c) 2008 Sonatype, Inc. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
@@ -15,26 +15,30 @@
import java.io.BufferedReader;
-import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
-import org.codehaus.plexus.logging.AbstractLogEnabled;
+import javax.inject.Inject;
+import javax.inject.Named;
+import javax.inject.Singleton;
+
import org.sonatype.plexus.components.cipher.DefaultPlexusCipher;
import org.sonatype.plexus.components.cipher.PlexusCipher;
import org.sonatype.plexus.components.cipher.PlexusCipherException;
import org.sonatype.plexus.components.sec.dispatcher.model.SettingsSecurity;
/**
- * @plexus.component role-hint="default"
* @author Oleg Gusakov
*/
+@Singleton
+@Named
public class DefaultSecDispatcher
-extends AbstractLogEnabled
-implements SecDispatcher
+ implements SecDispatcher
{
+ private static final String DEFAULT_CONFIGURATION = "~/.settings-security.xml";
+
public static final String SYSTEM_PROPERTY_SEC_LOCATION = "settings.security";
public static final String TYPE_ATTR = "type";
@@ -45,32 +49,47 @@ public class DefaultSecDispatcher
/**
* DefaultHandler
- *
- * @plexus.requirement
*/
- protected PlexusCipher _cipher;
+ protected final PlexusCipher _cipher;
/**
* All available dispatchers
- *
- * @plexus.requirement role="org.sonatype.plexus.components.sec.dispatcher.PasswordDecryptor"
*/
- protected Map _decryptors;
+ protected final Map _decryptors;
/**
- *
- * @plexus.configuration default-value="~/.settings-security.xml"
+ * Configuration file
*/
- protected String _configurationFile = "~/.settings-security.xml";
+ protected String _configurationFile;
+
+ @Inject
+ public DefaultSecDispatcher( final PlexusCipher _cipher,
+ final Map _decryptors,
+ @Named( "${_configurationFile:-" + DEFAULT_CONFIGURATION + "}" )
+ final String _configurationFile )
+ {
+ this._cipher = _cipher;
+ this._decryptors = _decryptors;
+ this._configurationFile = _configurationFile;
+ }
+
+ /**
+ * Ctor to be used in tests and other simplified cases (no decryptors and config).
+ */
+ public DefaultSecDispatcher( final PlexusCipher _cipher ) {
+ this( _cipher, new HashMap(), DEFAULT_CONFIGURATION );
+ }
// ---------------------------------------------------------------
+
+ @Override
public String decrypt( String str )
throws SecDispatcherException
{
if( ! isEncryptedString( str ) )
return str;
- String bare = null;
+ String bare;
try
{
@@ -83,9 +102,9 @@ public String decrypt( String str )
try
{
- Map attr = stripAttributes( bare );
+ Map attr = stripAttributes( bare );
- String res = null;
+ String res;
SettingsSecurity sec = getSec();
@@ -97,14 +116,14 @@ public String decrypt( String str )
}
else
{
- String type = (String) attr.get( TYPE_ATTR );
+ String type = attr.get( TYPE_ATTR );
if( _decryptors == null )
throw new SecDispatcherException( "plexus container did not supply any required dispatchers - cannot lookup "+type );
- Map conf = SecUtil.getConfig( sec, type );
+ Map conf = SecUtil.getConfig( sec, type );
- PasswordDecryptor dispatcher = (PasswordDecryptor) _decryptors.get( type );
+ PasswordDecryptor dispatcher = _decryptors.get( type );
if( dispatcher == null )
throw new SecDispatcherException( "no dispatcher for hint "+type );
@@ -135,7 +154,7 @@ private String strip( String str )
return str;
}
- private Map stripAttributes( String str )
+ private Map stripAttributes( String str )
{
int start = str.indexOf( ATTR_START );
int stop = str.indexOf( ATTR_STOP );
@@ -146,17 +165,17 @@ private Map stripAttributes( String str )
String attrs = str.substring( start+1, stop ).trim();
- if( attrs == null || attrs.length() < 1 )
+ if( attrs.length() < 1 )
return null;
- Map res = null;
+ Map res = null;
StringTokenizer st = new StringTokenizer( attrs, ", " );
while( st.hasMoreTokens() )
{
if( res == null )
- res = new HashMap( st.countTokens() );
+ res = new HashMap<>( st.countTokens() );
String pair = st.nextToken();
@@ -183,7 +202,9 @@ private Map stripAttributes( String str )
return null;
}
+
//----------------------------------------------------------------------------
+
private boolean isEncryptedString( String str )
{
if( str == null )
@@ -191,7 +212,9 @@ private boolean isEncryptedString( String str )
return _cipher.isEncryptedString( str );
}
+
//----------------------------------------------------------------------------
+
private SettingsSecurity getSec()
throws SecDispatcherException
{
@@ -210,7 +233,9 @@ private SettingsSecurity getSec()
return sec;
}
+
//----------------------------------------------------------------------------
+
private String getMaster( SettingsSecurity sec )
throws SecDispatcherException
{
@@ -238,43 +263,40 @@ public void setConfigurationFile( String file )
{
_configurationFile = file;
}
- //----------------------------------------------------------------------------
- // ***************************************************************
- /**
- * Encryption helper
- * @throws IOException
- */
//---------------------------------------------------------------
+
private static boolean propertyExists( String [] values, String [] av )
{
if( values != null )
{
- for( int i=0; i< values.length; i++ )
- {
- String p = System.getProperty( values[i] );
-
- if( p != null )
+ for ( String item : values ) {
+ String p = System.getProperty( item );
+
+ if ( p != null ) {
return true;
+ }
}
if( av != null )
- for( int i=0; i< values.length; i++ )
- for( int j=0; j< av.length; j++ )
- {
- if( ("--"+values[i]).equals( av[j] ) )
+ for ( String value : values )
+ for ( String s : av ) {
+ if ( ( "--" + value ).equals( s ) ) {
return true;
+ }
}
}
return false;
}
- private static final void usage()
+ private static void usage()
{
- System.out.println("usage: java -jar ...jar [-m|-p]\n-m: encrypt master password\n-p: encrypt password");
+ System.out.println( "usage: java -jar ...jar [-m|-p]\n-m: encrypt master password\n-p: encrypt password" );
}
+
//---------------------------------------------------------------
+
public static void main( String[] args )
throws Exception
{
@@ -291,7 +313,9 @@ else if( "-p".equals( args[0] ) || propertyExists( SYSTEM_PROPERTY_SERVER_PASSWO
else
usage();
}
+
//---------------------------------------------------------------
+
private static void show( boolean showMaster )
throws Exception
{
@@ -309,9 +333,8 @@ private static void show( boolean showMaster )
System.out.println("\n");
DefaultPlexusCipher dc = new DefaultPlexusCipher();
- DefaultSecDispatcher dd = new DefaultSecDispatcher();
- dd._cipher = dc;
-
+ DefaultSecDispatcher dd = new DefaultSecDispatcher( dc );
+
if( showMaster )
System.out.println( dc.encryptAndDecorate( pass, DefaultSecDispatcher.SYSTEM_PROPERTY_SEC_LOCATION ) );
else
@@ -320,6 +343,4 @@ private static void show( boolean showMaster )
System.out.println( dc.encryptAndDecorate( pass, dd.getMaster(sec) ) );
}
}
- //---------------------------------------------------------------
- //---------------------------------------------------------------
}
diff --git a/src/main/java/org/sonatype/plexus/components/sec/dispatcher/PasswordDecryptor.java b/src/main/java/org/sonatype/plexus/components/sec/dispatcher/PasswordDecryptor.java
index 705707f..4491d7f 100644
--- a/src/main/java/org/sonatype/plexus/components/sec/dispatcher/PasswordDecryptor.java
+++ b/src/main/java/org/sonatype/plexus/components/sec/dispatcher/PasswordDecryptor.java
@@ -1,4 +1,4 @@
-/**
+/*
* Copyright (c) 2008 Sonatype, Inc. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
@@ -24,8 +24,6 @@
*/
public interface PasswordDecryptor
{
- public static String ROLE = PasswordDecryptor.class.getName();
-
/**
* decrypt given encrypted string
*
@@ -36,6 +34,5 @@ public interface PasswordDecryptor
*
* @throws SecDispatcherException
*/
- String decrypt( String str, Map attributes, Map config )
- throws SecDispatcherException;
+ String decrypt( String str, Map attributes, Map config ) throws SecDispatcherException;
}
diff --git a/src/main/java/org/sonatype/plexus/components/sec/dispatcher/PasswordDecryptorException.java b/src/main/java/org/sonatype/plexus/components/sec/dispatcher/PasswordDecryptorException.java
deleted file mode 100644
index 3b364f2..0000000
--- a/src/main/java/org/sonatype/plexus/components/sec/dispatcher/PasswordDecryptorException.java
+++ /dev/null
@@ -1,63 +0,0 @@
-/**
- * Copyright (c) 2008 Sonatype, Inc. All rights reserved.
- *
- * This program is licensed to you under the Apache License Version 2.0,
- * and you may not use this file except in compliance with the Apache License Version 2.0.
- * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the Apache License Version 2.0 is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
- */
-
-package org.sonatype.plexus.components.sec.dispatcher;
-
-/**
- *
- *
- * @author Oleg Gusakov
- * @version $Id$
- *
- */
-public class PasswordDecryptorException
- extends Exception
-{
-
- /**
- *
- */
- public PasswordDecryptorException()
- {
- // TODO Auto-generated constructor stub
- }
-
- /**
- * @param message
- */
- public PasswordDecryptorException( String message )
- {
- super( message );
- // TODO Auto-generated constructor stub
- }
-
- /**
- * @param cause
- */
- public PasswordDecryptorException( Throwable cause )
- {
- super( cause );
- // TODO Auto-generated constructor stub
- }
-
- /**
- * @param message
- * @param cause
- */
- public PasswordDecryptorException( String message, Throwable cause )
- {
- super( message, cause );
- // TODO Auto-generated constructor stub
- }
-
-}
diff --git a/src/main/java/org/sonatype/plexus/components/sec/dispatcher/SecDispatcher.java b/src/main/java/org/sonatype/plexus/components/sec/dispatcher/SecDispatcher.java
index c61f56d..9e5d3ca 100644
--- a/src/main/java/org/sonatype/plexus/components/sec/dispatcher/SecDispatcher.java
+++ b/src/main/java/org/sonatype/plexus/components/sec/dispatcher/SecDispatcher.java
@@ -1,4 +1,4 @@
-/**
+/*
* Copyright (c) 2008 Sonatype, Inc. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
@@ -13,7 +13,6 @@
package org.sonatype.plexus.components.sec.dispatcher;
-
/**
* This component decrypts a string, passed to it
*
@@ -21,11 +20,9 @@
*/
public interface SecDispatcher
{
- public static String ROLE = SecDispatcher.class.getName();
-
- public static final String [] SYSTEM_PROPERTY_MASTER_PASSWORD = new String [] {"settings.master.password","settings-master-password"};
+ String [] SYSTEM_PROPERTY_MASTER_PASSWORD = new String [] {"settings.master.password","settings-master-password"};
- public static final String [] SYSTEM_PROPERTY_SERVER_PASSWORD = new String [] {"settings.server.password","settings-server-password"};
+ String [] SYSTEM_PROPERTY_SERVER_PASSWORD = new String [] {"settings.server.password","settings-server-password"};
/**
* decrypt given encrypted string
@@ -34,6 +31,5 @@ public interface SecDispatcher
* @return decrypted string
* @throws SecDispatcherException
*/
- String decrypt( String str )
- throws SecDispatcherException;
+ String decrypt( String str ) throws SecDispatcherException;
}
diff --git a/src/main/java/org/sonatype/plexus/components/sec/dispatcher/SecDispatcherException.java b/src/main/java/org/sonatype/plexus/components/sec/dispatcher/SecDispatcherException.java
index 5dde1f4..1ed35bf 100644
--- a/src/main/java/org/sonatype/plexus/components/sec/dispatcher/SecDispatcherException.java
+++ b/src/main/java/org/sonatype/plexus/components/sec/dispatcher/SecDispatcherException.java
@@ -1,4 +1,4 @@
-/**
+/*
* Copyright (c) 2008 Sonatype, Inc. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
@@ -16,11 +16,6 @@
public class SecDispatcherException
extends Exception
{
-
- public SecDispatcherException()
- {
- }
-
public SecDispatcherException( String message )
{
super( message );
@@ -35,5 +30,4 @@ public SecDispatcherException( String message, Throwable cause )
{
super( message, cause );
}
-
}
diff --git a/src/main/java/org/sonatype/plexus/components/sec/dispatcher/SecUtil.java b/src/main/java/org/sonatype/plexus/components/sec/dispatcher/SecUtil.java
index 46d2534..d698048 100644
--- a/src/main/java/org/sonatype/plexus/components/sec/dispatcher/SecUtil.java
+++ b/src/main/java/org/sonatype/plexus/components/sec/dispatcher/SecUtil.java
@@ -1,4 +1,4 @@
-/**
+/*
* Copyright (c) 2008 Sonatype, Inc. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
@@ -13,14 +13,11 @@
package org.sonatype.plexus.components.sec.dispatcher;
-import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
-import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -76,7 +73,7 @@ public static SettingsSecurity read( String location, boolean cycle )
}
//---------------------------------------------------------------------------------------------------------------
private static InputStream toStream( String resource )
- throws MalformedURLException, IOException
+ throws IOException
{
if( resource == null )
return null;
@@ -88,53 +85,46 @@ private static InputStream toStream( String resource )
String protocol = resource.substring( 0, ind );
resource = resource.substring( ind + PROTOCOL_DELIM_LEN );
- for( int i=0; i getConfig( SettingsSecurity sec, String name )
{
if( name == null )
return null;
- List cl = sec.getConfigurations();
+ List cl = sec.getConfigurations();
- if( cl == null )
+ if( cl == null || cl.isEmpty() )
return null;
-
- for( Iterator i = cl.iterator(); i.hasNext(); )
- {
- Config cf = (Config) i.next();
-
- if( !name.equals( cf.getName() ) )
+
+ for ( Config cf : cl ) {
+ if ( !name.equals( cf.getName() ) ) {
continue;
-
- List pl = cf.getProperties();
-
- if( pl == null || pl.isEmpty() )
+ }
+
+ List pl = cf.getProperties();
+
+ if ( pl == null || pl.isEmpty() ) {
return null;
-
- Map res = new HashMap( pl.size() );
+ }
- for( Iterator j = pl.iterator(); j.hasNext(); )
- {
- ConfigProperty p = (ConfigProperty) j.next();
-
+ Map res = new HashMap<>( pl.size() );
+
+ for ( ConfigProperty p : pl ) {
res.put( p.getName(), p.getValue() );
}
-
+
return res;
}
return null;
}
- //---------------------------------------------------------------------------------------------------------------
}
diff --git a/src/test/java/org/sonatype/plexus/components/sec/dispatcher/SecUtilTest.java b/src/test/java/org/sonatype/plexus/components/sec/dispatcher/SecUtilTest.java
index dd5ae69..5a83ab8 100644
--- a/src/test/java/org/sonatype/plexus/components/sec/dispatcher/SecUtilTest.java
+++ b/src/test/java/org/sonatype/plexus/components/sec/dispatcher/SecUtilTest.java
@@ -1,4 +1,4 @@
-/**
+/*
* Copyright (c) 2008 Sonatype, Inc. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
@@ -18,7 +18,8 @@
import java.io.FileWriter;
import java.util.Map;
-import junit.framework.TestCase;
+import org.junit.Before;
+import org.junit.Test;
import org.sonatype.plexus.components.cipher.DefaultPlexusCipher;
import org.sonatype.plexus.components.sec.dispatcher.model.Config;
@@ -26,6 +27,9 @@
import org.sonatype.plexus.components.sec.dispatcher.model.SettingsSecurity;
import org.sonatype.plexus.components.sec.dispatcher.model.io.xpp3.SecurityConfigurationXpp3Writer;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
/**
*
*
@@ -34,7 +38,6 @@
*
*/
public class SecUtilTest
-extends TestCase
{
String _pw = "{1wQaa6S/o8MH7FnaTNL53XmhT5O0SEGXQi3gC49o6OY=}";
@@ -47,8 +50,9 @@ public class SecUtilTest
String _propName = "pname";
String _propVal = "pval";
-
- protected void setUp()
+
+ @Before
+ public void prepare()
throws Exception
{
System.setProperty( DefaultSecDispatcher.SYSTEM_PROPERTY_SEC_LOCATION, "./target/sec.xml" );
@@ -78,6 +82,7 @@ protected void setUp()
new SecurityConfigurationXpp3Writer().write( new FileWriter("./target/sec1.xml"), sec );
}
+ @Test
public void testRead()
throws Exception
{
@@ -96,12 +101,12 @@ public void testRead()
assertEquals( _propVal, conf.get( _propName ) );
}
+ @Test
public void testDecrypt()
throws Exception
{
- DefaultSecDispatcher sd = new DefaultSecDispatcher();
- sd._cipher = new DefaultPlexusCipher();
-
+ DefaultSecDispatcher sd = new DefaultSecDispatcher(new DefaultPlexusCipher());
+
String pass = sd.decrypt( _encrypted );
assertNotNull( pass );