Skip to content

Commit 7830ddc

Browse files
authored
Merge pull request #94 from ssh-morty/master
ADD connection tags and access group CA key APIs as well as examples
2 parents 2a1e15f + 356c117 commit 7830ddc

File tree

5 files changed

+210
-0
lines changed

5 files changed

+210
-0
lines changed

examples/example-access-group-key.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# An example how to create a CA key for a access group and delete it.
2+
# Import the PrivX python library.
3+
import sys
4+
5+
import config
6+
7+
# this importation for demonstration purpose only
8+
# for proper importation of privx_api module
9+
# see https://github.com/SSHcom/privx-sdk-for-python#getting-started
10+
try:
11+
# Running example with pip-installed SDK
12+
import privx_api
13+
except ImportError:
14+
# Running example without installing SDK
15+
from utils import load_privx_api_lib_path
16+
17+
load_privx_api_lib_path()
18+
import privx_api
19+
20+
# eplace with the params of the access group ID and ca key ID
21+
ACCESS_GROUP_ID = "ACCESS_GROUP_ID"
22+
CA_ID = "CA_ID"
23+
24+
# Initialize the API.
25+
api = privx_api.PrivXAPI(
26+
config.HOSTNAME,
27+
config.HOSTPORT,
28+
config.CA_CERT,
29+
config.OAUTH_CLIENT_ID,
30+
config.OAUTH_CLIENT_SECRET,
31+
)
32+
33+
# Authenticate.
34+
# NOTE: fill in your credentials from secure storage, this is just an example
35+
api.authenticate(config.API_CLIENT_ID, config.API_CLIENT_SECRET)
36+
37+
def get_access_group_CA_key(access_group_id):
38+
result = api.get_access_group_CA_key(access_group_id)
39+
if result.ok:
40+
return result.data
41+
else:
42+
print(result.data.get("details"))
43+
sys.exit()
44+
45+
46+
def delete_access_group_CA_key(access_group_id, ca_id):
47+
result = api.delete_access_group_CA_key(access_group_id, ca_id)
48+
if result.ok:
49+
print(result)
50+
return
51+
else:
52+
print(result.data.get("details"))
53+
sys.exit()
54+
55+
56+
def main():
57+
ca_id = get_access_group_CA_key(ACCESS_GROUP_ID)
58+
print(ca_id)
59+
delete_access_group_CA_key(ACCESS_GROUP_ID, ca_id)
60+
61+
if __name__ == "__main__":
62+
main()

examples/get-connection-tags.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""
2+
This example updates connection tags and fetch connection tags.
3+
4+
"""
5+
import sys
6+
7+
import config
8+
9+
# this importation for demonstration purpose only
10+
# for proper importation of privx_api module
11+
# see https://github.com/SSHcom/privx-sdk-for-python#getting-started
12+
try:
13+
# Running example with pip-installed SDK
14+
import privx_api
15+
except ImportError:
16+
# Running example without installing SDK
17+
from utils import load_privx_api_lib_path
18+
19+
load_privx_api_lib_path()
20+
import privx_api
21+
22+
# Replace with the params of the connection tags
23+
CONNECTION_ID = "CONNECTION_ID"
24+
OFFSET = 0
25+
LIMIT = 100
26+
SORTDIR = "asc"
27+
QUERY = ""
28+
CONNECTION_TAGS = ["CONNECTION_TAGS"]
29+
30+
# Initialize the API.
31+
api = privx_api.PrivXAPI(
32+
config.HOSTNAME,
33+
config.HOSTPORT,
34+
config.CA_CERT,
35+
config.OAUTH_CLIENT_ID,
36+
config.OAUTH_CLIENT_SECRET,
37+
)
38+
39+
# Authenticate.
40+
# NOTE: fill in your credentials from secure storage, this is just an example
41+
api.authenticate(config.API_CLIENT_ID, config.API_CLIENT_SECRET)
42+
43+
44+
def get_connection_tags(offset, limit, sort_dir, query):
45+
"""Fetch connection tags object"""
46+
connection = api.get_connection_tags(offset, limit, sort_dir, query)
47+
if connection.ok:
48+
return connection.data
49+
50+
print(connection.data["details"])
51+
sys.exit(1)
52+
53+
def update_connection_tags(conn_id: str, connection_tags):
54+
"""Update connection tags object"""
55+
result = api.update_connection_tags(conn_id, connection_tags)
56+
if result.ok:
57+
return
58+
59+
print(result.data["details"])
60+
sys.exit(1)
61+
62+
def main():
63+
"""Update and fetch the connection tags."""
64+
update_connection_tags(CONNECTION_ID, CONNECTION_TAGS)
65+
connection_tags = get_connection_tags(OFFSET, LIMIT, SORTDIR, QUERY)
66+
print(connection_tags)
67+
68+
if __name__ == "__main__":
69+
main()

privx_api/authorizer.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,34 @@ def delete_access_group(self, access_group_id: str) -> PrivXAPIResponse:
530530
UrlEnum.AUTHORIZER.ACCESS_GROUP, path_params={"id": access_group_id}
531531
)
532532
return PrivXAPIResponse(response_status, HTTPStatus.OK, data)
533+
534+
def get_access_group_CA_key(
535+
self,
536+
access_group_id: str,
537+
) -> PrivXAPIResponse:
538+
"""
539+
Create access group CA key.
540+
541+
Returns:
542+
PrivXAPIResponse
543+
"""
544+
response_status, data = self._http_post(
545+
UrlEnum.AUTHORIZER.CREATE_ACCESS_GROUP_CA_KEY,
546+
path_params={"id": access_group_id},
547+
)
548+
return PrivXAPIResponse(response_status, HTTPStatus.OK, data)
549+
550+
def delete_access_group_CA_key(self, access_group_id: str, ca_id: str) -> PrivXAPIResponse:
551+
"""
552+
Delete access group CA key.
553+
554+
Returns:
555+
PrivXStreamResponse
556+
"""
557+
response_status, data = self._http_delete(
558+
UrlEnum.AUTHORIZER.DELETE_ACCESS_GROUP_CA_KEY, path_params={"id": access_group_id, "ca_id": ca_id}
559+
)
560+
return PrivXAPIResponse(response_status, HTTPStatus.OK, data)
533561

534562
def search_certificates(
535563
self,

privx_api/connection_manager.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,48 @@ def get_connection(self, connection_id: str) -> PrivXAPIResponse:
8888
)
8989
return PrivXAPIResponse(response_status, HTTPStatus.OK, data)
9090

91+
def get_connection_tags(
92+
self,
93+
offset: Optional[int] = None,
94+
limit: Optional[int] = None,
95+
sort_dir: Optional[str] = None,
96+
query: Optional[str] = None,
97+
) -> PrivXAPIResponse:
98+
"""
99+
Get connection tags.
100+
101+
Returns:
102+
PrivXAPIResponse
103+
"""
104+
search_params = self._get_search_params(
105+
offset=offset,
106+
limit=limit,
107+
sortdir=sort_dir,
108+
query=query,
109+
)
110+
111+
response_status, data = self._http_get(
112+
UrlEnum.CONNECTION_MANAGER.CONNECTION_TAGS,
113+
query_params=search_params,
114+
)
115+
return PrivXAPIResponse(response_status, HTTPStatus.OK, data)
116+
117+
def update_connection_tags(
118+
self, connection_id: str, connection_tags
119+
) -> PrivXAPIResponse:
120+
"""
121+
Update connection tags
122+
123+
Returns:
124+
PrivXAPIResponse
125+
"""
126+
response_status, data = self._http_put(
127+
UrlEnum.CONNECTION_MANAGER.UPDATE_CONNECTION_TAGS,
128+
path_params={"connection_id": connection_id},
129+
body=get_value(connection_tags, []),
130+
)
131+
return PrivXAPIResponse(response_status, HTTPStatus.OK, data)
132+
91133
def create_trail_download_handle(
92134
self, connection_id: str, channel_id: str, file_id: str
93135
) -> PrivXAPIResponse:

privx_api/enums.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ class ConnectionManagerEnum:
200200
UEBA_CONNECTION_COUNT = "CONNECTION_MANAGER.UEBA_CONNECTION_COUNT"
201201
UEBA_INTERNAL_STATUS = "CONNECTION_MANAGER.UEBA_INTERNAL_STATUS"
202202
UEBA_STATUS = "CONNECTION_MANAGER.UEBA_STATUS"
203+
CONNECTION_TAGS = "CONNECTION_MANAGER.CONNECTION_TAGS"
204+
UPDATE_CONNECTION_TAGS = "CONNECTION_MANAGER.UPDATE_CONNECTION_TAGS"
205+
203206

204207
urls = {
205208
ACCESS_ROLE: "/connection-manager/api/v1/connections/access_roles/{role_id}",
@@ -236,6 +239,8 @@ class ConnectionManagerEnum:
236239
UEBA_CONNECTION_COUNT: "/connection-manager/api/v1/ueba/query-connection-count",
237240
UEBA_STATUS: "/connection-manager/api/v1/ueba/status",
238241
UEBA_INTERNAL_STATUS: "/connection-manager/api/v1/ueba/status/internal",
242+
CONNECTION_TAGS: "/connection-manager/api/v1/connections/tags",
243+
UPDATE_CONNECTION_TAGS: "/connection-manager/api/v1/connections/{connection_id}/tags",
239244
}
240245

241246

@@ -369,6 +374,8 @@ class TrailIndexEnum:
369374
class AuthorizerEnum:
370375
ACCESS_GROUP = "AUTHORIZER.ACCESS_GROUP"
371376
ACCESS_GROUPS = "AUTHORIZER.ACCESS_GROUPS"
377+
CREATE_ACCESS_GROUP_CA_KEY = "AUTHORIZER.CREATE_ACCESS_GROUP_CA_KEY"
378+
DELETE_ACCESS_GROUP_CA_KEY = "AUTHORIZER.DELETE_ACCESS_GROUP_CA_KEY"
372379
AUTHORIZER_CERT = "AUTHORIZER.AUTHORIZER_CERT"
373380
AUTHORIZER_CERT_ID = "AUTHORIZER.AUTHORIZER_CERT_ID"
374381
CARRIER_CONFIG_SESSION_ID = "AUTHORIZER.CARRIER_CONFIG_SESSION_ID"
@@ -402,6 +409,8 @@ class AuthorizerEnum:
402409
urls = {
403410
ACCESS_GROUP: "/authorizer/api/v1/accessgroups/{id}",
404411
ACCESS_GROUPS: "/authorizer/api/v1/accessgroups",
412+
CREATE_ACCESS_GROUP_CA_KEY: "/authorizer/api/v1/accessgroups/{id}/cas",
413+
DELETE_ACCESS_GROUP_CA_KEY: "/authorizer/api/v1/accessgroups/{id}/cas/{ca_id}",
405414
AUTHORIZER_CERT: "/authorizer/api/v1/cas",
406415
AUTHORIZER_CERT_ID: "/authorizer/api/v1/cas/{id}",
407416
CARRIER_CONFIG_SESSION_ID: "/authorizer/api/v1/carrier/conf"

0 commit comments

Comments
 (0)