Skip to content
This repository was archived by the owner on Jul 10, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions anchorecli/cli/archives.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ def rules():
is_flag=True,
help="If true, make this a global rule (admin only)",
)
@click.option(
"--max-images-per-account",
help="Set the maximum number of images per account",
type=int,
)
def rule_add(
days_old,
tag_versions_newer,
Expand All @@ -249,6 +254,7 @@ def rule_add(
repository_selector,
tag_selector,
is_global,
max_images_per_account,
):
"""
Add an analyzed image to the analysis archive
Expand All @@ -259,6 +265,8 @@ def rule_add(

archive|delete: the transition to execute - archive or delete. delete transitions occur on already archived analysis, not on the active image analysis

max_images_per_account: The maximum number of images per account. If specified, no selector should be. Also, it can only be specified on a global rule

"""
ecode = 0

Expand All @@ -272,6 +280,20 @@ def rule_add(
ecode = 0
anchorecli.cli.utils.doexit(ecode)

if max_images_per_account and not is_global:
print("Error: max_images_per_account can only be specified on a global rule")
anchorecli.cli.utils.doexit(2)

if max_images_per_account and is_selector_default(
repository_selector, registry_selector, tag_selector
):
repository_selector = ""
registry_selector = ""
tag_selector = ""
elif max_images_per_account:
print("Error: Selector cannot be specified along with max_images_per_account")
anchorecli.cli.utils.doexit(2)

try:
ret = anchorecli.clients.apiexternal.add_transition_rule(
config,
Expand All @@ -282,6 +304,7 @@ def rule_add(
tag_selector,
transition,
is_global,
max_images_per_account,
)
ecode = anchorecli.cli.utils.get_ecode(ret)
if ret["success"]:
Expand All @@ -305,6 +328,10 @@ def rule_add(
anchorecli.cli.utils.doexit(ecode)


def is_selector_default(repo, registry, tag):
return repo == "*" and registry == "*" and tag == "*"


@rules.command(name="get", short_help="Show detail for a specific transition rule")
@click.argument("rule_id", nargs=1)
def rule_get(rule_id):
Expand Down
3 changes: 3 additions & 0 deletions anchorecli/clients/apiexternal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2375,6 +2375,7 @@ def add_transition_rule(
selector_tag="*",
transition="archive",
is_global=False,
max_images_per_account=None,
):
"""
POST /archives/rules
Expand All @@ -2387,6 +2388,7 @@ def add_transition_rule(
:param selector_tag: Wild-card supported string to match registry (e.g. 'docker.io', '*', or '*amazonaws.com')
:param transition: which transition to use, either 'archive' or 'delete'
:param is_global: should the rule be a global rule (bool)
:param max_images_per_account: the maximum number of images per account (must be only
:return:
"""

Expand Down Expand Up @@ -2420,6 +2422,7 @@ def add_transition_rule(
"analysis_age_days": analysis_age_days,
"transition": transition,
"system_global": is_global,
"max_images_per_account": max_images_per_account,
}

try:
Expand Down