Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[APPSEC-6712] Remote configuration config_state #2794

Merged
merged 1 commit into from
Apr 17, 2023
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 lib/datadog/core/remote/configuration/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ def parse(hash)
end

attr_reader :path, :data, :hashes
attr_accessor :version

def initialize(path:, data:)
@path = path
@data = data
@hashes = {}
@version = 0
end

def hexdigest(type)
Expand Down
16 changes: 15 additions & 1 deletion lib/datadog/core/remote/configuration/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def initialize(repository)
@repository = repository
@root_version = repository.root_version
@targets_version = repository.targets_version
@config_states = []
@config_states = contents_to_config_states(repository.contents)
@has_error = false
@error = ''
@opaque_backend_state = repository.opaque_backend_state
Expand All @@ -87,6 +87,18 @@ def initialize(repository)

private

def contents_to_config_states(contents)
return [] if contents.empty?

contents.map do |content|
{
id: content.path.config_id,
version: content.version,
product: content.path.product
}
end
end

def contents_to_cached_target_files(contents)
return [] if contents.empty?

Expand Down Expand Up @@ -164,6 +176,7 @@ def initialize(path, target, content)
def apply(repository)
return unless repository[@path].nil?

@content.version = @target.version
repository.contents << @content

@path
Expand All @@ -184,6 +197,7 @@ def initialize(path, target, content)
def apply(repository)
return if repository[@path].nil?

@content.version = @target.version
repository.contents[@path] = @content

@path
Expand Down
8 changes: 5 additions & 3 deletions lib/datadog/core/remote/configuration/target.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,18 @@ class << self
def parse(hash)
length = Integer(hash['length'])
digests = Configuration::DigestList.parse(hash['hashes'])
version = Integer(hash['custom']['v'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is failing:

NoMethodError:
       undefined method `[]' for nil:NilClass
     # ./lib/datadog/core/remote/configuration/target.rb:51:in `parse'
     # ./spec/datadog/core/remote/configuration/target_spec.rb:178:in `block (3 levels) in <top (required)>'
     # ./spec/datadog/core/remote/configuration/target_spec.rb:199:in `block (5 levels) in <top (required)>'


new(digests: digests, length: length)
new(digests: digests, length: length, version: version)
end
end

attr_reader :length, :digests
attr_reader :length, :digests, :version

def initialize(digests:, length:)
def initialize(digests:, length:, version:)
@digests = digests
@length = length
@version = version
end

private_class_method :new
Expand Down
2 changes: 2 additions & 0 deletions sig/datadog/core/remote/configuration/content.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ module Datadog

attr_reader hashes: Hash[Symbol, String]

attr_accessor version: Integer

@length: Integer

def initialize: (path: Configuration::Path, data: StringIO) -> void
Expand Down
4 changes: 3 additions & 1 deletion sig/datadog/core/remote/configuration/repository.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module Datadog

attr_reader repository: Repository

attr_reader config_states: Array[untyped]
attr_reader config_states: Array[Hash[Symbol, untyped]]

attr_reader cached_target_files: Array[Hash[Symbol, untyped]]

Expand All @@ -49,6 +49,8 @@ module Datadog
private

def contents_to_cached_target_files: (ContentList contents) -> Array[Hash[Symbol, untyped]]

def contents_to_config_states: (ContentList contents) -> Array[Hash[Symbol, untyped]]
end

class Transaction
Expand Down
4 changes: 3 additions & 1 deletion sig/datadog/core/remote/configuration/target.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ module Datadog

attr_reader digests: DigestList

attr_reader version: Integer

def self.parse: (::Hash[::String, untyped] hash) -> Target

def initialize: (digests: DigestList, length: ::Integer) -> void
def initialize: (digests: DigestList, length: ::Integer, version: Integer) -> void

def check: (untyped content) -> bool
end
Expand Down
27 changes: 20 additions & 7 deletions spec/datadog/appsec/remote_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,28 @@
}.to_json
end
let(:receiver) { described_class.receivers[0] }
let(:target) do
Datadog::Core::Remote::Configuration::Target.parse(
{
'custom' => {
'v' => 1,
},
'hashes' => { 'sha256' => Digest::SHA256.hexdigest(rules_data.to_json) },
'length' => rules_data.to_s.length
}
)
end
let(:content) do
Datadog::Core::Remote::Configuration::Content.parse(
{
path: 'datadog/603646/ASM_DD/latest/config',
content: StringIO.new(rules_data)
}
)
end
let(:transaction) do
repository.transaction do |_repository, transaction|
content = Datadog::Core::Remote::Configuration::Content.parse(
{
path: 'datadog/603646/ASM_DD/latest/config',
content: StringIO.new(rules_data)
}
)
transaction.insert(content.path, nil, content)
transaction.insert(content.path, target, content)
end
end
let(:repository) { Datadog::Core::Remote::Configuration::Repository.new }
Expand Down
72 changes: 72 additions & 0 deletions spec/datadog/core/remote/configuration/respository_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
subject(:repository) { described_class.new }
let(:raw_target) do
{
'custom' => {
'v' => 1,
},
'hashes' => { 'sha256' => Digest::SHA256.hexdigest(raw.to_json) },
'length' => 645
}
Expand Down Expand Up @@ -103,6 +106,12 @@
)
end

let(:new_target) do
updated_raw_target = raw_target.dup
updated_raw_target['custom']['v'] += 1
Datadog::Core::Remote::Configuration::Target.parse(updated_raw_target)
end

describe '#transaction' do
it 'yields self and a new Repository::Transaction instance' do
expect do |b|
Expand Down Expand Up @@ -350,5 +359,68 @@
end
end
end

describe '#config_states' do
context 'without changes' do
it 'return empty array' do
expect(repository.state.config_states).to eq([])
end
end

context 'with changes' do
let(:expected_config_states) do
[
{ :id => path.config_id, :product => path.product, :version => 1 }
]
end

context 'insert' do
it 'return config_states' do
repository.transaction do |_repository, transaction|
transaction.insert(path, target, content)
end

expect(repository.state.config_states).to eq(expected_config_states)
end
end

context 'update' do
it 'return config_states' do
repository.transaction do |_repository, transaction|
transaction.insert(path, target, content)
end

expect(repository.state.config_states).to eq(expected_config_states)

repository.transaction do |_repository, transaction|
transaction.update(path, new_target, new_content)
end

expected_updated_config_states = [
{ :id => path.config_id, :product => path.product, :version => 2 }
]

expect(repository.state.config_states).to_not eq(expected_config_states)
expect(repository.state.config_states).to eq(expected_updated_config_states)
end
end

context 'delete' do
it 'return config_states' do
repository.transaction do |_repository, transaction|
transaction.insert(path, target, content)
end

expect(repository.state.config_states).to eq(expected_config_states)

repository.transaction do |_repository, transaction|
transaction.delete(path)
end

expect(repository.state.config_states).to eq([])
end
end
end
end
end
end
3 changes: 3 additions & 0 deletions spec/datadog/core/remote/configuration/target_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@
describe Datadog::Core::Remote::Configuration::Target do
let(:raw_target) do
{
'custom' => {
'v' => 1,
},
'hashes' => { 'sha256' => Digest::SHA256.hexdigest(raw.to_json) },
'length' => 645
}
Expand Down