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
When you add a KMS plugin to your project (like trustweave-kms-aws or trustweave-kms-azure), the plugin automatically registers itself with KeyManagementServices. You can then create KMS instances by simply providing the provider name and configuration:
1
2
3
4
5
6
importorg.trustweave.kms.*// Create an AWS KMS instancevalkms=KeyManagementServices.create("aws",mapOf("region"to"us-east-1"))
The factory handles all the complexity of finding and instantiating the correct plugin for you.
Why It’s Useful
Simple API: Create KMS instances with a single line of code
Plugin Agnostic: Switch between providers without changing your code structure
Type Safety: Works with typed configuration builders for compile-time validation
Better Errors: Clear error messages that list available providers when something goes wrong
Automatic Discovery: All plugins are automatically available - just add the dependency
Many providers support typed configuration builders that provide compile-time safety and IDE autocomplete:
1
2
3
4
5
6
7
8
9
10
importorg.trustweave.kms.*importorg.trustweave.awskms.awsKmsOptions// Type-safe configuration with IDE autocompletevalkms=KeyManagementServices.create("aws",awsKmsOptions{region="us-east-1"accessKeyId="AKIA..."secretAccessKey="..."endpointOverride="http://localhost:4566"// For LocalStack})
Discovering Available Providers
Check which KMS providers are available in your project:
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.
Error Handling
The factory provides helpful error messages when a provider isn’t found:
1
2
3
4
5
6
7
8
9
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]}
Available Providers
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
InMemory KMS
Perfect for development and testing. No configuration required.
importorg.trustweave.kms.inmemory.*importorg.trustweave.kms.*importorg.trustweave.kms.KmsOptionKeys// Create service (no configuration needed)valkms=InMemoryKeyManagementService()// Generate a keyvalresult=kms.generateKey(Algorithm.Ed25519)when(result){isGenerateKeyResult.Success->{valkeyHandle=result.keyHandleprintln("Key created: ${keyHandle.id}")// Sign datavalsign=kms.sign(keyHandle.id,"Hello, World!".toByteArray())when(sign){isSignResult.Success->println("Signature created")isSignResult.Failure->println("Error: ${sign.reason}")}// Get public keyvalpublicKeyResult=kms.getPublicKey(keyHandle.id)when(publicKeyResult){isGetPublicKeyResult.Success->println("Public key: ${publicKeyResult.keyHandle.publicKeyJwk}")isGetPublicKeyResult.Failure->println("Error: ${publicKeyResult.reason}")}// Delete keyvaldeleteResult=kms.deleteKey(keyHandle.id)when(deleteResult){isDeleteKeyResult.Deleted->println("Key deleted")isDeleteKeyResult.NotFound->println("Key not found")}}isGenerateKeyResult.Failure->{println("Error: ${result.reason}")}}
AWS KMS
Configuration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
importorg.trustweave.awskms.*// Using IAM Role (Recommended for EC2/ECS/Lambda)valconfig=AwsKmsConfig.builder().region("us-east-1").build()// Or using Access Keysvalconfig=AwsKmsConfig.builder().region("us-east-1").accessKeyId("AKIA...").secretAccessKey("...").cacheTtlSeconds(300)// Optional: 5 minutes cache.build()valkms=AwsKeyManagementService(config)
importorg.trustweave.azurekms.*// Using Managed Identity (Recommended for Azure)valconfig=AzureKmsConfig.builder().vaultUrl("https://myvault.vault.azure.net").build()// Or using Service Principalvalconfig=AzureKmsConfig.builder().vaultUrl("https://myvault.vault.azure.net").clientId("your-client-id").clientSecret("your-client-secret").tenantId("your-tenant-id").build()valkms=AzureKeyManagementService(config)
importorg.trustweave.hashicorpkms.*// Using Token Authenticationvalconfig=VaultKmsConfig.builder().address("http://localhost:8200").token("hvs.xxx").transitPath("transit")// Optional, default is "transit".build()// Or using AppRolevalconfig=VaultKmsConfig.builder().address("http://localhost:8200").appRolePath("approle").roleId("role-id").secretId("secret-id").transitPath("transit").build()valkms=VaultKeyManagementService(config)
importorg.trustweave.kms.ibm.*valconfig=IbmKmsConfig.builder().apiKey("your-api-key").instanceId("your-instance-id").region("us-south")// Optional, default is "us-south".build()valkms=IbmKeyManagementService(config)
importorg.trustweave.kms.*importorg.trustweave.kms.KmsOptionKeys// Simple factory API - no ServiceLoader needed!// Create KMS instances directly by provider namevalawsKms=KeyManagementServices.create("aws",mapOf(KmsOptionKeys.REGIONto"us-east-1"))valazureKms=KeyManagementServices.create("azure",mapOf("vaultUrl"to"https://myvault.vault.azure.net"))valgoogleKms=KeyManagementServices.create("google-cloud-kms",mapOf("projectId"to"my-project"))valvaultKms=KeyManagementServices.create("vault",mapOf("address"to"http://localhost:8200"))valibmKms=KeyManagementServices.create("ibm",mapOf("apiKey"to"your-api-key"))valinMemoryKms=KeyManagementServices.create("inmemory")// Use KMSvalkms=awsKms
Discovering Available Providers
1
2
3
4
5
6
importorg.trustweave.kms.*// Get list of all available providersvalproviders=KeyManagementServices.availableProviders()println("Available providers: $providers")// Output: [aws, azure, google-cloud-kms, vault, ibm, inmemory, waltid]
Using the KMS Instance
Once created, use the KMS instance to generate keys, sign data, and manage cryptographic operations:
importorg.trustweave.kms.*valkms=KeyManagementServices.create("aws",mapOf("region"to"us-east-1"))// Generate a keyvalresult=kms.generateKey(Algorithm.Ed25519)when(result){isGenerateKeyResult.Success->{println("Key created: ${result.keyHandle.id}")// Sign data with the keyvalsignResult=kms.sign(result.keyHandle.id,"Hello, World!".toByteArray())when(signResult){isSignResult.Success->println("Signature created")isSignResult.Failure->println("Error: ${signResult.reason}")}}isGenerateKeyResult.Failure->{println("Error: ${result.reason}")}}