forked from google/atlassian-addons-audit-sheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audit.py
executable file
·226 lines (164 loc) · 6.46 KB
/
audit.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/local/bin/python3
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import requests
import json
import re
import pygsheets
from pprint import pprint
import httplib2
http_client = httplib2.Http(timeout=100)
# import configuration file
import config_audit as config
from datetime import datetime, timezone
from time import strftime, localtime
timestamp = datetime.now(timezone.utc).astimezone().strftime('%Y-%m-%d %H:%M:%S %Z')
parser = argparse.ArgumentParser(description='Audit Atlassian Add-Ons.')
parser.add_argument('hosts', metavar='hosts', type=str, nargs='+',
help='Select target host(s): jira, confluence, stash, or all')
args = parser.parse_args()
# Shared details
user = config.jira_credentials['user']
password = config.jira_credentials['password']
headers = {'X-Atlassian-Token': 'nocheck'}
marketurl = 'https://marketplace.atlassian.com/'
# Google Sheets details
client = pygsheets.authorize(http_client=http_client)
sheet_url = config.google_sheet_url
ss = client.open_by_url(sheet_url)
targets = {}
possibletargets = {'Confluence': config.target_url['confluence'],
'JIRA': config.target_url['jira'],
'Stash': config.target_url['stash']}
for i in args.hosts:
if i == 'jira':
targets['JIRA'] = possibletargets['JIRA']
if i == 'confluence':
targets['Confluence'] = possibletargets['Confluence']
if i == 'stash':
targets['Stash'] = possibletargets['Stash']
if i == 'all':
targets = possibletargets
for worksheet, base_url in targets.items():
sheet = ss.worksheet_by_title(worksheet)
# Get all the plugins recorded in sheet, to be checked later for disabled plugins (diff against installed)
sheetplugins = sheet.get_values(start=(2,2), end=(99,2), returnas='matrix', include_empty=False, include_all=False)
recorded = []
for sheetrow in sheetplugins:
recorded.append(sheetrow[0])
sheetkeys = recorded
recorded = sorted(recorded)
# Connect to Confluence, get the plugins
plugins_url = base_url + 'rest/plugins/1.0/?os_authType=basic'
r = requests.get(plugins_url, auth=(user, password), headers=headers)
data = json.loads(r.text)
allplugins = sorted(data["plugins"], key=lambda k: k['name'])
# Iterate through the plugins
installed = []
for plugin in allplugins:
if plugin["enabled"] and plugin["userInstalled"] and plugin["key"] != 'com.pyxis.greenhopper.jira':
name = plugin["name"]
description = plugin.get("description")
current = plugin["version"]
pluginkey = plugin["key"]
installed.append(pluginkey)
marketplacelink = marketurl + 'plugins/' + pluginkey + '/server/overview'
expiry = 'N/A'
sen = 'N/A'
# Check if it is a Marketplace plugin
addonslink = marketurl + "rest/2/addons/" + pluginkey
r = requests.get(addonslink)
if r.status_code == 404:
_2000 = _10000 = unlimited = latest = datacenter = "N/A"
else:
addon = json.loads(r.text)
if "pricing" in addon["_links"]:
r = requests.get(addonslink + "/pricing/server/live")
if r.status_code == 404:
_2000 = _10000 = unlimited = "N/A"
else:
pricedata = json.loads(r.text)
_2000 = pricedata["items"][6]["amount"]
_10000 = pricedata["items"][7]["amount"]
unlimited = pricedata["items"][7]["amount"]
# Check current license expiration
license_url = base_url + 'rest/plugins/1.0/' + pluginkey + '-key/license?os_authType=basic'
r = requests.get(license_url, auth=(user, password), headers=headers)
licensedata = json.loads(r.text)
if "maintenanceExpiryDate" in licensedata:
licensestamp = datetime.fromtimestamp(int(licensedata["maintenanceExpiryDate"]/1e3))
expiry = licensestamp.strftime('%m/%d/%Y')
if "supportEntitlementNumber" in licensedata:
sen = licensedata["supportEntitlementNumber"]
else:
_2000 = _10000 = unlimited = "free"
r = requests.get(addonslink + "/versions")
versions = json.loads(r.text)
for version in versions['_embedded']['versions']:
if version['deployment']['server']:
latest = version['name']
datacenter = version['deployment']['dataCenter']
break
print(name)
print(" " + description)
print(" " + pluginkey)
print(" " + current)
# print(" " + latest)
# print(" " + marketplacelink)
# print(" " + str(_2000))
# print(" " + str(_10000))
# print(" " + str(unlimited))
print(" " + expiry)
if pluginkey in sheetkeys:
row = sheetkeys.index(pluginkey) + 2
else:
sheet.insert_rows(1, number=1, values=None, inherit=False)
sheetkeys.insert(0, pluginkey)
row = 2
# # Unstrikethrough if previously disabled - super slow
# keycell[0].set_text_format('strikethrough', False)
# keycell[0].set_text_alignment('TOP', True) # for some reason set_text_format resets alignment
# unstrikename = sheet.cell('A' + str(row))
# unstrikename.set_text_format('strikethrough', False)
# unstrikename.set_text_alignment('TOP', True)
range = 'A' + str(row) + ':K' + str(row)
hyperlink = '=HYPERLINK("' + marketplacelink + '", "' + name + '")'
cell_list = [hyperlink]
cell_list.append(pluginkey)
cell_list.append(sen)
cell_list.append(description)
cell_list.append(str(current))
cell_list.append(str(latest))
cell_list.append(expiry)
cell_list.append(_2000)
cell_list.append(_10000)
cell_list.append(unlimited)
cell_list.append(datacenter)
outer_cell = [cell_list]
sheet.update_cells(crange=range, values=outer_cell)
# Strikethrough disabled plugins
disabled = list(set(recorded) - set(installed))
print('Disabled:')
pprint(disabled)
for strikekey in disabled:
strikerow = sheetkeys.index(strikekey) + 2
strikecell = sheet.cell((strikerow,1))
strikename = sheet.cell('A' + str(strikerow))
strikename.set_text_format('strikethrough', True)
strikename.set_text_alignment('TOP', True)
if strikename.note == '':
strikename.note = 'Disabled: ' + timestamp
# Add a timestamp
sheet.update_cell('A1', worksheet + ' Plugins Updated:\n' + timestamp)