code
stringlengths 10
805k
| def_use_chains
sequencelengths 0
667
|
---|---|
import re
from anchore_engine.apis.authorization import (
ActionBoundPermission,
Permission,
RequestingAccountValue,
get_authorizer,
)
from anchore_engine.apis.context import ApiRequestContextProxy
from anchore_engine.apis.exceptions import BadRequest
from anchore_engine.clients.services import internal_client_for
from anchore_engine.clients.services.catalog import CatalogClient
from anchore_engine.common.helpers import make_response_error
from anchore_engine.configuration.localconfig import (
ADMIN_ACCOUNT_NAME,
GLOBAL_RESOURCE_DOMAIN,
)
authorizer = get_authorizer()
digest_regex = re.compile("^sha256:[abcdef0-9]+$")
def handle_proxy_response(resp):
if issubclass(Exception, resp.__class__):
if hasattr(resp, "httpcode"):
return make_response_error(resp, in_httpcode=resp.httpcode), resp.httpcode
else:
return make_response_error(resp, in_httpcode=500), 500
else:
return resp, 200
@authorizer.requires([ActionBoundPermission(domain=RequestingAccountValue())])
def list_archives():
"""
GET /archives
:return: JSON object for archive summary
"""
client = internal_client_for(CatalogClient, ApiRequestContextProxy.namespace())
try:
return handle_proxy_response(client.list_archives())
except Exception as ex:
return handle_proxy_response(ex)
@authorizer.requires([ActionBoundPermission(domain=RequestingAccountValue())])
def list_analysis_archive_rules(system_global=True):
"""
GET /archives/rules
:return:
"""
client = internal_client_for(CatalogClient, ApiRequestContextProxy.namespace())
try:
return handle_proxy_response(
client.list_analysis_archive_rules(system_global=system_global)
)
except Exception as ex:
return handle_proxy_response(ex)
@authorizer.requires([ActionBoundPermission(domain=RequestingAccountValue())])
def create_analysis_archive_rule(rule):
"""
POST /archives/rules
:param rule: the rule's json object definition
:return:
"""
# Permission check on the system_global field, only admins
if rule.get("system_global"):
perm = Permission(GLOBAL_RESOURCE_DOMAIN, "createArchiveTransitionRule", "*")
# Will raise exception if unauthorized
authorizer.authorize(ApiRequestContextProxy.identity(), [perm])
# Validation for max_images_per_account
if (
not rule.get("system_global")
and rule.get("max_images_per_account", None) is not None
):
raise BadRequest(
"Cannot set max_images_per_account on a rule that isn't system_global", {}
)
client = internal_client_for(CatalogClient, ApiRequestContextProxy.namespace())
try:
return handle_proxy_response(client.add_analysis_archive_rule(rule))
except Exception as ex:
return handle_proxy_response(ex)
@authorizer.requires([ActionBoundPermission(domain=RequestingAccountValue())])
def delete_analysis_archive_rule(ruleId):
"""
DELETE /archives/rules/{ruleId}
:param ruleId:
:return:
"""
client = internal_client_for(CatalogClient, ApiRequestContextProxy.namespace())
try:
resp1 = handle_proxy_response(client.delete_analysis_archive_rule(ruleId))
if resp1[1] == 404 and ApiRequestContextProxy.namespace() != ADMIN_ACCOUNT_NAME:
# Yes, this is a bit ugly
# Get the rule, check if a global rule and adjust error code appropriately
try:
c2 = internal_client_for(CatalogClient, ADMIN_ACCOUNT_NAME)
r2 = c2.get_analysis_archive_rule(ruleId)
if r2 and r2.get("system_global", False):
return (
make_response_error(
"Non-admins cannot modify/delete system global rules",
in_httpcode=403,
),
403,
)
except Exception as ex:
pass
return resp1
except Exception as ex:
return handle_proxy_response(ex)
@authorizer.requires([ActionBoundPermission(domain=RequestingAccountValue())])
def get_analysis_archive_rule(ruleId):
"""
GET /archives/rules/{ruleId}
:param ruleId:
:return:
"""
client = internal_client_for(CatalogClient, ApiRequestContextProxy.namespace())
try:
resp1 = handle_proxy_response(client.get_analysis_archive_rule(ruleId))
if resp1[1] == 404 and ApiRequestContextProxy.namespace() != ADMIN_ACCOUNT_NAME:
# Yes, this is a bit ugly
# Get the rule, check if a global rule
try:
c2 = internal_client_for(CatalogClient, ADMIN_ACCOUNT_NAME)
r2 = handle_proxy_response(c2.get_analysis_archive_rule(ruleId))
if r2 and r2[1] == 200 and r2[0].get("system_global", False):
# Allow it
return handle_proxy_response(r2)
except Exception as ex:
pass
return resp1
except Exception as ex:
return handle_proxy_response(ex)
# @authorizer.requires([ActionBoundPermission(domain=RequestingAccountValue())])
# def get_analysis_archive_rule_history(ruleId):
# """
#
# GET /archives/rules/{ruleId}/history
#
# :param ruleId:
# :return: list of events for the rule
# """
# client = internal_client_for(CatalogClient, ApiRequestContextProxy.namespace())
# try:
# resp1 = handle_proxy_response(client.get_analysis_archive_rule_history(ruleId))
# if resp1[1] == 404 and ApiRequestContextProxy.namespace() != ADMIN_ACCOUNT_NAME:
# # Yes, this is a bit ugly
# # Get the rule, check if a global rule and adjust error code appropriately
# try:
# c2 = internal_client_for(CatalogClient, ADMIN_ACCOUNT_NAME)
# r2 = handle_proxy_response(c2.get_analysis_archive_rule(ruleId))
# if r2 and r2[1] == 200 and r2[0].get('system_global', False):
# return make_response_error('Non-admins cannot modify/delete system global rules', in_httpcode=403), 403
# except Exception as ex:
# pass
# return resp1
# except Exception as ex:
# return handle_proxy_response(ex)
@authorizer.requires([ActionBoundPermission(domain=RequestingAccountValue())])
def list_analysis_archive():
"""
GET /archives/images
:return: array of archivedimage json objects
"""
client = internal_client_for(CatalogClient, ApiRequestContextProxy.namespace())
try:
return handle_proxy_response(client.list_archived_analyses())
except Exception as ex:
return handle_proxy_response(ex)
@authorizer.requires([ActionBoundPermission(domain=RequestingAccountValue())])
def archive_image_analysis(imageReferences):
"""
POST /archives/images
:param imageReferences: list of json object that reference images to archive
:return:
"""
client = internal_client_for(CatalogClient, ApiRequestContextProxy.namespace())
try:
return handle_proxy_response(client.archive_analyses(imageReferences))
except Exception as ex:
return handle_proxy_response(ex)
@authorizer.requires([ActionBoundPermission(domain=RequestingAccountValue())])
def get_archived_analysis(imageDigest):
"""
GET /archives/images/{imageDigest}
:param imageDigest:
:return:
"""
client = internal_client_for(CatalogClient, ApiRequestContextProxy.namespace())
try:
return handle_proxy_response(client.get_archived_analysis(imageDigest))
except Exception as ex:
return handle_proxy_response(ex)
@authorizer.requires([ActionBoundPermission(domain=RequestingAccountValue())])
def delete_archived_analysis(imageDigest):
"""
DELETE /archives/images/{imageDigest}
:param imageDigest:
:return:
"""
client = internal_client_for(CatalogClient, ApiRequestContextProxy.namespace())
try:
return handle_proxy_response(client.delete_archived_analysis(imageDigest))
except Exception as e:
return handle_proxy_response(e)
| [
[
[
7,
9
],
[
617,
619
]
],
[
[
63,
84
],
[
999,
1020
],
[
1405,
1426
],
[
1880,
1901
],
[
2938,
2959
],
[
4175,
4196
],
[
6426,
6447
],
[
6859,
6880
],
[
7363,
7384
],
[
7820,
7841
]
],
[
[
90,
100
],
[
2196,
2206
]
],
[
[
106,
128
],
[
1028,
1050
],
[
1434,
1456
],
[
1909,
1931
],
[
2967,
2989
],
[
4204,
4226
],
[
6455,
6477
],
[
6888,
6910
],
[
7392,
7414
],
[
7849,
7871
]
],
[
[
134,
148
],
[
584,
598
]
],
[
[
192,
214
],
[
1206,
1228
],
[
1618,
1640
],
[
2344,
2366
],
[
2723,
2745
],
[
3171,
3193
],
[
3331,
3353
],
[
4402,
4424
],
[
4559,
4581
],
[
6651,
6673
],
[
7146,
7168
],
[
7602,
7624
],
[
8064,
8086
]
],
[
[
258,
268
],
[
2565,
2575
]
],
[
[
313,
332
],
[
1171,
1190
],
[
1583,
1602
],
[
2688,
2707
],
[
3136,
3155
],
[
3552,
3571
],
[
4367,
4386
],
[
4744,
4763
],
[
6616,
6635
],
[
7111,
7130
],
[
7567,
7586
],
[
8029,
8048
]
],
[
[
385,
398
],
[
1191,
1204
],
[
1603,
1616
],
[
2708,
2721
],
[
3156,
3169
],
[
3572,
3585
],
[
4387,
4400
],
[
4764,
4777
],
[
6636,
6649
],
[
7131,
7144
],
[
7587,
7600
],
[
8049,
8062
]
],
[
[
441,
460
],
[
791,
810
],
[
892,
911
],
[
3776,
3795
]
],
[
[
520,
538
],
[
3369,
3387
],
[
3587,
3605
],
[
4597,
4615
],
[
4779,
4797
]
],
[
[
544,
566
],
[
2207,
2229
]
],
[
[
571,
581
],
[
978,
988
],
[
1384,
1394
],
[
1859,
1869
],
[
2917,
2927
],
[
4154,
4164
],
[
6405,
6415
],
[
6838,
6848
],
[
7342,
7352
],
[
7799,
7809
],
[
2323,
2333
]
],
[
[
602,
614
]
],
[
[
659,
680
],
[
1266,
1287
],
[
1355,
1376
],
[
1678,
1699
],
[
1830,
1851
],
[
2783,
2804
],
[
2888,
2909
],
[
3232,
3253
],
[
4125,
4146
],
[
4463,
4484
],
[
4820,
4841
],
[
5016,
5037
],
[
5163,
5184
],
[
6711,
6732
],
[
6809,
6830
],
[
7206,
7227
],
[
7313,
7334
],
[
7662,
7683
],
[
7770,
7791
],
[
8124,
8145
],
[
8234,
8255
]
],
[
[
1060,
1073
]
],
[
[
1466,
1493
]
],
[
[
1941,
1969
]
],
[
[
2999,
3027
]
],
[
[
4236,
4261
]
],
[
[
6487,
6508
]
],
[
[
6920,
6942
]
],
[
[
7424,
7445
]
],
[
[
7881,
7905
]
]
] |