Commit 7a5b138d authored by David Trudgian's avatar David Trudgian
Browse files

Tidying up a bit

parent 59fa5439
from __future__ import print_function from __future__ import print_function
import requests import requests
import json
import sys import sys
from util import pretty_json, err_and_exit
def check_clair(API_URI, quiet): def check_clair(API_URI, quiet):
"""Check Clair is accessible by call to namespaces end point""" """Check Clair is accessible by call to namespaces end point"""
...@@ -15,8 +16,7 @@ def check_clair(API_URI, quiet): ...@@ -15,8 +16,7 @@ def check_clair(API_URI, quiet):
if not quiet: if not quiet:
sys.stderr.write("Found Clair server with %d namespaces\n" % namespace_count) sys.stderr.write("Found Clair server with %d namespaces\n" % namespace_count)
except Exception as e: except Exception as e:
sys.stderr.write("Error - couldn't access Clair v1 API at %s\n%s\n" % (API_URI, e.message)) err_and_exit("Error - couldn't access Clair v1 API at %s\n%s\n" % (API_URI, e.message), 1)
sys.exit(1)
return True return True
...@@ -35,13 +35,10 @@ def post_layer(API_URI, image_name, image_uri, quiet): ...@@ -35,13 +35,10 @@ def post_layer(API_URI, image_name, image_uri, quiet):
if not quiet: if not quiet:
sys.stderr.write("Image registered as layer with Clair\n") sys.stderr.write("Image registered as layer with Clair\n")
else: else:
pretty_response = json.dumps(r.json(), separators=(',', ':'), sort_keys=True, indent=2) err_and_exit("Failed registering image with Clair\n %s\n" % pretty_json(r), 1)
sys.stderr.write("Failed registering image with Clair\n %s\n" % pretty_response)
sys.exit(1)
except Exception as e: except Exception as e:
sys.stderr.write("Error - couldn't send image to Clair - %s\n" % (e)) err_and_exit("Error - couldn't send image to Clair - %s\n" % (e), 1)
sys.exit(1)
def get_report(API_URI, image_name): def get_report(API_URI, image_name):
...@@ -53,13 +50,10 @@ def get_report(API_URI, image_name): ...@@ -53,13 +50,10 @@ def get_report(API_URI, image_name):
if r.status_code == requests.codes.ok: if r.status_code == requests.codes.ok:
return r.json() return r.json()
else: else:
pretty_response = json.dumps(r.json(), separators=(',', ':'), sort_keys=True, indent=2) err_and_exit("Failed retrieving report from Clair\n %s\n" % pretty_json(r), 1)
sys.stderr.write("Failed retrieving report from Clair\n %s\n" % pretty_response)
sys.exit(1)
except Exception as e: except Exception as e:
sys.stderr.write("Error - couldn't retrieve report from Clair - %s\n" % (e)) err_and_exit("Error - couldn't retrieve report from Clair - %s\n" % (e), 1)
sys.exit(1)
def format_report_text(report): def format_report_text(report):
......
...@@ -9,6 +9,7 @@ from .clair import check_clair, post_layer, get_report, format_report_text ...@@ -9,6 +9,7 @@ from .clair import check_clair, post_layer, get_report, format_report_text
from .util import sha256 from .util import sha256
from .image import check_image, image_to_tgz, http_server from .image import check_image, image_to_tgz, http_server
@click.command() @click.command()
@click.option('--clair-uri', default="http://localhost:6060", @click.option('--clair-uri', default="http://localhost:6060",
help='Base URI for your Clair server') help='Base URI for your Clair server')
......
...@@ -3,16 +3,16 @@ import sys ...@@ -3,16 +3,16 @@ import sys
import tempfile import tempfile
from os import path, chdir from os import path, chdir
import click
from six.moves import SimpleHTTPServer, socketserver from six.moves import SimpleHTTPServer, socketserver
from .util import err_and_exit
def check_image(image): def check_image(image):
"""Check if specified image file exists""" """Check if specified image file exists"""
if not path.isfile(image): if not path.isfile(image):
click.secho('Error: Singularity image "%s" not found.' % image, fg='red', err=True) err_and_exit('Error: Singularity image "%s" not found.' % image, 66) # E_NOINPUT
sys.exit(66) # E_NOINPUT
return True return True
...@@ -31,8 +31,7 @@ def image_to_tgz(image, quiet): ...@@ -31,8 +31,7 @@ def image_to_tgz(image, quiet):
try: try:
subprocess.check_call(cmd) subprocess.check_call(cmd)
except (subprocess.CalledProcessError, OSError) as e: except (subprocess.CalledProcessError, OSError) as e:
sys.stderr.write("Error calling Singularity export to create .tar file\n%s" % e.message) err_and_exit("Error calling Singularity export to create .tar file\n%s" % e.message, 1)
sys.exit(1)
cmd = ['gzip', tar_file] cmd = ['gzip', tar_file]
...@@ -42,8 +41,7 @@ def image_to_tgz(image, quiet): ...@@ -42,8 +41,7 @@ def image_to_tgz(image, quiet):
try: try:
subprocess.check_call(cmd) subprocess.check_call(cmd)
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
sys.stderr.write("Error calling gzip export to compress .tar file\n%s" % e.message) err_and_exit("Error calling gzip export to compress .tar file\n%s" % e.message, 1)
sys.exit(1)
return (temp_dir, tar_gz_file) return (temp_dir, tar_gz_file)
......
import hashlib import hashlib
import json
import sys
def sha256(fname): def sha256(fname):
"""Compute sha256 hash for file fname"""
hash_sha256 = hashlib.sha256() hash_sha256 = hashlib.sha256()
with open(fname, "rb") as f: with open(fname, "rb") as f:
for chunk in iter(lambda: f.read(65536), b""): for chunk in iter(lambda: f.read(65536), b""):
hash_sha256.update(chunk) hash_sha256.update(chunk)
return hash_sha256.hexdigest() return hash_sha256.hexdigest()
def pretty_json(obj):
"""Format an object into json nicely"""
return json.dumps(obj.json(), separators=(',', ':'), sort_keys=True, indent=2)
def err_and_exit(message, code=1):
"""Write error to STDERR and exit with supplied code"""
sys.stderr.write(message)
sys.exit(code)
...@@ -6,6 +6,7 @@ from clair_singularity.cli import cli ...@@ -6,6 +6,7 @@ from clair_singularity.cli import cli
from .test_image import testimage from .test_image import testimage
@pytest.fixture @pytest.fixture
def runner(): def runner():
return CliRunner() return CliRunner()
...@@ -15,8 +16,11 @@ def test_help(runner): ...@@ -15,8 +16,11 @@ def test_help(runner):
result = runner.invoke(cli, ['--help']) result = runner.invoke(cli, ['--help'])
assert 'Usage:' in result.output assert 'Usage:' in result.output
def test_full_json(runner, testimage): def test_full_json(runner, testimage):
result = runner.invoke(cli, ['--quiet', '--json-output', '--bind-ip', '127.0.0.1', '--bind-port', '8081', '--clair-uri', 'http://127.0.0.1:6060', testimage]) result = runner.invoke(cli,
['--quiet', '--json-output', '--bind-ip', '127.0.0.1', '--bind-port', '8081', '--clair-uri',
'http://127.0.0.1:6060', testimage])
output = json.loads(result.output) output = json.loads(result.output)
# Using the shub://396 image and the 2017-08-21 clair db... # Using the shub://396 image and the 2017-08-21 clair db...
...@@ -32,7 +36,8 @@ def test_full_json(runner, testimage): ...@@ -32,7 +36,8 @@ def test_full_json(runner, testimage):
def test_full_text(runner, testimage): def test_full_text(runner, testimage):
result = runner.invoke(cli, ['--quiet', '--bind-ip', '127.0.0.1', '--bind-port', '8082', '--clair-uri', 'http://127.0.0.1:6060', testimage]) result = runner.invoke(cli, ['--quiet', '--bind-ip', '127.0.0.1', '--bind-port', '8082', '--clair-uri',
'http://127.0.0.1:6060', testimage])
# Check we do have some CVEs we expect reported here # Check we do have some CVEs we expect reported here
assert 'bash - 4.3-14ubuntu1.1' in result.output assert 'bash - 4.3-14ubuntu1.1' in result.output
assert 'CVE-2016-9401' in result.output assert 'CVE-2016-9401' in result.output
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment