-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
113 lines (91 loc) · 3.19 KB
/
index.js
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
const {google} = require('googleapis');
const axios = require('axios');
const constants = require('./constants');
const auth = new google.auth.GoogleAuth({
keyFile: 'keyfile.json',
scopes: constants.SCOPES,
});
/* istanbul ignore next */
exports.updateAuthorizedNetworks = async (req, res) => {
const authClient = await auth.getClient();
const sqlAdmin = await google.sqladmin({
version: 'v1beta4',
project: constants.PROJECT_ID,
auth: authClient
});
const instances = await sqlAdmin.instances.list({
project: constants.PROJECT_ID
});
let grafanaSourceIPs = [];
try {
grafanaSourceIPs = await this.getGrafanaSourceIPs();
} catch (error) {
return res.send(this.returnError(error.message), 500)
}
for (const instance of instances.data.items) {
const name = instance.name;
if(!constants.DATABASE_INSTANCES.includes(name)) continue;
const authorizedNetworks = instance.settings.ipConfiguration.authorizedNetworks;
// Remove any old Grafana source IPs from the current database instance
let authorizedNetworksToUpdate = this.filterOldGrafanaSourceIps(authorizedNetworks);
// Add the Grafana source IPs
authorizedNetworksToUpdate = this.addNewGrafanaSourceIps(authorizedNetworksToUpdate, grafanaSourceIPs)
try {
const updateResult = await sqlAdmin.instances.patch({
project: constants.PROJECT_ID,
instance: name,
requestBody: {
settings: {
ipConfiguration: {
authorizedNetworks: authorizedNetworksToUpdate
}
}
}
});
} catch (error) {
return res.send(this.returnError(error.message), 500)
}
}
return res.send(this.returnSuccess(), 200)
};
exports.getGrafanaSourceIPs = () => {
return new Promise(function(resolve, reject) {
axios.get(constants.URL_GRAFANA_SOURCE_IPS)
.then(response => {
resolve(response.data);
})
.catch(error => {
reject(error.message);
});
});
};
exports.addNewGrafanaSourceIps = (authorizedNetworksToUpdate, grafanaSourceIPs) => {
for(const ip of grafanaSourceIPs) {
authorizedNetworksToUpdate.push({
value: ip,
name: constants.NAME_PREFIX + ip,
kind: 'sql#aclEntry', // This is always set to this,
})
}
return authorizedNetworksToUpdate;
};
exports.filterOldGrafanaSourceIps = (array) => {
const authorizedNetworksToReturn = [];
// Add any existing authorized networks to the array to update
for(const authorizedNetwork of array) {
if(!authorizedNetwork.name.includes(constants.NAME_PREFIX)) authorizedNetworksToReturn.push(authorizedNetwork);
}
return authorizedNetworksToReturn;
};
exports.returnError = (error) => {
return {
'ok': false,
'message': error
}
};
exports.returnSuccess = (message = 'Database updated successfully') => {
return {
'ok': true,
'message': message
}
};