doppler-env 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 893704f04dbd20ba4c775a8ebe6b7dea5f55105fd7a8bf906c9029011f0c096a
4
+ data.tar.gz: 15ec07069155d0e97a9e2f7d9221c57e4b84e8e411cbe71b6f88526daffd1a5b
5
+ SHA512:
6
+ metadata.gz: 920874a6b0ada6aa3274954fdcb0225dca17ede31a0999434fbc457d193411b23b7d3c0e0cc0660e042477e32f373698eceee5daa06afd769fc92dd1a9a9d7b1
7
+ data.tar.gz: 7705aed9459ed35d13ab695975b688730317852bb87889044c4a2502a75cf17fa801c2ef8c9fa1a40103e6320975103d582d1575859b67d2967763cc272508da
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0 (Aug 4, 2022)
4
+
5
+ - Initial release
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,110 @@
1
+ # doppler-env
2
+
3
+ The doppler-env gem automates the injection of Doppler secrets as environment variables into any Ruby application and works in the terminal, RubyMine, and Visual Studio Code.
4
+
5
+ **NOTE: This is not meant to be used in production deployments. For production deployments, we recommend using our [Integrations](https://docs.doppler.com/docs/integrations) or our standard run methods for [Docker](https://docs.doppler.com/docs/dockerfile) and [CLI](https://docs.doppler.com/docs/cli).**
6
+
7
+ ## Motivation
8
+
9
+ The Doppler CLI provides the easiest method of injecting secrets into your application:
10
+
11
+ ```sh
12
+ doppler run -- ruby app.rb
13
+ ```
14
+
15
+ But when debugging with RubyMine or Visual Studio Code, a vendor-specific Ruby entry-point is used, preventing the Doppler CLI from acting as the application runner. At Doppler, we go to great lengths to [prevent secrets ending up on developer's machines](https://blog.doppler.com/how-to-prevent-secrets-from-ending-up-on-developers-machines) so downloading secrets to a .env file wasn't an option.
16
+
17
+ You can replicate dotenv gem behavior by requiring doppler-env in your project to inject Doppler secrets.
18
+
19
+ ## Setup
20
+
21
+ Ensure you have [installed the Doppler CLI](https://docs.doppler.com/docs/enclave-installation) locally and have [created a Doppler Project](https://docs.doppler.com/docs/create-project). Then authorize the Doppler CLI to retrieve secrets from your workplace by running:
22
+
23
+ ```sh
24
+ doppler login
25
+ ```
26
+
27
+ Then add `doppler-env` to your Bundler `Gemfile`:
28
+
29
+ ```ruby
30
+ gem "doppler-env"
31
+ ```
32
+
33
+ ## Configuration
34
+
35
+ ### Rails
36
+
37
+ For Rails applications, all you need to do is require `doppler-env` in your `config/environments/development.rb` file:
38
+
39
+ ```
40
+ require "doppler-env/load"
41
+ ```
42
+
43
+ Make sure it's the first require in that file.
44
+
45
+ ### General
46
+
47
+ First, require `doppler-env` in your project. Make sure it's required and loaded before any other libraries. To do this you can require the library and manually call `Doppler.load`:
48
+
49
+ ```ruby
50
+ # will cause your Doppler secrets to get injected into ENV for your application,
51
+ # but will not override any pre-existing ENV variables.
52
+ require "doppler-env"
53
+ DopplerEnv.load
54
+ ```
55
+
56
+ or
57
+
58
+ ```ruby
59
+ require "doppler-env/load"
60
+ ```
61
+
62
+ You can also force it to override pre-existing ENV variables with:
63
+
64
+ ```ruby
65
+ require "doppler-env"
66
+ DopplerEnv.load!
67
+ ```
68
+
69
+ After you have the library loading in your application, you need to configure which secrets to fetch for your application by either using the CLI in the root directory of your application:
70
+
71
+ ```sh
72
+ doppler setup
73
+ ```
74
+
75
+ or by setting the `DOPPLER_PROJECT` and `DOPPLER_CONFIG` environment variables in your debug configuration within RubyMine or Visual Studio Code.
76
+
77
+ Now whenever the Ruby interpreter is invoked for your application, secrets will be injected prior to your application being run:
78
+
79
+ ```sh
80
+ ruby app.rb
81
+
82
+ [doppler-env]: DOPPLER_ENV environment variable set. Fetching secrets using Doppler CLI.
83
+ [doppler-env]: Secrets loaded successfully:
84
+ [doppler-env]: {"DOPPLER_CONFIG"=>"dev", "DOPPLER_ENVIRONMENT"=>"dev", "DOPPLER_PROJECT"=>"example"}
85
+ ```
86
+
87
+ In restrictive environments where the use of the Doppler CLI isn't possible, set a `DOPPLER_TOKEN` environment variable with a [Service Token](https://docs.doppler.com/docs/service-tokens) to fetch secrets directly from the Doppler API:
88
+
89
+
90
+ ```sh
91
+ ruby app.rb
92
+
93
+ [doppler-env]: DOPPLER_TOKEN environment variable set. Fetching secrets from Doppler API.
94
+ [doppler-env]: Secrets loaded successfully:
95
+ [doppler-env]: {"DOPPLER_CONFIG"=>"dev", "DOPPLER_ENVIRONMENT"=>"dev", "DOPPLER_PROJECT"=>"example"}
96
+ ```
97
+
98
+ ## Acknowledgements
99
+
100
+ This approach to injecting environment variables was inspired by [dotenv](https://github.com/bkeepers/dotenv).
101
+
102
+ ## Issues
103
+
104
+ For any bug reports, issues, or enhancements, please [create a repository issue](https://github.com/DopplerHQ/ruby-doppler-env/issues/new).
105
+
106
+ # Support
107
+
108
+ You can get support in the [Doppler community forum](https://community.doppler.com/), find us on [Twitter](https://twitter.com/doppler), and for bugs or feature requests, [create an issue](https://github.com/DopplerHQ/ruby-doppler-env/issues/new) on the [DopplerHQ/ruby-doppler-env](https://github.com/DopplerHQ/ruby-doppler-env) GitHub repository.
109
+
110
+ If you need help, either use our in-product support or head over to the [Doppler Community Forum](https://community.doppler.com/) to get your questions answered by a member of the Doppler support team or
@@ -0,0 +1,2 @@
1
+ require "doppler-env"
2
+ DopplerEnv.load
@@ -0,0 +1,3 @@
1
+ module DopplerEnv
2
+ VERSION = "0.1.0".freeze
3
+ end
@@ -0,0 +1,124 @@
1
+ require "net/http"
2
+ require "json"
3
+ require "open3"
4
+
5
+ module DopplerEnv
6
+ DOPPLER_TOKEN = ENV["DOPPLER_TOKEN"]
7
+ DOPPLER_PROJECT = ENV["DOPPLER_PROJECT"]
8
+ DOPPLER_CONFIG = ENV["DOPPLER_CONFIG"]
9
+ DOPPLER_URL = URI("https://api.doppler.com/v3/configs/config/secrets/download")
10
+
11
+ # get path to Ruby binary being used for debug purposes
12
+ RUBY = File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name']).sub(/.*\s.*/m, '"\&"')
13
+
14
+ module_function
15
+
16
+ def log(message)
17
+ puts "[doppler-env]: #{message}"
18
+ end
19
+
20
+ def log_debug
21
+ log "[DEBUG] Token: #{DOPPLER_TOKEN && DOPPLER_TOKEN.to_s.split(".")[0...-1].push("*****").join(".").inspect || DOPPLER_TOKEN.inspect}"
22
+ log "[DEBUG] Project: #{DOPPLER_PROJECT.inspect}"
23
+ log "[DEBUG] Config: #{DOPPLER_CONFIG.inspect}"
24
+ log "[DEBUG] Ruby path: #{RUBY}"
25
+ log "[DEBUG] Working directory: #{Dir.getwd}"
26
+ end
27
+
28
+ # loads secrets into ENV so long as the variable doesn't already exist
29
+ def load_secrets(secrets)
30
+ secrets.each do |k,v|
31
+ ENV[k] ||= v
32
+ end
33
+ log "Secrets loaded successfully:"
34
+ log " project=#{ENV["DOPPLER_PROJECT"]} config=#{ENV["DOPPLER_CONFIG"]} environment=#{ENV["DOPPLER_ENVIRONMENT"]}"
35
+ end
36
+
37
+ # loads secrets into ENV, overwriting any variables that already exist
38
+ def load_secrets!(secrets)
39
+ secrets.each do |k,v|
40
+ ENV[k] = v
41
+ end
42
+ log "Secrets loaded successfully:"
43
+ log " project=#{ENV["DOPPLER_PROJECT"]} config=#{ENV["DOPPLER_CONFIG"]} environment=#{ENV["DOPPLER_ENVIRONMENT"]}"
44
+ end
45
+
46
+ # this method expects `doppler setup` to have already been run
47
+ def fetch_secrets_cli
48
+ command = "doppler secrets download --no-file".split(" ")
49
+
50
+ begin
51
+ stdout, stderr, status = Open3.capture3(*command)
52
+ rescue Errno::ENOENT
53
+ log "No secrets loaded. The Doppler CLI is not installed. See https://docs.doppler.com/docs/install-cli."
54
+ log_debug
55
+ return
56
+ end
57
+
58
+ if status.success?
59
+ JSON.parse(stdout)
60
+ else
61
+ log "Error: No secrets loaded. CLI failed to load secrets. Please make sure `doppler setup` has been run."
62
+ log_debug
63
+ return
64
+ end
65
+ end
66
+
67
+ # if using a service token, the project and config are inferred. if using a
68
+ # personal or CLI token you need to supply a project and config.
69
+ def fetch_secrets_api
70
+ unless DOPPLER_PROJECT && DOPPLER_CONFIG || DOPPLER_TOKEN.start_with?("dp.st")
71
+ log "Error: No secrets loaded. DOPPLER_PROJECT and DOPPLER_CONFIG environment variables must be set if using a CLI or Personal Token."
72
+ log_debug
73
+ return
74
+ end
75
+ params = { project: DOPPLER_PROJECT, config: DOPPLER_CONFIG, format: "json" }
76
+ DOPPLER_URL.query = URI.encode_www_form(params)
77
+
78
+ req = Net::HTTP::Get.new(DOPPLER_URL)
79
+ req.basic_auth DOPPLER_TOKEN, ""
80
+
81
+ res = Net::HTTP.start(DOPPLER_URL.hostname, DOPPLER_URL.port, use_ssl: true) do |http|
82
+ http.request(req)
83
+ end
84
+
85
+ case res
86
+ when Net::HTTPSuccess
87
+ return JSON.parse(res.body)
88
+ when Net::HTTPUnauthorized
89
+ log "Unauthorized: No secrets loaded. Please make sure you're using a valid Doppler token."
90
+ log_debug
91
+ else
92
+ log res.inspect
93
+ log "Error: No secrets loaded. A failure occurred while attempting to load secrets."
94
+ log_debug
95
+ end
96
+ end
97
+
98
+ def load(override = false)
99
+ # if `DOPPLER_TOKEN` is set, we use the API. if that isn't set, we use the
100
+ # CLI and expect `doppler setup` to have been run already.
101
+ if DOPPLER_TOKEN
102
+ log "DOPPLER_TOKEN environment variable set. Fetching secrets from Doppler API."
103
+ secrets = fetch_secrets_api
104
+ else
105
+ log "Fetching secrets using Doppler CLI."
106
+ secrets = fetch_secrets_cli
107
+ end
108
+
109
+ if secrets
110
+ # if `override` is true, we will override any existing env
111
+ # variables that existed with new ones coming in from Doppler. otherwise,
112
+ # pre-existing variables are left untouched.
113
+ if override
114
+ load_secrets!(secrets)
115
+ else
116
+ load_secrets(secrets)
117
+ end
118
+ end
119
+ end
120
+
121
+ def load!
122
+ load(true)
123
+ end
124
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: doppler-env
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Doppler Team
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-08-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Autoload secrets from Doppler.
14
+ email:
15
+ - bot@doppler.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - CHANGELOG.md
21
+ - LICENSE
22
+ - README.md
23
+ - lib/doppler-env.rb
24
+ - lib/doppler-env/load.rb
25
+ - lib/doppler-env/version.rb
26
+ homepage: https://github.com/dopplerhq/ruby-doppler-env
27
+ licenses:
28
+ - Apache-2.0
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubygems_version: 3.3.7
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: Autoload secrets from Doppler.
49
+ test_files: []