From e9b0c8bef0b6e6e714900bb5b9fab6aa6cb8e107 Mon Sep 17 00:00:00 2001 From: Matthew Keeler Date: Tue, 9 Jan 2024 11:00:59 -0500 Subject: [PATCH] feat: Redact anonymous attributes within feature events --- contract-tests/service.rb | 1 + lib/ldclient-rb/events.rb | 10 +++++++++- spec/events_spec.rb | 6 +++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/contract-tests/service.rb b/contract-tests/service.rb index d7d58720..2442a1c9 100644 --- a/contract-tests/service.rb +++ b/contract-tests/service.rb @@ -37,6 +37,7 @@ 'event-sampling', 'context-comparison', 'inline-context', + 'anonymous-redaction', ], }.to_json end diff --git a/lib/ldclient-rb/events.rb b/lib/ldclient-rb/events.rb index f0ea7fd5..59ae0440 100644 --- a/lib/ldclient-rb/events.rb +++ b/lib/ldclient-rb/events.rb @@ -460,6 +460,7 @@ class EventOutputFormatter def initialize(config) @context_filter = LaunchDarkly::Impl::ContextFilter.new(config.all_attributes_private, config.private_attributes) + @anonymous_context_filter = LaunchDarkly::Impl::ContextFilter.new(true, []) end # Transforms events into the format used for event sending. @@ -481,12 +482,19 @@ def make_output_events(events, summary) key: event.key, value: event.value, } + + out[:context] = if event.context.get_value('anonymous') + @anonymous_context_filter.filter(event.context) + else + @context_filter.filter(event.context) + end + out[:default] = event.default unless event.default.nil? out[:variation] = event.variation unless event.variation.nil? out[:version] = event.version unless event.version.nil? out[:prereqOf] = event.prereq_of unless event.prereq_of.nil? - out[:context] = @context_filter.filter(event.context) out[:reason] = event.reason unless event.reason.nil? + out when LaunchDarkly::Impl::MigrationOpEvent diff --git a/spec/events_spec.rb b/spec/events_spec.rb index 1cdf84ae..7ff7f24b 100644 --- a/spec/events_spec.rb +++ b/spec/events_spec.rb @@ -634,10 +634,14 @@ def identify_event(config, context, timestamp = starting_timestamp) # def feature_event(config, flag, context, variation, value, timestamp = starting_timestamp) context_filter = Impl::ContextFilter.new(config.all_attributes_private, config.private_attributes) + anonymous_filter = Impl::ContextFilter.new(true, []) + + redacted_context = context.get_value('anonymous') ? anonymous_filter.filter(context) : context_filter.filter(context) + out = { kind: 'feature', creationDate: timestamp, - context: context_filter.filter(context), + context: redacted_context, key: flag[:key], variation: variation, version: flag[:version],