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

org.kiwiproject.ansible.vault.VaultEncryptCommand Maven / Gradle / Ivy

Go to download

Kiwi is a utility library. We really like Google's Guava, and also use Apache Commons. But if they don't have something we need, and we think it is useful, this is where we put it.

There is a newer version: 4.5.2
Show newest version
package org.kiwiproject.ansible.vault;

import static java.util.Objects.isNull;
import static org.kiwiproject.base.KiwiPreconditions.checkArgumentNotBlank;
import static org.kiwiproject.base.KiwiPreconditions.checkArgumentNotNull;
import static org.kiwiproject.base.KiwiStrings.f;

import lombok.Builder;

import javax.annotation.Nullable;
import java.util.List;

/**
 * Generates {@code ansible-vault encrypt} commands.
 */
@Builder
public class VaultEncryptCommand implements OsCommand {

    private final String ansibleVaultPath;
    private final String vaultIdLabel;
    private final String vaultPasswordFilePath;
    private final String plainTextFilePath;

    /**
     * Create an instance.
     *
     * @param configuration     the {@link VaultConfiguration} to use
     * @param plainTextFilePath path to the plain text file to encrypt
     * @return the command
     */
    public static VaultEncryptCommand from(VaultConfiguration configuration, String plainTextFilePath) {
        return from(configuration, null, plainTextFilePath);
    }

    /**
     * Create an instance.
     *
     * @param configuration     the {@link VaultConfiguration} to use
     * @param vaultIdLabel      the label of the vault (for use with the {@code --vault-id} argument
     * @param plainTextFilePath path to the plain text file to encrypt
     * @return the command
     */
    public static VaultEncryptCommand from(VaultConfiguration configuration,
                                           @Nullable String vaultIdLabel,
                                           String plainTextFilePath) {
        checkArgumentNotNull(configuration, "configuration cannot be null");
        checkArgumentNotBlank(plainTextFilePath, "plainTextFilePath cannot be blank");

        return VaultEncryptCommand.builder()
                .ansibleVaultPath(configuration.getAnsibleVaultPath())
                .vaultIdLabel(vaultIdLabel)
                .vaultPasswordFilePath(configuration.getVaultPasswordFilePath())
                .plainTextFilePath(plainTextFilePath)
                .build();
    }

    @Override
    public List getCommandParts() {
        if (isNull(vaultIdLabel)) {
            return List.of(
                    ansibleVaultPath,
                    "encrypt",
                    "--vault-password-file", vaultPasswordFilePath,
                    plainTextFilePath
            );
        }

        return List.of(
                ansibleVaultPath,
                "encrypt",
                "--vault-id", vaultIdArgument(),
                plainTextFilePath
        );
    }

    private String vaultIdArgument() {
        return f("{}@{}", vaultIdLabel, vaultPasswordFilePath);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy