AWS KMS encryption and decryption package with convenient operations.
π Simple Encryption: Easy-to-use AWS KMS encryption and decryption operations β‘ Dual Modes: Supports both bytes and base64-encoded string operations π§ Environment Config: Convenient environment variable-based configuration π Dual Logging: Includes both slog and zap logging implementations π― Context Support: Built on AWS SDK v2 with context-based API
go get github.com/go-xlan/go-aws-kms
This example shows AWS KMS encryption/decryption with raw bytes using slog logging.
package main
import (
"context"
"fmt"
"os"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/kms"
"github.com/go-xlan/go-aws-kms/awskms"
"github.com/yyle88/must"
"github.com/yyle88/rese"
)
func main() {
// Set AWS region
region := must.Nice(os.Getenv("AWS_KMS_REGION_ID"))
// Load AWS configuration
cfg := rese.V1(config.LoadDefaultConfig(
context.Background(),
config.WithRegion(region),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(
os.Getenv("AWS_KMS_ACCESS_KEY"),
os.Getenv("AWS_KMS_SECRET_KEY"),
os.Getenv("AWS_SESSION_TOKEN"),
)),
config.WithLogger(awskms.NewSlogLogger()),
config.WithClientLogMode(aws.LogRequestWithBody|aws.LogResponseWithBody),
))
// Create KMS client
kmsClient := kms.NewFromConfig(cfg)
// Get encryption key ID from environment
encryptKeyID := must.Nice(os.Getenv("AWS_KMS_ENCRYPT_KEY_ID"))
// Create AwsKms instance
awsKms := awskms.NewAwsKms(kmsClient, encryptKeyID)
// Encrypt and decrypt bytes
fmt.Println("=== Bytes Encryption Example ===")
plaintext := []byte("sensitive data")
fmt.Printf("Message: %s\n", plaintext)
ciphertext := rese.A1(awsKms.Encrypt(plaintext))
fmt.Printf("Encrypted (bytes): %d bytes\n", len(ciphertext))
decrypted := rese.A1(awsKms.Decrypt(ciphertext))
fmt.Printf("Decrypted: %s\n", decrypted)
fmt.Println("\nβ
Success!")
}
β¬οΈ Source: Source
This example shows AWS KMS string encryption/decryption with base64 encoding using environment-based configuration.
package main
import (
"fmt"
"github.com/go-xlan/go-aws-kms/awskms"
"github.com/yyle88/rese"
)
func main() {
// Create AwsKms instance from environment variables
envOptions := awskms.NewEnvOptions()
awsKms := rese.P1(awskms.NewAwsKmsFromEnv(envOptions))
// Encrypt and decrypt string with base64
fmt.Println("=== String Encryption Example ===")
message := "secret message"
fmt.Printf("Message: %s\n", message)
encrypted := rese.C1(awsKms.Encrypts(message))
fmt.Printf("Encrypted (base64): %s\n", encrypted)
decryptedStr := rese.C1(awsKms.Decrypts(encrypted))
fmt.Printf("Decrypted: %s\n", decryptedStr)
fmt.Println("\nβ
Success!")
}
β¬οΈ Source: Source
NewAwsKms(client, keyID)
- Create AwsKms instance with KMS client and encryption IDEncrypt(plaintext)
- Encrypt bytes, returns encrypted bytesDecrypt(ciphertext)
- Decrypt bytes, returns plaintext bytesEncrypts(plaintext)
- Encrypt string, returns base64-encoded stringDecrypts(ciphertext)
- Decrypt base64 string, returns plaintext string
NewEnvOptions()
- Create environment options with default variable namesNewAwsKmsFromEnv(options)
- Create AwsKms instance from environment variables
NewSlogLogger()
- Create slog-based logger in AWS SDK operationsNewZapLogger()
- Create zap-based logger in AWS SDK operations
Using default environment variable names:
envOptions := awskms.NewEnvOptions()
awsKms, _ := awskms.NewAwsKmsFromEnv(envOptions)
Custom environment variable names:
envOptions := awskms.NewEnvOptions()
envOptions.
WithRegionID("CUSTOM_REGION").
WithAccessKeyID("CUSTOM_ACCESS_KEY").
WithSecretAccessKey("CUSTOM_SECRET_KEY").
WithEncryptKeyID("CUSTOM_KMS_KEY_ID")
awsKms, _ := awskms.NewAwsKmsFromEnv(envOptions)
Using slog:
cfg, _ := config.LoadDefaultConfig(
context.Background(),
config.WithLogger(awskms.NewSlogLogger()),
config.WithClientLogMode(aws.LogRequestWithBody | aws.LogResponseWithBody),
)
Using zap:
cfg, _ := config.LoadDefaultConfig(
context.Background(),
config.WithLogger(awskms.NewZapLogger()),
config.WithClientLogMode(aws.LogRequestWithBody | aws.LogResponseWithBody),
)
Encrypt and decrypt strings:
encrypted, _ := awsKms.Encrypts("secret message")
decrypted, _ := awsKms.Decrypts(encrypted)
Working with raw bytes:
plaintext := []byte("sensitive data")
ciphertext, _ := awsKms.Encrypt(plaintext)
decrypted, _ := awsKms.Decrypt(ciphertext)
MIT License. See LICENSE.
Contributions are welcome! Report bugs, suggest features, and contribute code:
- π Found a mistake? Open an issue on GitHub with reproduction steps
- π‘ Have a feature idea? Create an issue to discuss the suggestion
- π Documentation confusing? Report it so we can improve
- π Need new features? Share the use cases to help us understand requirements
- β‘ Performance issue? Help us optimize through reporting slow operations
- π§ Configuration problem? Ask questions about complex setups
- π’ Follow project progress? Watch the repo to get new releases and features
- π Success stories? Share how this package improved the workflow
- π¬ Feedback? We welcome suggestions and comments
New code contributions, follow this process:
- Fork: Fork the repo on GitHub (using the webpage UI).
- Clone: Clone the forked project (
git clone https://github.com/yourname/repo-name.git
). - Navigate: Navigate to the cloned project (
cd repo-name
) - Branch: Create a feature branch (
git checkout -b feature/xxx
). - Code: Implement the changes with comprehensive tests
- Testing: (Golang project) Ensure tests pass (
go test ./...
) and follow Go code style conventions - Documentation: Update documentation to support client-facing changes and use significant commit messages
- Stage: Stage changes (
git add .
) - Commit: Commit changes (
git commit -m "Add feature xxx"
) ensuring backward compatible code - Push: Push to the branch (
git push origin feature/xxx
). - PR: Open a merge request on GitHub (on the GitHub webpage) with detailed description.
Please ensure tests pass and include relevant documentation updates.
Welcome to contribute to this project via submitting merge requests and reporting issues.
Project Support:
- β Give GitHub stars if this project helps you
- π€ Share with teammates and (golang) programming friends
- π Write tech blogs about development tools and workflows - we provide content writing support
- π Join the ecosystem - committed to supporting open source and the (golang) development scene
Have Fun Coding with this package! πππ