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

docker container id: ensure sha256 format #2285

Merged
merged 1 commit into from
Oct 24, 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
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