kms.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package kms
  2. import (
  3. "context"
  4. "fmt"
  5. )
  6. // KMSProvider defines the interface for Key Management Service implementations
  7. type KMSProvider interface {
  8. // GenerateDataKey creates a new data encryption key encrypted under the specified KMS key
  9. GenerateDataKey(ctx context.Context, req *GenerateDataKeyRequest) (*GenerateDataKeyResponse, error)
  10. // Decrypt decrypts an encrypted data key using the KMS
  11. Decrypt(ctx context.Context, req *DecryptRequest) (*DecryptResponse, error)
  12. // DescribeKey validates that a key exists and returns its metadata
  13. DescribeKey(ctx context.Context, req *DescribeKeyRequest) (*DescribeKeyResponse, error)
  14. // GetKeyID resolves a key alias or ARN to the actual key ID
  15. GetKeyID(ctx context.Context, keyIdentifier string) (string, error)
  16. // Close cleans up any resources used by the provider
  17. Close() error
  18. }
  19. // GenerateDataKeyRequest contains parameters for generating a data key
  20. type GenerateDataKeyRequest struct {
  21. KeyID string // KMS key identifier (ID, ARN, or alias)
  22. KeySpec KeySpec // Specification for the data key
  23. EncryptionContext map[string]string // Additional authenticated data
  24. }
  25. // GenerateDataKeyResponse contains the generated data key
  26. type GenerateDataKeyResponse struct {
  27. KeyID string // The actual KMS key ID used
  28. Plaintext []byte // The plaintext data key (sensitive - clear from memory ASAP)
  29. CiphertextBlob []byte // The encrypted data key for storage
  30. }
  31. // DecryptRequest contains parameters for decrypting a data key
  32. type DecryptRequest struct {
  33. CiphertextBlob []byte // The encrypted data key
  34. EncryptionContext map[string]string // Must match the context used during encryption
  35. }
  36. // DecryptResponse contains the decrypted data key
  37. type DecryptResponse struct {
  38. KeyID string // The KMS key ID that was used for encryption
  39. Plaintext []byte // The decrypted data key (sensitive - clear from memory ASAP)
  40. }
  41. // DescribeKeyRequest contains parameters for describing a key
  42. type DescribeKeyRequest struct {
  43. KeyID string // KMS key identifier (ID, ARN, or alias)
  44. }
  45. // DescribeKeyResponse contains key metadata
  46. type DescribeKeyResponse struct {
  47. KeyID string // The actual key ID
  48. ARN string // The key ARN
  49. Description string // Key description
  50. KeyUsage KeyUsage // How the key can be used
  51. KeyState KeyState // Current state of the key
  52. Origin KeyOrigin // Where the key material originated
  53. }
  54. // KeySpec specifies the type of data key to generate
  55. type KeySpec string
  56. const (
  57. KeySpecAES256 KeySpec = "AES_256" // 256-bit AES key
  58. )
  59. // KeyUsage specifies how a key can be used
  60. type KeyUsage string
  61. const (
  62. KeyUsageEncryptDecrypt KeyUsage = "ENCRYPT_DECRYPT"
  63. KeyUsageGenerateDataKey KeyUsage = "GENERATE_DATA_KEY"
  64. )
  65. // KeyState represents the current state of a KMS key
  66. type KeyState string
  67. const (
  68. KeyStateEnabled KeyState = "Enabled"
  69. KeyStateDisabled KeyState = "Disabled"
  70. KeyStatePendingDeletion KeyState = "PendingDeletion"
  71. KeyStateUnavailable KeyState = "Unavailable"
  72. )
  73. // KeyOrigin indicates where the key material came from
  74. type KeyOrigin string
  75. const (
  76. KeyOriginAWS KeyOrigin = "AWS_KMS"
  77. KeyOriginExternal KeyOrigin = "EXTERNAL"
  78. KeyOriginCloudHSM KeyOrigin = "AWS_CLOUDHSM"
  79. KeyOriginAzure KeyOrigin = "AZURE_KEY_VAULT"
  80. KeyOriginGCP KeyOrigin = "GCP_KMS"
  81. KeyOriginOpenBao KeyOrigin = "OPENBAO"
  82. KeyOriginLocal KeyOrigin = "LOCAL"
  83. )
  84. // KMSError represents an error from the KMS service
  85. type KMSError struct {
  86. Code string // Error code (e.g., "KeyUnavailableException")
  87. Message string // Human-readable error message
  88. KeyID string // Key ID that caused the error (if applicable)
  89. }
  90. func (e *KMSError) Error() string {
  91. if e.KeyID != "" {
  92. return fmt.Sprintf("KMS error %s for key %s: %s", e.Code, e.KeyID, e.Message)
  93. }
  94. return fmt.Sprintf("KMS error %s: %s", e.Code, e.Message)
  95. }
  96. // Common KMS error codes
  97. const (
  98. ErrCodeKeyUnavailable = "KeyUnavailableException"
  99. ErrCodeAccessDenied = "AccessDeniedException"
  100. ErrCodeNotFoundException = "NotFoundException"
  101. ErrCodeInvalidKeyUsage = "InvalidKeyUsageException"
  102. ErrCodeKMSInternalFailure = "KMSInternalException"
  103. ErrCodeInvalidCiphertext = "InvalidCiphertextException"
  104. )
  105. // EncryptionContextKey constants for building encryption context
  106. const (
  107. EncryptionContextS3ARN = "aws:s3:arn"
  108. EncryptionContextS3Bucket = "aws:s3:bucket"
  109. EncryptionContextS3Object = "aws:s3:object"
  110. )
  111. // BuildS3EncryptionContext creates the standard encryption context for S3 objects
  112. // Following AWS S3 conventions from the documentation
  113. func BuildS3EncryptionContext(bucketName, objectKey string, useBucketKey bool) map[string]string {
  114. context := make(map[string]string)
  115. if useBucketKey {
  116. // When using S3 Bucket Keys, use bucket ARN as encryption context
  117. context[EncryptionContextS3ARN] = fmt.Sprintf("arn:aws:s3:::%s", bucketName)
  118. } else {
  119. // For individual object encryption, use object ARN as encryption context
  120. context[EncryptionContextS3ARN] = fmt.Sprintf("arn:aws:s3:::%s/%s", bucketName, objectKey)
  121. }
  122. return context
  123. }
  124. // ClearSensitiveData securely clears sensitive byte slices
  125. func ClearSensitiveData(data []byte) {
  126. if data != nil {
  127. for i := range data {
  128. data[i] = 0
  129. }
  130. }
  131. }