Last active
May 10, 2019 18:13
-
-
Save cburgmer/e22a3c078502606a8cb7e72fbf1d1713 to your computer and use it in GitHub Desktop.
Concourse CI 5 supports CC.XML. This is a proxy for unauthorised access to that new API.
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
#!/usr/bin/env node | |
// Concourse CI 5 supports CC.XML | |
// This small program will allow unauthorised access to the new API, while we | |
// figure out how to get existing monitors on to OAuth and friends. | |
const concourseUrl = 'http://localhost:8080'; | |
const port = 8081; | |
const flyTarget = 'proxy'; | |
const flyExecutablePath = 'fly'; | |
const niceRequire = (module) => { | |
try { | |
return require(module); | |
} catch(e) { | |
console.error("Please run 'npm i http-proxy yaml' first"); | |
process.exit(1); | |
} | |
}; | |
const http = niceRequire('http'), | |
httpProxy = niceRequire('http-proxy'), | |
fs = niceRequire('fs'), | |
os = niceRequire('os'), | |
path = niceRequire('path'), | |
yaml = niceRequire('yaml'); | |
const readToken = () => { | |
const flyrcPath = path.join(os.homedir(), '.flyrc'); | |
if (!fs.existsSync(flyrcPath)) { | |
throw new Error(`Try logging in via '${flyExecutablePath} login -t ${flyTarget} --concourse-url="${concourseUrl}"' first`); | |
} | |
const flyrc = fs.readFileSync(flyrcPath, 'utf8'); | |
const config = yaml.parse(flyrc); | |
if (!config.targets || !config.targets[flyTarget]) { | |
throw new Error(`Try logging in via '${flyExecutablePath} login -t ${flyTarget} --concourse-url="${concourseUrl}"' first`); | |
} | |
return config.targets[flyTarget].token.value; | |
}; | |
const isRequestForCcXml = url => /\/api\/v1\/teams\/[^/]+\/cc.xml/.test(url); | |
const proxy = httpProxy.createProxyServer({changeOrigin: true}); | |
proxy.on('proxyReq', function(proxyReq, req, res, options) { | |
if (isRequestForCcXml(req.url)) { | |
try { | |
const token = readToken(); | |
proxyReq.setHeader('Authorization', 'Bearer ' + token); | |
} catch (e) { | |
console.error(e); | |
} | |
} | |
}); | |
// fail early | |
readToken(); | |
console.log(`All ${concourseUrl}/*/cc.xml access points are now available without authorization. Try:`); | |
console.log(`curl -v "http://localhost:${port}/api/v1/teams/main/cc.xml"`); | |
http.createServer(function(req, res) { | |
proxy.web(req, res, { | |
target: concourseUrl | |
}, e => { | |
res.writeHead(500, { 'Content-Type': 'text/plain' }); | |
res.write(e.toString()); | |
res.end(); | |
}); | |
}).listen(port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Or use https://gist.github.com/spinningarrow/8ddd202e7e27568c754e71bb53a72b65