10000 add checks for PolicyLinks and ServiceIdentities · python-microservices/consulate@aeb8242 · GitHub
[go: up one dir, main page]

Skip to content

Commit aeb8242

Browse files
committed
add checks for PolicyLinks and ServiceIdentities
1 parent e01c068 commit aeb8242

File tree

2 files changed

+50
-0
lines changed
< 10000 span class="prc-TooltipV2-Tooltip-cYMVY" data-direction="se" aria-hidden="true" id=":R12plab:">Collapse file tree

2 files changed

+50
-0
lines changed

consulate/api/acl.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
44
"""
55
import logging
6+
import json
67

78
from consulate.models import acl as model
89
from consulate.api import base
@@ -11,6 +12,48 @@
1112
LOGGER = logging.getLogger(__name__)
1213

1314

15+
def __check_policylinks(policies):
16+
""" Checks if policies is formatted correctly.
17+
:param list policies: A list of PolicyLink.
18+
:param rtype: bool
19+
:raises: consulate.exceptions.ACLFormatError
20+
21+
"""
22+
for policy in policies:
23+
if not ('ID' in policy or 'Name' in policy):
24+
raise exceptions.ACLPolicyFormatError(str(policy))
25+
26+
return True
27+
28+
29+
def __check_service_identities(service_identities):
30+
""" Checks if service_identities is formatted correctly.
31+
:param list service_identities: A ServiceIdentity list
32+
:param rtype: bool
33+
:raises: consulate.exceptions.ACLFormatError
34+
35+
"""
36+
for service_identity in service_identities:
37+
if 'ServiceName' not in service_identity:
38+
raise exceptions.ACLPolicyFormatError(str(service_identity))
39+
40+
return True
41+
42+
43+
def __create_json_format(structures, check):
44+
"""Creates a json string from a structures provided check passes.
45+
:param list structure: a PolicyLinks or ServiceIdentities.
46+
:param function check: a function to check structure
47+
:param rtype: str
48+
49+
"""
50+
formatted = None
51+
if structures is not None and check(structures):
52+
formatted = json.dumps(structures)
53+
54+
return formatted
55+
56+
1457
class ACL(base.Endpoint):
1558
"""The ACL endpoints are used to create, update, destroy, and query ACL
1659
tokens.

consulate/exceptions.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ class ACLDisabled(ConsulateException):
2424
"""Raised when ACL related calls are made while ACLs are disabled"""
2525

2626

27+
class ACLFormatError(ConsulateException):
28+
"""Raised when PolicyLinks is missing 'ID' and 'Name' in a PolicyLink or
29+
when ServiceIdentities is missing 'ServiceName' field in a ServiceIdentity.
30+
31+
"""
32+
33+
2734
class Forbidden(ConsulateException):
2835
"""Raised when ACLs are enabled and the token does not validate"""
2936

0 commit comments

Comments
 (0)
0