google_cloud_env_secrets 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 90ac38d180204dec944c36ba57e7072e36e87dadbafee415fb864a25b0b24fcf
4
- data.tar.gz: 8373386dd3089abdad83128d2bff2ca5ac2d8ff954389a1f6810d67f6aee883f
3
+ metadata.gz: 8cae42f0b4569a668a8cd720672427c63862a2873c3029d811f6ffbe784ca0a5
4
+ data.tar.gz: fdff6f40d8b416e10e77b8097b23e6c222295d6cc02e90d981c3ad82b2176c36
5
5
  SHA512:
6
- metadata.gz: 0c9f4cfb80145fc506d78949ec3053e3a16994443a8245176d120f2c4597619eef889f62dd8997a79cd9979610c0a43dddfc158b54be2e9d0aa298427fde58e4
7
- data.tar.gz: 96d34802b6c79246c8252f96cd6635de3c1ee3d8ef55ccc993264f350488fa5449d6f57a3a272d14360b48f88e6d88b9e159c716f45319bd7ea000ad8c809f53
6
+ metadata.gz: c48d2996eb230b92c7a48fe5b8610bc1267d1da340237c53e18311506799a7a0e13978c1a8edb1d503e4aefa1badffe2b4ed2cdc3b4ea386a26dc6052ba836e5
7
+ data.tar.gz: 37de843da13be35009b5371c98403ee1ec8420182160b3ab8f7211e9db8a450a164b915e6c015ec3553b9f9d72b32d847956824a71b677915ae823e6e636fccf
data/README.md CHANGED
@@ -28,7 +28,12 @@ Configure this gem with environment vars:
28
28
  | `GOOGLE_APPLICATION_CREDENTIALS` | Manually set path to Google Application Credentials. |
29
29
  | `GOOGLE_PROJECT` | Manually set the Google project. Automatically detected otherwise. |
30
30
  | `GOOGLE_SECRETS_PREFIX` | Only load secrets that start with prefix. |
31
+ | `GOOGLE_SECRETS_FORCE` | Replace existing ENV vars with secret's value. Default `true`. |
31
32
 
33
+ Google Secrets are available after the [before_configuration hook](https://guides.rubyonrails.org/configuring.html#initialization-events).
34
+ You can call `GoogleCloudEnvSecrets.load` if you need the ENV secrets sooner than that.
35
+
36
+ See [docs](https://www.rubydoc.info/github/mattes/rails_google_cloud_env_secrets/main), too.
32
37
 
33
38
  ## Required IAM Roles
34
39
 
@@ -4,9 +4,11 @@ module GoogleCloudEnvSecrets
4
4
  attr_accessor :credentials
5
5
  attr_accessor :cache_secrets
6
6
  attr_accessor :prefix
7
+ attr_accessor :force
7
8
 
8
9
  def initialize
9
10
  @cache_secrets = true
11
+ @force = true
10
12
  end
11
13
  end
12
14
 
@@ -1,14 +1,25 @@
1
1
  module GoogleCloudEnvSecrets
2
2
  class Railtie < ::Rails::Railtie
3
- initializer "google_cloud_env_secrets.initialize", after: :bootstrap_hook do |app|
4
- GoogleCloudEnvSecrets.configure do |config|
5
- config.credentials = ENV["GOOGLE_APPLICATION_CREDENTIALS"] || nil
6
- config.project = ENV["GOOGLE_PROJECT"] || Google::Cloud.env.project_id
7
- config.prefix = ENV["GOOGLE_SECRETS_PREFIX"] || nil
8
- end
9
3
 
10
- secrets = GoogleCloudEnvSecrets.all
11
- GoogleCloudEnvSecrets.inject_env!(secrets)
4
+ # load Google Secrets during Rails `before_configuration` hook
5
+ config.before_configuration do
6
+ GoogleCloudEnvSecrets.load
12
7
  end
13
8
  end
9
+
10
+ # load Google Secrets into ENV
11
+ def self.load
12
+ GoogleCloudEnvSecrets.configure do |config|
13
+ config.credentials = ENV["GOOGLE_APPLICATION_CREDENTIALS"] || nil
14
+ config.project = ENV["GOOGLE_PROJECT"] || Google::Cloud.env.project_id
15
+ config.prefix = ENV["GOOGLE_SECRETS_PREFIX"] || nil
16
+
17
+ if ENV.has_key?("GOOGLE_SECRETS_FORCE")
18
+ config.force = ENV["GOOGLE_SECRETS_FORCE"]&.to_s&.downcase == "true"
19
+ end
20
+ end
21
+
22
+ secrets = GoogleCloudEnvSecrets.all
23
+ GoogleCloudEnvSecrets.inject_env!(secrets, GoogleCloudEnvSecrets.configuration.force)
24
+ end
14
25
  end
@@ -2,6 +2,11 @@ module GoogleCloudEnvSecrets
2
2
  def self.all
3
3
  @secrets = nil unless self.configuration.cache_secrets
4
4
  @secrets ||= begin
5
+ # Skip if not running on Google Cloud and credentials are not set explicitly
6
+ if self.configuration.credentials.nil? && Google::Cloud.env.project_id.nil?
7
+ return {}
8
+ end
9
+
5
10
  # Configure and initialize
6
11
  # https://googleapis.dev/ruby/google-cloud-secret_manager/latest/Google/Cloud/SecretManager.html
7
12
  Google::Cloud::SecretManager.configure do |config|
@@ -38,17 +43,22 @@ module GoogleCloudEnvSecrets
38
43
 
39
44
  secrets
40
45
  end
41
- @secrets
46
+
47
+ @secrets || {}
42
48
  end
43
49
 
44
50
  def self.find(name)
45
- self.all # make sure we have the secrets loaded
46
- @secrets[name.to_s]
51
+ self.all[name.to_s]
47
52
  end
48
53
 
49
- def self.inject_env!(secrets = {})
54
+ def self.inject_env!(secrets = {}, force = true, env = ENV)
50
55
  secrets.each do |name, value|
51
- ENV[name.to_s] = value
56
+ name = name.to_s
57
+ if force
58
+ env[name] = value
59
+ else
60
+ env[name] unless env.has_key?(name)
61
+ end
52
62
  end
53
63
  end
54
64
  end
@@ -1,3 +1,3 @@
1
1
  module GoogleCloudEnvSecrets
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google_cloud_env_secrets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthias Kadenbach
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-09 00:00:00.000000000 Z
11
+ date: 2020-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails