Skip to content

Commit d3f5864

Browse files
authored
Merge pull request #1496 from ckeditor/ci/4658-reassign-npm-tags-oidc
Introduce the `useOidc` option in `reassignNpmTags()`
2 parents 22532d2 + c4f43ba commit d3f5864

7 files changed

Lines changed: 101 additions & 56 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
type: Feature
3+
scope:
4+
- ckeditor5-dev-release-tools
5+
---
6+
7+
Introduced the `useOidc` option in the `reassignNpmTags()` task to support npm Trusted Publishing. When enabled, the task no longer verifies the npm account with `npm whoami` and the `npmOwner` option is not required. Instead, the task verifies that the `NPM_ID_TOKEN` environment variable is set. Changing dist-tags with OIDC requires npm 11.21.0 or newer and a trusted publisher that allows the `npm dist-tag` command.

‎packages/ckeditor5-dev-release-tools/lib/tasks/publishpackages.js‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,7 @@ export default async function publishPackages( options ) {
7979
attempts = 5
8080
} = options;
8181

82-
if ( useOidc ) {
83-
// npm exchanges the OIDC token only during supported operations, such as `npm publish`, and `npm whoami`
84-
// does not reflect Trusted Publishing authentication. Hence, only the token presence can be verified upfront.
85-
if ( !process.env.NPM_ID_TOKEN ) {
86-
throw new Error( 'The "NPM_ID_TOKEN" environment variable is required when publishing using npm Trusted Publishing (OIDC).' );
87-
}
88-
} else {
89-
await assertNpmAuthorization( npmOwner );
90-
}
82+
await assertNpmAuthorization( npmOwner, { useOidc } );
9183

9284
// Find packages that would be published...
9385
const packagePaths = await workspaces.findPathsToPackages( cwd, packagesDirectory );

‎packages/ckeditor5-dev-release-tools/lib/tasks/reassignnpmtags.js‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@ const execPromise = promisify( exec );
1919
* It supports built-in tags (e.g. `latest`, `staging`, `next`) and custom tags (e.g. `latest-v{X}`).
2020
* Each operation will be retried up to 3 times in case of failure.
2121
*
22+
* When using npm Trusted Publishing (`useOidc`), the `npm whoami` check is replaced by verifying that the `NPM_ID_TOKEN`
23+
* environment variable is set, as `npm whoami` does not reflect OIDC authentication. It requires npm 11.21.0 or newer
24+
* and a trusted publisher that allows the `npm dist-tag` command.
25+
*
2226
* @param {object} options
23-
* @param {string} options.npmOwner User that is authorized to release packages.
27+
* @param {string} [options.npmOwner] User that is authorized to release packages. Required unless `useOidc` is enabled.
28+
* @param {boolean} [options.useOidc=false] Whether to authorize using npm Trusted Publishing (OIDC). When enabled,
29+
* the `npm whoami` authorization check is skipped and the `NPM_ID_TOKEN` environment variable must be set instead.
2430
* @param {string} options.version Specifies the version of packages to reassign the tags for.
2531
* @param {Array.<string>} options.packages Array of packages' names to reassign tags for.
2632
* @param {string} [options.npmTag='latest'] Npm dist-tag to assign.
@@ -29,6 +35,7 @@ const execPromise = promisify( exec );
2935
export default async function reassignNpmTags( options ) {
3036
const {
3137
npmOwner,
38+
useOidc = false,
3239
version,
3340
packages,
3441
npmTag = 'latest'
@@ -38,7 +45,7 @@ export default async function reassignNpmTags( options ) {
3845
const packagesSkipped = [];
3946
const packagesUpdated = [];
4047

41-
await assertNpmAuthorization( npmOwner );
48+
await assertNpmAuthorization( npmOwner, { useOidc } );
4249

4350
const counter = tools.createSpinner( 'Reassigning npm tags...', { total: packages.length } );
4451
counter.start();

‎packages/ckeditor5-dev-release-tools/lib/utils/assertnpmauthorization.js‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,25 @@ import { tools } from '@ckeditor/ckeditor5-dev-utils';
88
/**
99
* Checks whether a user is logged to npm as the provided account name.
1010
*
11-
* @param {string} npmOwner Expected npm account name that should be logged into npm.
11+
* When using npm Trusted Publishing (`useOidc`), the check verifies that the `NPM_ID_TOKEN` environment variable is set
12+
* instead. npm exchanges the OIDC token only during supported operations, such as `npm publish` or `npm dist-tag`,
13+
* and `npm whoami` does not reflect Trusted Publishing authentication.
14+
*
15+
* @param {string} [npmOwner] Expected npm account name that should be logged into npm. Required unless `useOidc` is enabled.
16+
* @param {object} [options={}]
17+
* @param {boolean} [options.useOidc=false] Whether to verify the npm Trusted Publishing (OIDC) setup instead of the npm account.
1218
* @returns {Promise}
1319
*/
14-
export default async function assertNpmAuthorization( npmOwner ) {
20+
export default async function assertNpmAuthorization( npmOwner, { useOidc = false } = {} ) {
21+
if ( useOidc ) {
22+
// Only the token presence can be verified upfront.
23+
if ( !process.env.NPM_ID_TOKEN ) {
24+
throw new Error( 'The "NPM_ID_TOKEN" environment variable is required when using npm Trusted Publishing (OIDC).' );
25+
}
26+
27+
return;
28+
}
29+
1530
return tools.shExec( 'npm whoami', { verbosity: 'error', async: true } )
1631
.then( npmCurrentUser => {
1732
if ( npmOwner !== npmCurrentUser.trim() ) {

‎packages/ckeditor5-dev-release-tools/tests/tasks/publishpackages.js‎

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ describe( 'publishPackages()', () => {
102102
listrTask: {}
103103
} );
104104

105-
expect( vi.mocked( assertNpmAuthorization ) ).toHaveBeenCalledExactlyOnceWith( 'pepe' );
105+
expect( vi.mocked( assertNpmAuthorization ) ).toHaveBeenCalledExactlyOnceWith( 'pepe', { useOidc: false } );
106106
} );
107107

108108
it( 'should throw if npm authorization assertion failed', async () => {
@@ -115,53 +115,19 @@ describe( 'publishPackages()', () => {
115115
npmOwner: 'fake-pepe'
116116
} ) ).rejects.toThrow( 'You must be logged to npm as "pepe" to execute this release step.' );
117117

118-
expect( vi.mocked( assertNpmAuthorization ) ).toHaveBeenCalledExactlyOnceWith( 'fake-pepe' );
118+
expect( vi.mocked( assertNpmAuthorization ) ).toHaveBeenCalledExactlyOnceWith( 'fake-pepe', { useOidc: false } );
119119
} );
120120

121-
it( 'should not verify npm authorization when publishing using OIDC (`useOidc=true`)', async () => {
122-
vi.stubEnv( 'NPM_ID_TOKEN', 'oidc-token' );
121+
it( 'should pass the `useOidc` option to the npm authorization assertion', async () => {
122+
vi.mocked( workspaces.findPathsToPackages ).mockReset().mockResolvedValue( [] );
123123

124-
const promise = publishPackages( {
124+
await publishPackages( {
125125
packagesDirectory: 'packages',
126126
useOidc: true,
127127
listrTask: {}
128128
} );
129129

130-
await vi.advanceTimersToNextTimerAsync();
131-
await promise;
132-
133-
expect( vi.mocked( assertNpmAuthorization ) ).not.toHaveBeenCalled();
134-
expect( vi.mocked( executeInParallel ) ).toHaveBeenCalledOnce();
135-
} );
136-
137-
it( 'should throw when publishing using OIDC while the "NPM_ID_TOKEN" environment variable is not set', async () => {
138-
vi.stubEnv( 'NPM_ID_TOKEN', undefined );
139-
140-
await expect( publishPackages( {
141-
packagesDirectory: 'packages',
142-
useOidc: true,
143-
listrTask: {}
144-
} ) ).rejects.toThrow(
145-
'The "NPM_ID_TOKEN" environment variable is required when publishing using npm Trusted Publishing (OIDC).'
146-
);
147-
148-
expect( vi.mocked( assertNpmAuthorization ) ).not.toHaveBeenCalled();
149-
expect( vi.mocked( executeInParallel ) ).not.toHaveBeenCalled();
150-
} );
151-
152-
it( 'should throw when publishing using OIDC while the "NPM_ID_TOKEN" environment variable is empty', async () => {
153-
vi.stubEnv( 'NPM_ID_TOKEN', '' );
154-
155-
await expect( publishPackages( {
156-
packagesDirectory: 'packages',
157-
useOidc: true,
158-
listrTask: {}
159-
} ) ).rejects.toThrow(
160-
'The "NPM_ID_TOKEN" environment variable is required when publishing using npm Trusted Publishing (OIDC).'
161-
);
162-
163-
expect( vi.mocked( assertNpmAuthorization ) ).not.toHaveBeenCalled();
164-
expect( vi.mocked( executeInParallel ) ).not.toHaveBeenCalled();
130+
expect( vi.mocked( assertNpmAuthorization ) ).toHaveBeenCalledExactlyOnceWith( undefined, { useOidc: true } );
165131
} );
166132

167133
it( 'should verify npm authorization when the `useOidc` option is disabled explicitly', async () => {
@@ -174,7 +140,7 @@ describe( 'publishPackages()', () => {
174140
listrTask: {}
175141
} );
176142

177-
expect( vi.mocked( assertNpmAuthorization ) ).toHaveBeenCalledExactlyOnceWith( 'pepe' );
143+
expect( vi.mocked( assertNpmAuthorization ) ).toHaveBeenCalledExactlyOnceWith( 'pepe', { useOidc: false } );
178144
} );
179145

180146
it( 'should assert that each found directory is a package', async () => {

‎packages/ckeditor5-dev-release-tools/tests/tasks/reassignnpmtags.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ describe( 'reassignNpmTags()', () => {
6060
expect( stubs.exec ).not.toHaveBeenCalled();
6161
} );
6262

63+
it( 'should verify npm authorization using the provided npm account', async () => {
64+
await reassignNpmTags( { npmOwner: 'authorized-user', version: '1.0.1', packages: [] } );
65+
66+
expect( vi.mocked( assertNpmAuthorization ) ).toHaveBeenCalledExactlyOnceWith( 'authorized-user', { useOidc: false } );
67+
} );
68+
69+
it( 'should pass the `useOidc` option to the npm authorization assertion', async () => {
70+
stubs.exec.mockResolvedValue( { stdout: '+latest' } );
71+
72+
await reassignNpmTags( { useOidc: true, version: '1.0.1', packages: [ 'package1' ] } );
73+
74+
expect( vi.mocked( assertNpmAuthorization ) ).toHaveBeenCalledExactlyOnceWith( undefined, { useOidc: true } );
75+
expect( stubs.exec ).toHaveBeenCalledExactlyOnceWith( 'npm dist-tag add package1@1.0.1 latest' );
76+
} );
77+
6378
it( 'should skip updating tags when provided version matches existing version for tag latest', async () => {
6479
vi.mocked( columns ).mockReturnValue( 'package1 | package2' );
6580
stubs.exec.mockRejectedValue( new Error( 'is already set to version' ) );

‎packages/ckeditor5-dev-release-tools/tests/utils/assertnpmauthorization.js‎

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,48 @@ describe( 'assertNpmAuthorization()', () => {
4343
await expect( assertNpmAuthorization( 'pepe' ) )
4444
.rejects.toThrow( 'You must be logged to npm as "pepe" to execute this release step.' );
4545
} );
46-
} )
47-
;
46+
47+
it( 'should verify the npm account when the `useOidc` option is disabled explicitly', async () => {
48+
vi.mocked( tools ).shExec.mockResolvedValue( 'pepe' );
49+
50+
await assertNpmAuthorization( 'pepe', { useOidc: false } );
51+
52+
expect( vi.mocked( tools ).shExec ).toHaveBeenCalledExactlyOnceWith( 'npm whoami', expect.any( Object ) );
53+
} );
54+
55+
describe( 'npm Trusted Publishing (`useOidc=true`)', () => {
56+
it( 'should not call `npm whoami` when the "NPM_ID_TOKEN" environment variable is set', async () => {
57+
vi.stubEnv( 'NPM_ID_TOKEN', 'oidc-token' );
58+
59+
await assertNpmAuthorization( undefined, { useOidc: true } );
60+
61+
expect( vi.mocked( tools ).shExec ).not.toHaveBeenCalled();
62+
} );
63+
64+
it( 'should throw when the "NPM_ID_TOKEN" environment variable is not set', async () => {
65+
vi.stubEnv( 'NPM_ID_TOKEN', undefined );
66+
67+
await expect( assertNpmAuthorization( undefined, { useOidc: true } ) )
68+
.rejects.toThrow( 'The "NPM_ID_TOKEN" environment variable is required when using npm Trusted Publishing (OIDC).' );
69+
70+
expect( vi.mocked( tools ).shExec ).not.toHaveBeenCalled();
71+
} );
72+
73+
it( 'should throw when the "NPM_ID_TOKEN" environment variable is empty', async () => {
74+
vi.stubEnv( 'NPM_ID_TOKEN', '' );
75+
76+
await expect( assertNpmAuthorization( undefined, { useOidc: true } ) )
77+
.rejects.toThrow( 'The "NPM_ID_TOKEN" environment variable is required when using npm Trusted Publishing (OIDC).' );
78+
79+
expect( vi.mocked( tools ).shExec ).not.toHaveBeenCalled();
80+
} );
81+
82+
it( 'should ignore the provided npm account name', async () => {
83+
vi.stubEnv( 'NPM_ID_TOKEN', 'oidc-token' );
84+
85+
await assertNpmAuthorization( 'pepe', { useOidc: true } );
86+
87+
expect( vi.mocked( tools ).shExec ).not.toHaveBeenCalled();
88+
} );
89+
} );
90+
} );

0 commit comments

Comments
 (0)