Fix docker_container.tag to properly fetch from image name (#2052)

Fixes #2051

Images with repos containing port numbers will have multiple colons.

Signed-off-by: Matt Kulka <mkulka@parchment.com>
This commit is contained in:
Matt Kulka 2017-08-10 05:57:45 -07:00 committed by Christoph Hartmann
parent f579733205
commit 0fc870de30
5 changed files with 33 additions and 6 deletions

View file

@ -74,11 +74,13 @@ module Inspec::Resources
end end
def repo def repo
image.split(':')[0] unless image.nil? return if image_name_from_image.nil?
image_name_from_image.split(':')[0]
end end
def tag def tag
image.split(':')[1] unless image.nil? return if image_name_from_image.nil?
image_name_from_image.split(':')[1]
end end
def to_s def to_s
@ -88,6 +90,16 @@ module Inspec::Resources
private private
def image_name_from_image
return if image.nil?
# possible image names include:
# alpine
# ubuntu:14.04
# repo.example.com:5000/ubuntu
# repo.example.com:5000/ubuntu:1404
image.include?('/') ? image.split('/')[1] : image
end
def container_info def container_info
return @info if defined?(@info) return @info if defined?(@info)
opts = @opts opts = @opts

View file

@ -1,3 +1,4 @@
{ "id": "sha256:4a415e3663882fbc554ee830889c68a33b3585503892cc718a4698e91ef2a526", "repository": "alpine", "tag": "latest", "size": "3.99 MB", "digest": "\u003cnone\u003e", "createdat": "2017-03-03 21:32:37 +0100 CET", "createdsize": "7 weeks" } { "id": "sha256:4a415e3663882fbc554ee830889c68a33b3585503892cc718a4698e91ef2a526", "repository": "alpine", "tag": "latest", "size": "3.99 MB", "digest": "\u003cnone\u003e", "createdat": "2017-03-03 21:32:37 +0100 CET", "createdsize": "7 weeks" }
{ "id": "sha256:978d85d02b87aea199e4ae8664f6abf32fdea331884818e46b8a01106b114cee", "repository": "debian", "tag": "8", "size": "123 MB", "digest": "\u003cnone\u003e", "createdat": "2017-02-27 21:34:37 +0100 CET", "createdsize": "7 weeks" } { "id": "sha256:978d85d02b87aea199e4ae8664f6abf32fdea331884818e46b8a01106b114cee", "repository": "debian", "tag": "8", "size": "123 MB", "digest": "\u003cnone\u003e", "createdat": "2017-02-27 21:34:37 +0100 CET", "createdsize": "7 weeks" }
{ "id": "sha256:0ef2e08ed3fabfc44002ccb846c4f2416a2135affc3ce39538834059606f32dd", "repository": "ubuntu", "tag": "16.04", "size": "130 MB", "digest": "\u003cnone\u003e", "createdat": "2017-02-27 20:42:10 +0100 CET", "createdsize": "7 weeks" } { "id": "sha256:0ef2e08ed3fabfc44002ccb846c4f2416a2135affc3ce39538834059606f32dd", "repository": "ubuntu", "tag": "16.04", "size": "130 MB", "digest": "\u003cnone\u003e", "createdat": "2017-02-27 20:42:10 +0100 CET", "createdsize": "7 weeks" }
{ "id": "sha256:c4e5744dbe11a4f1970ba36d0aa3944c347ab232bb58fb86b240f1bb18a360c2", "repository": "repo.example.com:5000/ubuntu", "tag": "14.04", "size": "125 MB", "digest": "\u003cnone\u003e", "createdat": "2017-02-27 20:42:10 +0100 CET", "createdsize": "7 weeks" }

View file

@ -1,2 +1,4 @@
{"Command":"\"/bin/bash\"","CreatedAt":"2017-04-24 10:29:12 +0200 CEST","ID":"3def9aa450f8bd772c3d5b07e27ec934e5f58575e955367a0aca2d93e0687536","Image":"ubuntu:12.04","Labels":"","LocalVolumes":"0","Mounts":"","Names":"sleepy_khorana","Networks":"bridge","Ports":"","RunningFor":"29 minutes","Size":"0 B","Status":"Exited (127) 2 seconds ago"} {"Command":"\"/bin/bash\"","CreatedAt":"2017-04-24 10:29:12 +0200 CEST","ID":"3def9aa450f8bd772c3d5b07e27ec934e5f58575e955367a0aca2d93e0687536","Image":"ubuntu:12.04","Labels":"","LocalVolumes":"0","Mounts":"","Names":"sleepy_khorana","Networks":"bridge","Ports":"","RunningFor":"29 minutes","Size":"0 B","Status":"Exited (127) 2 seconds ago"}
{"Command":"\"/bin/sh\"","CreatedAt":"2017-04-22 22:44:42 +0200 CEST","ID":"d94f854370d2b02912e8fc636502bc72b74fbd567a7eba3fc6a52045bb28904e","Image":"alpine","Labels":"","LocalVolumes":"0","Mounts":"","Names":"laughing_austin","Networks":"bridge","Ports":"","RunningFor":"36 hours","Size":"0 B","Status":"Exited (0) 35 hours ago"} {"Command":"\"/bin/sh\"","CreatedAt":"2017-04-22 22:44:42 +0200 CEST","ID":"d94f854370d2b02912e8fc636502bc72b74fbd567a7eba3fc6a52045bb28904e","Image":"alpine","Labels":"","LocalVolumes":"0","Mounts":"","Names":"laughing_austin","Networks":"bridge","Ports":"","RunningFor":"36 hours","Size":"0 B","Status":"Exited (0) 35 hours ago"}
{"Command":"\"/bin/sh\"","CreatedAt":"2017-08-03 12:56:03 +0200 CEST","ID":"5a83c301f30ccd48579a74a84af6fdd0c0e0d66aacc7bb52abfa2ba2544c6c0c","Image":"repo.example.com:5000/ubuntu:14.04","Labels":"","LocalVolumes":"0","Mounts":"","Names":"heuristic_almeida","Networks":"bridge","Ports":"","RunningFor":"5 hours","Size":"0 B","Status":"Exited (0) 24 hours ago"}
{"Command":"\"/bin/sh\"","CreatedAt":"2017-08-03 12:56:03 +0200 CEST","ID":"5a83c301f30ccd48579a74a84af6fdd0c0e0d66aacc7bb52abfa2ba2544c6c0c","Image":"repo.example.com:5000/ubuntu","Labels":"","LocalVolumes":"0","Mounts":"","Names":"laughing_lamport","Networks":"bridge","Ports":"","RunningFor":"5 hours","Size":"0 B","Status":"Exited (0) 24 hours ago"}

View file

@ -26,6 +26,18 @@ describe 'Inspec::Resources::DockerContainer' do
_(resource.ports).must_equal '' _(resource.ports).must_equal ''
end end
it 'check image containing repo with port and tag gives correct tag' do
resource = load_resource('docker_container', 'heuristic_almeida')
_(resource.image).must_equal 'repo.example.com:5000/ubuntu:14.04'
_(resource.tag).must_equal '14.04'
end
it 'check image containing repo with port and no tag gives correct tag' do
resource = load_resource('docker_container', 'laughing_lamport')
_(resource.image).must_equal 'repo.example.com:5000/ubuntu'
_(resource.tag).must_be_nil
end
it 'prints as a docker resource' do it 'prints as a docker resource' do
resource = load_resource('docker_container', 'laughing_austin') resource = load_resource('docker_container', 'laughing_austin')
resource.to_s.must_equal 'Docker Container laughing_austin' resource.to_s.must_equal 'Docker Container laughing_austin'

View file

@ -8,14 +8,14 @@ describe 'Inspec::Resources::Docker' do
describe 'docker' do describe 'docker' do
it 'check docker container parsing' do it 'check docker container parsing' do
resource = load_resource('docker') resource = load_resource('docker')
_(resource.containers.ids).must_equal ['3def9aa450f8bd772c3d5b07e27ec934e5f58575e955367a0aca2d93e0687536', 'd94f854370d2b02912e8fc636502bc72b74fbd567a7eba3fc6a52045bb28904e'] _(resource.containers.ids).must_equal ['3def9aa450f8bd772c3d5b07e27ec934e5f58575e955367a0aca2d93e0687536', 'd94f854370d2b02912e8fc636502bc72b74fbd567a7eba3fc6a52045bb28904e', '5a83c301f30ccd48579a74a84af6fdd0c0e0d66aacc7bb52abfa2ba2544c6c0c', '5a83c301f30ccd48579a74a84af6fdd0c0e0d66aacc7bb52abfa2ba2544c6c0c']
_(resource.containers.names).must_equal ['sleepy_khorana', 'laughing_austin'] _(resource.containers.names).must_equal ['sleepy_khorana', 'laughing_austin', 'heuristic_almeida', 'laughing_lamport']
end end
it 'check docker image parsing' do it 'check docker image parsing' do
resource = load_resource('docker') resource = load_resource('docker')
_(resource.images.ids).must_equal ['sha256:4a415e3663882fbc554ee830889c68a33b3585503892cc718a4698e91ef2a526', 'sha256:978d85d02b87aea199e4ae8664f6abf32fdea331884818e46b8a01106b114cee', 'sha256:0ef2e08ed3fabfc44002ccb846c4f2416a2135affc3ce39538834059606f32dd'] _(resource.images.ids).must_equal ['sha256:4a415e3663882fbc554ee830889c68a33b3585503892cc718a4698e91ef2a526', 'sha256:978d85d02b87aea199e4ae8664f6abf32fdea331884818e46b8a01106b114cee', 'sha256:0ef2e08ed3fabfc44002ccb846c4f2416a2135affc3ce39538834059606f32dd', 'sha256:c4e5744dbe11a4f1970ba36d0aa3944c347ab232bb58fb86b240f1bb18a360c2']
_(resource.images.repositories).must_equal ["alpine", "debian", "ubuntu"] _(resource.images.repositories).must_equal ["alpine", "debian", "ubuntu", "repo.example.com:5000/ubuntu"]
end end
it 'check docker version parsing' do it 'check docker version parsing' do