Skip to content

Commit

Permalink
feat: add secure flag to gitlab config (#1129)
Browse files Browse the repository at this point in the history
* feat: add secure flag to gitlab config

* refactor: use lodash pickBy instead of merge

* test: add ca options tests

* docs: fix typo

* test: refactor gitlab test

---------

Co-authored-by: Alexandre von Brasche Figueiredo <alexandre.vonbraschefigueiredo@aconso.com>
  • Loading branch information
vonBrax and vonBrax authored Jul 10, 2024
1 parent 0ac44ba commit 7a3ce98
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 4 deletions.
1 change: 1 addition & 0 deletions config/release-it.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"tokenRef": "GITLAB_TOKEN",
"tokenHeader": "Private-Token",
"certificateAuthorityFile": null,
"secure": null,
"assets": null,
"origin": null,
"skipChecks": false
Expand Down
17 changes: 17 additions & 0 deletions docs/gitlab-releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,23 @@ specify the root CA certificate with `certificateAuthorityFile`, for example:
}
```

Alternatively, if you want to disable the server certificate verification against the list of supplied CAs, you can set
the `secure` flag to false:

```json
{
"gitlab": {
"release": true,
"tokenHeader": "PRIVATE-TOKEN",
"secure": false
}
}
```

The `secure` option is passed down to [got](https://github.com/sindresorhus/got), which in turn also forwards it to node's
[`https.request`](https://nodejs.org/api/https.html#httpsrequestoptions-callback) method as the `rejectUnauthorized` option.
The default value of `rejectUnauthorized` is `true`.

## Update the latest release

The latest GitLab release can be updated, e.g. to update the releases notes or add release assets.
Expand Down
15 changes: 11 additions & 4 deletions lib/plugin/gitlab/GitLab.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@ class GitLab extends Release {
super(...args);
this.registerPrompts(prompts);
this.assets = [];
const { certificateAuthorityFile } = this.options;
this.certificateAuthorityOption = certificateAuthorityFile
? { https: { certificateAuthority: fs.readFileSync(certificateAuthorityFile) } }
: {};
const { certificateAuthorityFile, secure } = this.options;

const httpsOptions = {
certificateAuthority: certificateAuthorityFile ? fs.readFileSync(certificateAuthorityFile) : undefined,
rejectUnauthorized: typeof secure === 'boolean' ? secure : undefined
};

// Remove keys with undefined values
const https = _.pickBy(httpsOptions, value => value !== undefined);

this.certificateAuthorityOption = _.isEmpty(https) ? {} : { https };
}

get client() {
Expand Down
3 changes: 3 additions & 0 deletions schema/gitlab.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
"certificateAuthorityFile": {
"default": null
},
"secure": {
"default": null
},
"assets": {
"default": null
},
Expand Down
48 changes: 48 additions & 0 deletions test/gitlab.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from 'node:fs';
import test from 'ava';
import sinon from 'sinon';
import nock from 'nock';
Expand Down Expand Up @@ -250,3 +251,50 @@ test('should skip checks', async t => {

t.is(gitlab.log.exec.args.filter(entry => /checkReleaseMilestones/.test(entry[0])).length, 0);
});

test('should handle certificate authority options', t => {
const sandbox = sinon.createSandbox();
sandbox.stub(fs, 'readFileSync').returns('test certificate');

{
const options = { gitlab: {} };
const gitlab = factory(GitLab, { options });
t.deepEqual(gitlab.certificateAuthorityOption, {});
}

{
const options = { gitlab: { certificateAuthorityFile: 'cert.crt' } };
const gitlab = factory(GitLab, { options });
t.deepEqual(gitlab.certificateAuthorityOption, { https: { certificateAuthority: 'test certificate' } });
}

{
const options = { gitlab: { secure: false } };
const gitlab = factory(GitLab, { options });
t.deepEqual(gitlab.certificateAuthorityOption, { https: { rejectUnauthorized: false } });
}

{
const options = { gitlab: { secure: true } };
const gitlab = factory(GitLab, { options });
t.deepEqual(gitlab.certificateAuthorityOption, { https: { rejectUnauthorized: true } });
}

{
const options = { gitlab: { certificateAuthorityFile: 'cert.crt', secure: true } };
const gitlab = factory(GitLab, { options });
t.deepEqual(gitlab.certificateAuthorityOption, {
https: { certificateAuthority: 'test certificate', rejectUnauthorized: true }
});
}

{
const options = { gitlab: { certificateAuthorityFile: 'cert.crt', secure: false } };
const gitlab = factory(GitLab, { options });
t.deepEqual(gitlab.certificateAuthorityOption, {
https: { certificateAuthority: 'test certificate', rejectUnauthorized: false }
});
}

sandbox.restore();
});
3 changes: 3 additions & 0 deletions types/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ export interface Config {

/** @default null */
certificateAuthorityFile?: any;

/** @default null */
secure?: boolean;

/** @default null */
assets?: any;
Expand Down

0 comments on commit 7a3ce98

Please sign in to comment.