diff --git a/sdcclient/_scanning.py b/sdcclient/_scanning.py index d181f307..6a44ce99 100644 --- a/sdcclient/_scanning.py +++ b/sdcclient/_scanning.py @@ -137,13 +137,20 @@ def query_image_content(self, image, content_type=""): - image: Input image can be in the following formats: registry/repo:tag - content_type: The content type can be one of the following types: - os: Operating System Packages + - files: Files - npm: Node.JS NPM Module - gem: Ruby GEM - - files: Files + - python: Python modules + - java: Java packages **Success Return Value** A JSON object representing the image content. ''' + content_type = content_type.lower() + supported_types = ["os", "files", "npm", "gem", "python", "java"] + if content_type not in supported_types: + return False, f"unsupported type provided: {content_type}, must be one of {supported_types}" + return self._query_image(image, query_group='content', query_type=content_type) def query_image_metadata(self, image, metadata_type=""): diff --git a/specs/secure/scanning/query_image_content_spec.py b/specs/secure/scanning/query_image_content_spec.py new file mode 100644 index 00000000..7ef4a3ba --- /dev/null +++ b/specs/secure/scanning/query_image_content_spec.py @@ -0,0 +1,59 @@ +import os + +from expects import expect, contain, have_keys, equal +from mamba import before, it, context, description + +from sdcclient import SdScanningClient +from specs import be_successful_api_call + +with description("Query Image Content", "integration") as self: + with before.each: + self.client = SdScanningClient(sdc_url=os.getenv("SDC_SECURE_URL", "https://secure.sysdig.com"), + token=os.getenv("SDC_SECURE_TOKEN")) + + with it("is able to retrieve the OS contents"): + ok, res = self.client.query_image_content("alpine:latest", "os") + + expect((ok, res)).to(be_successful_api_call) + expect(res["content"]).to(contain(have_keys("license", "origin", "package", "size", "type", "version"))) + expect(res["content_type"]).to(equal("os")) + + with it("is able to retrieve the npm contents"): + ok, res = self.client.query_image_content("alpine:latest", "npm") + + expect((ok, res)).to(be_successful_api_call) + expect(res["content_type"]).to(equal("npm")) + + with it("is able to retrieve the gem contents"): + ok, res = self.client.query_image_content("alpine:latest", "gem") + + expect((ok, res)).to(be_successful_api_call) + expect(res["content_type"]).to(equal("gem")) + + with it("is able to retrieve the python contents"): + ok, res = self.client.query_image_content("alpine:latest", "python") + + expect((ok, res)).to(be_successful_api_call) + expect(res["content_type"]).to(equal("python")) + + with it("is able to retrieve the java contents"): + ok, res = self.client.query_image_content("alpine:latest", "java") + + expect((ok, res)).to(be_successful_api_call) + expect(res["content_type"]).to(equal("java")) + + with it("is able to retrieve the files contents"): + ok, res = self.client.query_image_content("alpine:latest", "files") + + expect((ok, res)).to(be_successful_api_call) + expect(res["content"]).to( + contain(have_keys("filename", "gid", "linkdest", "mode", "sha256", "size", "type", "uid"))) + expect(res["content_type"]).to(equal("files")) + + with context("when the type is not in the supported list"): + with it("returns an error indicating the type is incorrect"): + ok, res = self.client.query_image_content("alpine:latest", "Unknown") + + expect((ok, res)).not_to(be_successful_api_call) + expect(res).to(equal( + "unsupported type provided: unknown, must be one of ['os', 'files', 'npm', 'gem', 'python', 'java']"))