Skip to content

Commit

Permalink
Merge pull request #348 from d3anp/347-confluence-pat
Browse files Browse the repository at this point in the history
enabling confluence personal access token to be passed in via command line or environment variable.
  • Loading branch information
yugoslavskiy authored Jul 11, 2021
2 parents 79d166c + 6eb38fd commit 5103d64
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 45 deletions.
20 changes: 16 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# Others
import getpass
import argparse
import os

if __name__ == '__main__':

Expand Down Expand Up @@ -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()

Expand All @@ -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.get('CONFLUENCE_PAT') is not 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,
Expand Down
135 changes: 94 additions & 41 deletions scripts/reactutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,16 +231,26 @@ 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
)
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. " +
Expand Down Expand Up @@ -274,6 +284,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():
Expand Down Expand Up @@ -303,13 +316,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():
Expand All @@ -322,13 +343,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)

Expand Down Expand Up @@ -361,23 +390,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:
Expand All @@ -386,13 +431,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"

Expand Down

0 comments on commit 5103d64

Please sign in to comment.