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

org.zodic.kubernetes.confcenter.SecretsPropertySource Maven / Gradle / Ivy

package org.zodic.kubernetes.confcenter;

import java.util.Base64;
import java.util.Map;

import io.fabric8.kubernetes.api.model.Secret;
import io.fabric8.kubernetes.client.KubernetesClient;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
import org.zodiac.sdk.toolkit.util.collection.CollUtil;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;
import org.zodic.kubernetes.base.constants.KubernetesConstants;

public class SecretsPropertySource extends KubernetesPropertySource {

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

    private static final String PREFIX = "secrets";

    public SecretsPropertySource(KubernetesClient client, Environment env, SecretsConfigInfo config) {
        super(getSourceName(env, client, env, config), getSourceData(env, client, env, config));
    }

    private static String getSourceName(Environment environment, KubernetesClient client, Environment env, SecretsConfigInfo config) {
        return new StringBuilder().append(PREFIX).append(KubernetesConstants.PROPERTY_SOURCE_NAME_SEPARATOR)
            .append(ConfigUtils.getApplicationName(env, config.getName(), config.getConfigurationTarget()))
            .append(KubernetesConstants.PROPERTY_SOURCE_NAME_SEPARATOR)
            .append(ConfigUtils.getApplicationNamespace(environment, client, config.getNamespace(), config.getConfigurationTarget())).toString();
    }

    private static Map getSourceData(Environment environment, KubernetesClient client, Environment env,
        SecretsConfigInfo config) {
        String name = ConfigUtils.getApplicationName(env, config.getName(), config.getConfigurationTarget());
        String namespace = ConfigUtils.getApplicationNamespace(environment, client, config.getNamespace(), config.getConfigurationTarget());
        Map result = CollUtil.map();

        if (config.isEnableApi()) {
            try {
                /*Read for secrets api (named).*/
                Secret secret;
                if (StrUtil.isEmpty(namespace)) {
                    secret = client.secrets().withName(name).get();
                } else {
                    secret = client.secrets().inNamespace(namespace).withName(name).get();
                }
                putAll(secret, result);

                /*Read for secrets api (label).*/
                if (!config.getLabels().isEmpty()) {
                    if (StrUtil.isEmpty(namespace)) {
                        client.secrets().withLabels(config.getLabels()).list().getItems()
                            .forEach(s -> putAll(s, result));
                    } else {
                        client.secrets().inNamespace(namespace).withLabels(config.getLabels()).list().getItems()
                            .forEach(s -> putAll(s, result));
                    }
                }
            } catch (Exception e) {
                LOG.warn("Can't read secret with name: [{}] or labels [{}] in namespace:[{}] (cause: {}). Ignoring",
                    name, config.getLabels(), namespace, e.getMessage());
            }
        }

        /*Read for secrets mount.*/
        putPathConfig(result, config.getPaths());

        return result;
    }

    private static void putAll(Secret secret, Map result) {
        if (secret != null && secret.getData() != null) {
            secret.getData().forEach((k, v) -> result.put(k, new String(Base64.getDecoder().decode(v)).trim()));
        }
    }

    @Override
    public String toString() {
        return getClass().getSimpleName() + " {name='" + this.name + "'}";
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy