Skip to content

Commit

Permalink
Merge pull request #41 from snyk/feat/bitbucket_server_support
Browse files Browse the repository at this point in the history
feat: add support for bitbucket server
  • Loading branch information
Dar Malovani authored May 22, 2017
2 parents fa1bcd7 + f5d6a56 commit 4d08d7f
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 9 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,16 @@ You can also use it as a dependency in a `package.json`. Details on this later.

Running the client will require a unique `BROKER_TOKEN` and a `BROKER_SERVER_URL` pointing to a broker server. Once you have these, add them to your environment and run the broker in client mode.

However, you may want to use default settings for the `ACCEPT` rules and your environment. This can be first generated using the `init <name>` command:
However, you may want to use default settings for the `ACCEPT` rules and your environment. This can be first generated using the `init <template>` command:

for Github use:
```bash
$ broker init snyk --verbose
$ broker init github --verbose
```

for Bitbucket Server use:
```bash
$ broker init bitbucket-server --verbose
```

This will generate two new files: `accept.json` and `.env`. If the files already exist in the current working directory, the `init` command will fail and not overwrite your local copies.
Expand Down
28 changes: 28 additions & 0 deletions client-templates/bitbucket-server/.env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# your unique broker identifier
BROKER_TOKEN=

# your personal username to your bitbucket server account
BITBUCKET_USERNAME=

# your personal password to your bitbucket server account
BITBUCKET_PASSWORD=

# the host where your Bitbucket Server is running, excluding scheme.
# for bitbucket.yourdomain.com
# this should be "bitbucket.yourdomain.com"
BITBUCKET=

# the url that the Bitbucket server API should be accessed at.
# for bitbucket.yourdomain.com this should be
# changed to "bitbucket.yourdomain.com/rest/api/1.0"
BITBUCKET_API=$BITBUCKET/rest/api/1.0

# the url of your broker client (including scheme and port)
# BROKER_CLIENT_URL=

# The URL of the Snyk broker server
BROKER_SERVER_URL=https://broker.snyk.io

# the fine detail accept rules that allow Snyk to make API requests to your
# bitbucket server instance
ACCEPT=accept.json
47 changes: 47 additions & 0 deletions client-templates/bitbucket-server/accept.json.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"private":
[
{
"//": "list the user's projects",
"method": "GET",
"path": "/projects",
"origin": "https://${BITBUCKET_USERNAME}:${BITBUCKET_PASSWORD}@${BITBUCKET_API}"
},
{
"//": "list the user's repos",
"method": "GET",
"path": "/repos",
"origin": "https://${BITBUCKET_USERNAME}:${BITBUCKET_PASSWORD}@${BITBUCKET_API}"
},
{
"//": "used to determine the full dependency tree",
"method": "GET",
"path": "/projects/:project/repos/:repo/browse*/package.json",
"origin": "https://${BITBUCKET_USERNAME}:${BITBUCKET_PASSWORD}@${BITBUCKET_API}"
},
{
"//": "used to determine the full dependency tree",
"method": "GET",
"path": "/projects/:project/repos/:repo/browse*/Gemfile.lock",
"origin": "https://${BITBUCKET_USERNAME}:${BITBUCKET_PASSWORD}@${BITBUCKET_API}"
},
{
"//": "used to determine the full dependency tree",
"method": "GET",
"path": "/projects/:project/repos/:repo/browse*/Gemfile",
"origin": "https://${BITBUCKET_USERNAME}:${BITBUCKET_PASSWORD}@${BITBUCKET_API}"
},
{
"//": "used to determine the full dependency tree",
"method": "GET",
"path": "/projects/:project/repos/:repo/browse*/pom.xml",
"origin": "https://${BITBUCKET_USERNAME}:${BITBUCKET_PASSWORD}@${BITBUCKET_API}"
},
{
"//": "used to check if there's any ignore rules or existing patches",
"method": "GET",
"path": "/projects/:project/repos/:repo/browse/.snyk",
"origin": "https://${BITBUCKET_USERNAME}:${BITBUCKET_PASSWORD}@${BITBUCKET_API}"
}
]
}
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions lib/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ function sanitise(raw) {
raw = raw.replace(new RegExp(config.GITHUB_TOKEN, 'igm'), 'GITHUB_TOKEN');
}

if (config.BITBUCKET_USERNAME) {
raw = raw.replace(new RegExp(config.BITBUCKET_USERNAME, 'igm'), 'BITBUCKET_USERNAME');
}

if (config.BITBUCKET_PASSWORD) {
raw = raw.replace(new RegExp(config.BITBUCKET_PASSWORD, 'igm'), 'BITBUCKET_PASSWORD');
}

return raw;
}

Expand Down
13 changes: 10 additions & 3 deletions lib/relay.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,16 @@ function requestHandler(filterRules) {
url: req.url,
status: response.status,
}, 'response');
res.status(response.status)
.set(response.headers)
.send(response.body);
const resp = res
.status(response.status)
.set(response.headers);

// keep chunked http requests without content-length header
if (undefsafe(response, 'headers.transfer-encoding') === 'chunked') {
resp.write(response.body);
return resp.end();
}
resp.send(response.body);
});
});
};
Expand Down
22 changes: 21 additions & 1 deletion test/functional/init.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test('init creates files from specified client-templates', t => {
if (err) { throw err; }
process.chdir(path);

init({_: ['snyk']})
init({_: ['github']})
.then(() => Promise.all([
fs.stat('.env'),
fs.stat('accept.json'),
Expand All @@ -25,3 +25,23 @@ test('init creates files from specified client-templates', t => {
});
});
});

test('init creates files from specified bitbucket', t => {
const originalWorkDir = process.cwd();
t.teardown(() => process.chdir(originalWorkDir));

tmp.dir({ unsafeCleanup: true }, (err, path) => {
if (err) { throw err; }
process.chdir(path);

init({_: ['bitbucket-server']})
.then(() => Promise.all([
fs.stat('.env'),
fs.stat('accept.json'),
]))
.then(stats => {
t.ok(stats.every(Boolean), 'all templated files created');
t.end();
});
});
});
19 changes: 18 additions & 1 deletion test/functional/server-client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ test('proxy requests originating from behind the broker server', t => {
t.same(res.body, swappedBody, 'body brokered');
t.end();
});
})
});

// the filtering happens in the broker client
t.test('block request for non-whitelisted url', t => {
Expand Down Expand Up @@ -165,6 +165,23 @@ test('proxy requests originating from behind the broker server', t => {
});
});

t.test('content-length is not set when using chunked http', t => {
const url = `http://localhost:${serverPort}/broker/${token}/echo-headers`;
request({ url, method: 'get',
headers: [{'Transfer-Encoding': 'chunked'}] }, (err, res) => {
t.notOk(res.headers['Content-Length'], 'no content-length header');
t.end();
});
});

t.test('content-length is set without chunked http', t => {
const url = `http://localhost:${serverPort}/broker/${token}/echo-headers`;
request({ url, method: 'get' }, (err, res) => {
t.ok(res.headers['Content-Length'], 'found content-length header');
t.end();
});
});

t.test('clean up', t => {
client.close();
setTimeout(() => {
Expand Down
4 changes: 2 additions & 2 deletions usage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
Commands:
client .............. run the broker in client mode
server .............. run the broker in server mode
init <project> ...... generate the client broker files required
for a given project
init <template> ..... generate the client broker files required
for a given template (github or bitbucket)

Flags:

Expand Down

0 comments on commit 4d08d7f

Please sign in to comment.