Skip to content

Instantly share code, notes, and snippets.

@alsmola
Last active April 17, 2018 11:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alsmola/691ede1589f5e71c609ee57705a0fb28 to your computer and use it in GitHub Desktop.
Save alsmola/691ede1589f5e71c609ee57705a0fb28 to your computer and use it in GitHub Desktop.
Confidant style KMS-based authentication in Go
/*
Copyright 2016 Alex Smolen (https://alexsmolen.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"encoding/base64"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/kms"
"os"
"time"
)
type EncryptionContextValue map[string]*string
func main() {
keyID := os.Getenv("KMS_KEY_ID")
region := os.Getenv("AWS_REGION")
from := os.Args[1]
to := os.Args[2]
now := time.Now().UTC()
minutes := 60
format := "20060102T150405Z"
end := now.Add(time.Duration(minutes) * time.Minute)
startTime := now.Format(format)
endTime := end.Format(format)
context := make(EncryptionContextValue)
plaintext := `{"not_before": "` + startTime + `", "not_after": "` + endTime + `"}`
context["from"] = aws.String(from)
context["to"] = aws.String(to)
context["user_type"] = aws.String("user")
config := &aws.Config{Region: aws.String(region)}
kmsClient := kms.New(session.New(), config)
// Encrypt plaintext to get token
encrypted, err := Encrypt(kmsClient, keyID, []byte(plaintext), context)
if err != nil {
panic(err)
}
fmt.Println("Encrypted (token to use): ", base64.StdEncoding.EncodeToString(encrypted))
// Decrypt ciphertext to verify everything worked
decrypted, err := Decrypt(kmsClient, encrypted, context)
if err != nil {
panic(err)
}
fmt.Println("Decrypted: ", string(decrypted))
}
func Encrypt(kmsClient *kms.KMS, keyID string, plaintext []byte, encContext EncryptionContextValue) ([]byte, error) {
params := &kms.EncryptInput{
Plaintext: plaintext,
EncryptionContext: encContext,
GrantTokens: []*string{},
KeyId: aws.String(keyID),
}
resp, err := kmsClient.Encrypt(params)
if err != nil {
panic(err)
}
return resp.CiphertextBlob, nil
}
func Decrypt(kmsClient *kms.KMS, ciphertext []byte, encContext EncryptionContextValue) ([]byte, error) {
params := &kms.DecryptInput{
CiphertextBlob: ciphertext,
EncryptionContext: encContext,
GrantTokens: []*string{},
}
resp, err := kmsClient.Decrypt(params)
if err != nil {
panic(err)
}
return resp.Plaintext, nil
}
@ryan-lane
Copy link

This is really great! Is there any way you can license this as Apache2. I'd love to pull this into a library and add support for multiple token versions.

@alsmola
Copy link
Author

alsmola commented Sep 1, 2016

@ryan-lane Done, I think. Let me know if that works.

@StevenACoffman
Copy link

So timely! Thank you for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment