Search in sources :

Example 1 with SecurityConfigurationXpp3Writer

use of org.sonatype.plexus.components.sec.dispatcher.model.io.xpp3.SecurityConfigurationXpp3Writer in project karaf by apache.

the class PasswordCommand method doAction.

@Override
public void doAction(String prefix, Dictionary<String, Object> config) throws Exception {
    if (ep && emp) {
        System.err.println("Please specify only one of --encrypt-password and --encrypt-master-password");
        return;
    }
    if (ep && persist) {
        System.err.println("Ordinary passwords are not persisted - use the encrypted password in either <proxy> or <server>");
        return;
    }
    if (ep) {
        // encrypt password using master password
        if (masterPassword == null) {
            System.err.println("Master password is not available");
            return;
        }
        String password = session.readLine("Password to encrypt: ", '*');
        System.out.println("Encrypted password: " + cipher.encryptAndDecorate(password, masterPassword));
        System.out.println("You can use this encrypted password when defining repositories and proxies");
        return;
    }
    if (emp) {
        if (persist && !confirm("Maven security settings will be stored in new file. This file will be used in org.ops4j.pax.url.mvn.security property. Continue? (y/N) ")) {
            return;
        }
        // encrypt master password using DefaultSecDispatcher.SYSTEM_PROPERTY_SEC_LOCATION
        String password = session.readLine("Master password to encrypt: ", '*');
        String encryptedPassword = cipher.encryptAndDecorate(password, DefaultSecDispatcher.SYSTEM_PROPERTY_SEC_LOCATION);
        System.out.println("Encrypted master password: " + encryptedPassword);
        if (persist) {
            SettingsSecurity settingsSecurity = new SettingsSecurity();
            settingsSecurity.setMaster(encryptedPassword);
            File dataDir = context.getDataFile(".");
            if (!dataDir.isDirectory()) {
                System.err.println("Can't access data directory for " + context.getBundle().getSymbolicName() + " bundle");
                return;
            }
            File newSecuritySettingsFile = nextSequenceFile(dataDir, RE_SECURITY_SETTINGS, PATTERN_SECURITY_SETTINGS);
            try (FileWriter fw = new FileWriter(newSecuritySettingsFile)) {
                new SecurityConfigurationXpp3Writer().write(fw, settingsSecurity);
            }
            System.out.println("New security settings stored in \"" + newSecuritySettingsFile.getCanonicalPath() + "\"");
            Configuration cmConfig = cm.getConfiguration(PID);
            config.put(prefix + PROPERTY_SECURITY_FILE, newSecuritySettingsFile.getCanonicalPath());
            cmConfig.update(config);
        }
    }
}
Also used : SettingsSecurity(org.sonatype.plexus.components.sec.dispatcher.model.SettingsSecurity) Configuration(org.osgi.service.cm.Configuration) FileWriter(java.io.FileWriter) SecurityConfigurationXpp3Writer(org.sonatype.plexus.components.sec.dispatcher.model.io.xpp3.SecurityConfigurationXpp3Writer) File(java.io.File)

Aggregations

File (java.io.File)1 FileWriter (java.io.FileWriter)1 Configuration (org.osgi.service.cm.Configuration)1 SettingsSecurity (org.sonatype.plexus.components.sec.dispatcher.model.SettingsSecurity)1 SecurityConfigurationXpp3Writer (org.sonatype.plexus.components.sec.dispatcher.model.io.xpp3.SecurityConfigurationXpp3Writer)1