/

Accessing Secrets

Prerequisites

Before accessing secrets, make sure you've installed and initialized the Secrets Manager SDK.

Basic Usage

After initializing the SDK, you can retrieve secrets with a simple API call:

Java
import com.bytehide.secrets.SecretsManager;

public class Example {
    public static void main(String[] args) {
        // Auto-initializes from environment variables
        String dbPassword = SecretsManager.get("database-connection");
        System.out.println("Connection string: " + dbPassword);
    }
}

Creating and Updating Secrets

You can create or update secrets directly from your application:

Java
import com.bytehide.secrets.SecretsManager;

// Create a new secret
boolean success = SecretsManager.set("api-key", "sk_test_abc123xyz");

// Update an existing secret
SecretsManager.set("database-connection", "jdbc:mysql://new-server:3306/myapp?user=admin&password=secret");

Secrets Not Found

When a secret doesn't exist, the SDK will throw an exception. Handle this gracefully:

Java
import com.bytehide.secrets.SecretsManager;

public class Example {
    public static void main(String[] args) {
        try {
            String secret = SecretsManager.get("non-existent-key");
        } catch (Exception ex) {
            // Handle missing secret
            System.out.println("Secret not found: " + ex.getMessage());

            // You might want to create the secret or use a default value
            SecretsManager.set("non-existent-key", "default-value");
        }
    }
}

Best Practices

Cache Configuration

The SDK includes built-in caching to improve performance:

Java
import com.bytehide.secrets.SecretsManager;

// Configure cache with 10 minutes TTL
SecretsManager.configureCache(true, 600000L); // 10 minutes in milliseconds

// Clear cache
SecretsManager.clearCache();

// Disable cache
SecretsManager.configureCache(false, null);

Security Best Practices

  • Never print or log full secret values
  • Don't store secret values in plain text files
  • Clear secrets from memory when no longer needed
  • Use environment-specific secrets

Common Usage Patterns

Configuration Helper

Create a configuration helper for your application:

Java
import com.bytehide.secrets.SecretsManager;

public class AppConfig {
    public static String getDatabaseConnection() {
        return SecretsManager.get("database-connection");
    }

    public static String getApiKey() {
        return SecretsManager.get("api-key");
    }

    public static int getCacheTimeout() {
        String value = SecretsManager.get("cache-timeout-minutes");
        try {
            return Integer.parseInt(value);
        } catch (NumberFormatException e) {
            return 10; // Default to 10
        }
    }
}

Spring Boot Integration

Integrate with Spring Boot applications:

Java
import com.bytehide.secrets.SecretsManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SecretsConfig {

    @Bean
    public String databaseUrl() {
        return SecretsManager.get("database-connection");
    }

    @Bean
    public String apiKey() {
        return SecretsManager.get("api-key");
    }
}

Next Steps

Previous
Installation