Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for puppet using gcloud CLI #7

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,14 @@ jobs:
--gkms-location="${GCP_LOCATION}" \
--gkms-keyring="${GCP_KEYRING}" \
--gkms-crypto-key="${GCP_CRYPTO_KEY}" \
--gkms-auth-type="serviceaccount" \
--gkms-credentials=./credentials.json \
-n gkms -o string -s "${GITHUB_SHA}" > ./cipher.txt
bundle exec eyaml decrypt --gkms-project="${GCP_PROJECT}" \
--gkms-location="${GCP_LOCATION}" \
--gkms-keyring="${GCP_KEYRING}" \
--gkms-crypto-key="${GCP_CRYPTO_KEY}" \
--gkms-auth-type="serviceaccount" \
--gkms-credentials=./credentials.json \
-n gkms -f ./cipher.txt > ./plain.txt
echo "Input: ${GITHUB_SHA}"
Expand Down
41 changes: 32 additions & 9 deletions lib/hiera/backend/eyaml/encryptors/gkms.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
# frozen_string_literal: true

begin
require 'google/cloud/kms'
require 'google/cloud/kms/v1'
rescue LoadError
raise StandardError, 'hiera-eyaml-gkms requires the google-cloud-kms gem'
end

require 'hiera/backend/eyaml/encryptor'
require 'hiera/backend/eyaml/utils'
require 'hiera/backend/eyaml/options'
require 'hiera/backend/eyaml/encryptors/gkms/version'
require 'google/cloud/kms'
require 'google/cloud/kms/v1'

class Hiera
module Backend
Expand Down Expand Up @@ -46,6 +41,11 @@ class Gkms < Encryptor
credentials: {
desc: 'GCP Service Account credentials',
type: :string
},
use_gcloud_cli: {
desc: 'Use gcloud CLI to encrypt/decrypt secrets',
type: :bool,
default: false
}
}

Expand Down Expand Up @@ -84,11 +84,34 @@ def self.key_path
end

def self.encrypt(plaintext)
kms_client.encrypt(name: key_path, plaintext: plaintext).ciphertext
use_gcloud_cli = option :use_gcloud_cli
if use_gcloud_cli
enc_plaintext = Base64.encode64(plaintext)
project = option :project
location = option :location
key_ring = option :keyring
crypto_key = option :crypto_key
`echo "#{enc_plaintext}" | gcloud kms encrypt --location #{location} --keyring #{key_ring} --key #{crypto_key} --project #{project} --plaintext-file - --ciphertext-file -`
else
kms_client.encrypt(name: key_path, plaintext: plaintext).ciphertext
end
end

def self.decrypt(ciphertext)
kms_client.decrypt(name: key_path, ciphertext: ciphertext).plaintext
use_gcloud_cli = option :use_gcloud_cli
if use_gcloud_cli
project = option :project
location = option :location
key_ring = option :keyring
crypto_key = option :crypto_key

decryptor = Encryptor.find 'Gkms'
ciphertext = decryptor.encode(ciphertext)
resp = `echo #{ciphertext} | base64 -d | gcloud kms decrypt --location #{location} --keyring #{key_ring} --key #{crypto_key} --project #{project} --plaintext-file - --ciphertext-file -`
Base64.decode64(resp)
else
kms_client.decrypt(name: key_path, ciphertext: ciphertext).plaintext
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/hiera/backend/eyaml/encryptors/gkms/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Backend
module Eyaml
module Encryptors
module GkmsVersion
VERSION = '0.2.0'
VERSION = '0.2.1'
end
end
end
Expand Down