Skip to content

Commit

Permalink
Merge pull request #782 from flippercloud/rails-credentials
Browse files Browse the repository at this point in the history
Load Cloud secrets from Rails credentials
  • Loading branch information
bkeepers authored Dec 7, 2023
2 parents a268fe6 + 1fb3d2a commit 6979bc7
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ All notable changes to this project will be documented in this file.
conf.statsd = my_client
end
```
* Load cloud secrets from Rails credentials (https://github.com/flippercloud/flipper/pull/782)
```bash
$ rails credentials:edit
```
```yaml
flipper:
cloud_token: <your-cloud-token>
cloud_sync_secret: <your-webhook-secret>
```
## 1.0.0
Expand Down
4 changes: 4 additions & 0 deletions lib/flipper/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class Engine < Rails::Engine
end

initializer "flipper.default", before: :load_config_initializers do |app|
# Load cloud secrets from Rails credentials
ENV["FLIPPER_CLOUD_TOKEN"] ||= app.credentials.dig(:flipper, :cloud_token)
ENV["FLIPPER_CLOUD_SYNC_SECRET"] ||= app.credentials.dig(:flipper, :cloud_sync_secret)

require 'flipper/cloud' if cloud?

Flipper.configure do |config|
Expand Down
36 changes: 35 additions & 1 deletion spec/flipper/engine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Class.new(Rails::Application) do
config.eager_load = false
config.logger = ActiveSupport::Logger.new($stdout)
end
end.instance
end

before do
Expand Down Expand Up @@ -242,6 +242,40 @@
end
end

context 'with cloud secrets in Rails.credentials' do
around do |example|
# Create temporary directory for Rails.root to write credentials to
# Once Rails 5.2 support is dropped, this can all be replaced with
# `config.credentials.content_path = Tempfile.new.path`
Dir.mktmpdir do |dir|
Dir.chdir(dir) do
Dir.mkdir("#{dir}/config")

example.run
end
end
end

before do
# Set master key which is needed to write credentials
ENV["RAILS_MASTER_KEY"] = "a" * 32

application.credentials.write(YAML.dump({
flipper: {
cloud_token: "credentials-token",
cloud_sync_secret: "credentials-secret",
}
}))
end

it "enables cloud" do
application.initialize!
expect(ENV["FLIPPER_CLOUD_TOKEN"]).to eq("credentials-token")
expect(ENV["FLIPPER_CLOUD_SYNC_SECRET"]).to eq("credentials-secret")
expect(Flipper.instance).to be_a(Flipper::Cloud::DSL)
end
end

it "includes model methods" do
subject
require 'active_record'
Expand Down

0 comments on commit 6979bc7

Please sign in to comment.