-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
240 lines (215 loc) · 6.9 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
const spawnSync = require('child_process').spawnSync;
const fs = require('fs');
const os = require('os');
const path = require('path');
const process = require('process');
const versionMap = {
'8': '8.15.2',
'7': '7.17.24',
'8.15': '8.15.2',
'8.14': '8.14.3',
'8.13': '8.13.4',
'8.12': '8.12.2',
'8.11': '8.11.4',
'8.10': '8.10.4',
'8.9': '8.9.2',
'8.8': '8.8.2',
'8.7': '8.7.1',
'8.6': '8.6.2',
'8.5': '8.5.3',
'8.4': '8.4.3',
'8.3': '8.3.3',
'8.2': '8.2.3',
'8.1': '8.1.3',
'8.0': '8.0.1',
'7.17': '7.17.24',
'7.16': '7.16.3',
'7.15': '7.15.2',
'7.14': '7.14.2',
'7.13': '7.13.4',
'7.12': '7.12.1',
'7.11': '7.11.1',
'7.10': '7.10.2',
'7.9': '7.9.3',
'7.8': '7.8.1',
'7.7': '7.7.1',
'7.6': '7.6.2',
'7.5': '7.5.2',
'7.4': '7.4.2',
'7.3': '7.3.2',
'7.2': '7.2.1',
'7.1': '7.1.1',
'7.0': '7.0.1'
};
function run() {
const args = Array.from(arguments);
console.log(args.join(' '));
const command = args.shift();
// spawn is safer and more lightweight than exec
const ret = spawnSync(command, args, {stdio: 'inherit'});
if (ret.status !== 0) {
throw ret.error;
}
}
function addToEnv(value) {
fs.appendFileSync(process.env.GITHUB_ENV, `${value}\n`);
}
function addToPath(value) {
fs.appendFileSync(process.env.GITHUB_PATH, `${value}\n`);
}
function getVersion() {
let version = process.env['INPUT_ELASTICSEARCH-VERSION'] || '8';
if (versionMap[version]) {
version = versionMap[version];
}
if (!/^[78]\.\d{1,2}\.\d{1,2}$/.test(version)) {
throw `Elasticsearch version not supported: ${version}`;
}
return version;
}
function isWindows() {
return process.platform == 'win32';
}
// no JDK version is ideal, but deprecated
function getUrl() {
let url;
if (process.platform == 'darwin') {
if (process.arch == 'arm64') {
url = `https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${elasticsearchVersion}-darwin-aarch64.tar.gz`;
} else {
url = `https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${elasticsearchVersion}-darwin-x86_64.tar.gz`;
}
} else if (isWindows()) {
url = `https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${elasticsearchVersion}-windows-x86_64.zip`;
} else {
if (process.arch == 'arm64') {
url = `https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${elasticsearchVersion}-linux-aarch64.tar.gz`;
} else {
url = `https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-${elasticsearchVersion}-linux-x86_64.tar.gz`;
}
}
return url;
}
function download() {
const url = getUrl();
if (isWindows()) {
run('curl', '-s', '-o', 'elasticsearch.zip', url);
run('unzip', '-q', 'elasticsearch.zip');
} else {
run('wget', '-q', '-O', 'elasticsearch.tar.gz', url);
run('tar', 'xfz', 'elasticsearch.tar.gz');
}
if (!fs.existsSync(cacheDir)) {
fs.mkdirSync(cacheDir, {recursive: true});
}
if (isWindows()) {
// fix for: cross-device link not permitted
run('mv', `elasticsearch-${elasticsearchVersion}`, esHome)
} else {
fs.renameSync(`elasticsearch-${elasticsearchVersion}`, esHome);
}
}
// log4j
// Elasticsearch 6 and 7 are not susceptible to RCE due to Java Security Manager
// set flag to prevent information leak via DNS
// https://discuss.elastic.co/t/apache-log4j2-remote-code-execution-rce-vulnerability-cve-2021-44228-esa-2021-31/291476
function fixLog4j() {
const jvmOptionsPath = path.join(esHome, 'config', 'jvm.options');
if (!fs.readFileSync(jvmOptionsPath).includes('log4j2.formatMsgNoLookups')) {
fs.appendFileSync(jvmOptionsPath, '\n-Dlog4j2.formatMsgNoLookups=true\n');
// needed for Elasticsearch < 6.5
// but remove for all versions
if (!isWindows()) {
const coreJarPath = fs.readdirSync(path.join(esHome, 'lib')).filter(fn => fn.includes('log4j-core-'))[0];
if (coreJarPath) {
run('zip', '-q', '-d', path.join(esHome, 'lib', coreJarPath), 'org/apache/logging/log4j/core/lookup/JndiLookup.class');
}
} else if (elasticsearchVersion < '6.5') {
throw 'Elasticsearch version not available';
}
}
}
function installPlugins() {
let plugins = (process.env['INPUT_PLUGINS'] || '').trim();
if (plugins.length > 0) {
console.log('Installing plugins');
// split here instead of above since JS returns [''] for empty array
plugins = plugins.split(/\s*[,\n]\s*/);
// validate
plugins.forEach( function(plugin) {
if (!/^\w\S+$/.test(plugin)) {
throw `Invalid plugin: ${plugin}`;
}
});
// install multiple plugins at once with Elasticsearch 7.6+
// https://www.elastic.co/guide/en/elasticsearch/plugins/7.6/installing-multiple-plugins.html
const versionParts = elasticsearchVersion.split('.');
const atOnce = parseInt(versionParts[0]) >= 7 && parseInt(versionParts[1]) >= 6;
let pluginCmd = path.join(esHome, 'bin', 'elasticsearch-plugin');
if (isWindows()) {
pluginCmd += '.bat';
}
if (atOnce) {
run(pluginCmd, 'install', '--silent', '--batch', ...plugins);
} else {
plugins.forEach( function(plugin) {
run(pluginCmd, 'install', '--silent', '--batch', plugin);
});
}
}
}
function setConfig(dir) {
let config = process.env['INPUT_CONFIG'] || '';
config += '\n';
config += 'discovery.type: single-node\n';
const [majorVersion, minorVersion, patchVersion] = elasticsearchVersion.split('.');
if (parseInt(majorVersion) >= 8 || parseInt(minorVersion) >= 13) {
config += 'xpack.security.enabled: false\n';
}
const file = path.join(dir, 'config', 'elasticsearch.yml');
// overwrite instead of append to play nicely with caching
// alternatively, could append to copy of original file
fs.writeFileSync(file, config);
}
function startServer() {
if (isWindows()) {
const serviceCmd = path.join(esHome, 'bin', 'elasticsearch-service.bat');
run(serviceCmd, 'install');
run(serviceCmd, 'start');
} else {
run(path.join(esHome, 'bin', 'elasticsearch'), '-d');
}
}
function getPort() {
const config = process.env['INPUT_CONFIG'] || '';
const match = config.match(/\bhttp\.port: +(\d{4,5})\b/);
return match ? parseInt(match[1]) : 9200;
}
function waitForReady() {
console.log("Waiting for server to be ready");
for (let i = 0; i < 30; i++) {
let ret = spawnSync('curl', ['-s', `localhost:${getPort()}`]);
if (ret.status === 0) {
break;
}
spawnSync('sleep', ['1']);
}
}
const elasticsearchVersion = getVersion();
const cacheDir = path.join(os.homedir(), 'elasticsearch');
const esHome = path.join(cacheDir, elasticsearchVersion);
if (!fs.existsSync(esHome)) {
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'elasticsearch-'));
process.chdir(tmpDir);
download();
fixLog4j();
installPlugins();
} else {
console.log('Elasticsearch cached');
fixLog4j();
}
setConfig(esHome);
startServer();
waitForReady();
addToEnv(`ES_HOME=${esHome}`);
addToPath(path.join(esHome, 'bin'));