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

com.infusers.core.secrets.az.AzSecretManager Maven / Gradle / Ivy

There is a newer version: 2024.12.0008
Show newest version
package com.infusers.core.secrets.az;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.security.keyvault.secrets.SecretClient;
import com.azure.security.keyvault.secrets.SecretClientBuilder;
import com.azure.security.keyvault.secrets.models.SecretProperties;
import com.google.gson.JsonObject;
import com.infusers.core.secrets.SecretManager;


public class AzSecretManager implements SecretManager {
	private Logger log = LogManager.getLogger(AzSecretManager.class);
	
    @Override    
	public boolean isCloudEnvironment() {
		return true;
	}	
	
    @Override    	
	public String getSecret(String region, String secretName) {
    	String keyVaultUrl = null;
        try {
        	
            keyVaultUrl = "https://"+secretName+".vault.azure.net/";

            SecretClient secretClient = new SecretClientBuilder()
                .vaultUrl(keyVaultUrl)
                .credential(new DefaultAzureCredentialBuilder().build())
                .buildClient();
            
            // Retrieve a list of key properties
            List secretProperties = secretClient.listPropertiesOfSecrets().stream().collect(Collectors.toList());
            
			log.warn("AzSecretManager.getSecret() :: FOUND!! secretName = "+secretName+" :: keyVaultUrl = "+keyVaultUrl);

            // Convert key properties list to JSON
            return convertToJSON(secretProperties, secretClient);
            
        } 
        catch (Exception e) {
			log.error("AzSecretManager.getSecret()" + e.getMessage()+" :: secretName = "+secretName+" :: keyVaultUrl = "+keyVaultUrl);
        }
        return null;
    }	
    
    private static String convertToJSON(List secretPropertiesList, SecretClient secretClient) {
        JsonObject jsonObject = new JsonObject();

        for (SecretProperties secretProperties : secretPropertiesList) {
            
            String name = secretProperties.getName();
            String value = secretClient.getSecret(name).getValue();
            jsonObject.addProperty(name, value);
        }

        return jsonObject.toString();
    }
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy