-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
2 changed files
with
35 additions
and
32 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,24 @@ | ||
class EncryptedParameters | ||
# Class that manages KMS keys in AWS. | ||
class KmsKey | ||
attr_reader :arn | ||
module Moonshot | ||
class EncryptedParameters | ||
# Class that manages KMS keys in AWS. | ||
class KmsKey | ||
attr_reader :arn | ||
|
||
def initialize(arn) | ||
@arn = arn | ||
@kms_client = Aws::KMS::Client.new | ||
end | ||
def initialize(arn) | ||
@arn = arn | ||
@kms_client = Aws::KMS::Client.new | ||
end | ||
|
||
def self.create | ||
resp = Aws::KMS::Client.new.create_key | ||
arn = resp.key_metadata.arn | ||
def self.create | ||
resp = Aws::KMS::Client.new.create_key | ||
arn = resp.key_metadata.arn | ||
|
||
new(arn) | ||
end | ||
new(arn) | ||
end | ||
|
||
def delete | ||
@kms_client.schedule_key_deletion(key_id: @arn, pending_window_in_days: 7) | ||
def delete | ||
@kms_client.schedule_key_deletion(key_id: @arn, pending_window_in_days: 7) | ||
end | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,24 @@ | ||
require 'base64' | ||
module Moonshot | ||
class EncryptedParameters | ||
# Class that can encrypt and decrypt parameters using KMS. | ||
class ParameterEncrypter | ||
# @param [String] key_arn The ARN for the KMS key. | ||
def initialize(key_arn) | ||
@kms_client = Aws::KMS::Client.new | ||
@key_arn = key_arn | ||
end | ||
|
||
class EncryptedParameters | ||
# Class that can encrypt and decrypt parameters using KMS. | ||
class ParameterEncrypter | ||
# @param [String] key_arn The ARN for the KMS key. | ||
def initialize(key_arn) | ||
@kms_client = Aws::KMS::Client.new | ||
@key_arn = key_arn | ||
end | ||
|
||
# Encrypt and base64 encode the parameter value. | ||
# | ||
# @param [String] param_value The parameter to encrypt. | ||
# @return [String] base64 encoded encrypted ciphertext. | ||
def encrypt(param_value) | ||
resp = @kms_client.encrypt(key_id: @key_arn, plaintext: param_value) | ||
# Encrypt and base64 encode the parameter value. | ||
# | ||
# @param [String] param_value The parameter to encrypt. | ||
# @return [String] base64 encoded encrypted ciphertext. | ||
def encrypt(param_value) | ||
resp = @kms_client.encrypt(key_id: @key_arn, plaintext: param_value) | ||
|
||
# Use strict here to avoid newlines which cause issues with parameters. | ||
Base64.strict_encode64(resp.ciphertext_blob) | ||
# Use strict here to avoid newlines which cause issues with parameters. | ||
Base64.strict_encode64(resp.ciphertext_blob) | ||
end | ||
end | ||
end | ||
end |