<commit_before>package org.safehaus.subutai.core.security.impl;


import java.util.Set;

import org.safehaus.subutai.common.command.CommandException;
import org.safehaus.subutai.common.command.CommandResult;
import org.safehaus.subutai.common.util.CollectionUtil;
import org.safehaus.subutai.core.peer.api.CommandUtil;
import org.safehaus.subutai.core.peer.api.ContainerHost;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;


/**
 * Ssh manager for adding/exchanging ssh keys and enabling password-less communication
 */
public class SshManager
{

    private static final Logger LOG = LoggerFactory.getLogger( SshManager.class.getName() );

    private Set<ContainerHost> containerHosts;

    private String keys;
    private Commands commands;
    private CommandUtil commandUtil;


    public SshManager( Set<ContainerHost> containerHosts )
    {
        Preconditions.checkArgument( !CollectionUtil.isCollectionEmpty( containerHosts ), "Agent list is empty" );
        this.containerHosts = containerHosts;
        this.commands = new Commands();
        this.commandUtil = new CommandUtil();
    }


    public void append( String key ) throws SSHManagerException
    {
        for ( ContainerHost host : containerHosts )
        {
            try
            {
                commandUtil.execute( commands.getAppendSSHCommand( key ), host );
            }
            catch ( CommandException e )
            {
                LOG.error( String.format( "Error in append: %s", e.getMessage() ), e );
                throw new SSHManagerException( e.getMessage() );
            }
        }
    }


    public void execute() throws SSHManagerException
    {
        create();
        read();
        write();
        config();
    }


    private void create() throws SSHManagerException
    {
        for ( ContainerHost host : containerHosts )
        {
            try
            {
                commandUtil.execute( commands.getCreateSSHCommand(), host );
            }
            catch ( CommandException e )
            {
                LOG.error( String.format( "Error in create: %s", e.getMessage() ), e );
                throw new SSHManagerException( e.getMessage() );
            }
        }
    }


    private void read() throws SSHManagerException
    {
        StringBuilder value = new StringBuilder();

        for ( ContainerHost host : containerHosts )
        {
            try
            {
                CommandResult command = commandUtil.execute( commands.getReadSSHCommand(), host );
                if ( !Strings.isNullOrEmpty( command.getStdOut() ) )
                {
                    value.append( command.getStdOut() );
                }
            }
            catch ( CommandException e )
            {
                LOG.error( String.format( "Error in read: %s", e.getMessage() ), e );
                throw new SSHManagerException( e.getMessage() );
            }
        }
        if ( Strings.isNullOrEmpty( keys ) )
        {
            throw new SSHManagerException( "Could not read ssh keys from containers" );
        }
        keys = value.toString();
    }


    private void write() throws SSHManagerException
    {
        for ( ContainerHost host : containerHosts )
        {
            try
            {
                commandUtil.execute( commands.getWriteSSHCommand( keys ), host );
            }
            catch ( CommandException e )
            {
                LOG.error( String.format( "Error in write: %s", e.getMessage() ), e );
                throw new SSHManagerException( e.getMessage() );
            }
        }
    }


    private void config() throws SSHManagerException
    {
        for ( ContainerHost host : containerHosts )
        {
            try
            {
                commandUtil.execute( commands.getConfigSSHCommand(), host );
            }
            catch ( CommandException e )
            {
                LOG.error( String.format( "Error in config: %s", e.getMessage() ), e );
                throw new SSHManagerException( e.getMessage() );
            }
        }
    }


    public void execute( ContainerHost containerHost ) throws SSHManagerException
    {
        Preconditions.checkNotNull( containerHost, "Container host is null" );

        create( containerHost );
        containerHosts.add( containerHost );
        read();
        write();
        config();
    }


    private void create( ContainerHost containerHost ) throws SSHManagerException
    {
        try
        {
            commandUtil.execute( commands.getCreateSSHCommand(), containerHost );
        }
        catch ( CommandException e )
        {
            LOG.error( String.format( "Error in create: %s", e.getMessage() ), e );
            throw new SSHManagerException( e.getMessage() );
        }
    }
}<commit_msg>SUBUTAI-1874 - Security Manager in progress<commit_after>package org.safehaus.subutai.core.security.impl;


import java.util.Set;

import org.safehaus.subutai.common.command.CommandException;
import org.safehaus.subutai.common.command.CommandResult;
import org.safehaus.subutai.common.util.CollectionUtil;
import org.safehaus.subutai.core.peer.api.CommandUtil;
import org.safehaus.subutai.core.peer.api.ContainerHost;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;


/**
 * Ssh manager for adding/exchanging ssh keys and enabling password-less communication
 */
public class SshManager
{

    private static final Logger LOG = LoggerFactory.getLogger( SshManager.class.getName() );

    private Set<ContainerHost> containerHosts;

    private String keys;
    private Commands commands;
    private CommandUtil commandUtil;


    public SshManager( Set<ContainerHost> containerHosts )
    {
        Preconditions.checkArgument( !CollectionUtil.isCollectionEmpty( containerHosts ), "Agent list is empty" );
        this.containerHosts = containerHosts;
        this.commands = new Commands();
        this.commandUtil = new CommandUtil();
    }


    public void append( String key ) throws SSHManagerException
    {
        for ( ContainerHost host : containerHosts )
        {
            try
            {
                commandUtil.execute( commands.getAppendSSHCommand( key ), host );
            }
            catch ( CommandException e )
            {
                LOG.error( String.format( "Error in append: %s", e.getMessage() ), e );
                throw new SSHManagerException( e.getMessage() );
            }
        }
    }


    public void execute() throws SSHManagerException
    {
        create();
        read();
        write();
        config();
    }


    public void execute( ContainerHost containerHost ) throws SSHManagerException
    {
        Preconditions.checkNotNull( containerHost, "Container host is null" );

        create( containerHost );
        containerHosts.add( containerHost );
        read();
        write();
        config();
    }


    private void create() throws SSHManagerException
    {
        for ( ContainerHost host : containerHosts )
        {
            try
            {
                commandUtil.execute( commands.getCreateSSHCommand(), host );
            }
            catch ( CommandException e )
            {
                LOG.error( String.format( "Error in create: %s", e.getMessage() ), e );
                throw new SSHManagerException( e.getMessage() );
            }
        }
    }


    private void read() throws SSHManagerException
    {
        StringBuilder value = new StringBuilder();

        for ( ContainerHost host : containerHosts )
        {
            try
            {
                CommandResult command = commandUtil.execute( commands.getReadSSHCommand(), host );
                if ( !Strings.isNullOrEmpty( command.getStdOut() ) )
                {
                    value.append( command.getStdOut() );
                }
            }
            catch ( CommandException e )
            {
                LOG.error( String.format( "Error in read: %s", e.getMessage() ), e );
                throw new SSHManagerException( e.getMessage() );
            }
        }
        if ( Strings.isNullOrEmpty( keys ) )
        {
            throw new SSHManagerException( "Could not read ssh keys from containers" );
        }
        keys = value.toString();
    }


    private void write() throws SSHManagerException
    {
        for ( ContainerHost host : containerHosts )
        {
            try
            {
                commandUtil.execute( commands.getWriteSSHCommand( keys ), host );
            }
            catch ( CommandException e )
            {
                LOG.error( String.format( "Error in write: %s", e.getMessage() ), e );
                throw new SSHManagerException( e.getMessage() );
            }
        }
    }


    private void config() throws SSHManagerException
    {
        for ( ContainerHost host : containerHosts )
        {
            try
            {
                commandUtil.execute( commands.getConfigSSHCommand(), host );
            }
            catch ( CommandException e )
            {
                LOG.error( String.format( "Error in config: %s", e.getMessage() ), e );
                throw new SSHManagerException( e.getMessage() );
            }
        }
    }


    private void create( ContainerHost containerHost ) throws SSHManagerException
    {
        try
        {
            commandUtil.execute( commands.getCreateSSHCommand(), containerHost );
        }
        catch ( CommandException e )
        {
            LOG.error( String.format( "Error in create: %s", e.getMessage() ), e );
            throw new SSHManagerException( e.getMessage() );
        }
    }
}
{'fraction_non_alphanumeric': 0.09421319796954315, 'fraction_numerical': 0.0008121827411167512, 'mean_word_length': 1.835636154289004, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7065864', 'n_tokens_mistral': 2563, 'n_tokens_neox': 2463, 'n_words': 600}