forked from docusealco/docuseal
-
Notifications
You must be signed in to change notification settings - Fork 0
CP-11565 - Update webhook secret to load for new accounts #49
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4f08403
CP-11565 - Update webhook secret to load for new accounts
bernardodsanderson a6b1170
CP-11565 - Rubocop fix
bernardodsanderson d8115dc
CP-11565 - Make it easier to configure webhooks
bernardodsanderson 76e4fdb
CP-11565 - Make CareerPlug webhook URL configurable
bernardodsanderson e59866b
CP-11565 - Update CareerPlug webhook URL to use ENV fetch without
bernardodsanderson 6981c65
CP-11565 - Add CareerPlug webhook vars to env setup
bernardodsanderson f8a5666
Change account webhook creation from after_create to after_commit
bernardodsanderson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -174,6 +174,8 @@ fetch_env_variables() { | |
| export SECURED_STORAGE_BUCKET=$(echo "$SECRET_JSON" | jq -r '.secured_storage_bucket') | ||
| export SECURED_STORAGE_REGION=$(echo "$SECRET_JSON" | jq -r '.secured_storage_region') | ||
| export ENCRYPTION_SECRET=$(echo "$SECRET_JSON" | jq -r '.ENCRYPTION_SECRET // empty') | ||
| export CAREERPLUG_WEBHOOK_SECRET=$(echo "$SECRET_JSON" | jq -r '.CAREERPLUG_WEBHOOK_SECRET // empty') | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add CAREERPLUG_WEBHOOK_URL? |
||
| export CAREERPLUG_WEBHOOK_URL=$(echo "$SECRET_JSON" | jq -r '.CAREERPLUG_WEBHOOK_URL // empty') | ||
|
|
||
|
|
||
| # Validate that we got the values | ||
|
|
@@ -198,24 +200,11 @@ fetch_env_variables() { | |
| # Write variables to .env.staging file | ||
| echo "Writing environment variables to .env.staging..." | ||
|
|
||
| # Remove existing DB_HOST, REDIS_URL, and S3_ATTACHMENTS_BUCKET lines if they exist | ||
| # Remove existing environment variables if they exist | ||
| if [ -f "./.env.staging" ]; then | ||
| echo "Removing existing variables from .env.staging" | ||
| grep -v "^DB_HOST=" ./.env.staging > ./.env.staging.tmp || true | ||
| grep -v "^REDIS_URL=" ./.env.staging.tmp > ./.env.staging || true | ||
| grep -v "^S3_ATTACHMENTS_BUCKET=" ./.env.staging.tmp > ./.env.staging || true | ||
| grep -v "^AIRBRAKE_ID=" ./.env.staging.tmp > ./.env.staging || true | ||
| grep -v "^AIRBRAKE_KEY=" ./.env.staging.tmp > ./.env.staging || true | ||
| grep -v "^NEWRELIC_LICENSE_KEY=" ./.env.staging.tmp > ./.env.staging || true | ||
| grep -v "^NEWRELIC_APP_NAME=" ./.env.staging.tmp > ./.env.staging || true | ||
| grep -v "^NEWRELIC_MONITOR_MODE=" ./.env.staging.tmp > ./.env.staging || true | ||
| grep -v "^CF_URL=" ./.env.staging.tmp > ./.env.staging || true | ||
| grep -v "^CF_KEY_PAIR_ID=" ./.env.staging.tmp > ./.env.staging || true | ||
| grep -v "^CF_KEY_SECRET=" ./.env.staging.tmp > ./.env.staging || true | ||
| grep -v "^SECURED_STORAGE_BUCKET=" ./.env.staging.tmp > ./.env.staging || true | ||
| grep -v "^SECURED_STORAGE_REGION=" ./.env.staging.tmp > ./.env.staging || true | ||
| grep -v "^ENCRYPTION_SECRET=" ./.env.staging.tmp > ./.env.staging || true | ||
| rm -f ./.env.staging.tmp | ||
| grep -Ev "^(DB_HOST|REDIS_URL|S3_ATTACHMENTS_BUCKET|AIRBRAKE_ID|AIRBRAKE_KEY|NEWRELIC_LICENSE_KEY|NEWRELIC_APP_NAME|NEWRELIC_MONITOR_MODE|CF_URL|CF_KEY_PAIR_ID|CF_KEY_SECRET|SECURED_STORAGE_BUCKET|SECURED_STORAGE_REGION|ENCRYPTION_SECRET|CAREERPLUG_WEBHOOK_SECRET|CAREERPLUG_WEBHOOK_URL)=" ./.env.staging > ./.env.staging.tmp || true | ||
| mv ./.env.staging.tmp ./.env.staging | ||
| fi | ||
|
|
||
| # Append the new credentials | ||
|
|
@@ -239,6 +228,18 @@ fetch_env_variables() { | |
| echo "✓ ENCRYPTION_SECRET written to .env.staging" | ||
| fi | ||
|
|
||
| # Add CareerPlug webhook secret if it exists | ||
| if [ -n "$CAREERPLUG_WEBHOOK_SECRET" ]; then | ||
| echo "CAREERPLUG_WEBHOOK_SECRET=$CAREERPLUG_WEBHOOK_SECRET" >> ./.env.staging | ||
| echo "✓ CAREERPLUG_WEBHOOK_SECRET written to .env.staging" | ||
| fi | ||
|
|
||
| # Add CareerPlug webhook URL if it exists | ||
| if [ -n "$CAREERPLUG_WEBHOOK_URL" ]; then | ||
| echo "CAREERPLUG_WEBHOOK_URL=$CAREERPLUG_WEBHOOK_URL" >> ./.env.staging | ||
| echo "✓ CAREERPLUG_WEBHOOK_URL written to .env.staging" | ||
| fi | ||
|
|
||
| echo "✓ Environment variables successfully retrieved and written to .env.staging" | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| namespace :webhooks do | ||
| desc 'Configure CareerPlug webhook secret from CAREERPLUG_WEBHOOK_SECRET env var' | ||
| task configure_careerplug: :environment do | ||
| secret = ENV.fetch('CAREERPLUG_WEBHOOK_SECRET') do | ||
| if Rails.env.development? | ||
| 'development_webhook_secret' | ||
| else | ||
| abort 'CAREERPLUG_WEBHOOK_SECRET environment variable is required' | ||
| end | ||
| end | ||
|
|
||
| webhook_urls = WebhookUrl.where('url LIKE ? OR url LIKE ? OR url LIKE ?', | ||
| '%careerplug%', '%cpats%', '%localhost:3000%') | ||
|
|
||
| if webhook_urls.any? | ||
| webhook_urls.find_each do |webhook_url| | ||
| webhook_url.update!(secret: { 'X-CareerPlug-Secret' => secret }) | ||
| puts "Updated webhook secret for #{webhook_url.url}" | ||
| end | ||
| puts "Updated #{webhook_urls.count} webhook URL(s)" | ||
| else | ||
| puts 'No CareerPlug webhook URLs found. Available webhooks:' | ||
| WebhookUrl.find_each { |w| puts " - #{w.id}: #{w.url}" } | ||
| end | ||
| end | ||
|
|
||
| desc 'Set up development webhook URLs for all accounts (creates URLs + configures secret)' | ||
| task setup_development: :environment do | ||
| abort 'This task is only for development' unless Rails.env.development? | ||
|
|
||
| url = 'http://localhost:3000/api/docuseal/events' | ||
| secret = { 'X-CareerPlug-Secret' => 'development_webhook_secret' } | ||
| events = %w[form.viewed form.started form.completed form.declined] | ||
|
|
||
| created = 0 | ||
| updated = 0 | ||
|
|
||
| Account.find_each do |account| | ||
| webhook_url = WebhookUrl.find_or_initialize_by(account: account, sha1: Digest::SHA1.hexdigest(url)) | ||
|
|
||
| if webhook_url.new_record? | ||
| webhook_url.assign_attributes(url: url, events: events, secret: secret) | ||
| webhook_url.save! | ||
| created += 1 | ||
| puts "Created webhook URL for account #{account.id}: #{account.name}" | ||
| elsif webhook_url.secret != secret | ||
| webhook_url.update!(secret: secret) | ||
| updated += 1 | ||
| puts "Updated webhook secret for account #{account.id}: #{account.name}" | ||
| end | ||
| end | ||
|
|
||
| puts "Done: #{created} created, #{updated} updated" | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe Account, '#create_careerplug_webhook' do | ||
| around do |example| | ||
| original_secret = ENV.fetch('CAREERPLUG_WEBHOOK_SECRET', nil) | ||
| original_url = ENV.fetch('CAREERPLUG_WEBHOOK_URL', nil) | ||
|
|
||
| # Set required env vars for webhook creation | ||
| ENV['CAREERPLUG_WEBHOOK_SECRET'] = 'test_secret' | ||
| ENV['CAREERPLUG_WEBHOOK_URL'] = 'http://example.com/webhook' | ||
|
|
||
| example.run | ||
|
|
||
| # Restore original env vars | ||
| ENV['CAREERPLUG_WEBHOOK_SECRET'] = original_secret | ||
| ENV['CAREERPLUG_WEBHOOK_URL'] = original_url | ||
| end | ||
|
|
||
| describe 'CareerPlug webhook creation' do | ||
| it 'creates webhook after successful account creation' do | ||
| account = build(:account) | ||
| expect(account.webhook_urls).to be_empty | ||
|
|
||
| account.save! | ||
|
|
||
| expect(account.webhook_urls.count).to eq(1) | ||
| webhook = account.webhook_urls.first | ||
| expect(webhook.url).to eq('http://example.com/webhook') | ||
| expect(webhook.events).to eq(['form.viewed', 'form.started', 'form.completed', 'form.declined']) | ||
| expect(webhook.secret).to eq({ 'X-CareerPlug-Secret' => 'test_secret' }) | ||
| end | ||
|
|
||
| it 'does not create webhook if account creation fails' do | ||
| # This test verifies that after_commit behavior works correctly | ||
| # by simulating a transaction rollback | ||
|
|
||
| expect do | ||
| described_class.transaction do | ||
| create(:account) | ||
| # Simulate some error that would cause rollback | ||
| raise ActiveRecord::Rollback | ||
| end | ||
| end.not_to change(described_class, :count) | ||
|
|
||
| expect do | ||
| described_class.transaction do | ||
| create(:account) | ||
| raise ActiveRecord::Rollback | ||
| end | ||
| end.not_to change(WebhookUrl, :count) | ||
| end | ||
|
|
||
| it 'does not create webhook when CAREERPLUG_WEBHOOK_SECRET is blank' do | ||
| original_secret = ENV.fetch('CAREERPLUG_WEBHOOK_SECRET', nil) | ||
| ENV['CAREERPLUG_WEBHOOK_SECRET'] = '' | ||
|
|
||
| account = create(:account) | ||
| expect(account.webhook_urls.count).to eq(0) | ||
|
|
||
| ENV['CAREERPLUG_WEBHOOK_SECRET'] = original_secret | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was there a reason for removing this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is needed since Docker containers in ECS restart frequently, and the .env.production file persists across restarts, causing duplicates without this cleanup logic. At least that's my understanding 😅