forked from sshniro/asg-importer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
148 lines (125 loc) · 5.58 KB
/
app.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
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
const fetch = require("node-fetch");
const cron = require("node-cron");
const utils = require("./utils");
let SR_URL = process.env.SR_URL;
let EFS_URL = process.env.EFS_URL;
let X_API_KEY = process.env.X_API_KEY;
let CLIENT_ID = process.env.CLIENT_ID;
let CLIENT_SECRET = process.env.CLIENT_SECRET;
let PUBLIC_KEY = process.env.PUBLIC_KEY;
let ROOT_PREFIX = "apis";
let currentIncrement = 10;
var availableRoutes = [];
function nextSlot(current, direction) {
let inc = direction === 'down' ? -1 : 1;
let next = current;
while (availableRoutes.indexOf(next += inc) > -1) ;
return next;
}
function fillAvailableRoutes(asrJSON){
let nodes = asrJSON['node']['nodes'];
if (nodes !== undefined && nodes.length !== 0) {
for (let i = 0; i < nodes.length; i++) {
let key = nodes[i].key;
let routeID = key.substring(key.lastIndexOf("/") + 1, key.length);
availableRoutes.push(Number(routeID));
}
}
}
const syncData = async SR_URL => {
try {
const srResponse = await fetch(SR_URL);
const srJSON = await srResponse.json();
const asgResponse = await fetch(`${EFS_URL}/apisix/admin/routes`);
const asgJSON = await asgResponse.json();
utils.createOrUpdateEcoEndpoint(asgJSON, EFS_URL, X_API_KEY);
utils.createServiceRegistryEndpoint(SR_URL, EFS_URL, X_API_KEY);
fillAvailableRoutes(asgJSON);
// iterate through all the requests
let apis = srJSON.services;
if (apis !== undefined) {
for (let i = 0; i < apis.length; i++) {
let rootAPI = apis[i];
for (let j = 0; j < rootAPI.apis.length; j++) {
try{
let api = rootAPI.apis[j];
let url = new URL(api.url);
let host = url.hostname;
let port = url.port;
if(port === undefined || port === ""){
if (url.protocol === "http:") {
port = 80
}else {
port = 443
}
}
let hostPort = `${host}:${port}`;
let prefix = rootAPI.id + "/" + rootAPI.apis[j].id;
let matchingRoute = utils.getMatchingRoute(asgJSON, hostPort, prefix, url.pathname, ROOT_PREFIX);
if (matchingRoute === null) {
matchingRoute = nextSlot(currentIncrement);
currentIncrement += 5;
}
let regexReplace = `${url.pathname}$1`;
if (url.pathname === undefined || url.pathname === "") {
regexReplace = `/$1`;
}
let regex = `^/${ROOT_PREFIX}/${prefix}${url.pathname}(.*)`;
const body = {
uri: `/${ROOT_PREFIX}/${prefix}${url.pathname}*`,
plugins: {
"proxy-rewrite": {
"regex_uri": [regex, regexReplace]
},
"openid-connect": {
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET,
"discovery": `${EFS_URL}/auth/realms/master/.well-known/openid-configuration`,
"bearer_only": true,
"realm": "master",
// "introspection_endpoint": `${EFS_URL}/auth/realms/master/protocol/openid-connect/token/introspect`
"token_signing_alg_values_expected": "RS256",
"public_key": PUBLIC_KEY
}
},
upstream: {
"type": "roundrobin",
"nodes": {
[hostPort]: 1
}
}
};
if (url.protocol === "https:") {
body.plugins['proxy-rewrite']["scheme"] = "https"
}
fetch(`${EFS_URL}/apisix/admin/routes/${matchingRoute}`, {
method: 'PUT',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json',
'X-API-KEY': X_API_KEY
},
})
.then(res => {
return res.json()
})
.then(json => {
console.log(`route_create request ${JSON.stringify(json)}`)
})
.catch((err) => {
console.log(`error occurred while creating the route: ${JSON.stringify(err)}`)
})
}catch (e) {
console.log(`error when parsing the routes: ${JSON.stringify(e)}`)
}
}
}
}
} catch (error) {
console.log(error);
}
};
syncData(SR_URL);
cron.schedule("0 */1 * * *", function() {
syncData(SR_URL);
});