-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcwa_getEvents.py
179 lines (146 loc) · 7.26 KB
/
cwa_getEvents.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python
#################################################################################################################
# Copyright 2018 Symantec Corporation. All rights reserved.
# Script to get Events
# You need to provide Client ID and Client Secret Key in config.ini file under Credentials sections, to generate
# auth token.
# These keys can be found on CWA customer portal, once you are logged in navigate to Settings->API Keys
# The config file must be in the same directory as of the script.
#
# !!!Usage!!!: Update values of following variables with in the script prior to execute it
#
# get_events_page_size ; get_events_page_number ; get_events_start_date ; get_events_end_date
# get_events_product_id ; get_events_service_name ; get_events_additional_filter
# then execute script python
# python cwa_getEvents.py
# On success, this script will produce json output of the events found as per the request.
# for rest of the request.
#################################################################################################################
import json
from typing import List
import requests
import configparser
import os
import logging
from pathlib import Path
# create logger
logger = logging.getLogger("cwa_getEvents")
logger.setLevel(logging.INFO)
# create console handler (ch) and set level to debug
ch = logging.StreamHandler()
# create formatter
formatter = logging.Formatter("%(asctime)s: %(levelname)s: %(message)s",
"%Y-%m-%d %H:%M:%S")
# add formatter to console handler
ch.setFormatter(formatter)
# add console handler to logger
logger.addHandler(ch)
# Setting variables from config.ini file
AUTHURL = 'AuthUrl'
CLIENT_ID = 'ClientId'
CLIENT_SECRET = 'ClientSecretKey'
CONFIG_CREDS_SECTION = 'Credentials'
CONFIG_URL_SECTION = 'RequestURL'
GET_EVENTS_URL = 'GetEventsUrl'
# Getting current working directory
Current_Working_Dir = os.getcwd()
logger.info("cwa_getEvents: Current working Directory is " + Current_Working_Dir)
logger.info("cwa_getEvents: Checking if config file present in current directory")
config_file = Path(Current_Working_Dir+'/config.ini')
# Checking if config file present
if not config_file.is_file():
logger.error("cwa_getEvents: File config.ini not found in current working directory, "
"place config.ini fle in directory \n " + Current_Working_Dir)
exit()
else :
logger.info("cwa_getEvents: Configfile found in directory " + Current_Working_Dir)
# Creating http request and headers
auth_headers = {}
access_token: None
authurl: None
get_events_url: None
x_epmp_customer_id: None
x_epmp_domain_id: None
get_events_page_size = 100
get_events_page_number = 0
get_events_start_date = "2018-10-20T00:00:00.000Z"
get_events_end_date = "2018-10-27T23:59:00.000Z"
get_events_product_id = "5B5FF903-27D7-46AF-AA63-EE24D0BD41CD"
get_events_service_name = "IAM Password Policy"
get_events_additional_filter = "(product_uid='"+get_events_product_id+"') && " \
"(service_name='"+get_events_service_name+"')"
get_events_display_labels = False
# Defining method to hit http request and to generate auth token
def get_authentication_token():
token_cust_domain_id = False
try:
auth_request_json = json.dumps(set_request_headers.auth_request)
logger.info("get_authentication_token(): Hitting http request to generate auth token")
auth_response = requests.post(authurl, data=auth_request_json, headers=auth_headers)
if auth_response.status_code == 200:
logger.info("get_authentication_token(): auth token generated successfully, "
"http status code is " + str(auth_response.status_code))
global access_token
access_token = auth_response.json()['access_token']
global x_epmp_customer_id
x_epmp_customer_id = auth_response.json()['x-epmp-customer-id']
global x_epmp_domain_id
x_epmp_domain_id = auth_response.json()['x-epmp-domain-id']
print("access_token :: " + access_token)
print("customer_id :: " + x_epmp_customer_id)
print("domain_id :: " + x_epmp_domain_id)
token_cust_domain_id = True
else:
logger.error("get_authentication_token(): Response from http auth not received status code is " + str(auth_response.status_code))
except Exception as ex:
logger.error("get_authentication_token(): Exception occurred while hitting http request to generate token" + str(ex))
return token_cust_domain_id
def set_request_headers():
set_request_headers.auth_request = {}
headers_got_set = False
try:
logger.info("set_request_headers(): Parsing and reading values from config files")
config = configparser.ConfigParser()
config.read(config_file)
client_id = config.get(CONFIG_CREDS_SECTION, CLIENT_ID)
client_secret = config.get(CONFIG_CREDS_SECTION, CLIENT_SECRET)
global authurl
authurl = config.get(CONFIG_URL_SECTION, AUTHURL)
global get_events_url
get_events_url = config.get(CONFIG_URL_SECTION, GET_EVENTS_URL)
if client_id == "" or client_secret == "" or authurl == "" or get_events_url == "":
logger.error("set_request_headers(): One or more values empty in config_file")
return headers_got_set
else:
set_request_headers.auth_request['client_id'] = client_id
set_request_headers.auth_request['client_secret'] = client_secret
auth_headers['Content-type'] = 'application/json'
headers_got_set = True
except Exception as ex:
logger.error("generateAuthToken: Exception occurred while reading values from config file " + str(ex))
return headers_got_set
def get_events_details():
global auth_headers
get_events_details.get_events_request_body = {'pageSize': get_events_page_size, 'pageNumber': get_events_page_number,
'startDate': get_events_start_date, 'endDate': get_events_end_date,
'additionalFilters': get_events_additional_filter,
'displayLabels': get_events_display_labels }
auth_headers['Authorization'] = access_token
auth_headers['x-epmp-customer-id'] = x_epmp_customer_id
auth_headers['x-epmp-domain-id'] = x_epmp_domain_id
try:
logger.info("get_events_details(): Hitting http request get events details ")
request_json = json.dumps(get_events_details.get_events_request_body)
get_events_details_response = requests.post(get_events_url, data=request_json, headers=auth_headers)
if get_events_details_response.status_code == 200:
logger.info("get_events_details(): Get events successful status code "
+ str(get_events_details_response.status_code))
print(get_events_details_response.text)
else:
logger.error("get_events_details(): Failed to get data for events, status code is " +
str(get_events_details.status_code))
except Exception as ex:
logger.error("get_events_details(): Exception occurred while getting events details " + str(ex))
if set_request_headers():
if get_authentication_token():
get_events_details()