google_cloud_env_secrets 0.1.3 → 1.0.0
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 +4 -4
- data/README.md +1 -1
- data/lib/google_cloud_env_secrets/config.rb +11 -2
- data/lib/google_cloud_env_secrets/railtie.rb +8 -4
- data/lib/google_cloud_env_secrets/secrets.rb +6 -2
- data/lib/google_cloud_env_secrets/version.rb +1 -1
- data/lib/tasks/google_cloud_env_secrets_tasks.rake +6 -2
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cf3d77b020c1903519351fb8f207f5f10f1d8df9841fd0fef85a48293cc53df5
|
4
|
+
data.tar.gz: c076fe09aaeceee9298c32260938179537bd53782cbd3138bcaa9ed56996a2b2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 26dd2b920d1847a9f527226afafdcb35645009da8930dc8d33f71faeceff49ba0816174b896d1dbbacabe434a0abd118c2a47a1fe7dc493bff9c364f8e53eaeb
|
7
|
+
data.tar.gz: 92fb6cf48c62971bad44b425166b64ba75a5992b5e41756095bb08c529c7a388df7f6f6625a4cf47ba7841ae3857ab76346975b86c46729614ec441ad1731933
|
data/README.md
CHANGED
@@ -28,7 +28,7 @@ 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
|
-
| `
|
31
|
+
| `GOOGLE_SECRETS_OVERLOAD` | Replace existing ENV vars with secret's value. Default `true`. |
|
32
32
|
|
33
33
|
Google Secrets are available after the [before_configuration hook](https://guides.rubyonrails.org/configuring.html#initialization-events).
|
34
34
|
You can call `GoogleCloudEnvSecrets.load` if you need the ENV secrets sooner than that.
|
@@ -1,14 +1,16 @@
|
|
1
|
+
require "json"
|
2
|
+
|
1
3
|
module GoogleCloudEnvSecrets
|
2
4
|
class Configuration
|
3
5
|
attr_accessor :project
|
4
6
|
attr_accessor :credentials
|
5
7
|
attr_accessor :cache_secrets
|
6
8
|
attr_accessor :prefix
|
7
|
-
attr_accessor :
|
9
|
+
attr_accessor :overload
|
8
10
|
|
9
11
|
def initialize
|
10
12
|
@cache_secrets = true
|
11
|
-
@
|
13
|
+
@overload = true
|
12
14
|
end
|
13
15
|
end
|
14
16
|
|
@@ -20,4 +22,11 @@ module GoogleCloudEnvSecrets
|
|
20
22
|
self.configuration ||= Configuration.new
|
21
23
|
yield(configuration)
|
22
24
|
end
|
25
|
+
|
26
|
+
def self.parse_project_from_credentials(credentials)
|
27
|
+
j = JSON.load(File.open(credentials))
|
28
|
+
j["project_id"]
|
29
|
+
rescue
|
30
|
+
nil
|
31
|
+
end
|
23
32
|
end
|
@@ -5,21 +5,25 @@ module GoogleCloudEnvSecrets
|
|
5
5
|
config.before_configuration do
|
6
6
|
GoogleCloudEnvSecrets.load
|
7
7
|
end
|
8
|
+
|
9
|
+
rake_tasks do
|
10
|
+
load "tasks/google_cloud_env_secrets_tasks.rake"
|
11
|
+
end
|
8
12
|
end
|
9
13
|
|
10
14
|
# load Google Secrets into ENV
|
11
15
|
def self.load
|
12
16
|
GoogleCloudEnvSecrets.configure do |config|
|
13
17
|
config.credentials = ENV["GOOGLE_APPLICATION_CREDENTIALS"] || nil
|
14
|
-
config.project = ENV["GOOGLE_PROJECT"] || Google::Cloud.env.project_id
|
18
|
+
config.project = ENV["GOOGLE_PROJECT"] || GoogleCloudEnvSecrets.parse_project_from_credentials(config.credentials) || Google::Cloud.env.project_id
|
15
19
|
config.prefix = ENV["GOOGLE_SECRETS_PREFIX"] || nil
|
16
20
|
|
17
|
-
if ENV.has_key?("
|
18
|
-
config.
|
21
|
+
if ENV.has_key?("GOOGLE_SECRETS_OVERLOAD")
|
22
|
+
config.overload = ENV["GOOGLE_SECRETS_OVERLOAD"]&.to_s&.downcase == "true"
|
19
23
|
end
|
20
24
|
end
|
21
25
|
|
22
26
|
secrets = GoogleCloudEnvSecrets.all
|
23
|
-
GoogleCloudEnvSecrets.inject_env!(secrets, GoogleCloudEnvSecrets.configuration.
|
27
|
+
GoogleCloudEnvSecrets.inject_env!(secrets, GoogleCloudEnvSecrets.configuration.overload)
|
24
28
|
end
|
25
29
|
end
|
@@ -51,10 +51,14 @@ module GoogleCloudEnvSecrets
|
|
51
51
|
self.all[name.to_s]
|
52
52
|
end
|
53
53
|
|
54
|
-
def self.
|
54
|
+
def self.exists?(name)
|
55
|
+
self.all.has_key?(name.to_s)
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.inject_env!(secrets = {}, overload = true, env = ENV)
|
55
59
|
secrets.each do |name, value|
|
56
60
|
name = name.to_s
|
57
|
-
if
|
61
|
+
if overload
|
58
62
|
env[name] = value
|
59
63
|
else
|
60
64
|
env[name] ||= value
|
@@ -1,5 +1,9 @@
|
|
1
1
|
desc "Fetch Google Cloud Secret"
|
2
2
|
task :google_cloud_secret do
|
3
|
-
name =
|
4
|
-
|
3
|
+
name = ENV["NAME"].strip
|
4
|
+
|
5
|
+
fail "#{name} not found" unless GoogleCloudEnvSecrets.exists?(name)
|
6
|
+
|
7
|
+
$stdout.sync = true
|
8
|
+
print GoogleCloudEnvSecrets.find(name)
|
5
9
|
end
|