Skip to content

Commit 323ef0b

Browse files
Merge pull request #39 from bufferoverflow/feat/gitlab-11.2-user-api-improvements
feat: gitlab-11.2-group-api-improvements
2 parents 2b45a0b + 207a490 commit 323ef0b

File tree

5 files changed

+182
-63
lines changed

5 files changed

+182
-63
lines changed

README.md

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,20 @@ as authentication provider for the private npm registry
88
[![build](https://travis-ci.org/bufferoverflow/verdaccio-gitlab.svg?branch=master)](https://travis-ci.org/bufferoverflow/verdaccio-gitlab)
99
[![dependencies](https://david-dm.org/bufferoverflow/verdaccio-gitlab/status.svg)](https://david-dm.org/bufferoverflow/verdaccio-gitlab)
1010

11-
The main goal and the difference from other sinopia/verdaccio plugins are
11+
The main goal and differences from other sinopia/verdaccio plugins are
1212
the following:
1313

1414
- no admin token required
1515
- user authenticates with Personal Access Token
16-
- owned groups (no subgroups) are added to the user
17-
- publish packages if package scope or name is an owned group name
16+
- access & publish packages depending on user rights in gitlab
1817

1918
> This is experimental!
2019
20+
## Gitlab Version Compatibility
21+
22+
- If `legacy_mode: false` or undefined (default mode): Gitlab 11.2+
23+
- If `legacy_mode: true`: Gitlab 9.0+
24+
2125
## Use it
2226

2327
You need at least node version 8.x.x, codename **carbon**.
@@ -44,9 +48,6 @@ listen:
4448
auth:
4549
gitlab:
4650
url: https://gitlab.com
47-
authCache:
48-
enabled: true
49-
ttl: 300
5051

5152
uplinks:
5253
npmjs:
@@ -88,33 +89,76 @@ and publish packages:
8889
yarn publish --registry http://localhost:4873
8990
```
9091

91-
> **NOTE**: In order to publish packages, you need to create or be part of
92-
a GitLab group (as owner) which has the same name as your package name. You
93-
also need a fresh login, so that Verdaccio recognizes your owned groups.
92+
## Access Levels
93+
94+
Access and publish access rights depend on the mode used.
95+
96+
### Normal Mode
97+
98+
In the default mode, packages are available:
99+
100+
- *access* is allowed depending on verdaccio `package` configuration
101+
directives (unauthenticated / authenticated)
102+
- *publish* is allowed if the package name matches the logged in user
103+
id, or if the package name / scope of the package matches one of the
104+
user groups and the user has `auth.gitlab.publish` access rights on
105+
the group
106+
107+
For instance, assuming the following configuration:
108+
109+
- `auth.gitlab.publish` = `$maintainer`
110+
- the gitlab user `sample_user` has access to group `group1` as
111+
`$maintainer` and `group2` as `$reporter`
112+
- then this user could publish any of the npm packages:
113+
- `sample_user`
114+
- any package under `group1/**`
115+
- error if the user tries to publish any package under `group2/**`
116+
117+
### Legacy Mode
118+
119+
If using the legacy mode, the system behaves as in normal mode with
120+
fixed configuration `auth.gitlab.publish` = `$owner`
121+
122+
## Configuration Options
123+
124+
The full set of configuration options is:
125+
126+
```yaml
127+
auth:
128+
gitlab:
129+
url: <url>
130+
authCache:
131+
enabled: <boolean>
132+
ttl: <integer>
133+
legacy_mode: <boolean>
134+
publish: <string>
135+
```
136+
137+
<!-- markdownlint-disable MD013 -->
138+
| Option | Default | Type | Description |
139+
| ------ | ------- | ---- | ----------- |
140+
| `url` | `<empty>` | url | mandatory, the url of the gitlab server |
141+
| `authCache: enabled` | `true` | boolean | activate in-memory authentication cache |
142+
| `authCache: ttl` | `300` (`0`=unlimited) | integer | time-to-live of entries in the authentication cache, in seconds |
143+
| `legacy_mode` | `false` | boolean | gitlab versions pre-11.2 do not support groups api queries based on access level; this enables the legacy behaviour of only allowing npm publish operations on groups where the logged in user has owner rights |
144+
| `publish` | `$maintainer` | [`$guest`, `$reporter`, `$developer`, `$maintainer`, `$owner`] | group minimum access level of the logged in user required for npm publish operations (does not apply in legacy mode) |
145+
<!-- markdownlint-enable MD013 -->
94146

95147
## Authentication Cache
96148

97149
In order to avoid too many authentication requests to the underlying
98150
gitlab instance, the plugin provides an in-memory cache that will save
99151
the detected groups of the users for a configurable ttl in seconds.
100-
No clear-text password will be saved in-memory, just an SHA-256 hash
101-
and the groups information.
152+
153+
No clear-text password will is saved in-memory, just an SHA-256 hash of
154+
the user+password, plus the groups information.
102155

103156
By default, the cache will be enabled and the credentials will be stored
104157
for 300 seconds. The ttl is checked on access, but there's also an
105158
internal timer that will check expired values regularly, so data of
106159
users not actively interacting with the system will also be eventually
107160
invalidated.
108161

109-
```yaml
110-
auth:
111-
gitlab:
112-
url: https://gitlab.com
113-
authCache:
114-
enabled: (default true)
115-
ttl: (default: 300)
116-
```
117-
118162
*Please note* that this implementation is in-memory and not
119163
multi-process; if the cluster module is used for starting several
120164
verdaccio processes, each process will store its own copy of the cache,

conf/docker.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ auth:
1111
authCache:
1212
enabled: true
1313
ttl: 300
14+
publish: $maintainer
1415

1516
uplinks:
1617
npmjs:
@@ -31,5 +32,5 @@ packages:
3132
gitlab: true
3233

3334
logs:
34-
- {type: stdout, format: pretty, level: info }
35+
- { type: stdout, format: pretty, level: info }
3536
#- {type: file, path: verdaccio.log, level: info}

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ version: '3'
22

33
services:
44
gitlab:
5-
image: 'gitlab/gitlab-ce:latest'
5+
image: 'gitlab/gitlab-ce:nightly'
66
restart: always
77
environment:
88
- GITLAB_ROOT_PASSWORD=verdaccio

src/authcache.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class AuthCache {
2424
});
2525
this.storage.on('expired', (key, value) => {
2626
if (this.logger.trace()) {
27-
this.logger.trace(`[gitlab] expired key: ${key} with value: ${value}`);
27+
this.logger.trace(`[gitlab] expired key: ${key} with value:`, value);
2828
}
2929
});
3030
}
@@ -45,15 +45,19 @@ export class AuthCache {
4545

4646
}
4747

48+
export type UserDataGroups = {
49+
publish: string[]
50+
};
51+
4852
export class UserData {
4953
_username: string;
50-
_groups: string[];
54+
_groups: UserDataGroups;
5155

5256
get username(): string { return this._username; }
53-
get groups(): string[] { return this._groups; }
54-
set groups(groups: string[]) { this._groups = groups; }
57+
get groups(): UserDataGroups { return this._groups; }
58+
set groups(groups: UserDataGroups) { this._groups = groups; }
5559

56-
constructor(username: string, groups: string[]) {
60+
constructor(username: string, groups: UserDataGroups) {
5761
this._username = username;
5862
this._groups = groups;
5963
}

0 commit comments

Comments
 (0)