Version: 1.0 Last Updated: 2025-01-27 Status: Current
Overview
This guide provides comprehensive configuration documentation for all TrustWeave KMS plugins. All plugins implement the KeyManagementService interface and use the Result-based API for type-safe error handling.
KeyManagementServices is a factory that simplifies creating KMS instances from any available plugin. It automatically discovers and manages all KMS plugins on your classpath, making it easy to switch between different key management providers.
How It Works with Plugins
When you add a KMS plugin dependency to your project (e.g., trustweave-kms-aws), the plugin automatically registers itself with KeyManagementServices. The factory maintains a registry of all available plugins and can instantiate them on demand.
Example: Adding AWS KMS plugin to your build.gradle.kts:
importorg.trustweave.kms.*try{valkms=KeyManagementServices.create("unknown-provider")}catch(e:IllegalArgumentException){println(e.message)// Output: KMS provider 'unknown-provider' not found. // Available providers: [aws, azure, google-cloud-kms, vault, ibm, inmemory, waltid]}
Instance Caching
KeyManagementServices uses caching to improve performance and avoid expensive setup costs. Calling create() multiple times with the same provider and configuration returns the same cached instance.
Provider Names Reference
Provider
Name
Configuration Required
AWS KMS
aws
Yes (region)
Azure Key Vault
azure
Yes (vaultUrl)
Google Cloud KMS
google-cloud-kms
Yes (projectId, location)
HashiCorp Vault
vault
Yes (address, token)
IBM Key Protect
ibm
Yes (apiKey, instanceId)
InMemory
inmemory
No
WaltID
waltid
Depends on configuration
Best Practices
Use Typed Configuration When Available: Provides compile-time safety and better IDE support
Handle Errors Gracefully: Always catch IllegalArgumentException when provider name might be dynamic
Check Provider Availability: Use availableProviders() to verify a provider is available before attempting to create it
Use Factory for All Code: KeyManagementServices.create() is the recommended way to create KMS instances
// Direct provider usage (not recommended - use factory instead)valkms=provider.create(mapOf("param1"to"value1","param2"to"value2"))
5. Result-Based API
All operations return sealed Result types:
1
2
3
4
5
6
7
8
9
10
valresult=kms.generateKey(Algorithm.Ed25519)when(result){isGenerateKeyResult.Success->{valkeyHandle=result.keyHandle// Use keyHandle}isGenerateKeyResult.Failure->{// Handle error}}
AWS KMS
Configuration Class
1
2
3
4
5
6
7
8
9
data classAwsKmsConfig(valregion:String,// RequiredvalaccessKeyId:String?=null,// Optional (uses IAM role if not provided)valsecretAccessKey:String?=null,// OptionalvalsessionToken:String?=null,// Optional (for temporary credentials)valendpointOverride:String?=null,// Optional (for local testing)valpendingWindowInDays:Int?=null,// Optional (7-30 days, default 30)valcacheTtlSeconds:Long?=300// Optional (default 5 minutes))
importorg.trustweave.awskms.*// Using IAM Role (Recommended for EC2/ECS/Lambda)valconfig=AwsKmsConfig.builder().region("us-east-1").build()// Using Access Keysvalconfig=AwsKmsConfig.builder().region("us-east-1").accessKeyId("AKIAIOSFODNN7EXAMPLE").secretAccessKey("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY").build()// With Temporary Credentialsvalconfig=AwsKmsConfig.builder().region("us-east-1").accessKeyId("AKIAIOSFODNN7EXAMPLE").secretAccessKey("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY").sessionToken("temporary-session-token").build()// With Custom Cache TTLvalconfig=AwsKmsConfig.builder().region("us-east-1").cacheTtlSeconds(600)// 10 minutes.build()// With Custom Pending Windowvalconfig=AwsKmsConfig.builder().region("us-east-1").pendingWindowInDays(7)// Minimum 7 days.build()
data classAzureKmsConfig(valvaultUrl:String,// Required (must be HTTPS)valclientId:String?=null,// Optional (uses Managed Identity if not provided)valclientSecret:String?=null,// OptionalvaltenantId:String?=null,// OptionalvalendpointOverride:String?=null// Optional (for local testing))
Builder Pattern
1
2
3
4
5
6
7
8
9
10
11
12
13
14
importorg.trustweave.azurekms.*// Using Managed Identity (Recommended for Azure VMs/App Service/Functions)valconfig=AzureKmsConfig.builder().vaultUrl("https://myvault.vault.azure.net").build()// Using Service Principalvalconfig=AzureKmsConfig.builder().vaultUrl("https://myvault.vault.azure.net").clientId("your-client-id").clientSecret("your-client-secret").tenantId("your-tenant-id").build()
importorg.trustweave.googlekms.*// Using Application Default Credentials (Recommended)valconfig=GoogleKmsConfig.builder().projectId("my-project").location("us-east1").keyRing("my-key-ring")// Optional.build()// Using Service Account JSON Filevalconfig=GoogleKmsConfig.builder().projectId("my-project").location("us-east1").keyRing("my-key-ring").credentialsPath("/path/to/service-account.json").build()// Using Service Account JSON Stringvalconfig=GoogleKmsConfig.builder().projectId("my-project").location("us-east1").credentialsJson("""{"type":"service_account",...}""").build()// With Custom Cache TTLvalconfig=GoogleKmsConfig.builder().projectId("my-project").location("us-east1").cacheTtlSeconds(600)// 10 minutes.build()
importorg.trustweave.hashicorpkms.*// Using Token Authentication (Default)valconfig=VaultKmsConfig.builder().address("http://localhost:8200").token("hvs.xxx").transitPath("transit")// Optional, default is "transit".build()// Using AppRole Authenticationvalconfig=VaultKmsConfig.builder().address("http://localhost:8200").appRolePath("approle").roleId("role-id").secretId("secret-id").transitPath("transit").build()// With Namespace (Vault Enterprise)valconfig=VaultKmsConfig.builder().address("http://localhost:8200").token("hvs.xxx").namespace("admin").transitPath("transit").build()
data classIbmKmsConfig(valapiKey:String,// RequiredvalinstanceId:String,// Requiredvalregion:String="us-south",// Optional (default: "us-south")valserviceUrl:String?=null,// OptionalvalendpointOverride:String?=null// Optional (for local testing))
Builder Pattern
1
2
3
4
5
6
7
importorg.trustweave.kms.ibm.*valconfig=IbmKmsConfig.builder().apiKey("your-api-key").instanceId("your-instance-id").region("us-south")// Optional, default is "us-south".build()
Note: Keys are stored in memory only. Suitable for development and testing only.
SPI Auto-Discovery
All plugins register themselves via Java ServiceLoader. You can discover and use them programmatically:
1
2
3
4
5
6
7
8
9
10
11
12
importorg.trustweave.kms.*importorg.trustweave.kms.KmsOptionKeys// Simple factory API - no ServiceLoader needed!// Create KMS instances directly by provider namevalkms=KeyManagementServices.create("aws",mapOf(KmsOptionKeys.REGIONto"us-east-1"))// Or get list of available providersvalavailableProviders=KeyManagementServices.availableProviders()println("Available providers: $availableProviders")
Provider Names
aws - AWS KMS
azure - Azure Key Vault
google-cloud-kms - Google Cloud KMS
vault - HashiCorp Vault
ibm - IBM Key Protect
inmemory - InMemory KMS
waltid - WaltID KMS
Common Options
All plugins support common options via KmsOptionKeys constants: