-
-
Notifications
You must be signed in to change notification settings - Fork 16
fix(web): harden observability env handling and Sentry log redaction #917
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 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
be9efdb
fix(web): harden observability env handling and Sentry log redaction
gildesmarais 1934b7d
Make Sentry logs opt-in and update deployment docs
gildesmarais bebb667
fix(web): enforce opt-in sentry log forwarding
gildesmarais 1c9af03
chore(openapi): sync generated contract artifacts
gildesmarais fd4bf7c
fix(ci): provide build metadata in docker smoke task
gildesmarais ef57f1a
fix(observability): harden sentry log bridge and docs
gildesmarais 0d4e207
fix(docs/openapi): quickstart README and deterministic openapi genera…
gildesmarais 6a312e1
docs(readme): include AUTO_SOURCE_ENABLED in quickstart exports
gildesmarais 6667d52
chore(openapi): refresh generated frontend types
gildesmarais 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Html2rss | ||
| module Web | ||
| module Boot | ||
| ## | ||
| # Configures Sentry boot-time error and structured log capture. | ||
| module Sentry | ||
| class << self | ||
| # @return [void] | ||
| def configure! | ||
| return unless configure? | ||
|
|
||
| Bundler.require(:sentry) | ||
| require 'sentry-ruby' | ||
| initialize_sentry! | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # @return [Boolean] | ||
| def configure? | ||
| RuntimeEnv.sentry_enabled? && !sentry_initialized? | ||
| end | ||
|
|
||
| # @return [void] | ||
| def initialize_sentry! | ||
| ::Sentry.init do |config| | ||
| apply_settings(config) | ||
| end | ||
| end | ||
|
|
||
| # @param config [Object] | ||
| # @return [void] | ||
| def apply_settings(config) | ||
| config.dsn = RuntimeEnv.sentry_dsn | ||
| config.environment = RuntimeEnv.rack_env | ||
| config.enable_logs = RuntimeEnv.sentry_logs_enabled? | ||
| config.send_default_pii = false | ||
| config.release = release_name | ||
| end | ||
|
|
||
| # @return [String] | ||
| def release_name | ||
| "#{RuntimeEnv.build_tag}+#{RuntimeEnv.git_sha}" | ||
| end | ||
|
|
||
| # @return [Boolean] | ||
| def sentry_initialized? | ||
| defined?(::Sentry) && ::Sentry.respond_to?(:initialized?) && ::Sentry.initialized? | ||
| end | ||
| end | ||
| end | ||
| end | ||
| 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
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,109 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Html2rss | ||
| module Web | ||
| ## | ||
| # Captures boot-time environment configuration and scrubs selected secrets | ||
| # from the process environment after validation. | ||
| module RuntimeEnv | ||
| SENSITIVE_KEYS = %w[HTML2RSS_SECRET_KEY HEALTH_CHECK_TOKEN SENTRY_DSN].freeze | ||
| BOOT_METADATA_KEYS = %w[BUILD_TAG GIT_SHA RACK_ENV SENTRY_ENABLE_LOGS].freeze | ||
|
|
||
| class << self | ||
| # @return [void] | ||
| def capture! | ||
| @values = tracked_env_values.freeze # rubocop:disable ThreadSafety/ClassInstanceVariable | ||
| scrub_sensitive_env! | ||
| nil | ||
| end | ||
|
|
||
| # @return [void] | ||
| def reset! | ||
| @values = nil # rubocop:disable ThreadSafety/ClassInstanceVariable | ||
| end | ||
|
|
||
| # @return [String] | ||
| def secret_key | ||
| fetch('HTML2RSS_SECRET_KEY') | ||
| end | ||
|
|
||
| # @return [String] | ||
| def health_check_token | ||
| fetch('HEALTH_CHECK_TOKEN', '') | ||
| end | ||
|
|
||
| # @return [String, nil] | ||
| def sentry_dsn | ||
| fetch('SENTRY_DSN', nil) | ||
| end | ||
|
|
||
| # @return [Boolean] | ||
| def sentry_enabled? | ||
| !sentry_dsn.to_s.strip.empty? | ||
| end | ||
|
|
||
| # @return [Boolean] | ||
| def sentry_logs_enabled? | ||
| parse_boolean(fetch('SENTRY_ENABLE_LOGS', 'false'), default: false) | ||
| end | ||
|
|
||
| # @return [String] | ||
| def build_tag | ||
| fetch('BUILD_TAG', 'unknown') | ||
| end | ||
|
|
||
| # @return [String] | ||
| def git_sha | ||
| fetch('GIT_SHA', 'unknown') | ||
| end | ||
|
|
||
| # @return [String] | ||
| def rack_env | ||
| fetch('RACK_ENV', ENV.fetch('RACK_ENV', 'development')) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # @param key [String] | ||
| # @param default [Object] | ||
| # @return [Object] | ||
| def fetch(key, default = :__missing__) | ||
| return ENV.fetch(key) if ENV.key?(key) | ||
|
|
||
| values = @values || {} # rubocop:disable ThreadSafety/ClassInstanceVariable | ||
| return values.fetch(key) if values.key?(key) | ||
| return default unless default == :__missing__ | ||
|
|
||
| raise KeyError, "key not found: #{key}" | ||
| end | ||
|
|
||
| # @return [Hash{String=>String}] | ||
| def tracked_env_values | ||
| (SENSITIVE_KEYS + BOOT_METADATA_KEYS).each_with_object({}) do |key, memo| | ||
| memo[key] = ENV[key] if ENV.key?(key) | ||
| end | ||
| end | ||
|
|
||
| # @return [void] | ||
| def scrub_sensitive_env! | ||
| return nil if rack_env == 'test' | ||
|
|
||
| SENSITIVE_KEYS.each { |key| ENV.delete(key) } | ||
| nil | ||
| end | ||
|
|
||
| # @param value [Object] | ||
| # @param default [Boolean] | ||
| # @return [Boolean] | ||
| def parse_boolean(value, default:) | ||
| normalized = value.to_s.strip.downcase | ||
| return true if normalized == 'true' | ||
| return false if normalized == 'false' | ||
| return default if normalized.empty? | ||
|
|
||
| raise ArgumentError, "Malformed env 'SENTRY_ENABLE_LOGS': expected true/false, got '#{value}'" | ||
| end | ||
| end | ||
| end | ||
| 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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.