Skip to content
This repository was archived by the owner on Jul 10, 2024. It is now read-only.

Commit 9ea297a

Browse files
committed
Throw exception and gracefully fail if parent callback is missing from context.
Signed-off-by: Daniel Palmer <dan.palmer@anchore.com>
1 parent 6a5b560 commit 9ea297a

File tree

12 files changed

+218
-182
lines changed

12 files changed

+218
-182
lines changed

anchorecli/cli/account.py

Lines changed: 60 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,19 @@ def execute():
4949
@click.pass_context
5050
def get_current_user(ctx):
5151
global whoami
52+
ecode = 0
5253

53-
ctx.parent.obj.execute_callback()
54+
try:
55+
anchorecli.cli.utils.handle_parent_callback(ctx)
56+
except RuntimeError as err:
57+
print(
58+
anchorecli.cli.utils.format_error_output(
59+
config, "get_current_user", {}, err
60+
)
61+
)
62+
ecode = 2
63+
anchorecli.cli.utils.doexit(ecode)
5464

55-
ecode = 0
5665
print(anchorecli.cli.utils.format_output(config, "account_whoami", {}, whoami))
5766
anchorecli.cli.utils.doexit(ecode)
5867

@@ -70,11 +79,11 @@ def add(ctx, account_name, email):
7079
EMAIL: email address associated with account (optional)
7180
7281
"""
73-
ctx.parent.obj.execute_callback()
74-
7582
ecode = 0
7683

7784
try:
85+
anchorecli.cli.utils.handle_parent_callback(ctx)
86+
7887
ret = anchorecli.clients.apiexternal.add_account(
7988
config, account_name=account_name, email=email
8089
)
@@ -104,11 +113,11 @@ def get(ctx, account_name):
104113
ACCOUNT_NAME: name of new account to create
105114
106115
"""
107-
ctx.parent.obj.execute_callback()
108-
109116
ecode = 0
110117

111118
try:
119+
anchorecli.cli.utils.handle_parent_callback(ctx)
120+
112121
ret = anchorecli.clients.apiexternal.get_account(
113122
config, account_name=account_name
114123
)
@@ -136,10 +145,11 @@ def get(ctx, account_name):
136145
@click.pass_context
137146
def list_accounts(ctx):
138147
""""""
139-
ctx.parent.obj.execute_callback()
140-
141148
ecode = 0
149+
142150
try:
151+
anchorecli.cli.utils.handle_parent_callback(ctx)
152+
143153
ret = anchorecli.clients.apiexternal.list_accounts(config)
144154
ecode = anchorecli.cli.utils.get_ecode(ret)
145155
if ret["success"]:
@@ -171,10 +181,19 @@ def delete(ctx, account_name, dontask):
171181
ACCOUNT_NAME: name of account to delete (must be disabled first)
172182
173183
"""
174-
ctx.parent.obj.execute_callback()
175-
176184
ecode = 0
177185

186+
try:
187+
anchorecli.cli.utils.handle_parent_callback(ctx)
188+
except RuntimeError as err:
189+
print(
190+
anchorecli.cli.utils.format_error_output(
191+
config, "account_delete", {}, err
192+
)
193+
)
194+
ecode = 2
195+
anchorecli.cli.utils.doexit(ecode)
196+
178197
answer = "n"
179198
if dontask:
180199
answer = "y"
@@ -227,11 +246,11 @@ def enable(ctx, account_name):
227246
ACCOUNT_NAME: name of account to enable
228247
229248
"""
230-
ctx.parent.obj.execute_callback()
231-
232249
ecode = 0
233250

234251
try:
252+
anchorecli.cli.utils.handle_parent_callback(ctx)
253+
235254
ret = anchorecli.clients.apiexternal.enable_account(
236255
config, account_name=account_name
237256
)
@@ -263,11 +282,11 @@ def disable(ctx, account_name):
263282
ACCOUNT_NAME: name of account to disable
264283
265284
"""
266-
ctx.parent.obj.execute_callback()
267-
268285
ecode = 0
269286

270287
try:
288+
anchorecli.cli.utils.handle_parent_callback(ctx)
289+
271290
ret = anchorecli.clients.apiexternal.disable_account(
272291
config, account_name=account_name
273292
)
@@ -314,14 +333,14 @@ def user_add(ctx, user_name, user_password, account):
314333
ACCOUNT: optional name of the account to act as
315334
316335
"""
317-
ctx.parent.obj.execute_callback()
318-
319-
if not account:
320-
account = whoami.get("account", {}).get("name", None)
321-
322336
ecode = 0
323337

324338
try:
339+
anchorecli.cli.utils.handle_parent_callback(ctx)
340+
341+
if not account:
342+
account = whoami.get("account", {}).get("name", None)
343+
325344
# do some input validation
326345
if not re.match(".{6,128}$", user_password):
327346
raise Exception(
@@ -362,14 +381,14 @@ def user_delete(ctx, user_name, account):
362381
ACCOUNT: optional name of the account to act as
363382
364383
"""
365-
ctx.parent.obj.execute_callback()
366-
367-
if not account:
368-
account = whoami.get("account", {}).get("name", None)
369-
370384
ecode = 0
371385

372386
try:
387+
anchorecli.cli.utils.handle_parent_callback(ctx)
388+
389+
if not account:
390+
account = whoami.get("account", {}).get("name", None)
391+
373392
ret = anchorecli.clients.apiexternal.del_user(
374393
config, account_name=account, user_name=user_name
375394
)
@@ -401,14 +420,14 @@ def user_get(ctx, user_name, account):
401420
ACCOUNT: optional name of the account to act as
402421
403422
"""
404-
ctx.parent.obj.execute_callback()
405-
406-
if not account:
407-
account = whoami.get("account", {}).get("name", None)
408-
409423
ecode = 0
410424

411425
try:
426+
anchorecli.cli.utils.handle_parent_callback(ctx)
427+
428+
if not account:
429+
account = whoami.get("account", {}).get("name", None)
430+
412431
ret = anchorecli.clients.apiexternal.get_user(
413432
config, account_name=account, user_name=user_name
414433
)
@@ -439,14 +458,14 @@ def user_list(ctx, account):
439458
ACCOUNT: optional name of the account to act as
440459
441460
"""
442-
ctx.parent.obj.execute_callback()
443-
444-
if not account:
445-
account = whoami.get("account", {}).get("name", None)
446-
447461
ecode = 0
448462

449463
try:
464+
anchorecli.cli.utils.handle_parent_callback(ctx)
465+
466+
if not account:
467+
account = whoami.get("account", {}).get("name", None)
468+
450469
ret = anchorecli.clients.apiexternal.list_users(config, account_name=account)
451470
ecode = anchorecli.cli.utils.get_ecode(ret)
452471
if ret["success"]:
@@ -477,16 +496,16 @@ def user_setpassword(ctx, user_password, username, account):
477496
ACCOUNT: optional name of the account to act as
478497
479498
"""
480-
ctx.parent.obj.execute_callback()
481-
482-
if not account:
483-
account = whoami.get("account", {}).get("name", None)
484-
if not username:
485-
username = whoami.get("user", {}).get("username", None)
486-
487499
ecode = 0
488500

489501
try:
502+
anchorecli.cli.utils.handle_parent_callback(ctx)
503+
504+
if not account:
505+
account = whoami.get("account", {}).get("name", None)
506+
if not username:
507+
username = whoami.get("user", {}).get("username", None)
508+
490509
ret = anchorecli.clients.apiexternal.update_user_password(
491510
config,
492511
account_name=account,

anchorecli/cli/archives.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ def image_restore(ctx, image_digest):
4343
"""
4444
Add an analyzed image to the analysis archive
4545
"""
46-
ctx.parent.obj.execute_callback()
47-
4846
ecode = 0
4947

5048
try:
49+
anchorecli.cli.utils.handle_parent_callback(ctx)
50+
5151
if not re.match(digest_regex, image_digest):
5252
raise Exception(
5353
"Invalid image digest {}. Must conform to regex: {}".format(
@@ -86,11 +86,11 @@ def image_add(ctx, image_digests):
8686
"""
8787
Add an analyzed image to the analysis archive
8888
"""
89-
ctx.parent.obj.execute_callback()
90-
9189
ecode = 0
9290

9391
try:
92+
anchorecli.cli.utils.handle_parent_callback(ctx)
93+
9494
for digest in image_digests:
9595
if not re.match(digest_regex, digest):
9696
raise Exception(
@@ -129,11 +129,11 @@ def image_get(ctx, digest):
129129
"""
130130
INPUT_IMAGE: Input image can be in the following formats: Image Digest, ImageID or registry/repo:tag
131131
"""
132-
ctx.parent.obj.execute_callback()
133-
134132
ecode = 0
135133

136134
try:
135+
anchorecli.cli.utils.handle_parent_callback(ctx)
136+
137137
ret = anchorecli.clients.apiexternal.get_archived_analysis(config, digest)
138138

139139
if ret:
@@ -168,11 +168,11 @@ def image_get(ctx, digest):
168168
@images.command(name="list", short_help="List all archived image analyses")
169169
@click.pass_context
170170
def list_archived_analyses(ctx):
171-
ctx.parent.obj.execute_callback()
172-
173171
ecode = 0
174172

175173
try:
174+
anchorecli.cli.utils.handle_parent_callback(ctx)
175+
176176
ret = anchorecli.clients.apiexternal.list_archived_analyses(config)
177177
ecode = anchorecli.cli.utils.get_ecode(ret)
178178
if ret["success"]:
@@ -204,11 +204,11 @@ def image_delete(ctx, digest, force):
204204
"""
205205
INPUT_IMAGE: Input image can be in the following formats: Image Digest, ImageID or registry/repo:tag
206206
"""
207-
ctx.parent.obj.execute_callback()
208-
209207
ecode = 0
210208

211209
try:
210+
anchorecli.cli.utils.handle_parent_callback(ctx)
211+
212212
ret = anchorecli.clients.apiexternal.delete_archived_analysis(config, digest)
213213

214214
if ret:
@@ -283,21 +283,21 @@ def rule_add(
283283
archive|delete: the transition to execute - archive or delete. delete transitions occur on already archived analysis, not on the active image analysis
284284
285285
"""
286-
ctx.parent.obj.execute_callback()
287-
288286
ecode = 0
289287

290-
if days_old == 0 and tag_versions_newer == 0:
291-
resp = click.prompt(
292-
"Are you sure you want to use 0 for both days old limit and number of tag versions newer? WARNING: This will archive all images that match the registry/repo/tag selectors as soon as they are analyzed",
293-
type=click.Choice(["y", "n"]),
294-
default="n",
295-
)
296-
if resp.lower() != "y":
297-
ecode = 0
298-
anchorecli.cli.utils.doexit(ecode)
299-
300288
try:
289+
anchorecli.cli.utils.handle_parent_callback(ctx)
290+
291+
if days_old == 0 and tag_versions_newer == 0:
292+
resp = click.prompt(
293+
"Are you sure you want to use 0 for both days old limit and number of tag versions newer? WARNING: This will archive all images that match the registry/repo/tag selectors as soon as they are analyzed",
294+
type=click.Choice(["y", "n"]),
295+
default="n",
296+
)
297+
if resp.lower() != "y":
298+
ecode = 0
299+
anchorecli.cli.utils.doexit(ecode)
300+
301301
ret = anchorecli.clients.apiexternal.add_transition_rule(
302302
config,
303303
days_old,
@@ -334,11 +334,11 @@ def rule_add(
334334
@click.argument("rule_id", nargs=1)
335335
@click.pass_context
336336
def rule_get(ctx, rule_id):
337-
ctx.parent.obj.execute_callback()
338-
339337
ecode = 0
340338

341339
try:
340+
anchorecli.cli.utils.handle_parent_callback(ctx)
341+
342342
ret = anchorecli.clients.apiexternal.get_transition_rule(config, rule_id)
343343

344344
if ret:
@@ -369,11 +369,11 @@ def rule_get(ctx, rule_id):
369369
@rules.command(name="list", short_help="List all transition rules for the account")
370370
@click.pass_context
371371
def list_transition_rules(ctx):
372-
ctx.parent.obj.execute_callback()
373-
374372
ecode = 0
375373

376374
try:
375+
anchorecli.cli.utils.handle_parent_callback(ctx)
376+
377377
ret = anchorecli.clients.apiexternal.list_transition_rules(config)
378378
ecode = anchorecli.cli.utils.get_ecode(ret)
379379
if ret["success"]:
@@ -401,11 +401,11 @@ def list_transition_rules(ctx):
401401
@click.argument("rule_id")
402402
@click.pass_context
403403
def rule_delete(ctx, rule_id):
404-
ctx.parent.obj.execute_callback()
405-
406404
ecode = 0
407405

408406
try:
407+
anchorecli.cli.utils.handle_parent_callback(ctx)
408+
409409
ret = anchorecli.clients.apiexternal.delete_transition_rule(config, rule_id)
410410

411411
if ret:

anchorecli/cli/evaluate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ def check(ctx, input_image, show_history, detail, tag, policy):
4444
"""
4545
INPUT_IMAGE: Input image can be in the following formats: Image Digest, ImageID or registry/repo:tag
4646
"""
47-
ctx.parent.obj.execute_callback()
48-
4947
ecode = 0
5048

5149
try:
50+
anchorecli.cli.utils.handle_parent_callback(ctx)
51+
5252
itype, image, imageDigest = anchorecli.cli.utils.discover_inputimage(
5353
config, input_image
5454
)

0 commit comments

Comments
 (0)