Skip to content

Commit

Permalink
Merge pull request #2285 from newrelic/docker_container_id_cgroups_v2…
Browse files Browse the repository at this point in the history
…_is_sha256_formatted

docker container id: ensure sha256 format
  • Loading branch information
fallwith authored Oct 24, 2023
2 parents 594b231 + 32887a0 commit 4a30b54
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/new_relic/agent/system_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
module NewRelic
module Agent
module SystemInfo
DOCKER_CGROUPS_V2_PATTERN = %r{.*/docker/containers/([0-9a-f]{64})/.*}.freeze

def self.ruby_os_identifier
RbConfig::CONFIG['target_os']
end
Expand Down Expand Up @@ -202,7 +204,7 @@ def self.docker_container_id_for_cgroupsv2
mountinfo = proc_try_read('/proc/self/mountinfo')
return unless mountinfo

Regexp.last_match(1) if mountinfo =~ %r{/docker/containers/([^/]+)/}
Regexp.last_match(1) if mountinfo =~ DOCKER_CGROUPS_V2_PATTERN
end

def self.parse_docker_container_id(cgroup_info)
Expand Down
18 changes: 16 additions & 2 deletions test/new_relic/agent/system_info_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,29 @@ def setup
# BEGIN cgroups v2
def test_docker_container_id_is_gleaned_from_mountinfo_for_cgroups_v2
skip_unless_minitest5_or_above

container_id = "And Autumn leaves lie thick and still o'er land that is lost now"
container_id = '3145490ee377105a4d3a7abd55083c61c0c2d616d786614e755176433c648d09'
mountinfo = "line1\nline2\n/docker/containers/#{container_id}/other/content\nline4\nline5"
NewRelic::Agent::SystemInfo.stub :ruby_os_identifier, 'linux' do
NewRelic::Agent::SystemInfo.stub :proc_try_read, mountinfo, %w[/proc/self/mountinfo] do
assert_equal container_id, NewRelic::Agent::SystemInfo.docker_container_id
end
end
end

def test_docker_container_id_must_match_sha_256_format
skip_unless_minitest5_or_above
bogus_container_ids = %w[3145490ee377105a4d3a7abd55083c61c0c2d616d786614e755176433c648d0
3145490ee377105a4d3a7abd55083c61c0c2d616d78g614e755176433c648d09
3145490ee377105a4d3a7abd55083C61c0c2d616d786614e755176433c648d09]
bogus_container_ids.each do |id|
mountinfo = "line1\nline2\n/docker/containers/#{id}/other/content\nline4\nline5"
NewRelic::Agent::SystemInfo.stub :ruby_os_identifier, 'linux' do
NewRelic::Agent::SystemInfo.stub :proc_try_read, mountinfo, %w[/proc/self/mountinfo] do
refute NewRelic::Agent::SystemInfo.docker_container_id
end
end
end
end
# END cgroups v2

each_cross_agent_test :dir => 'proc_meminfo', :pattern => '*.txt' do |file|
Expand Down

0 comments on commit 4a30b54

Please sign in to comment.