Skip to content

Instantly share code, notes, and snippets.

@cburgmer
Last active May 10, 2019 18:13
Show Gist options
  • Save cburgmer/e22a3c078502606a8cb7e72fbf1d1713 to your computer and use it in GitHub Desktop.
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.
#!/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);
@cburgmer
Copy link
Author

cburgmer commented May 8, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment