Skip to content

Commit c2eb498

Browse files
committed
Add filter_redact_anonymous
1 parent a3bfde1 commit c2eb498

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

lib/ldclient-rb/events.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ def make_output_events(events, summary)
486486
out[:variation] = event.variation unless event.variation.nil?
487487
out[:version] = event.version unless event.version.nil?
488488
out[:prereqOf] = event.prereq_of unless event.prereq_of.nil?
489-
out[:context] = @context_filter.filter(event.context, true)
489+
out[:context] = @context_filter.filter_redact_anonymous(event.context)
490490
out[:reason] = event.reason unless event.reason.nil?
491491

492492
out
@@ -554,7 +554,7 @@ def make_output_events(events, summary)
554554
kind: IDENTIFY_KIND,
555555
creationDate: event.timestamp,
556556
key: event.context.fully_qualified_key,
557-
context: @context_filter.filter(event.context, false),
557+
context: @context_filter.filter(event.context),
558558
}
559559

560560
when LaunchDarkly::Impl::CustomEvent
@@ -572,7 +572,7 @@ def make_output_events(events, summary)
572572
{
573573
kind: INDEX_KIND,
574574
creationDate: event.timestamp,
575-
context: @context_filter.filter(event.context, false),
575+
context: @context_filter.filter(event.context),
576576
}
577577

578578
when LaunchDarkly::Impl::DebugEvent
@@ -581,7 +581,7 @@ def make_output_events(events, summary)
581581
kind: DEBUG_KIND,
582582
creationDate: original.timestamp,
583583
key: original.key,
584-
context: @context_filter.filter(original.context, false),
584+
context: @context_filter.filter(original.context),
585585
value: original.value,
586586
}
587587
out[:default] = original.default unless original.default.nil?

lib/ldclient-rb/impl/context_filter.rb

+19-2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,27 @@ def initialize(all_attributes_private, private_attributes)
2020
# redaction applied.
2121
#
2222
# @param context [LaunchDarkly::LDContext]
23-
# @param redact_anonymous [Boolean]
2423
# @return [Hash]
2524
#
26-
def filter(context, redact_anonymous)
25+
def filter(context)
26+
internal_filter(context, false)
27+
end
28+
29+
#
30+
# Return a hash representation of the provided context with attribute
31+
# redaction applied.
32+
#
33+
# If a context is anonyomous, all attributes will be redacted except
34+
# for key, kind, and anonymous.
35+
#
36+
# @param context [LaunchDarkly::LDContext]
37+
# @return [Hash]
38+
#
39+
def filter_redact_anonymous(context)
40+
internal_filter(context, true)
41+
end
42+
43+
private def internal_filter(context, redact_anonymous)
2744
return filter_single_context(context, true, redact_anonymous) unless context.multi_kind?
2845

2946
filtered = {kind: 'multi'}

spec/events_spec.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ def index_event(config, context, timestamp = starting_timestamp)
601601
out = {
602602
kind: "index",
603603
creationDate: timestamp,
604-
context: context_filter.filter(context, false),
604+
context: context_filter.filter(context),
605605
}
606606
JSON.parse(out.to_json, symbolize_names: true)
607607
end
@@ -618,7 +618,7 @@ def identify_event(config, context, timestamp = starting_timestamp)
618618
kind: "identify",
619619
creationDate: timestamp,
620620
key: context.fully_qualified_key,
621-
context: context_filter.filter(context, false),
621+
context: context_filter.filter(context),
622622
}
623623
JSON.parse(out.to_json, symbolize_names: true)
624624
end
@@ -634,7 +634,7 @@ def identify_event(config, context, timestamp = starting_timestamp)
634634
#
635635
def feature_event(config, flag, context, variation, value, timestamp = starting_timestamp)
636636
context_filter = Impl::ContextFilter.new(config.all_attributes_private, config.private_attributes)
637-
redacted_context = context_filter.filter(context, true)
637+
redacted_context = context_filter.filter_redact_anonymous(context)
638638

639639
out = {
640640
kind: 'feature',
@@ -693,7 +693,7 @@ def debug_event(config, flag, context, variation, value, timestamp = starting_ti
693693
variation: variation,
694694
version: flag[:version],
695695
value: value,
696-
context: context_filter.filter(context, false),
696+
context: context_filter.filter(context),
697697
}
698698
JSON.parse(out.to_json, symbolize_names: true)
699699
end

0 commit comments

Comments
 (0)