diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 7f26da4e..86ba8cfb 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -65,6 +65,10 @@ export OTEL_TRACES_EXPORTER=otlp,console # Multiple Exporters export OTEL_TRACES_EXPORTER=jaeger # Third-party Exporters (e.g. jaeger, add gem opentelemetry-exporter-jaeger before solarwinds_apm) ``` +The API token from `SW_APM_SERVICE_KEY` is automatically added as an `authorization` header only for a SolarWinds Observability OTLP endpoint (`https://otel.collector.*.*.solarwinds.com`) and when neither the general OTLP headers variable nor that signal's headers variable has a non-empty value. It is automatically added to the signal-specific variable (e.g. `OTEL_EXPORTER_OTLP_TRACES_HEADERS`), never to `OTEL_EXPORTER_OTLP_HEADERS`. + +To route telemetry from an OTel Collector in your environment to SolarWinds Observability, please configure the API token in your OTelCol's exporter. + #### Service Name By default the service name portion of the service key is used, e.g. `my-service` if the service key is `SW_APM_SERVICE_KEY=api-token:my-service`. If the `OTEL_SERVICE_NAME` or `OTEL_RESOURCE_ATTRIBUTES` environment variable is used to specify a service name, it will take precedence over the default. The precedence is as follows: diff --git a/lib/solarwinds_apm/support/otlp_endpoint.rb b/lib/solarwinds_apm/support/otlp_endpoint.rb index 124d4e84..dc5a5add 100644 --- a/lib/solarwinds_apm/support/otlp_endpoint.rb +++ b/lib/solarwinds_apm/support/otlp_endpoint.rb @@ -6,6 +6,7 @@ # # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +require 'uri' require_relative 'service_key_checker' module SolarWindsAPM @@ -13,11 +14,10 @@ module SolarWindsAPM class OTLPEndPoint attr_reader :token, :service_name - SWO_APM_ENDPOINT_REGEX = /^apm\.collector\.([a-z]{2}-\d{2})\.([^.]+)\.solarwinds\.com(?::\d+)?$/ + SWO_APM_ENDPOINT_REGEX = /\Aapm\.collector\.([a-z]{2}-\d{2})\.([a-z0-9-]+)\.solarwinds\.com(?::\d+)?\z/ SWO_APM_ENDPOINT_DEFAULT = 'apm.collector.na-01.cloud.solarwinds.com:443' - SWO_OTLP_GENERAL_ENDPOINT_REGEX = %r{^https://otel\.collector\.[a-z0-9-]+\.[a-z0-9-]+\.solarwinds\.com(?::\d+)?$} - SWO_OTLP_SIGNAL_ENDPOINT_REGEX = %r{^https://otel\.collector\.[a-z0-9-]+\.[a-z0-9-]+\.solarwinds\.com(?::\d+)?/v1/(?:logs|metrics|traces)$} + SWO_OTLP_HOST_REGEX = /\Aotel\.collector\.[a-z0-9-]+\.[a-z0-9-]+\.solarwinds\.com\z/ SWO_OTLP_ENDPOINT_DEFAULT = 'https://otel.collector.na-01.cloud.solarwinds.com:443' OTEL_SIGNAL_TYPE = %w[TRACES METRICS LOGS].freeze @@ -37,8 +37,8 @@ def config_otlp_token_and_endpoint @service_name = service_key_checker.service_name OTEL_SIGNAL_TYPE.each do |data_type| - config_token(data_type) configure_otlp_endpoint(data_type, matches) + config_token(data_type) end end @@ -47,16 +47,13 @@ def config_otlp_token_and_endpoint # The OTLP export endpoint is SWO, i.e. host is otel.collector.*.*.solarwinds.com def config_token(data_type) data_type_upper = data_type.upcase + signal_headers_env = "OTEL_EXPORTER_OTLP_#{data_type_upper}_HEADERS" return unless @token + return unless ENV[signal_headers_env].to_s.empty? && ENV['OTEL_EXPORTER_OTLP_HEADERS'].to_s.empty? - if ENV["OTEL_EXPORTER_OTLP_#{data_type_upper}_HEADERS"].to_s.empty? && ENV["OTEL_EXPORTER_OTLP_#{data_type_upper}_ENDPOINT"].to_s.match?(SWO_OTLP_SIGNAL_ENDPOINT_REGEX) - ENV["OTEL_EXPORTER_OTLP_#{data_type_upper}_HEADERS"] = "authorization=Bearer #{@token}" - elsif ENV['OTEL_EXPORTER_OTLP_HEADERS'].to_s.empty? && ENV['OTEL_EXPORTER_OTLP_ENDPOINT'].to_s.match?(SWO_OTLP_GENERAL_ENDPOINT_REGEX) - ENV['OTEL_EXPORTER_OTLP_HEADERS'] = "authorization=Bearer #{@token}" - end - - ENV['OTEL_EXPORTER_OTLP_HEADERS'] = "authorization=Bearer #{@token}" if ENV['OTEL_EXPORTER_OTLP_HEADERS'].to_s.empty? && ENV["OTEL_EXPORTER_OTLP_#{data_type_upper}_HEADERS"].to_s.empty? + endpoint = effective_endpoint(data_type_upper) + ENV[signal_headers_env] = "authorization=Bearer #{@token}" if swo_otlp_endpoint?(endpoint) end def configure_otlp_endpoint(data_type, matches) @@ -83,14 +80,43 @@ def configure_otlp_endpoint(data_type, matches) end end + # Same precedence as the OpenTelemetry OTLP exporters: signal-specific endpoint, then the general endpoint. + def effective_endpoint(data_type_upper) + ENV.fetch("OTEL_EXPORTER_OTLP_#{data_type_upper}_ENDPOINT", nil) || ENV.fetch('OTEL_EXPORTER_OTLP_ENDPOINT', nil) + end + + # Parse the URL rather than pattern-matching the raw string, so userinfo, path or + # newline tricks cannot make a non-SWO host look like a SWO one. + def swo_otlp_endpoint?(endpoint) + return false if endpoint.to_s.empty? + + uri = URI.parse(endpoint.to_s) + uri.scheme == 'https' && uri.userinfo.nil? && uri.host.to_s.downcase.match?(SWO_OTLP_HOST_REGEX) + rescue URI::Error + false + end + + # Only scheme, host and port are logged, never userinfo or query that may carry credentials. + def sanitize_endpoint(endpoint) + uri = URI.parse(endpoint.to_s.strip) + return '' if uri.host.nil? + + "#{uri.scheme}://#{uri.host}:#{uri.port}" + rescue URI::Error + '' + end + # only valid value is apm.collector.*.*.solarwinds.com # If SW APM config for collector is not set, the fallback: apm.collector.na-01.cloud.solarwinds.com def resolve_get_setting_endpoint(matches) return if matches&.size == 3 unless ENV['SW_APM_COLLECTOR'].to_s.empty? + # SW_APM_COLLECTOR is expected as host:port; add a scheme only if the user did not, so sanitize_endpoint can parse it. + collector_value = ENV.fetch('SW_APM_COLLECTOR', nil) + collector = sanitize_endpoint(collector_value.include?('://') ? collector_value : "https://#{collector_value}") SolarWindsAPM.logger.warn do - "[#{self.class}/#{__method__}] SW_APM_COLLECTOR format invalid: #{ENV.fetch('SW_APM_COLLECTOR', nil)}. Valid format: apm.collector.*.*.solarwinds.com" + "[#{self.class}/#{__method__}] SW_APM_COLLECTOR format invalid: #{collector}. Valid format: apm.collector.*.*.solarwinds.com" end end ENV['SW_APM_COLLECTOR'] = SWO_APM_ENDPOINT_DEFAULT diff --git a/test/minitest_helper.rb b/test/minitest_helper.rb index 998fb89b..f33170a4 100644 --- a/test/minitest_helper.rb +++ b/test/minitest_helper.rb @@ -54,7 +54,7 @@ def extract(carrier, context: ::OpenTelemetry::Context.current, getter: ::OpenTe # Dummy Exporter for testing module Exporter class Dummy - def flush(); end + def flush; end end end diff --git a/test/patch/sw_mysql2_patch_integrate_test.rb b/test/patch/sw_mysql2_patch_integrate_test.rb index 0715c703..ecf34909 100644 --- a/test/patch/sw_mysql2_patch_integrate_test.rb +++ b/test/patch/sw_mysql2_patch_integrate_test.rb @@ -12,13 +12,12 @@ require './lib/solarwinds_apm/support' require './lib/solarwinds_apm/patch/tag_sql/sw_dbo_utils' -# rubocop:disable Naming/MethodName +# rubocop:disable-next Naming/MethodName module SolarWindsAPM module Span def self.createSpan(trans_name, domain, span_time, has_error); end end end -# rubocop:enable Naming/MethodName describe 'mysql2 patch integrate test' do puts "\n\033[1m=== TEST RUN MYSQL2 PATCH TEST: #{RUBY_VERSION} #{File.basename(__FILE__)} #{Time.now.strftime('%Y-%m-%d %H:%M')} ===\033[0m\n" diff --git a/test/patch/sw_pg_patch_integrate_test.rb b/test/patch/sw_pg_patch_integrate_test.rb index 1fa58aa0..f423b8ee 100644 --- a/test/patch/sw_pg_patch_integrate_test.rb +++ b/test/patch/sw_pg_patch_integrate_test.rb @@ -12,13 +12,12 @@ require './lib/solarwinds_apm/support' require './lib/solarwinds_apm/patch/tag_sql/sw_dbo_utils' -# rubocop:disable Naming/MethodName +# rubocop:disable-next Naming/MethodName module SolarWindsAPM module Span def self.createSpan(trans_name, domain, span_time, has_error); end end end -# rubocop:enable Naming/MethodName def pg_dbo_integration_verification(sql, finished_spans) pattern = %r{/\*traceparent='[\da-f-]+'*\*/$} diff --git a/test/sampling_test_helper.rb b/test/sampling_test_helper.rb index 1fb5848b..e9ebb7ac 100644 --- a/test/sampling_test_helper.rb +++ b/test/sampling_test_helper.rb @@ -133,7 +133,7 @@ def check_counters(metric_exporter, counters = []) end end -# rubocop:disable Lint/DuplicateMethods +# rubocop:disable-next Lint/DuplicateMethods class OboeTestSampler < SolarWindsAPM::OboeSampler attr_accessor :response_headers, :local_settings, :request_headers @@ -155,7 +155,6 @@ def request_headers(_params) @request_headers end end -# rubocop:enable Lint/DuplicateMethods def replace_sampler(sampler) OpenTelemetry.tracer_provider.sampler = OpenTelemetry::SDK::Trace::Samplers.parent_based( diff --git a/test/support/otlp_endpoint_test.rb b/test/support/otlp_endpoint_test.rb index a3351d78..9672388a 100644 --- a/test/support/otlp_endpoint_test.rb +++ b/test/support/otlp_endpoint_test.rb @@ -10,6 +10,9 @@ require './lib/solarwinds_apm/support/utils' # BUNDLE_GEMFILE=gemfiles/unit.gemfile bundle exec ruby -I test test/support/otlp_endpoint_test.rb +OTLP_TEST_TOKEN = '0123456789abcde0123456789abcde0123456789abcde0123456789abcde1234' +OTLP_TEST_SERVICE_KEY = "#{OTLP_TEST_TOKEN}:my_service".freeze + describe 'OTLP Endpoint Test' do before do @original_env = ENV.to_h.dup @@ -37,6 +40,18 @@ def assert_signal_endpoint_default assert_equal 'https://otel.collector.na-01.cloud.solarwinds.com:443/v1/logs', ENV.fetch('OTEL_EXPORTER_OTLP_LOGS_ENDPOINT', nil) end + def assert_signal_headers_bearer(*signals) + signals.each do |signal| + assert_equal "authorization=Bearer #{OTLP_TEST_TOKEN}", ENV.fetch("OTEL_EXPORTER_OTLP_#{signal}_HEADERS", nil) + end + end + + def assert_no_bearer_anywhere + %w[OTEL_EXPORTER_OTLP_HEADERS OTEL_EXPORTER_OTLP_TRACES_HEADERS OTEL_EXPORTER_OTLP_METRICS_HEADERS OTEL_EXPORTER_OTLP_LOGS_HEADERS].each do |name| + refute_includes ENV.fetch(name, '').to_s, OTLP_TEST_TOKEN, "#{name} must not carry the service key token" + end + end + def assert_singal_headers_nil(general_singal_header: true) assert_nil(ENV.fetch('OTEL_EXPORTER_OTLP_METRICS_HEADERS', nil)) assert_nil(ENV.fetch('OTEL_EXPORTER_OTLP_TRACES_HEADERS', nil)) @@ -60,17 +75,16 @@ def assert_singal_headers_nil(general_singal_header: true) end it 'correct formatted SW_APM_SERVICE_KEY, no auth token set for NON-SWO OTEL PROTO' do - ENV['SW_APM_SERVICE_KEY'] = '0123456789abcde0123456789abcde0123456789abcde0123456789abcde1234:my_service' + ENV['SW_APM_SERVICE_KEY'] = OTLP_TEST_SERVICE_KEY ENV['OTEL_EXPORTER_OTLP_ENDPOINT'] = 'http://0.0.0.0:4317' _setup - assert_equal '0123456789abcde0123456789abcde0123456789abcde0123456789abcde1234', @endpoint.instance_variable_get(:@token) + assert_equal OTLP_TEST_TOKEN, @endpoint.instance_variable_get(:@token) assert_equal 'http://0.0.0.0:4317', ENV.fetch('OTEL_EXPORTER_OTLP_ENDPOINT', nil) - assert_equal 'authorization=Bearer 0123456789abcde0123456789abcde0123456789abcde0123456789abcde1234', ENV.fetch('OTEL_EXPORTER_OTLP_HEADERS', nil) assert_signal_endpoint_nil - assert_singal_headers_nil(general_singal_header: false) + assert_singal_headers_nil end it 'correct formatted SW_APM_SERVICE_KEY with headers set' do @@ -112,11 +126,11 @@ def assert_singal_headers_nil(general_singal_header: true) assert_equal '0123456789abcde0123456789abcde0123456789abcde0123456789abcde1234', @endpoint.instance_variable_get(:@token) - assert_equal 'authorization=Bearer 0123456789abcde0123456789abcde0123456789abcde0123456789abcde1234', ENV.fetch('OTEL_EXPORTER_OTLP_HEADERS', nil) + assert_nil ENV.fetch('OTEL_EXPORTER_OTLP_HEADERS', nil) + assert_signal_headers_bearer('TRACES', 'METRICS', 'LOGS') assert_equal 'https://otel.collector.na-01.cloud.solarwinds.com:443', ENV.fetch('OTEL_EXPORTER_OTLP_ENDPOINT', nil) assert_signal_endpoint_nil - assert_singal_headers_nil(general_singal_header: false) end it 'wrong formatted SW_APM_SERVICE_KEY APM PROTO' do @@ -132,17 +146,18 @@ def assert_singal_headers_nil(general_singal_header: true) assert_singal_headers_nil end - it 'correct formatted SW_APM_SERVICE_KEY APM PROTO, no auth token set due to no OTEL PROTO' do - ENV['SW_APM_SERVICE_KEY'] = '0123456789abcde0123456789abcde0123456789abcde0123456789abcde1234:my_service' + it 'correct formatted SW_APM_SERVICE_KEY APM PROTO, auth token set on derived SWO signal endpoints' do + ENV['SW_APM_SERVICE_KEY'] = OTLP_TEST_SERVICE_KEY ENV['SW_APM_COLLECTOR'] = 'apm.collector.na-01.cloud.solarwinds.com:443' _setup - assert_equal '0123456789abcde0123456789abcde0123456789abcde0123456789abcde1234', @endpoint.instance_variable_get(:@token) + assert_equal OTLP_TEST_TOKEN, @endpoint.instance_variable_get(:@token) assert_nil(ENV.fetch('OTEL_EXPORTER_OTLP_ENDPOINT', nil)) + assert_nil(ENV.fetch('OTEL_EXPORTER_OTLP_HEADERS', nil)) assert_signal_endpoint_default - assert_singal_headers_nil(general_singal_header: false) + assert_signal_headers_bearer('TRACES', 'METRICS', 'LOGS') end it 'no OTEL ENDPOINT and no SW_APM_COLLECTOR' do @@ -276,6 +291,16 @@ def assert_singal_headers_nil(general_singal_header: true) assert_equal SolarWindsAPM::OTLPEndPoint::SWO_APM_ENDPOINT_DEFAULT, ENV.fetch('SW_APM_COLLECTOR', nil) end + it 'does not log credentials, path or query of an invalid collector' do + ENV['SW_APM_COLLECTOR'] = 'user:secret@collector.example.com:443/path?token=x' + endpoint = SolarWindsAPM::OTLPEndPoint.new + message = nil + SolarWindsAPM.logger.stub(:warn, ->(_msg = nil, &block) { message = block&.call }) do + endpoint.send(:resolve_get_setting_endpoint, nil) + end + assert_equal '[SolarWindsAPM::OTLPEndPoint/resolve_get_setting_endpoint] SW_APM_COLLECTOR format invalid: https://collector.example.com:443. Valid format: apm.collector.*.*.solarwinds.com', message + end + it 'falls back to default for empty collector' do ENV.delete('SW_APM_COLLECTOR') endpoint = SolarWindsAPM::OTLPEndPoint.new @@ -322,7 +347,7 @@ def assert_singal_headers_nil(general_singal_header: true) end describe 'config_token' do - it 'sets token on general headers when matching SWO endpoint' do + it 'sets token on signal headers (not general) when general endpoint is SWO' do ENV.delete('OTEL_EXPORTER_OTLP_HEADERS') ENV.delete('OTEL_EXPORTER_OTLP_TRACES_HEADERS') ENV['OTEL_EXPORTER_OTLP_ENDPOINT'] = 'https://otel.collector.na-01.cloud.solarwinds.com:443' @@ -331,7 +356,8 @@ def assert_singal_headers_nil(general_singal_header: true) endpoint.instance_variable_set(:@token, 'my-token') endpoint.config_token('TRACES') - assert_equal 'authorization=Bearer my-token', ENV.fetch('OTEL_EXPORTER_OTLP_HEADERS', nil) + assert_nil ENV.fetch('OTEL_EXPORTER_OTLP_HEADERS', nil) + assert_equal 'authorization=Bearer my-token', ENV.fetch('OTEL_EXPORTER_OTLP_TRACES_HEADERS', nil) end it 'sets token on signal headers when signal endpoint matches' do @@ -365,17 +391,164 @@ def assert_singal_headers_nil(general_singal_header: true) assert_equal 'existing=header', ENV.fetch('OTEL_EXPORTER_OTLP_TRACES_HEADERS', nil) end - it 'sets fallback token when no endpoints configured' do - ENV.delete('OTEL_EXPORTER_OTLP_HEADERS') - ENV.delete('OTEL_EXPORTER_OTLP_TRACES_HEADERS') - ENV.delete('OTEL_EXPORTER_OTLP_ENDPOINT') - ENV.delete('OTEL_EXPORTER_OTLP_TRACES_ENDPOINT') + it 'does not set a fallback token when no endpoint is configured' do + endpoint = SolarWindsAPM::OTLPEndPoint.new + endpoint.instance_variable_set(:@token, 'my-token') + endpoint.config_token('TRACES') + + assert_nil ENV.fetch('OTEL_EXPORTER_OTLP_HEADERS', nil) + assert_nil ENV.fetch('OTEL_EXPORTER_OTLP_TRACES_HEADERS', nil) + end + + it 'does not set token when signal endpoint is non-SWO' do + ENV['OTEL_EXPORTER_OTLP_TRACES_ENDPOINT'] = 'http://collector.internal:4318/v1/traces' endpoint = SolarWindsAPM::OTLPEndPoint.new endpoint.instance_variable_set(:@token, 'my-token') endpoint.config_token('TRACES') - assert_equal 'authorization=Bearer my-token', ENV.fetch('OTEL_EXPORTER_OTLP_HEADERS', nil) + assert_nil ENV.fetch('OTEL_EXPORTER_OTLP_HEADERS', nil) + assert_nil ENV.fetch('OTEL_EXPORTER_OTLP_TRACES_HEADERS', nil) + end + + it 'does not add signal token when user set general headers' do + ENV['OTEL_EXPORTER_OTLP_HEADERS'] = 'x-custom=1' + ENV['OTEL_EXPORTER_OTLP_TRACES_ENDPOINT'] = 'https://otel.collector.na-01.cloud.solarwinds.com:443/v1/traces' + + endpoint = SolarWindsAPM::OTLPEndPoint.new + endpoint.instance_variable_set(:@token, 'my-token') + endpoint.config_token('TRACES') + + assert_equal 'x-custom=1', ENV.fetch('OTEL_EXPORTER_OTLP_HEADERS', nil) + assert_nil ENV.fetch('OTEL_EXPORTER_OTLP_TRACES_HEADERS', nil) + end + end + + # Regression tests: the service key must never be sent to a non-SolarWinds OTLP endpoint + describe 'service key is not sent to non-SWO endpoints' do + it 'custom http traces endpoint gets no bearer, other signals use SWO with bearer' do + ENV['SW_APM_SERVICE_KEY'] = OTLP_TEST_SERVICE_KEY + ENV['OTEL_EXPORTER_OTLP_TRACES_ENDPOINT'] = 'http://my-collector:4318/v1/traces' + + _setup + + assert_nil ENV.fetch('OTEL_EXPORTER_OTLP_HEADERS', nil) + assert_nil ENV.fetch('OTEL_EXPORTER_OTLP_TRACES_HEADERS', nil) + assert_signal_headers_bearer('METRICS', 'LOGS') + end + + it 'custom https general endpoint gets no bearer for any signal' do + ENV['SW_APM_SERVICE_KEY'] = OTLP_TEST_SERVICE_KEY + ENV['OTEL_EXPORTER_OTLP_ENDPOINT'] = 'https://collector.example.com:4318' + + _setup + + assert_no_bearer_anywhere + assert_signal_endpoint_nil + end + + it 'general SWO endpoint with custom metrics endpoint does not leak bearer to metrics' do + ENV['SW_APM_SERVICE_KEY'] = OTLP_TEST_SERVICE_KEY + ENV['OTEL_EXPORTER_OTLP_ENDPOINT'] = 'https://otel.collector.na-01.cloud.solarwinds.com:443' + ENV['OTEL_EXPORTER_OTLP_METRICS_ENDPOINT'] = 'http://my-collector:4318/v1/metrics' + + _setup + + assert_nil ENV.fetch('OTEL_EXPORTER_OTLP_HEADERS', nil) + assert_nil ENV.fetch('OTEL_EXPORTER_OTLP_METRICS_HEADERS', nil) + assert_signal_headers_bearer('TRACES', 'LOGS') + end + + it 'user trace header with custom metrics/logs endpoints does not install a global bearer' do + ENV['SW_APM_SERVICE_KEY'] = OTLP_TEST_SERVICE_KEY + ENV['OTEL_EXPORTER_OTLP_TRACES_HEADERS'] = 'x-lab=control' + ENV['OTEL_EXPORTER_OTLP_ENDPOINT'] = 'http://my-collector:4318' + + _setup + + assert_equal 'x-lab=control', ENV.fetch('OTEL_EXPORTER_OTLP_TRACES_HEADERS', nil) + assert_no_bearer_anywhere + end + + it 'user supplied headers are preserved for custom collector forwarding to SWO' do + ENV['SW_APM_SERVICE_KEY'] = OTLP_TEST_SERVICE_KEY + ENV['OTEL_EXPORTER_OTLP_ENDPOINT'] = 'http://my-collector:4318' + ENV['OTEL_EXPORTER_OTLP_HEADERS'] = "authorization=Bearer #{OTLP_TEST_TOKEN}" + + _setup + + assert_equal "authorization=Bearer #{OTLP_TEST_TOKEN}", ENV.fetch('OTEL_EXPORTER_OTLP_HEADERS', nil) + assert_singal_headers_nil(general_singal_header: false) + end + + it 'no service key sets no headers with custom endpoint' do + ENV['OTEL_EXPORTER_OTLP_TRACES_ENDPOINT'] = 'http://my-collector:4318/v1/traces' + + _setup + + assert_singal_headers_nil + end + + it 'SW_APM_COLLECTOR with userinfo/path trick falls back to default SWO collector' do + ENV['SW_APM_SERVICE_KEY'] = OTLP_TEST_SERVICE_KEY + ENV['SW_APM_COLLECTOR'] = 'apm.collector.na-01.foo@localhost:1234/.solarwinds.com' + + _setup + + assert_equal SolarWindsAPM::OTLPEndPoint::SWO_APM_ENDPOINT_DEFAULT, ENV.fetch('SW_APM_COLLECTOR', nil) + assert_signal_endpoint_default + assert_signal_headers_bearer('TRACES', 'METRICS', 'LOGS') + end + + it 'SW_APM_COLLECTOR with embedded newline falls back to default SWO collector' do + ENV['SW_APM_COLLECTOR'] = "evil.example.com\napm.collector.na-01.cloud.solarwinds.com" + + _setup + + assert_equal SolarWindsAPM::OTLPEndPoint::SWO_APM_ENDPOINT_DEFAULT, ENV.fetch('SW_APM_COLLECTOR', nil) + end + end + + describe 'sanitize_endpoint' do + it 'drops userinfo, path and query' do + endpoint = SolarWindsAPM::OTLPEndPoint.new + assert_equal 'http://collector:4318', endpoint.sanitize_endpoint('http://user:secret@collector:4318/v1/traces?token=x') + assert_equal '', endpoint.sanitize_endpoint('not a url') + assert_equal '', endpoint.sanitize_endpoint(nil) + end + end + + describe 'swo_otlp_endpoint?' do + before { @endpoint = SolarWindsAPM::OTLPEndPoint.new } + + [ + 'https://otel.collector.na-01.cloud.solarwinds.com:443', + 'https://otel.collector.na-01.cloud.solarwinds.com', + 'https://otel.collector.eu-01.cloud.solarwinds.com:443/v1/traces', + 'https://OTEL.collector.na-01.cloud.solarwinds.com/v1/metrics' + ].each do |url| + it "accepts #{url}" do + assert @endpoint.swo_otlp_endpoint?(url) + end + end + + [ + nil, + '', + 'http://otel.collector.na-01.cloud.solarwinds.com:443/v1/traces', + 'https://otel.collector.na-01.cloud.solarwinds.com.evil.example/v1/traces', + 'https://otel.collector.na-01.cloud.solarwinds.com@evil.example/v1/traces', + 'https://user@otel.collector.na-01.cloud.solarwinds.com/v1/traces', + 'https://evil.example/otel.collector.na-01.cloud.solarwinds.com', + "https://evil.example\nhttps://otel.collector.na-01.cloud.solarwinds.com", + ' https://otel.collector.na-01.cloud.solarwinds.com:443/v1/traces', + "https://otel.collector.na-01.cloud.solarwinds.com:443/v1/traces\n", + 'https://collector.example.com:4318', + 'not a url' + ].each do |url| + it "rejects #{url.inspect}" do + refute @endpoint.swo_otlp_endpoint?(url) + end end end end