Skip to content

Commit

Permalink
feat: add support for bitbucket server
Browse files Browse the repository at this point in the history
adds the rules for using Bitbucket server.
Also, add supports to chunked http requests, if they are sent to the
broker, to avoid Content-length and Transfer-Encoding headers to be
mixed.
  • Loading branch information
Dar Malovani committed May 22, 2017
1 parent fa1bcd7 commit e3afd01
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 8 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 --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/.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/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.
4 changes: 4 additions & 0 deletions lib/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ function sanitise(raw) {
raw = raw.replace(new RegExp(config.GITHUB_TOKEN, 'igm'), 'GITHUB_TOKEN');
}

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']})
.then(() => Promise.all([
fs.stat('.env'),
fs.stat('accept.json'),
]))
.then(stats => {
t.ok(stats.every(Boolean), 'all templated files created');
t.end();
});
});
});
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 e3afd01

Please sign in to comment.