Last active
April 17, 2018 11:30
-
-
Save alsmola/691ede1589f5e71c609ee57705a0fb28 to your computer and use it in GitHub Desktop.
Confidant style KMS-based authentication in Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 | |
} |
So timely! Thank you for this!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ryan-lane Done, I think. Let me know if that works.