This repository has been archived by the owner on Jan 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Superconfig for Environment and Vault in the Backend (#273)
adds superconfig in the same way as the monolith
- Loading branch information
1 parent
1c4f4db
commit 04652fb
Showing
30 changed files
with
297 additions
and
244 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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
AWS_REGION=us-east-1 | ||
AWS_ACCESS_KEY_ID=fake-key | ||
AWS_SECRET_ACCESS_KEY=fake-access-key | ||
DYNAMO_REGISTRATIONS_TABLE=registrations-development | ||
RAILS_LOG_TO_STDOUT=true | ||
JWT_SECRET=jwt-test-secret | ||
SECRET_KEY_BASE=a003fdc6f113ff7d295596a02192c7116a76724ba6d3071043eefdd16f05971be0dc58f244e67728757b2fb55ae7a41e1eb97c1fe247ddaeb6caa97cea32120c |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
AWS_REGION=us-east-1 | ||
AWS_ACCESS_KEY_ID=fake-key | ||
AWS_SECRET_ACCESS_KEY=fake-access-key | ||
DYNAMO_REGISTRATIONS_TABLE=registrations-development | ||
JWT_SECRET=jwt-test-secret | ||
SECRET_KEY_BASE=a003fdc6f113ff7d295596a02192c7116a76724ba6d3071043eefdd16f05971be0dc58f244e67728757b2fb55ae7a41e1eb97c1fe247ddaeb6caa97cea32120c |
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
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
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
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'superconfig' | ||
|
||
EnvConfig = SuperConfig.new do | ||
# We don't have RAILS_ENV in ruby | ||
if ENV.fetch('CODE_ENVIRONMENT', 'development') == 'development' | ||
mandatory :LOCALSTACK_ENDPOINT, :string | ||
# Have to be the same as in localstack to simulate authentication | ||
mandatory :AWS_ACCESS_KEY_ID, :string | ||
mandatory :AWS_SECRET_ACCESS_KEY, :string | ||
optional :WCA_HOST, :string, '' | ||
optional :CODE_ENVIRONMENT, 'development' | ||
else | ||
mandatory :QUEUE_URL, :string | ||
mandatory :WCA_HOST, :string | ||
mandatory :CODE_ENVIRONMENT, :string | ||
end | ||
# We even need the AWS_REGION in dev because we fake authenticate with localstack | ||
mandatory :AWS_REGION, :string | ||
mandatory :PROMETHEUS_EXPORTER, :string | ||
mandatory :DYNAMO_REGISTRATIONS_TABLE, :string | ||
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
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'superconfig' | ||
|
||
require_relative 'env_config' | ||
|
||
SuperConfig::Base.class_eval do | ||
# The skeleton is stolen from the source code of the `superconfig` gem, file lib/superconfig.rb:104 | ||
# (method SuperConfig::Base#credential). The inner Vault fetching logic is custom-written :) | ||
def vault(secret_name, &block) | ||
define_singleton_method(secret_name) do | ||
@__cache__["_vault_#{secret_name}".to_sym] ||= begin | ||
value = self.vault_read(secret_name)[:value] | ||
block ? block.call(value) : value | ||
end | ||
end | ||
end | ||
|
||
private def vault_read(secret_name) | ||
Vault.with_retries(Vault::HTTPConnectionError, Vault::HTTPError) do |attempt, e| | ||
puts "Received exception #{e} from Vault - attempt #{attempt}" if e.present? | ||
|
||
secret = Vault.logical.read("kv/data/#{EnvConfig.VAULT_APPLICATION}/#{secret_name}") | ||
raise "Tried to read #{secret_name}, but doesn't exist" unless secret.present? | ||
|
||
secret.data[:data] | ||
end | ||
end | ||
end | ||
|
||
AppSecrets = SuperConfig.new do | ||
if Rails.env.production? | ||
require_relative 'vault_config' | ||
|
||
vault :JWT_SECRET | ||
vault :SECRET_KEY_BASE | ||
|
||
else | ||
mandatory :JWT_SECRET, :string | ||
mandatory :SECRET_KEY_BASE, :string | ||
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
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
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
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
This file was deleted.
Oops, something went wrong.
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
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
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
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
Oops, something went wrong.