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: 2025.01.0002
Show newest version
package com.infusers.core.secrets.az;

import java.io.IOException;
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.AbstractCloudSecretManager;


public class AzSecretManager extends AbstractCloudSecretManager {
	private Logger log = LogManager.getLogger(AzSecretManager.class);
	private static final String CLASS_NAME = AzSecretManager.class.getSimpleName();
	
    @Override    
	public boolean isCloudEnvironment() {
		return true;
	}	
    
    @Override    
	public String getSecretKeyPrefix() {
		return null;
	}    
    
    @Override        
    public boolean hasDBSecrets() {
    	return true;
    }
    
    @Override        	
    public boolean hasRabbitMQSecrets() {
    	return true;
    }
    
    @Override        
    public boolean hasOtherSecrets() {
    	return true;
    }
    
    @Override        
    public boolean hasEmailSecrets() {
    	return false;
    }	    
	
    @Override    	
	public String getSecret(String region, String secretName) throws IOException {
    	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.debug(CLASS_NAME+".getSecret() :: FOUND!! secretName = "+secretName+" :: keyVaultUrl = "+keyVaultUrl);

            // Convert key properties list to JSON
            return convertToJSON(secretProperties, secretClient);
            
        } 
        catch (Exception e) {
			log.error(CLASS_NAME+".getSecret()" + e.getMessage()+" :: secretName = "+secretName+" :: keyVaultUrl = "+keyVaultUrl);
			throw e;
        }
    }	
    
    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