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

com.lordofthejars.nosqlunit.vault.DataLoader Maven / Gradle / Ivy

package com.lordofthejars.nosqlunit.vault;

import com.bettercloud.vault.response.AuthResponse;
import org.yaml.snakeyaml.Yaml;

import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

public class DataLoader {

    private static final String TOKENS = "tokens";

    private VaultConnection vault;

    public DataLoader(VaultConnection vault) {
        this.vault = vault;
    }

    public void load(InputStream inputStream) {
        final Yaml yaml = new Yaml();
        final Object load = yaml.load(inputStream);

        if (areOnlySecrets(load)) {
            final List>> secrets = (List) load;

            insertTokens(secrets);
            insertSecrets(secrets);
        }
    }

    private void insertSecrets(List>> secrets) {
        for (Map> secret : secrets) {

            final Set>> entries = secret.entrySet();

            for (Map.Entry> entry : entries) {
                String backend = entry.getKey();
                if (!TOKENS.equals(backend)) {
                    vault.writeLogical(backend, entry.getValue());
                }
            }
        }
    }

    private void insertTokens(List>> data) {
        final Optional>> tokens = data.stream()
                .flatMap(e -> e.entrySet().stream())
                .filter(e -> TOKENS.equals(e.getKey()))
                .findFirst();

        if (tokens.isPresent()) {
            final List> tokenDefinitions = (List>) tokens.get().getValue();

            for (final Map tokenDefinition : tokenDefinitions) {
                final AuthResponse authResponse = vault.createToken()
                        .displayName(asString(tokenDefinition, "displayName"))
                        .meta(asMapOfStrings(tokenDefinition, "meta"))
                        .noDefaultPolicy(asBoolean(tokenDefinition, "noDefaultPolicy"))
                        .noParent(asBoolean(tokenDefinition, "noParent"))
                        .policies(asListOfStrings(tokenDefinition, "policies"))
                        .uuid(asString(tokenDefinition, "uuid"))
                        .ttl(asInteger(tokenDefinition, "ttl"))
                        .numUses(asLong(tokenDefinition, "numUses"))
                        .create();

                if (tokenDefinition.containsKey("secrets")) {
                    List>> secrets = (List>>) tokenDefinition.get("secrets");
                    try {
                        vault.updateToken(authResponse.getAuthClientToken());
                        insertSecrets(secrets);
                    } finally {
                        vault.reconnectToOriginal();
                    }
                }

            }

        }
    }

    private boolean areOnlySecrets(Object load) {
        return load instanceof List;
    }

    private Integer asInteger(Map elements, String key) {
        if (elements.containsKey(key)) {
            return (Integer) elements.get(key);
        }

        return null;
    }

    private Long asLong(Map elements, String key) {
        if (elements.containsKey(key)) {
            if (elements.get(key) instanceof Integer) {
                ((Integer) elements.get(key)).longValue();
            } else {
                return (Long) elements.get(key);
            }
        }

        return null;
    }

    private Map asMapOfStrings(Map elements, String key) {
        if (elements.containsKey(key)) {
            Map map = (Map) elements.get(key);

            return toStringMap(map);
        }

        return null;
    }

    private List asListOfStrings(Map elements, String key) {
        if (elements.containsKey(key)) {
            return (List) elements.get(key);
        }

        return null;
    }

    private Boolean asBoolean(Map elements, String key) {
        if (elements.containsKey(key)) {
            return (Boolean) elements.get(key);
        }

        return null;
    }

    private String asString(Map elements, String key) {
        if (elements.containsKey(key)) {
            return (String) elements.get(key);
        }

        return null;
    }

    private Map toStringMap(Map elements) {
        return elements.entrySet()
                .stream()
                .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().toString()));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy