Skip to content

Commit 30336ba

Browse files
committed
Accept shorthands for specfile
Signed-off-by: Matthias Büchse <matthias.buechse@alasca.cloud>
1 parent 09f7e68 commit 30336ba

3 files changed

Lines changed: 30 additions & 20 deletions

File tree

Tests/cli.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,24 @@
1313
import click
1414
import yaml
1515

16-
from scs_cert_lib import load_spec, annotate_validity, add_period
16+
from scs_cert_lib import load_spec, annotate_validity, add_period, normalize_scope
1717

1818

19+
HERE = os.path.dirname(__file__)
1920
DEFAULT_SPECPATH = {
20-
'50393e6f-2ae1-4c5c-a62c-3b75f2abef3f': './scs-compatible-iaas.yaml',
21-
'1fffebe6-fd4b-44d3-a36c-fc58b4bb0180': './scs-compatible-kaas.yaml',
21+
'50393e6f-2ae1-4c5c-a62c-3b75f2abef3f': os.path.join(HERE, 'scs-compatible-iaas.yaml'),
22+
'1fffebe6-fd4b-44d3-a36c-fc58b4bb0180': os.path.join(HERE, 'scs-compatible-kaas.yaml'),
2223
}
2324

2425
logger = logging.getLogger(__name__)
2526

2627

28+
def _load_spec(specpath):
29+
specpath = DEFAULT_SPECPATH.get(normalize_scope(specpath), specpath)
30+
with open(specpath, "r", encoding="UTF-8") as fileobj:
31+
return load_spec(yaml.load(fileobj, Loader=yaml.SafeLoader))
32+
33+
2734
def select_valid(versions: list) -> list:
2835
return [version for version in versions if version['_explicit_validity']]
2936

@@ -37,10 +44,9 @@ def cli():
3744
@click.option('--version', '-V', 'version', type=str, default=None)
3845
@click.option('--tests', '-t', 'tests', type=str, default=None)
3946
@click.option('--section', '-S', 'sections', type=str, multiple=True)
40-
@click.argument('specpath', type=click.Path(exists=True))
47+
@click.argument('specpath', type=str)
4148
def select(specpath, version, sections, tests):
42-
with open(specpath, "r", encoding="UTF-8") as specfile:
43-
spec = load_spec(yaml.load(specfile, Loader=yaml.SafeLoader))
49+
spec = _load_spec(specpath)
4450
checkdate = datetime.date.today()
4551
annotate_validity(spec['timeline'], spec['versions'], checkdate)
4652
if version is None:
@@ -121,13 +127,12 @@ def score(specpath, subject, score_yaml, report_yaml):
121127
if not subject:
122128
subject = scorecard['subject']
123129
if not specpath:
124-
specpath = DEFAULT_SPECPATH[scorecard['scope']]
130+
specpath = scorecard['scope']
125131
elif not subject:
126132
raise click.UsageError('need to supply at least one of -s or -S')
127133
elif not specpath:
128134
raise click.UsageError('need to supply at least one of --spec or -S')
129-
with open(specpath, "r", encoding="UTF-8") as specfile:
130-
spec = load_spec(yaml.load(specfile, Loader=yaml.SafeLoader))
135+
spec = _load_spec(specpath)
131136
scopeuuid = spec['uuid']
132137
snippet = yaml.load(sys.stdin.read(), Loader=yaml.SafeLoader)
133138
if not snippet:
@@ -161,8 +166,7 @@ def score(specpath, subject, score_yaml, report_yaml):
161166
@click.option('--spec', 'specpath', type=click.Path(exists=True))
162167
@click.argument('score_yaml', type=click.Path(exists=False))
163168
def init(specpath, subject, score_yaml):
164-
with open(specpath, "r", encoding="UTF-8") as specfile:
165-
spec = load_spec(yaml.load(specfile, Loader=yaml.SafeLoader))
169+
spec = _load_spec(specpath)
166170
scorecard = {
167171
'subject': subject,
168172
'scope': spec['uuid'],

Tests/scs_cert_lib.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
logger = logging.getLogger(__name__)
1616
__VERSION__ = "20260623"
1717

18+
SCOPE_ALIASES = {
19+
'sci': '50393e6f-2ae1-4c5c-a62c-3b75f2abef3f',
20+
'scs-compatible-iaas': '50393e6f-2ae1-4c5c-a62c-3b75f2abef3f',
21+
'sck': '1fffebe6-fd4b-44d3-a36c-fc58b4bb0180',
22+
'scs-compatible-kaas': '1fffebe6-fd4b-44d3-a36c-fc58b4bb0180',
23+
}
1824
# valid keywords for various parts of the spec, to be checked using `check_keywords`
1925
KEYWORDS = {
2026
'spec': ('uuid', 'name', 'url', 'versions', 'prerequisite', 'variables', 'scripts', 'groups', 'timeline'),
@@ -241,3 +247,7 @@ def make_report(scopeuuid, subject, results, log, creator=None, checked_at=None)
241247
},
242248
"log": log,
243249
}
250+
251+
252+
def normalize_scope(ident):
253+
return SCOPE_ALIASES.get(ident, ident)

compliance-monitor/monitor.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@
5151

5252

5353
try:
54-
from scs_cert_lib import load_spec, annotate_validity, add_period, eval_buckets, evaluate
54+
from scs_cert_lib import load_spec, annotate_validity, add_period, eval_buckets, evaluate, normalize_scope
5555
except ImportError:
5656
# the following course of action is not unproblematic because the Tests directory will be
5757
# mounted to the Docker instance, hence it's hard to tell what version we are gonna get;
5858
# however, unlike the reloading of the config, the import only happens once, and at that point
5959
# in time, both monitor.py and scs_cert_lib.py should come from the same git checkout
6060
import sys; sys.path.insert(0, os.path.abspath('../Tests')) # noqa: E702
61-
from scs_cert_lib import load_spec, annotate_validity, add_period, eval_buckets, evaluate
61+
from scs_cert_lib import load_spec, annotate_validity, add_period, eval_buckets, evaluate, normalize_scope
6262

6363

6464
class Settings:
@@ -634,7 +634,7 @@ def _scope_name(uuid):
634634

635635

636636
def _scope_url(uuid):
637-
scope = get_scopes().get(_resolve_scope(uuid))
637+
scope = get_scopes().get(normalize_scope(uuid))
638638
return scope['url'] if scope else '(n/a)'
639639

640640

@@ -696,10 +696,6 @@ def _resolve_group_locally(groups, subject, prefix=GROUP_PREFIX):
696696
return None, [subject]
697697

698698

699-
def _resolve_scope(scopeuuid):
700-
return SCOPE_ALIASES.get(scopeuuid, scopeuuid)
701-
702-
703699
@app.get("/{view_type}/detail/{subject}/{scopeuuid}")
704700
async def get_detail(
705701
request: Request,
@@ -712,7 +708,7 @@ async def get_detail(
712708

713709

714710
def _make_detail_view(conn, view_type, subject, scopeuuid, include_drafts=False):
715-
scopeuuid = _resolve_scope(scopeuuid)
711+
scopeuuid = normalize_scope(scopeuuid)
716712
with conn.cursor() as cur:
717713
group, subjects = _resolve_group(cur, subject)
718714
rows2 = []
@@ -825,7 +821,7 @@ async def get_healthz(request: Request):
825821
@pass_context
826822
def pick_filter(ctx, results, scopeuuid, *subjects):
827823
"""Jinja filter to pick scope results from `results` for given `subject` and `scope`"""
828-
scopeuuid = _resolve_scope(scopeuuid)
824+
scopeuuid = normalize_scope(scopeuuid)
829825
# simple case (backwards compatible): precisely one subject
830826
if len(subjects) == 1:
831827
group, subjects = _resolve_group_locally(ctx['groups'], subjects[0])

0 commit comments

Comments
 (0)