From 8ca676456094acdc0ed2674c48778478a4c7bc5b Mon Sep 17 00:00:00 2001 From: Daniele De Lorenzi Date: Wed, 29 May 2024 22:25:44 +0200 Subject: [PATCH] feat(access_key): Implement teamId in create_access_key and implement update_access_key (#251) Signed-off-by: Daniele De Lorenzi --- examples/create_access_keys.py | 47 +++++++++++++++++++++++++++++ examples/update_access_keys.py | 54 ++++++++++++++++++++++++++++++++++ sdcclient/_common.py | 31 +++++++++++++++++-- 3 files changed, 130 insertions(+), 2 deletions(-) create mode 100755 examples/create_access_keys.py create mode 100755 examples/update_access_keys.py diff --git a/examples/create_access_keys.py b/examples/create_access_keys.py new file mode 100755 index 00000000..a683e92d --- /dev/null +++ b/examples/create_access_keys.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python +# +# List all the access keys in a Sysdig Monitor environment. The token you provide must +# have Admin rights. +# + +import sys + +from sdcclient import SdcClient + +# +# Parse arguments +# +if len(sys.argv) != 2: + print('usage: %s ' % sys.argv[0]) + print('You can find your token at https://app.sysdigcloud.com/#/settings/user') + print('For this script to work, the user for the token must have Admin rights') + sys.exit(1) + +sdc_token = sys.argv[1] + +# Maximum number of agents allowed to connect for this access key. Set to '' if not required +agent_limit = '' +# Number of agent licenses that are ALWAYS available to this access key. This directly counts against the maximum number of available licenses. Set to '' if not required. +agent_reserved = '' +# Team ID to which to assign the access key. Team ID must be valid. Set to '' if not required. +team_id = '' + + +# +# Instantiate the SDC client +# +sdclient = SdcClient(sdc_token, 'https://app.sysdigcloud.com') + +# +# Get the configuration +# +ok, res = sdclient.create_access_key( + agent_limit, + agent_reserved, + team_id) + +if ok: + print('Access Key: {}\nTeam ID: {}\nAgent Limit: {}\nAgent Reserved: {}\n==========='.format(res['customerAccessKey']['accessKey'], res['customerAccessKey']['teamId'], res['customerAccessKey']['limit'], res['customerAccessKey']['reservation'])) +else: + print(res) + sys.exit(1) diff --git a/examples/update_access_keys.py b/examples/update_access_keys.py new file mode 100755 index 00000000..27e9803d --- /dev/null +++ b/examples/update_access_keys.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python +# +# List all the access keys in a Sysdig Monitor environment. The token you provide must +# have Admin rights. +# + +import sys + +from sdcclient import SdcClient + +# +# Parse arguments +# +if len(sys.argv) != 2: + print('usage: %s ' % sys.argv[0]) + print('You can find your token at https://app.sysdigcloud.com/#/settings/user') + print('For this script to work, the user for the token must have Admin rights') + sys.exit(1) + +sdc_token = sys.argv[1] + +# Access Key that needs to be updated +accessKey = '' +# Maximum number of agents allowed to connect for this access key. Set to '' if not required +agent_limit = '' +# Number of agent licenses that are ALWAYS available to this access key. This directly counts against the maximum number of available licenses. Set to '' if not required. +agent_reserved = '' +# Team ID to which to assign the access key. Team ID must be valid. Set to '' if not required. +team_id = '' + + +# +# Instantiate the SDC client +# +sdclient = SdcClient(sdc_token, 'https://app.sysdigcloud.com') + +# +# Get the configuration +# +if accessKey: + ok, res = sdclient.update_access_key( + accessKey, + agent_limit, + agent_reserved, + team_id) +else: + print('Please specify the Access Key that you would like to be updated') + sys.exit(1) + +if ok: + print('Access Key: {}\nTeam ID: {}\nAgent Limit: {}\nAgent Reserved: {}\n==========='.format(res['customerAccessKey']['accessKey'], res['customerAccessKey']['teamId'], res['customerAccessKey']['limit'], res['customerAccessKey']['reservation'])) +else: + print(res) + sys.exit(1) diff --git a/sdcclient/_common.py b/sdcclient/_common.py index 8d05d2f3..e565168a 100644 --- a/sdcclient/_common.py +++ b/sdcclient/_common.py @@ -1044,7 +1044,7 @@ def list_access_keys(self): res = self.http.get(self.url + '/api/customer/accessKeys', headers=self.hdrs, verify=self.ssl_verify) return self._request_result(res) - def create_access_key(self): + def create_access_key(self, agent_limit=None, agent_reserved=None, team_id=None): ''' **Description** Create a new access key for Sysdig Monitor/Secure @@ -1052,7 +1052,34 @@ def create_access_key(self): **Reslut** The access keys object ''' - res = self.http.post(self.url + '/api/customer/accessKeys', headers=self.hdrs, verify=self.ssl_verify) + access_key_payload = { + "customerAccessKey": { + "limit": agent_limit, + "reservation": agent_reserved, + "teamId": team_id + } + } + + res = self.http.post(self.url + '/api/customer/accessKeys', headers=self.hdrs, verify=self.ssl_verify, data=json.dumps(access_key_payload)) + return self._request_result(res) + + def update_access_key(self, access_key, agent_limit=None, agent_reserved=None, team_id=None): + ''' + **Description** + Create a new access key for Sysdig Monitor/Secure + + **Reslut** + The access keys object + ''' + access_key_payload = { + "customerAccessKey": { + "limit": agent_limit, + "reservation": agent_reserved, + "teamId": team_id + } + } + + res = self.http.put(self.url + '/api/customer/accessKeys/' + access_key, headers=self.hdrs, verify=self.ssl_verify, data=json.dumps(access_key_payload)) return self._request_result(res) def disable_access_key(self, access_key):