-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
152 lines (135 loc) · 4.32 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
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
const fs = require('fs');
const ziti = require('ziti-sdk-nodejs');
const UV_EOF = -4095;
const zitiInit = async (zitiFile) => {
return new Promise((resolve, reject) => {
var rc = ziti.ziti_init(zitiFile, (init_rc) => {
if (init_rc < 0) {
return reject(`init_rc = ${init_rc}`);
}
return resolve();
});
if (rc < 0) {
return reject(`rc = ${rc}`);
}
});
};
const zitiServiceAvailable = async (service) => {
return new Promise((resolve, reject) => {
ziti.ziti_service_available(service, (obj) => {
if (obj.status != 0) {
console.log(`service ${service} not available, status: ${status}`);
return reject(status);
} else {
console.log(`service ${service} available`);
return resolve();
}
});
});
}
const zitiHttpRequest = async (url, method, headers) => {
return new Promise((resolve) => {
ziti.Ziti_http_request(
url,
method,
headers,
(obj) => { // on_req callback
console.log('on_req callback: req is: %o', obj.req);
return resolve(obj.req);
},
(obj) => { // on_resp callback
console.log(`on_resp status: ${obj.code} ${obj.status}`);
if (obj.code != 200) {
core.setFailed(`on_resp failure: ${obj.status}`);
process.exit(-1);
}
process.exit(0);
},
(obj) => { // on_resp_body callback
// not expecting any body...
if (obj.len === UV_EOF) {
console.log('response complete')
process.exit(0);
} else if (obj.len < 0) {
core.setFailed(`on_resp failure: ${obj.len}`);
process.exit(-1);
}
if (obj.body) {
let str = Buffer.from(obj.body).toString();
console.log(`on_resp_body len: ${obj.len}, body: ${str}`);
} else {
console.log(`on_resp_body len: ${obj.len}`);
}
});
});
};
const zitiHttpRequestData = async (req, buf) => {
ziti.Ziti_http_request_data(
req,
buf,
(obj) => { // on_req_body callback
if (obj.status < 0) {
reject(obj.status);
} else {
resolve(obj);
}
});
};
console.log('Going async...');
(async function() {
try {
const zidFile = './zid.json'
const zitiId = process.env.ZITI_IDENTITY;
const webhookUrl = process.env.WEBHOOK_URL;
const webhookPayload = process.env.WEBHOOK_PAYLOAD;
if (zitiId === '') {
console.log(`ZITI_IDENTITY env var was not specified`);
process.exit(-1);
}
if (webhookUrl === '') {
console.log(`WEBHOOK_URL env var was not specified`);
process.exit(-1);
}
if (webhookPayload === '') {
console.log(`WEBHOOK_PAYLOAD env var was not specified`);
process.exit(-1);
}
// Write zitiId to file
fs.writeFileSync(zidFile, zitiId);
// First make sure we can initialize Ziti
await zitiInit(zidFile).catch((err) => {
console.log(`zitiInit failed: ${err}`);
process.exit(-1);
});
// Make sure we have ziti service available
// Note: ziti-sdk-nodejs (currently) requires service name to match URL host
// (TODO: write an issue to change this - no reason that should need to match, and can lead to errors)
let url = new URL(webhookUrl);
let serviceName = url.hostname;
await zitiServiceAvailable(serviceName).catch((err) => {
console.log(`zitiServiceAvailable failed: ${err}`);
process.exit(-1);
});
// Get the JSON webhook payload for the event that triggered the workflow. We expect it to already be stringify'd.
const payload = `${webhookPayload}`;
var payloadBuf = Buffer.from(payload, 'utf8');
// Send it over Ziti
let headersArray = [
'User-Agent: GitLab-Hookshot/ziti-gitlab-webhook',
'Content-Type: application/json',
`Content-Length: ${payloadBuf.length}`,
];
let req = await zitiHttpRequest(webhookUrl, 'POST', headersArray).catch((err) => {
console.log(`zitiHttpRequest failed: ${err}`);
process.exit(-1);
});
// Send the payload
results = await zitiHttpRequestData(req, payloadBuf).catch((err) => {
console.log(`zitiHttpRequestData failed: ${err}`);
process.exit(-1);
});
ziti.Ziti_http_request_end(req);
} catch (error) {
console.log(error.message);
}
}());