-
Notifications
You must be signed in to change notification settings - Fork 0
/
verifyC1V1Connectivity.py
112 lines (71 loc) · 3.85 KB
/
verifyC1V1Connectivity.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
import json
import os
import boto3
import urllib3
serviceConnectionStatusDict = {
"0": "RED",
"1": "GREEN"
}
# Retrieve SSM Parameter value based on parameter key passed.
def getV1SsmParameter(ssmClient, paramKey):
parameter = ssmClient.get_parameter(Name=paramKey, WithDecryption=True)
return parameter ['Parameter']['Value']
# Enable Trend Remote Support status on the Vision One.
def enableTrendRemoteSupport(http, v1ApiBaseUrl, httpHeaders):
body = {
'enabled': True
}
r = http.request('PUT', v1ApiBaseUrl + "/remoteSupport", headers=httpHeaders, body=json.dumps(body))
return json.loads(r.data)
# Retrieve Trend Remote Support status from Vision One.
def getTrendRemoteSupportStatus(http, v1ApiBaseUrl, httpHeaders):
r = http.request('GET', v1ApiBaseUrl + "/remoteSupport", headers=httpHeaders)
getTrendRemoteSupportStatusResponse = json.loads(r.data)
if "data" in getTrendRemoteSupportStatusResponse:
if getTrendRemoteSupportStatusResponse["code"] == "Success":
return True
return False
def listSupportedProducts(http, v1ApiBaseUrl, httpHeaders):
r = http.request('GET', v1ApiBaseUrl + "/connectors", headers=httpHeaders)
return json.loads(r.data)
def checkServiceConnectionStatus(http, v1ApiBaseUrl, httpHeaders, v1ConnectedProductList):
serviceConnectionStatusResponse = listSupportedProducts(http, v1ApiBaseUrl, httpHeaders)
if "data" in serviceConnectionStatusResponse:
if serviceConnectionStatusResponse["code"] == "Success":
for service in serviceConnectionStatusResponse["data"]:
if service["productId"] in v1ConnectedProductList:
if not service["isConnected"]:
raise Exception('Error: Product "' + str(service["productId"]) + '" is not connected to the Vision One account')
else:
print("Service", str(service["productId"]), "is Connected and the status is", serviceConnectionStatusDict[str(service["status"])])
return True
else:
raise Exception('Error: Vision One API endpoint errored out or is unavailable.')
else:
raise Exception('Error:', str(serviceConnectionStatusResponse["error"]["code"]))
def main(event, context):
# Read AWS Lambda Environment variables into the Lambda runtime as variables.
awsRegion = str(os.environ.get("awsRegion"))
v1AuthToken = str(os.environ.get("v1AuthToken"))
v1ConnectedProductList = str(os.environ.get("v1ConnectedProductList"))
v1ApiBaseUrlSSMKey = os.environ.get("v1ApiBaseUrlSSMKey")
if v1ConnectedProductList[-1] == ",":
v1ConnectedProductList = v1ConnectedProductList[:-1].replace(" ", "").split(",")
else:
v1ConnectedProductList = v1ConnectedProductList.replace(" ", "").split(",")
# Creating an SSM Client to store values in the AWS SSM Parameter Store.
ssmClient = boto3.client('ssm', region_name=awsRegion)
# Get Vision One API Base URL from the AWS SSM Parameter Store.
v1ApiBaseUrl = getV1SsmParameter(ssmClient, v1ApiBaseUrlSSMKey)
http = urllib3.PoolManager()
headers = {
"Content-Type": "application/json;charset=utf-8",
"Authorization": "Bearer " + v1AuthToken
}
# Enable Trend Remote Support on the Vision One Console.
print("enableTrendRemoteSupport - " + str(enableTrendRemoteSupport(http, v1ApiBaseUrl, headers)))
# Verify Trend Remote Support status is enabled.
print("getTrendRemoteSupportStatus - " + str(getTrendRemoteSupportStatus(http, v1ApiBaseUrl, headers)))
# Run service connection status on the Vision One console to ensure services in the v1ConnectedProductList list is connected and the status is GREEN.
checkServiceConnectionStatus(http, v1ApiBaseUrl, headers, v1ConnectedProductList)
return True