Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move secrets to ENV #13268

Open
wants to merge 37 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
73e44af
Move secrets to ENV
alecslupu Jul 31, 2024
8634bda
Remove more .secrets occurences
alecslupu Jul 31, 2024
91bc534
Fix failing specs
alecslupu Jul 31, 2024
012081b
Fix system
alecslupu Jul 31, 2024
f3b290c
fix specs
alecslupu Aug 1, 2024
4569e08
Fix more tests
alecslupu Aug 1, 2024
aae814f
Fix more specs
alecslupu Aug 1, 2024
ad69d2f
Refactor
alecslupu Aug 2, 2024
85acec6
Remove some secret references
alecslupu Aug 2, 2024
ce5a250
Fix Env
alecslupu Aug 2, 2024
8f52f83
fix pipeline
alecslupu Aug 2, 2024
4b08369
Merge branch 'develop' into chore/remove-secrets
alecslupu Aug 14, 2024
60641cf
Merge branch 'develop' of github.com:decidim/decidim into chore/remov…
alecslupu Sep 9, 2024
8ea739d
Merge branch 'develop' of github.com:decidim/decidim into chore/remov…
alecslupu Sep 17, 2024
712c0e1
Fix specs
alecslupu Sep 17, 2024
cbc94a9
Running linters
alecslupu Sep 17, 2024
54df9ea
fix spec
alecslupu Sep 17, 2024
516c4e6
Merge branch 'develop' of github.com:decidim/decidim into chore/remov…
alecslupu Sep 20, 2024
1feb8b9
Merge branch 'develop' of github.com:decidim/decidim into chore/remov…
alecslupu Sep 20, 2024
89a75de
Simplify env variable usage
alecslupu Sep 20, 2024
45a5b07
Fix generation
alecslupu Sep 20, 2024
7ee6849
Fix pipeline
alecslupu Sep 21, 2024
649fa9f
Merge branch 'develop' of github.com:decidim/decidim into chore/remov…
alecslupu Oct 4, 2024
42878e6
Merge branch 'develop' of github.com:decidim/decidim into chore/remov…
alecslupu Oct 8, 2024
85ee841
Merge branch 'develop' of github.com:decidim/decidim into chore/remov…
alecslupu Oct 19, 2024
bf4a417
Fix pages spec
alecslupu Oct 19, 2024
c3377f4
Adjust omniauth_providers
alecslupu Oct 19, 2024
7f3c54f
Merge branch 'develop' of github.com:decidim/decidim into chore/remov…
alecslupu Oct 30, 2024
ad0ef46
Merge branch 'develop' of github.com:decidim/decidim into chore/remov…
alecslupu Nov 4, 2024
bd1bbe1
Fix pipeline
alecslupu Nov 4, 2024
785df93
Merge branch 'develop' of github.com:decidim/decidim into chore/remov…
alecslupu Dec 9, 2024
53c955a
Running linters
alecslupu Dec 9, 2024
1ececa0
Merge branch 'develop' of github.com:decidim/decidim into chore/remov…
alecslupu Dec 13, 2024
04bf2d7
Merge branch 'develop' into chore/remove-secrets
alecslupu Dec 19, 2024
476d59c
Merge branch 'develop' into chore/remove-secrets
alecslupu Dec 23, 2024
d3ac20e
Merge branch 'develop' of github.com:decidim/decidim into chore/remov…
alecslupu Dec 23, 2024
6c19db1
Merge branch 'chore/remove-secrets' of github.com:decidim/decidim int…
alecslupu Dec 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def direct_message_types
end

def meet_push_notifications_requirements?
Rails.application.secrets.dig(:vapid, :enabled) || false
Decidim::Env.new("VAPID_PUBLIC_KEY").present?
end
end
end
2 changes: 1 addition & 1 deletion decidim-core/app/models/decidim/omniauth_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Decidim
class OmniauthProvider
def self.available
Rails.application.secrets[:omniauth] || {}
Decidim.omniauth_providers
end

def self.enabled
Expand Down
6 changes: 3 additions & 3 deletions decidim-core/app/services/decidim/send_push_notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SendPushNotification
#
# @return [Array<Net::HTTPCreated>, nil] the result of the dispatch or nil if user or subscription are empty
def perform(notification, title = nil)
return unless Rails.application.secrets.dig(:vapid, :enabled)
return if Decidim::Env.new("VAPID_PUBLIC_KEY").blank?
raise ArgumentError, "Need to provide a title if the notification is a PushNotificationMessage" if notification.is_a?(Decidim::PushNotificationMessage) && title.nil?

user = notification.user
Expand Down Expand Up @@ -66,8 +66,8 @@ def build_payload(message_params, subscription)
p256dh: subscription["p256dh"],
auth: subscription["auth"],
vapid: {
public_key: Rails.application.secrets.vapid[:public_key],
private_key: Rails.application.secrets.vapid[:private_key]
public_key: ENV.fetch("VAPID_PUBLIC_KEY", nil),
private_key: ENV.fetch("VAPID_PRIVATE_KEY", nil)
}
}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
</div>
</div>

<input id="vapidPublicKey" name="vapid_public_key" type="hidden" value="<%= Base64.urlsafe_decode64(Rails.application.secrets.vapid[:public_key]).bytes %>">
<input id="vapidPublicKey" name="vapid_public_key" type="hidden" value="<%= Base64.urlsafe_decode64(ENV["VAPID_PUBLIC_KEY"]).bytes %>">
<input id="subKeys" name="sub_key" type="hidden" value="<%= current_user.notifications_subscriptions.keys %>">
<% end %>

Expand Down
2 changes: 1 addition & 1 deletion decidim-core/config/initializers/omniauth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def setup_provider_proc(provider, config_mapping = {})
end

Rails.application.config.middleware.use OmniAuth::Builder do
omniauth_config = Rails.application.secrets[:omniauth]
omniauth_config = Decidim.omniauth_providers

if omniauth_config
if omniauth_config[:developer].present?
Expand Down
2 changes: 1 addition & 1 deletion decidim-core/lib/decidim/asset_router/storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def default_options
# @return [Hash] The remote storage options hash
def remote_storage_options
@remote_storage_options ||= {
host: Rails.application.secrets.dig(:storage, :cdn_host)
host: ENV.fetch("STORAGE_CDN_HOST", nil)
}.compact
end

Expand Down
27 changes: 27 additions & 0 deletions decidim-core/lib/decidim/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,33 @@ def self.reset_all_column_information
{}
end

config_accessor :omniauth_providers do
{
developer: {
enabled: Rails.env.development? || Rails.env.test?,
icon: "phone-line"
},
facebook: {
enabled: Decidim::Env.new("OMNIAUTH_FACEBOOK_APP_ID").present?,
app_id: ENV.fetch("OMNIAUTH_FACEBOOK_APP_ID", nil),
app_secret: ENV.fetch("OMNIAUTH_FACEBOOK_APP_SECRET", nil),
icon: "facebook-fill"
},
twitter: {
enabled: Decidim::Env.new("OMNIAUTH_TWITTER_API_KEY").present?,
api_key: ENV.fetch("OMNIAUTH_TWITTER_API_KEY", nil),
api_secret: ENV.fetch("OMNIAUTH_TWITTER_API_SECRET", nil),
icon: "twitter-x-fill"
},
google_oauth2: {
enabled: Decidim::Env.new("OMNIAUTH_GOOGLE_CLIENT_ID").present?,
icon: "google-fill",
client_id: ENV.fetch("OMNIAUTH_GOOGLE_CLIENT_ID", nil),
client_secret: ENV.fetch("OMNIAUTH_GOOGLE_CLIENT_SECRET", nil)
}
}
end

# Public: Registers a global engine. This method is intended to be used
# by component engines that also offer unscoped functionality
#
Expand Down
4 changes: 2 additions & 2 deletions decidim-core/lib/decidim/organization_settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ def defaults_hash
end

def default_maximum_attachment_size
(Rails.application.secrets.decidim[:maximum_attachment_size].presence || 10).to_f
Decidim::Env.new("DECIDIM_MAXIMUM_ATTACHMENT_SIZE", "10").to_f
end

def default_maximum_avatar_size
(Rails.application.secrets.decidim[:maximum_avatar_size].presence || 5).to_f
Decidim::Env.new("DECIDIM_MAXIMUM_AVATAR_SIZE", "5").to_f
end
end

Expand Down
6 changes: 3 additions & 3 deletions decidim-core/spec/forms/notifications_settings_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ module Decidim
describe "#meet_push_notifications_requirements?" do
context "when the notifications requirements are met" do
before do
Rails.application.secrets[:vapid] = { enabled: true }
ENV["VAPID_PUBLIC_KEY"] = "FOO BAR"
end

it "returns true" do
Expand All @@ -188,7 +188,7 @@ module Decidim

context "when vapid secrets are not present" do
before do
Rails.application.secrets.delete(:vapid)
ENV["VAPID_PUBLIC_KEY"] = ""
end

it "returns false" do
Expand All @@ -198,7 +198,7 @@ module Decidim

context "when the notifications requirements are not met" do
before do
Rails.application.secrets[:vapid] = { enabled: false }
ENV["VAPID_PUBLIC_KEY"] = nil
end

it "returns false" do
Expand Down
3 changes: 1 addition & 2 deletions decidim-core/spec/lib/asset_router/storage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,7 @@ module Decidim::AssetRouter

context "when the CDN host is defined" do
before do
allow(Rails.application.secrets).to receive(:dig).and_call_original
allow(Rails.application.secrets).to receive(:dig).with(:storage, :cdn_host).and_return("https://cdn.example.org")
ENV["STORAGE_CDN_HOST"] = "https://cdn.example.org"
end

it "creates the route to the CDN blob" do
Expand Down
4 changes: 1 addition & 3 deletions decidim-core/spec/lib/attribute_encryptor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ module Decidim
before do
# Temporarily change the secret so that it matches the secret used
# when encrypting the value.
allow(Rails.application.secrets).to receive(
:secret_key_base
).and_return("testsecret")
allow(Rails.application).to receive(:secret_key_base).and_return("testsecret")
end

it "returns the decrypted value" do
Expand Down
3 changes: 2 additions & 1 deletion decidim-core/spec/lib/decidim_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@
describe ".force_ssl" do
let!(:orig_force_ssl) { described_class.force_ssl }
let(:rails_env) { "test" }
let(:env) { ActiveSupport::EnvironmentInquirer.new(rails_env) }

before do
allow(Rails).to receive(:env).and_return(rails_env)
allow(Rails).to receive(:env).and_return(env)
load "#{Decidim::Core::Engine.root}/lib/decidim/core.rb"
end

Expand Down
5 changes: 3 additions & 2 deletions decidim-core/spec/lib/organization_settings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,9 @@ module Decidim
let(:maximum_attachment_size) { 20 }

before do
allow(Rails.application.secrets.decidim).to receive(:[]).and_call_original
allow(Rails.application.secrets.decidim).to receive(:[]).with(:maximum_attachment_size).and_return(maximum_attachment_size)
allow(ENV).to receive(:fetch).and_call_original
allow(ENV).to receive(:fetch).with("DECIDIM_MAXIMUM_ATTACHMENT_SIZE", nil).and_return(maximum_attachment_size.to_s)

# defaults method is memoized, we need to reset it to make sure it uses the stubbed values
described_class.instance_variable_set(:@defaults, nil)
end
Expand Down
15 changes: 9 additions & 6 deletions decidim-core/spec/models/decidim/organization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ module Decidim
facebook: {
enabled: true,
app_id: "fake-facebook-app-id",
app_secret: "fake-facebook-app-secret"
app_secret: "fake-facebook-app-secret",
icon: "phone"
},
twitter: {
enabled: true,
api_key: "fake-twitter-api-key",
api_secret: "fake-twitter-api-secret"
api_secret: "fake-twitter-api-secret",
icon: "phone"
},
google_oauth2: {
enabled: true,
client_id: nil,
client_secret: nil
client_secret: nil,
icon: "phone"
}
}
end
Expand Down Expand Up @@ -96,14 +99,14 @@ module Decidim
end

context "when providers are not enabled in secrets.yml" do
let!(:previous_omniauth_secrets) { Rails.application.secrets[:omniauth] }
let!(:previous_omniauth_secrets) { Decidim.omniauth_providers }

before do
Rails.application.secrets[:omniauth] = nil
Decidim.omniauth_providers = {}
end

after do
Rails.application.secrets[:omniauth] = previous_omniauth_secrets
Decidim.omniauth_providers = previous_omniauth_secrets
end

it "returns no providers" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
let(:user) { create(:user, notification_settings: { subscriptions: }) }

before do
Rails.application.secrets[:vapid] = { enabled: true, public_key: "public_key", private_key: "private_key" }
ENV["VAPID_PUBLIC_KEY"] = "public_key"
ENV["VAPID_PRIVATE_KEY"] = "private_key"
end

shared_examples "send a push notification" do
context "without vapid settings config" do
before do
Rails.application.secrets.delete(:vapid)
ENV["VAPID_PUBLIC_KEY"] = ""
ENV["VAPID_PRIVATE_KEY"] = ""
end

describe "#perform" do
Expand All @@ -27,7 +29,7 @@

context "without vapid enabled" do
before do
Rails.application.secrets[:vapid] = { enabled: false }
ENV["VAPID_PUBLIC_KEY"] = ""
end

describe "#perform" do
Expand Down
8 changes: 5 additions & 3 deletions decidim-core/spec/system/account_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,9 @@

context "when VAPID keys are set" do
before do
Rails.application.secrets[:vapid] = vapid_keys
ENV["VAPID_PUBLIC_KEY"] = vapid_keys[:public_key]
ENV["VAPID_PRIVATE_KEY"] = vapid_keys[:private_key]

driven_by(:pwa_chrome)
switch_to_host(organization.host)
login_as user, scope: :user
Expand Down Expand Up @@ -383,7 +385,7 @@

context "when VAPID is disabled" do
before do
Rails.application.secrets[:vapid] = { enabled: false }
ENV["VAPID_PUBLIC_KEY"] = ""
driven_by(:pwa_chrome)
switch_to_host(organization.host)
login_as user, scope: :user
Expand All @@ -397,7 +399,7 @@

context "when VAPID keys are not set" do
before do
Rails.application.secrets.delete(:vapid)
ENV["VAPID_PUBLIC_KEY"] = nil
driven_by(:pwa_chrome)
switch_to_host(organization.host)
login_as user, scope: :user
Expand Down
4 changes: 2 additions & 2 deletions decidim-core/spec/uploaders/application_uploader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ module Decidim
before do
allow(Rails.env).to receive(:development?).and_return(false)
allow(Rails.env).to receive(:test?).and_return(false)
allow(Rails.application.secrets).to receive(:dig).and_call_original
allow(Rails.application.secrets).to receive(:dig).with(:storage, :cdn_host).and_return(cdn_host)
allow(ENV).to receive(:fetch).and_call_original
allow(ENV).to receive(:fetch).with("STORAGE_CDN_HOST", nil).and_return(cdn_host)
end

it "returns a URL containing the CDN configurations" do
Expand Down
6 changes: 3 additions & 3 deletions decidim-generators/lib/decidim/generators/app_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def add_storage_provider
abort("#{providers} is not supported as storage provider, please use local, s3, gcs or azure") unless (providers - %w(local s3 gcs azure)).empty?
gsub_file "config/environments/production.rb",
/config.active_storage.service = :local/,
"config.active_storage.service = Rails.application.secrets.dig(:storage, :provider) || :local"
%{config.active_storage.service = Decidim::Env.new("STORAGE_PROVIDER", "local").to_s}

add_production_gems do
gem "aws-sdk-s3", require: false if providers.include?("s3")
Expand Down Expand Up @@ -335,8 +335,8 @@ def decidim_initializer
/#{Regexp.escape("# config.available_locales = %w(en ca es)")}/,
"config.available_locales = %w(#{options[:locales].gsub(",", " ")})"
gsub_file "config/initializers/decidim.rb",
/#{Regexp.escape("config.available_locales = Rails.application.secrets.decidim[:available_locales].presence || [:en]")}/,
"# config.available_locales = Rails.application.secrets.decidim[:available_locales].presence || [:en]"
/#{Regexp.escape("config.available_locales = Decidim::Env.new(\"DECIDIM_AVAILABLE_LOCALES\", \"ca,cs,de,en,es,eu,fi,fr,it,ja,nl,pl,pt,ro\").to_array.to_json")}/,
"# config.available_locales = Decidim::Env.new(\"DECIDIM_AVAILABLE_LOCALES\", \"ca,cs,de,en,es,eu,fi,fr,it,ja,nl,pl,pt,ro\").to_array.to_json"
end

def dev_performance_config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ test:
<<: *default
database: <%= app_name %>_test<%%= ENV.fetch('TEST_ENV_NUMBER', "") %>

# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
# You never want to store sensitive information, like your database password,
# in your source code. If your source code is ever seen by anyone,
# they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
Expand Down
Loading
Loading