All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.smartics.maven.plugin.buildmetadata.util.SettingsDecrypter Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2006-2019 smartics, Kronseder & Reiner GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package de.smartics.maven.plugin.buildmetadata.util;

import org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher;
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
import org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException;

import java.io.File;

/**
 * Helper to decrypt passwords from the settings.
 */
public final class SettingsDecrypter {
  // ********************************* Fields *********************************

  // --- constants ------------------------------------------------------------

  // --- members --------------------------------------------------------------

  /**
   * The Maven infrastructure to decrypt passwords from the settings.
   */
  private final SecDispatcher securityDispatcher;

  /**
   * The location of the settings-security.xml.
   */
  private final String settingsSecurityLocation;

  // ****************************** Initializer *******************************

  // ****************************** Constructors ******************************

  /**
   * Default constructor.
   *
   * @param securityDispatcher the Maven infrastructure to decrypt passwords
   *        from the settings.
   * @param settingsSecurityLocation the location of the
   *        settings-security.
   */
  public SettingsDecrypter(final SecDispatcher securityDispatcher,
      final String settingsSecurityLocation) {
    this.securityDispatcher = securityDispatcher;
    this.settingsSecurityLocation = init(settingsSecurityLocation);
  }

  // ****************************** Inner Classes *****************************

  // ********************************* Methods ********************************

  // --- init -----------------------------------------------------------------

  private static String init(final String settingsSecurityLocation) {
    final File file = new File(settingsSecurityLocation);
    if (!file.canRead()) {
      return null;
    }

    System.setProperty(DefaultSecDispatcher.SYSTEM_PROPERTY_SEC_LOCATION,
        settingsSecurityLocation);
    return settingsSecurityLocation;
  }

  // --- get&set --------------------------------------------------------------

  // --- business -------------------------------------------------------------

  /**
   * Decrypts the given value if the security dispatcher is initialized with a
   * valid configuration.
   *
   * @param encrypted the value to decrypt.
   * @return the decrypted value or the unchanged {@code encrypted}.
   * @throws SecDispatcherException if the decryption failed.
   */
  public String decrypt(final String encrypted) throws SecDispatcherException {
    if (settingsSecurityLocation != null) {
      return securityDispatcher.decrypt(encrypted);
    }
    return encrypted;
  }

  // --- object basics --------------------------------------------------------

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy