From 2eadbfd3a8a09415d6a3f9e1ff69963107e1a1c7 Mon Sep 17 00:00:00 2001 From: d3anp <3758845+d3anp@users.noreply.github.com> Date: Fri, 9 Jul 2021 12:39:27 +1000 Subject: [PATCH 1/3] enabling confluence persnoal access token to be passed in via command line or environment variable --- main.py | 20 +++++-- scripts/reactutils.py | 136 +++++++++++++++++++++++++++++------------- 2 files changed, 111 insertions(+), 45 deletions(-) diff --git a/main.py b/main.py index 7cc5bc07..96a005c6 100755 --- a/main.py +++ b/main.py @@ -15,6 +15,7 @@ # Others import getpass import argparse +import os if __name__ == '__main__': @@ -59,6 +60,10 @@ parser.add_argument('-i', '--init', action='store_true', help="Build initial pages or directories " + "depending on the export type") + parser.add_argument('-cpat', '--confluence-pat', action='store', + help='Personal Access Token used to export analytics \ + to Confluence. Confluence Username and passsword \ + promted if this parameter is not provided') args = parser.parse_args() @@ -69,12 +74,19 @@ init=args.init) elif args.confluence: - print("Provide Confluence credentials\n") + if args.confluence_pat: + print("Using parameter supplied Confluence Personal Access Token") + auth = args.confluence_pat + elif os.environ['CONFLUENCE_PAT'] != None: + print("Using environment vairable supplied Confluence Personal Access Token") + auth = os.environ['CONFLUENCE_PAT'] + else: + print("Provide Confluence credentials\n") - mail = input("Login: ") - password = getpass.getpass(prompt='Password: ', stream=None) + mail = input("Login: ") + password = getpass.getpass(prompt='Password: ', stream=None) - auth = HTTPBasicAuth(mail, password) + auth = HTTPBasicAuth(mail, password) UpdateAttackMapping() ReactPopulateConfluence(auth=auth, auto=args.auto, ra=args.responseactions, rp=args.responseplaybook, diff --git a/scripts/reactutils.py b/scripts/reactutils.py index 6b43615c..182fc9c4 100755 --- a/scripts/reactutils.py +++ b/scripts/reactutils.py @@ -231,16 +231,27 @@ def confluence_get_page_id(apipath, auth, space, title): "Content-Type": "application/json" } + if isinstance(auth,str): + headers["Authorization"] = "Basic {}".format(auth) + url = apipath + "content" space_page_url = url + '?spaceKey=' + space + '&title=' \ + title + '&expand=space' - response = requests.request( - "GET", - space_page_url, - headers=headers, - auth=auth - ) + if isinstance(auth,str): + response = requests.request( + "GET", + space_page_url, + headers=headers + ) + print('here') + else: + response = requests.request( + "GET", + space_page_url, + headers=headers, + auth=auth + ) if response.status_code == 401: print("Unauthorized Response. Try to use a token instead of a password. " + @@ -274,6 +285,9 @@ def push_to_confluence(data, apipath, auth): "Content-Type": "application/json" } + if isinstance(auth,str): + headers["Authorization"] = "Basic {}".format(auth) + alldata = True for i in ["title", "spacekey", "parentid", "confluencecontent"]: if i not in data.keys(): @@ -303,13 +317,21 @@ def push_to_confluence(data, apipath, auth): } payload = json.dumps(dict_payload) - response = requests.request( - "POST", - url, - data=payload, - headers=headers, - auth=auth - ) + if isinstance(auth,str): + response = requests.request( + "POST", + url, + data=payload, + headers=headers + ) + else: + response = requests.request( + "POST", + url, + data=payload, + headers=headers, + auth=auth + ) resp = json.loads(response.text) if "data" in resp.keys(): @@ -322,13 +344,21 @@ def push_to_confluence(data, apipath, auth): data["title"] ) - response = requests.request( - "GET", - url + "/%s?expand=body.storage,version" % str(cid), - data=payload, - headers=headers, - auth=auth - ) + if isinstance(auth,str): + response = requests.request( + "GET", + url + "/%s?expand=body.storage,version" % str(cid), + data=payload, + headers=headers + ) + else: + response = requests.request( + "GET", + url + "/%s?expand=body.storage,version" % str(cid), + data=payload, + headers=headers, + auth=auth + ) resp = json.loads(response.text) @@ -361,23 +391,39 @@ def push_to_confluence(data, apipath, auth): dict_payload["version"] = {"number": i} payload = json.dumps(dict_payload) - response = requests.request( - "PUT", - url + "/%s" % str(cid), - data=payload, - headers=headers, - auth=auth - ) + if isinstance(auth,str): + response = requests.request( + "PUT", + url + "/%s" % str(cid), + data=payload, + headers=headers + ) + else: + response = requests.request( + "PUT", + url + "/%s" % str(cid), + data=payload, + headers=headers, + auth=auth + ) return "Page updated" except KeyError: - response = requests.request( - "GET", - url + "/%s/" % str(cid), - data=payload, - headers=headers, - auth=auth - ) + if isinstance(auth,str): + response = requests.request( + "GET", + url + "/%s/" % str(cid), + data=payload, + headers=headers + ) + else: + response = requests.request( + "GET", + url + "/%s/" % str(cid), + data=payload, + headers=headers, + auth=auth + ) resp = json.loads(response.text) try: @@ -386,13 +432,21 @@ def push_to_confluence(data, apipath, auth): dict_payload["version"] = resp["version"] payload = json.dumps(dict_payload) - response = requests.request( - "PUT", - url + "/%s" % str(cid), - data=payload, - headers=headers, - auth=auth - ) + if isinstance(auth,str): + response = requests.request( + "PUT", + url + "/%s" % str(cid), + data=payload, + headers=headers + ) + else: + response = requests.request( + "PUT", + url + "/%s" % str(cid), + data=payload, + headers=headers, + auth=auth + ) return "Page updated" From 12bbbd49c9e33e854432eabebbf4993662ccb07f Mon Sep 17 00:00:00 2001 From: yugoslavskiy Date: Mon, 12 Jul 2021 00:39:19 +0200 Subject: [PATCH 2/3] Update reactutils.py --- scripts/reactutils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/reactutils.py b/scripts/reactutils.py index 182fc9c4..3ed98b6e 100755 --- a/scripts/reactutils.py +++ b/scripts/reactutils.py @@ -244,7 +244,6 @@ def confluence_get_page_id(apipath, auth, space, title): space_page_url, headers=headers ) - print('here') else: response = requests.request( "GET", From 6eb38fd46613d5b75ab8a9662658d3aaacf1e3b0 Mon Sep 17 00:00:00 2001 From: yugoslavskiy Date: Mon, 12 Jul 2021 01:06:08 +0200 Subject: [PATCH 3/3] changed to os.environ.get() to avoid exception on non existing variable --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 96a005c6..c8251ede 100755 --- a/main.py +++ b/main.py @@ -77,7 +77,7 @@ if args.confluence_pat: print("Using parameter supplied Confluence Personal Access Token") auth = args.confluence_pat - elif os.environ['CONFLUENCE_PAT'] != None: + elif os.environ.get('CONFLUENCE_PAT') is not None: print("Using environment vairable supplied Confluence Personal Access Token") auth = os.environ['CONFLUENCE_PAT'] else: