Skip to content

Update codebase #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 35 additions & 27 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>

<artifactId>plexus-sec-dispatcher</artifactId>
<version>1.5-SNAPSHOT</version>
<version>2.0-SNAPSHOT</version>

<name>Plexus Security Dispatcher Component</name>

Expand All @@ -33,14 +33,46 @@

<properties>
<javaVersion>7</javaVersion>
<sisuVersion>0.3.4</sisuVersion>
</properties>

<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-cipher</artifactId>
<version>2.0</version>
</dependency>

<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>org.eclipse.sisu</groupId>
<artifactId>org.eclipse.sisu.inject</artifactId>
<version>${sisuVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.eclipse.sisu</groupId>
<artifactId>sisu-maven-plugin</artifactId>
<version>0.3.4</version>
<version>${sisuVersion}</version>
<executions>
<execution>
<goals>
Expand All @@ -59,7 +91,7 @@
<models>
<model>src/main/mdo/settings-security.mdo</model>
</models>
<useJava5>false</useJava5>
<useJava5>true</useJava5>
</configuration>
<executions>
<execution>
Expand All @@ -74,28 +106,4 @@
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-utils</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-cipher</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-container-default</artifactId>
<version>1.0-alpha-9-stable-1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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</a>
*/
@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";
Expand All @@ -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<String, PasswordDecryptor> _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<String, PasswordDecryptor> _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<String, PasswordDecryptor>(), DEFAULT_CONFIGURATION );
}

// ---------------------------------------------------------------

@Override
public String decrypt( String str )
throws SecDispatcherException
{
if( ! isEncryptedString( str ) )
return str;

String bare = null;
String bare;

try
{
Expand All @@ -83,9 +102,9 @@ public String decrypt( String str )

try
{
Map attr = stripAttributes( bare );
Map<String, String> attr = stripAttributes( bare );

String res = null;
String res;

SettingsSecurity sec = getSec();

Expand All @@ -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<String, String> 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 );
Expand Down Expand Up @@ -135,7 +154,7 @@ private String strip( String str )
return str;
}

private Map stripAttributes( String str )
private Map<String, String> stripAttributes( String str )
{
int start = str.indexOf( ATTR_START );
int stop = str.indexOf( ATTR_STOP );
Expand All @@ -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<String, String> 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();

Expand All @@ -183,15 +202,19 @@ private Map stripAttributes( String str )

return null;
}

//----------------------------------------------------------------------------

private boolean isEncryptedString( String str )
{
if( str == null )
return false;

return _cipher.isEncryptedString( str );
}

//----------------------------------------------------------------------------

private SettingsSecurity getSec()
throws SecDispatcherException
{
Expand All @@ -210,7 +233,9 @@ private SettingsSecurity getSec()

return sec;
}

//----------------------------------------------------------------------------

private String getMaster( SettingsSecurity sec )
throws SecDispatcherException
{
Expand Down Expand Up @@ -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
{
Expand All @@ -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
{
Expand All @@ -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
Expand All @@ -320,6 +343,4 @@ private static void show( boolean showMaster )
System.out.println( dc.encryptAndDecorate( pass, dd.getMaster(sec) ) );
}
}
//---------------------------------------------------------------
//---------------------------------------------------------------
}
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -24,8 +24,6 @@
*/
public interface PasswordDecryptor
{
public static String ROLE = PasswordDecryptor.class.getName();

/**
* decrypt given encrypted string
*
Expand All @@ -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;
}
Loading