Skip to content

Commit

Permalink
Add filter_redact_anonymous
Browse files Browse the repository at this point in the history
  • Loading branch information
keelerm84 committed Jan 10, 2024
1 parent a3bfde1 commit c2eb498
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
8 changes: 4 additions & 4 deletions lib/ldclient-rb/events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ def make_output_events(events, summary)
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, true)
out[:context] = @context_filter.filter_redact_anonymous(event.context)
out[:reason] = event.reason unless event.reason.nil?

out
Expand Down Expand Up @@ -554,7 +554,7 @@ def make_output_events(events, summary)
kind: IDENTIFY_KIND,
creationDate: event.timestamp,
key: event.context.fully_qualified_key,
context: @context_filter.filter(event.context, false),
context: @context_filter.filter(event.context),
}

when LaunchDarkly::Impl::CustomEvent
Expand All @@ -572,7 +572,7 @@ def make_output_events(events, summary)
{
kind: INDEX_KIND,
creationDate: event.timestamp,
context: @context_filter.filter(event.context, false),
context: @context_filter.filter(event.context),
}

when LaunchDarkly::Impl::DebugEvent
Expand All @@ -581,7 +581,7 @@ def make_output_events(events, summary)
kind: DEBUG_KIND,
creationDate: original.timestamp,
key: original.key,
context: @context_filter.filter(original.context, false),
context: @context_filter.filter(original.context),
value: original.value,
}
out[:default] = original.default unless original.default.nil?
Expand Down
21 changes: 19 additions & 2 deletions lib/ldclient-rb/impl/context_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,27 @@ def initialize(all_attributes_private, private_attributes)
# redaction applied.
#
# @param context [LaunchDarkly::LDContext]
# @param redact_anonymous [Boolean]
# @return [Hash]
#
def filter(context, redact_anonymous)
def filter(context)
internal_filter(context, false)
end

#
# Return a hash representation of the provided context with attribute
# redaction applied.
#
# If a context is anonyomous, all attributes will be redacted except
# for key, kind, and anonymous.
#
# @param context [LaunchDarkly::LDContext]
# @return [Hash]
#
def filter_redact_anonymous(context)
internal_filter(context, true)
end

private def internal_filter(context, redact_anonymous)
return filter_single_context(context, true, redact_anonymous) unless context.multi_kind?

filtered = {kind: 'multi'}
Expand Down
8 changes: 4 additions & 4 deletions spec/events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ def index_event(config, context, timestamp = starting_timestamp)
out = {
kind: "index",
creationDate: timestamp,
context: context_filter.filter(context, false),
context: context_filter.filter(context),
}
JSON.parse(out.to_json, symbolize_names: true)
end
Expand All @@ -618,7 +618,7 @@ def identify_event(config, context, timestamp = starting_timestamp)
kind: "identify",
creationDate: timestamp,
key: context.fully_qualified_key,
context: context_filter.filter(context, false),
context: context_filter.filter(context),
}
JSON.parse(out.to_json, symbolize_names: true)
end
Expand All @@ -634,7 +634,7 @@ 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)
redacted_context = context_filter.filter(context, true)
redacted_context = context_filter.filter_redact_anonymous(context)

out = {
kind: 'feature',
Expand Down Expand Up @@ -693,7 +693,7 @@ def debug_event(config, flag, context, variation, value, timestamp = starting_ti
variation: variation,
version: flag[:version],
value: value,
context: context_filter.filter(context, false),
context: context_filter.filter(context),
}
JSON.parse(out.to_json, symbolize_names: true)
end
Expand Down

0 comments on commit c2eb498

Please sign in to comment.