Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions contract-tests/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
'event-sampling',
'context-comparison',
'polling-gzip',
'inline-context',
'anonymous-redaction',
],
}.to_json
end
Expand Down
4 changes: 3 additions & 1 deletion lib/ldclient-rb/events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -481,12 +481,14 @@ def make_output_events(events, summary)
key: event.key,
value: event.value,
}

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[:contextKeys] = event.context.keys
out[:context] = @context_filter.filter_redact_anonymous(event.context)
out[:reason] = event.reason unless event.reason.nil?

out

when LaunchDarkly::Impl::MigrationOpEvent
Expand Down
37 changes: 29 additions & 8 deletions lib/ldclient-rb/impl/context_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,32 @@ def initialize(all_attributes_private, private_attributes)
# @return [Hash]
#
def filter(context)
return filter_single_context(context, true) unless context.multi_kind?
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'}
(0...context.individual_context_count).each do |i|
c = context.individual_context(i)
next if c.nil?

filtered[c.kind] = filter_single_context(c, false)
filtered[c.kind] = filter_single_context(c, false, redact_anonymous)
end

filtered
Expand All @@ -43,22 +61,24 @@ def filter(context)
# @param include_kind [Boolean]
# @return [Hash]
#
private def filter_single_context(context, include_kind)
private def filter_single_context(context, include_kind, redact_anonymous)
filtered = {key: context.key}

filtered[:kind] = context.kind if include_kind
filtered[:anonymous] = true if context.get_value(:anonymous)

anonymous = context.get_value(:anonymous)
filtered[:anonymous] = true if anonymous

redacted = []
private_attributes = @private_attributes.concat(context.private_attributes)

name = context.get_value(:name)
if !name.nil? && !check_whole_attribute_private(:name, private_attributes, redacted)
if !name.nil? && !check_whole_attribute_private(:name, private_attributes, redacted, anonymous && redact_anonymous)
filtered[:name] = name
end

context.get_custom_attribute_names.each do |attribute|
unless check_whole_attribute_private(attribute, private_attributes, redacted)
unless check_whole_attribute_private(attribute, private_attributes, redacted, anonymous && redact_anonymous)
value = context.get_value(attribute)
filtered[attribute] = redact_json_value(nil, attribute, value, private_attributes, redacted)
end
Expand All @@ -75,10 +95,11 @@ def filter(context)
# @param attribute [Symbol]
# @param private_attributes [Array<Reference>]
# @param redacted [Array<Symbol>]
# @param redact_all [Boolean]
# @return [Boolean]
#
private def check_whole_attribute_private(attribute, private_attributes, redacted)
if @all_attributes_private
private def check_whole_attribute_private(attribute, private_attributes, redacted, redact_all)
if @all_attributes_private || redact_all
redacted << attribute
return true
end
Expand Down
22 changes: 13 additions & 9 deletions spec/events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module LaunchDarkly
output = flush_and_get_events(ep, sender)
expect(output).to contain_exactly(
eq(index_event(default_config, context)),
eq(feature_event(flag, context, 1, 'value')),
eq(feature_event(default_config, flag, context, 1, 'value')),
include(:kind => "summary")
)
end
Expand Down Expand Up @@ -69,7 +69,7 @@ module LaunchDarkly
output = flush_and_get_events(ep, sender)
expect(output).to contain_exactly(
eq(index_event(default_config, context)),
eq(feature_event(flag, context, 1, 'value'))
eq(feature_event(default_config, flag, context, 1, 'value'))
)
end
end
Expand All @@ -83,7 +83,7 @@ module LaunchDarkly
output = flush_and_get_events(ep, sender)
expect(output).to contain_exactly(
eq(index_event(config, context)),
eq(feature_event(flag, context, 1, 'value')),
eq(feature_event(config, flag, context, 1, 'value')),
include(:kind => "summary")
)
end
Expand All @@ -98,7 +98,7 @@ module LaunchDarkly
output = flush_and_get_events(ep, sender)
expect(output).to contain_exactly(
eq(index_event(config, context)),
eq(feature_event(flag, context, 1, 'value')),
eq(feature_event(config, flag, context, 1, 'value')),
include(:kind => "summary")
)
end
Expand Down Expand Up @@ -142,7 +142,7 @@ module LaunchDarkly
output = flush_and_get_events(ep, sender)
expect(output).to contain_exactly(
eq(index_event(default_config, context)),
eq(feature_event(flag, context, 1, 'value')),
eq(feature_event(default_config, flag, context, 1, 'value')),
eq(debug_event(default_config, flag, context, 1, 'value')),
include(:kind => "summary")
)
Expand Down Expand Up @@ -207,8 +207,8 @@ module LaunchDarkly
output = flush_and_get_events(ep, sender)
expect(output).to contain_exactly(
eq(index_event(default_config, context)),
eq(feature_event(flag1, context, 1, 'value', starting_timestamp)),
eq(feature_event(flag2, context, 1, 'value', starting_timestamp + 1)),
eq(feature_event(default_config, flag1, context, 1, 'value', starting_timestamp)),
eq(feature_event(default_config, flag2, context, 1, 'value', starting_timestamp + 1)),
include(:kind => "summary")
)
end
Expand Down Expand Up @@ -624,18 +624,22 @@ def identify_event(config, context, timestamp = starting_timestamp)
end

#
# @param config [Config]
# @param flag [Hash]
# @param context [LDContext]
# @param variation [Integer]
# @param value [any]
# @param timestamp [Integer]
# @return [Hash]
#
def feature_event(flag, context, variation, value, 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_redact_anonymous(context)

out = {
kind: 'feature',
creationDate: timestamp,
contextKeys: context.keys,
context: redacted_context,
key: flag[:key],
variation: variation,
version: flag[:version],
Expand Down