-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5843 from spalger/implement/randomBasePathInDev
[server/dev] inject a random basepath
- Loading branch information
Showing
10 changed files
with
193 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { Server } from 'hapi'; | ||
import { notFound } from 'boom'; | ||
import { merge, sample } from 'lodash'; | ||
import { format as formatUrl } from 'url'; | ||
import { fromNode } from 'bluebird'; | ||
import { Agent as HttpsAgent } from 'https'; | ||
import { readFileSync } from 'fs'; | ||
|
||
import Config from '../../server/config/config'; | ||
import setupConnection from '../../server/http/setup_connection'; | ||
import setupLogging from '../../server/logging'; | ||
|
||
const alphabet = 'abcdefghijklmnopqrztuvwxyz'.split(''); | ||
|
||
export default class BasePathProxy { | ||
constructor(clusterManager, userSettings) { | ||
this.clusterManager = clusterManager; | ||
this.server = new Server(); | ||
|
||
const config = Config.withDefaultSchema(userSettings); | ||
|
||
this.targetPort = config.get('dev.basePathProxyTarget'); | ||
this.basePath = config.get('server.basePath'); | ||
|
||
const { cert } = config.get('server.ssl'); | ||
if (cert) { | ||
this.proxyAgent = new HttpsAgent({ | ||
ca: readFileSync(cert) | ||
}); | ||
} | ||
|
||
if (!this.basePath) { | ||
this.basePath = `/${sample(alphabet, 3).join('')}`; | ||
config.set('server.basePath', this.basePath); | ||
} | ||
|
||
setupLogging(null, this.server, config); | ||
setupConnection(null, this.server, config); | ||
this.setupRoutes(); | ||
} | ||
|
||
setupRoutes() { | ||
const { server, basePath, targetPort } = this; | ||
|
||
server.route({ | ||
method: 'GET', | ||
path: '/', | ||
handler(req, reply) { | ||
return reply.redirect(basePath); | ||
} | ||
}); | ||
|
||
server.route({ | ||
method: '*', | ||
path: `${basePath}/{kbnPath*}`, | ||
handler: { | ||
proxy: { | ||
passThrough: true, | ||
xforward: true, | ||
agent: this.proxyAgent, | ||
mapUri(req, callback) { | ||
callback(null, formatUrl({ | ||
protocol: server.info.protocol, | ||
hostname: server.info.host, | ||
port: targetPort, | ||
pathname: req.params.kbnPath, | ||
query: req.query, | ||
})); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
server.route({ | ||
method: '*', | ||
path: `/{oldBasePath}/{kbnPath*}`, | ||
handler(req, reply) { | ||
const {oldBasePath, kbnPath = ''} = req.params; | ||
|
||
const isGet = req.method === 'get'; | ||
const isBasePath = oldBasePath.length === 3; | ||
const isApp = kbnPath.slice(0, 4) === 'app/'; | ||
|
||
if (isGet && isBasePath && isApp) { | ||
return reply.redirect(`${basePath}/${kbnPath}`); | ||
} | ||
|
||
return reply(notFound()); | ||
} | ||
}); | ||
} | ||
|
||
async listen() { | ||
await fromNode(cb => this.server.start(cb)); | ||
this.server.log(['listening', 'info'], `basePath Proxy running at ${this.server.info.uri}${this.basePath}`); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.