forked from fangli/kibana-authentication-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
155 lines (134 loc) · 5.33 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
149
150
151
152
153
154
155
/**
* Hosts the latest kibana3 and elasticsearch behind Google OAuth2 Authentication
* with nodejs and express.
* License: MIT
* Copyright: Funplus Game Inc.
* Author: Fang Li.
* Project: https://github.com/fangli/kibana-authentication-proxy
*/
var express = require('express');
var https = require('https');
var http = require('http');
var fs = require('fs');
var config = require('./config');
var app = express();
app.use(express.logger());
app.use(function(req, res, next){
req.url = req.url.replace(/\/\//,'/');
next();
});
console.log('Server starting...');
if (!config.base_path) {
config.base_path="";
console.log("No base_path specified in config so using /");
}
// Index Filtering
function readAndInitIndexFilterFile() {
global.index_filter_usregex=new Object();
var index_filter_data=fs.readFileSync(config.index_filter_file,'utf8');
var userregexes=index_filter_data.split('\n');
for (var userregex in userregexes) {
var usre=userregexes[userregex].match(/^([^:]+):(.+)/);
if (usre) {
global.index_filter_usregex[usre[1]]=usre[2];
}
}
}
global.index_filter_usregex=new Object();
if (!config.index_filter_trigger) {
config.index_filter_trigger='^logstash-';
}
if (!config.index_filter_file) {
config.index_filter_file=false;
console.log("No index_filter_file specified so not using index filtering");
} else {
if ( fs.existsSync(config.index_filter_file) ) {
console.log("index_filter_file specified, read and parsed - so using it");
readAndInitIndexFilterFile();
fs.watchFile(config.index_filter_file, { persistent: true, interval: 5007 }, function(curr,prev) {
if (curr.mtime.getTime() != prev.mtime.getTime()) {
console.log('INDEX FILTER File was changed, so reloading values');
readAndInitIndexFilterFile();
}
});
} else {
config.index_filter_file=false;
console.log("index_filter_file specified but not found in fs so not using index filtering");
}
}
app.use(express.cookieParser());
app.use(express.session({ secret: config.cookie_secret }));
// Authentication
function readAndInitBasicAuthFile() {
config.basic_auth_users=new Array();
var basic_auth_users=fs.readFileSync(config.basic_auth_file,'utf8');
var userpass=basic_auth_users.split('\n');
for (var userpass_index in userpass) {
var uspa=userpass[userpass_index].match(/^([^:]+):(.+)/);
if (uspa) {
config.basic_auth_users[config.basic_auth_users.length]={"user": uspa[1], "password": uspa[2]};
}
}
}
if (config.enable_basic_auth && config.basic_auth_file && fs.existsSync(config.basic_auth_file)) {
console.log('basic_auth_file defined and found, so reading it ...');
readAndInitBasicAuthFile();
fs.watchFile(config.basic_auth_file, { persistent: true, interval: 5007 }, function(curr,prev) {
if (curr.mtime.getTime() != prev.mtime.getTime()) {
console.log('BASIC AUTH File was changed, so reloading values');
readAndInitBasicAuthFile();
}
});
}
require('./lib/basic-auth').configureBasic(express, app, config);
require('./lib/google-oauth').configureOAuth(express, app, config);
require('./lib/cas-auth.js').configureCas(express, app, config);
// Setup ES proxy
require('./lib/es-proxy').configureESProxy(app, config.es_host, config.es_port,
config.es_username, config.es_password, config.base_path, config.index_filter_trigger);
// Serve config.js for kibana3
// We should use special config.js for the frontend and point the ES to __es/
app.get(config.base_path + '/config.js', kibana3configjs);
// Serve all kibana3 frontend files
app.use(express.compress());
app.use(config.base_path + '/', express.static(__dirname + '/kibana/src', {maxAge: config.brower_cache_maxage || 0}));
run();
function run() {
if (config.enable_ssl_port === true) {
var options = {
key: fs.readFileSync(config.ssl_key_file),
cert: fs.readFileSync(config.ssl_cert_file),
};
https.createServer(options, app).listen(config.listen_port_ssl);
console.log('Server listening on ' + config.listen_port_ssl + '(SSL)');
}
http.createServer(app).listen(config.listen_port);
console.log('Server listening on ' + config.listen_port);
}
function kibana3configjs(req, res) {
function getKibanaIndex() {
var raw_index = config.kibana_es_index;
var user_type = config.which_auth_type_for_kibana_index;
var user;
if (raw_index.indexOf('%user%') > -1) {
if (user_type === 'google') {
user = req.googleOauth.id;
} else if (user_type === 'basic') {
user = req.user;
} else if (user_type === 'cas') {
user = req.session.cas_user_name;
} else {
user = 'unknown';
}
return raw_index.replace(/%user%/gi, user);
} else {
return raw_index;
}
}
res.setHeader('Content-Type', 'application/javascript');
res.end("define(['settings'], " +
"function (Settings) {'use strict'; return new Settings({elasticsearch: '" + config.base_path + "/__es', default_route : '/dashboard/file/default.json'," +
"kibana_index: '" +
getKibanaIndex() +
"', panel_names: ['histogram', 'map', 'pie', 'table', 'filtering', 'timepicker', 'text', 'hits', 'column', 'trends', 'bettermap', 'query', 'terms', 'sparklines'] }); });");
}