mirror of
https://github.com/hanzokms/terraform-provider.git
synced 2026-10-30 20:29:45 +00:00
72 lines
2.0 KiB
Go
72 lines
2.0 KiB
Go
package kmsclient
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
type DecodedSymmetricEncryptionDetails = struct {
|
|
Cipher []byte
|
|
IV []byte
|
|
Tag []byte
|
|
Key []byte
|
|
}
|
|
|
|
// Define a generic not found error type.
|
|
type NotFoundError struct {
|
|
Resource string
|
|
ID string
|
|
}
|
|
|
|
func GetBase64DecodedSymmetricEncryptionDetails(key string, cipher string, IV string, tag string) (DecodedSymmetricEncryptionDetails, error) {
|
|
cipherx, err := base64.StdEncoding.DecodeString(cipher)
|
|
if err != nil {
|
|
return DecodedSymmetricEncryptionDetails{}, fmt.Errorf("Base64DecodeSymmetricEncryptionDetails: Unable to decode cipher text [err=%v]", err)
|
|
}
|
|
|
|
keyx, err := base64.StdEncoding.DecodeString(key)
|
|
if err != nil {
|
|
return DecodedSymmetricEncryptionDetails{}, fmt.Errorf("Base64DecodeSymmetricEncryptionDetails: Unable to decode key [err=%v]", err)
|
|
}
|
|
|
|
IVx, err := base64.StdEncoding.DecodeString(IV)
|
|
if err != nil {
|
|
return DecodedSymmetricEncryptionDetails{}, fmt.Errorf("Base64DecodeSymmetricEncryptionDetails: Unable to decode IV [err=%v]", err)
|
|
}
|
|
|
|
tagx, err := base64.StdEncoding.DecodeString(tag)
|
|
if err != nil {
|
|
return DecodedSymmetricEncryptionDetails{}, fmt.Errorf("Base64DecodeSymmetricEncryptionDetails: Unable to decode tag [err=%v]", err)
|
|
}
|
|
|
|
return DecodedSymmetricEncryptionDetails{
|
|
Key: keyx,
|
|
Cipher: cipherx,
|
|
IV: IVx,
|
|
Tag: tagx,
|
|
}, nil
|
|
}
|
|
|
|
func GetSymmetricKeyFromServiceToken(serviceToken string) (privateKey string, err error) {
|
|
serviceTokenParts := strings.SplitN(serviceToken, ".", 4)
|
|
if len(serviceTokenParts) < 4 {
|
|
return "", errors.New("invalid service token entered. Please double check your service token and try again")
|
|
}
|
|
|
|
return serviceTokenParts[3], nil
|
|
}
|
|
|
|
func (e *NotFoundError) Error() string {
|
|
return fmt.Sprintf("%s with ID %s not found", e.Resource, e.ID)
|
|
}
|
|
|
|
// A helper function to create a new NotFoundError.
|
|
func NewNotFoundError(resource, id string) error {
|
|
return &NotFoundError{
|
|
Resource: resource,
|
|
ID: id,
|
|
}
|
|
}
|