From e8a92b8238a15cd060696d92454f279b7167995a Mon Sep 17 00:00:00 2001 From: Henning Jacobs Date: Fri, 3 Jun 2016 17:52:33 +0200 Subject: [PATCH] #39 use Clair URL from Pier One tags --- pierone/cli.py | 35 +++++------------------------------ tests/test_cli.py | 7 ++++++- 2 files changed, 11 insertions(+), 31 deletions(-) diff --git a/pierone/cli.py b/pierone/cli.py index efdc834..833c3ab 100644 --- a/pierone/cli.py +++ b/pierone/cli.py @@ -23,7 +23,6 @@ help='Use alternative output format') url_option = click.option('--url', help='Pier One URL', metavar='URI') -clair_url_option = click.option('--clair-url', help='Clair URL', metavar='CLAIR_URI') CVE_STYLES = { 'TOO_OLD': { @@ -136,28 +135,6 @@ def set_pierone_url(config: dict, url: str) -> None: return url -def set_clair_url(config: dict, url: str) -> None: - '''Read Clair URL from cli, from config file or from stdin.''' - url = url or config.get('clair_url') - - while not url: - url = click.prompt('Please enter the Clair URL', type=UrlType()) - - try: - requests.get(url, timeout=5) - except: - error('Could not reach {}'.format(url)) - url = None - - if '://' not in url: - # issue 63: gracefully handle URLs without scheme - url = 'https://{}'.format(url) - - config['clair_url'] = url - stups_cli.config.store_config(config, 'pierone') - return url - - @click.group(cls=AliasedGroup, context_settings=CONTEXT_SETTINGS) @click.option('-V', '--version', is_flag=True, callback=print_version, expose_value=False, is_eager=True, help='Print the current version number and exit.') @@ -223,11 +200,11 @@ def get_tags(url, team, art, access_token): return r.json() -def get_clair_features(url, layer_id, access_token): - if layer_id is None: +def get_clair_features(clair_details_url, access_token): + if not clair_details_url: return [] - r = request(url, '/v1/layers/{}?vulnerabilities&features'.format(layer_id), access_token) + r = request(clair_details_url, '?vulnerabilities&features', access_token) if r.status_code == 404: # empty list of tags (layer does not exist) return [] @@ -307,19 +284,17 @@ def tags(config, team: str, artifact, url, output, limit): @click.argument('artifact') @click.argument('tag') @url_option -@clair_url_option @output_option @click.pass_obj -def cves(config, team, artifact, tag, url, clair_url, output): +def cves(config, team, artifact, tag, url, output): '''List all CVE's found by Clair service for a specific artifact tag''' set_pierone_url(config, url) - set_clair_url(config, clair_url) rows = [] token = get_token() for artifact_tag in get_tags(config.get('url'), team, artifact, token): if artifact_tag['name'] == tag: - installed_software = get_clair_features(config.get('clair_url'), artifact_tag.get('clair_id'), token) + installed_software = get_clair_features(artifact_tag.get('clair_details'), token) for software_pkg in installed_software: for cve in software_pkg.get('Vulnerabilities', []): rows.append({ diff --git a/tests/test_cli.py b/tests/test_cli.py index 552a8e5..013d23a 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -179,6 +179,7 @@ def test_tags(monkeypatch, tmpdir): "created_by": "myuser", "image": "sha256:here", "clair_id": "sha256:here", + "clair_details": "https://clair.example.org/foo/", "severity_fix_available": None, "severity_no_fix_available": None }, @@ -189,6 +190,7 @@ def test_tags(monkeypatch, tmpdir): "created_by": "myuser", "image": "sha256:here", "clair_id": "sha256:here", + "clair_details": "https://clair.example.org/foo/", "severity_fix_available": "clair:CouldntFigureOut", "severity_no_fix_available": "clair:CouldntFigureOut" }, @@ -199,6 +201,7 @@ def test_tags(monkeypatch, tmpdir): "created_by": "myuser", "image": "sha256:here", "clair_id": "sha256:here", + "clair_details": "https://clair.example.org/foo/", "severity_fix_available": "clair:NoCVEsFound", "severity_no_fix_available": "clair:NoCVEsFound" }, @@ -209,6 +212,7 @@ def test_tags(monkeypatch, tmpdir): "created_by": "myuser", "image": "sha256:here", "clair_id": "sha256:here", + "clair_details": "https://clair.example.org/foo/", "severity_fix_available": "High", "severity_no_fix_available": "Medium" } @@ -288,6 +292,7 @@ def test_cves(monkeypatch, tmpdir): "created_by": "myuser", "image": "sha256:here", "clair_id": "sha256:here", + "clair_details": "https://clair.example.org/some/path", "severity_fix_available": "High", "severity_no_fix_available": "Medium" } @@ -304,7 +309,7 @@ def test_cves(monkeypatch, tmpdir): ] runner = CliRunner() - monkeypatch.setattr('stups_cli.config.load_config', lambda x: {'url': 'foobar', 'clair_url': 'barfoo'}) + monkeypatch.setattr('stups_cli.config.load_config', lambda x: {'url': 'foobar'}) monkeypatch.setattr('zign.api.get_token', MagicMock(return_value='tok123')) monkeypatch.setattr('os.path.expanduser', lambda x: x.replace('~', str(tmpdir))) monkeypatch.setattr('pierone.api.session.get', MagicMock(return_value=response))