Skip to content

Commit bcecd91

Browse files
committed
Handle OIDC & Repository feature and add tests
1 parent 2afab71 commit bcecd91

File tree

3 files changed

+119
-9
lines changed

3 files changed

+119
-9
lines changed

lib/utils.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,26 @@ class Utils {
112112
if (jfrogCredentials.password) {
113113
core.setSecret(jfrogCredentials.password);
114114
}
115+
Utils.validateOidcSupported(jfrogCredentials);
115116
return jfrogCredentials;
116117
}
118+
/**
119+
* Validates OIDC auth method is supported by the JFrog CLI version
120+
* @param jfrogCredentials
121+
*/
122+
static validateOidcSupported(jfrogCredentials) {
123+
const version = core.getInput(Utils.CLI_VERSION_ARG);
124+
const downloadRepository = core.getInput(Utils.CLI_REMOTE_ARG);
125+
// Cannot download from repository while using OIDC, as we don't have the credentials yet.
126+
if (!!downloadRepository && !jfrogCredentials.oidcProviderName) {
127+
throw new Error(`Download repository feature is not supported while using OIDC.`);
128+
}
129+
if (!!version) {
130+
if (jfrogCredentials.oidcProviderName && (0, semver_1.lt)(version, Utils.MIN_OIDC_SUPPORTED_VERSION)) {
131+
throw new Error(`OIDC provider is specified, but the JFrog CLI version ${version} does not support OIDC. Minimum required version is ${Utils.MIN_OIDC_SUPPORTED_VERSION}.\n Please update your JFrog CLI version or downgrade the setup-jfrog-cli action to v4.5.6.`);
132+
}
133+
}
134+
}
117135
static getAndAddCliToPath(jfrogCredentials) {
118136
return __awaiter(this, void 0, void 0, function* () {
119137
let version = core.getInput(Utils.CLI_VERSION_ARG);
@@ -772,6 +790,8 @@ Utils.JFROG_CLI_COMMAND_SUMMARY_OUTPUT_DIR_ENV = 'JFROG_CLI_COMMAND_SUMMARY_OUTP
772790
Utils.MIN_CLI_VERSION_JOB_SUMMARY = '2.66.0';
773791
// Code scanning sarif expected file extension.
774792
Utils.CODE_SCANNING_FINAL_SARIF_FILE = 'final.sarif';
793+
// Version which OIDC was introduced to the CLI
794+
Utils.MIN_OIDC_SUPPORTED_VERSION = '2.75.0';
775795
// Inputs
776796
// Version input
777797
Utils.CLI_VERSION_ARG = 'version';

src/utils.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ export class Utils {
4949
private static readonly MIN_CLI_VERSION_JOB_SUMMARY: string = '2.66.0';
5050
// Code scanning sarif expected file extension.
5151
private static readonly CODE_SCANNING_FINAL_SARIF_FILE: string = 'final.sarif';
52+
// Version which OIDC was introduced to the CLI
53+
public static readonly MIN_OIDC_SUPPORTED_VERSION: string = '2.75.0';
5254

5355
// Inputs
5456
// Version input
@@ -163,9 +165,33 @@ export class Utils {
163165
if (jfrogCredentials.password) {
164166
core.setSecret(jfrogCredentials.password);
165167
}
168+
169+
Utils.validateOidcSupported(jfrogCredentials);
170+
166171
return jfrogCredentials;
167172
}
168173

174+
/**
175+
* Validates OIDC auth method is supported by the JFrog CLI version
176+
* @param jfrogCredentials
177+
*/
178+
public static validateOidcSupported(jfrogCredentials: JfrogCredentials) {
179+
const version: string = core.getInput(Utils.CLI_VERSION_ARG);
180+
const downloadRepository: string = core.getInput(Utils.CLI_REMOTE_ARG);
181+
182+
// Cannot download from repository while using OIDC, as we don't have the credentials yet.
183+
if (!!downloadRepository && !jfrogCredentials.oidcProviderName) {
184+
throw new Error(`Download repository feature is not supported while using OIDC.`);
185+
}
186+
if (!!version) {
187+
if (jfrogCredentials.oidcProviderName && lt(version, Utils.MIN_OIDC_SUPPORTED_VERSION)) {
188+
throw new Error(
189+
`OIDC provider is specified, but the JFrog CLI version ${version} does not support OIDC. Minimum required version is ${Utils.MIN_OIDC_SUPPORTED_VERSION}.\n Please update your JFrog CLI version or downgrade the setup-jfrog-cli action to v4.5.6.`,
190+
);
191+
}
192+
}
193+
}
194+
169195
public static async getAndAddCliToPath(jfrogCredentials: JfrogCredentials) {
170196
let version: string = core.getInput(Utils.CLI_VERSION_ARG);
171197
let cliRemote: string = core.getInput(Utils.CLI_REMOTE_ARG);

test/main.spec.ts

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@ import * as os from 'os';
22
import * as core from '@actions/core';
33

44
import { DownloadDetails, JfrogCredentials, Utils } from '../src/utils';
5-
import * as jsYaml from 'js-yaml';
6-
import * as fs from 'fs';
7-
import * as path from 'path';
85
import semver = require('semver/preload');
96

107
jest.mock('os');
118
jest.mock('@actions/core');
12-
jest.mock('semver');
139
jest.mock('@actions/core');
1410
jest.mock('fs', () => ({
1511
promises: {
@@ -359,7 +355,6 @@ describe('Job Summaries', () => {
359355
});
360356

361357
describe('isJobSummarySupported', () => {
362-
const MIN_CLI_VERSION_JOB_SUMMARY: string = '2.66.0';
363358
const LATEST_CLI_VERSION: string = 'latest';
364359

365360
beforeEach(() => {
@@ -374,17 +369,13 @@ describe('isJobSummarySupported', () => {
374369
it('should return true if the version is greater than or equal to the minimum supported version', () => {
375370
const version: string = '2.66.0';
376371
jest.spyOn(core, 'getInput').mockReturnValue(version);
377-
(semver.gte as jest.Mock).mockReturnValue(true);
378372
expect(Utils.isJobSummarySupported()).toBe(true);
379-
expect(semver.gte).toHaveBeenCalledWith(version, MIN_CLI_VERSION_JOB_SUMMARY);
380373
});
381374

382375
it('should return false if the version is less than the minimum supported version', () => {
383376
const version: string = '2.65.0';
384377
jest.spyOn(core, 'getInput').mockReturnValue(version);
385-
(semver.gte as jest.Mock).mockReturnValue(false);
386378
expect(Utils.isJobSummarySupported()).toBe(false);
387-
expect(semver.gte).toHaveBeenCalledWith(version, MIN_CLI_VERSION_JOB_SUMMARY);
388379
});
389380
});
390381

@@ -511,3 +502,76 @@ describe('Utils.collectJfrogCredentialsFromEnvVars', () => {
511502
}).toThrow('JF_USER is configured, but the JF_PASSWORD or JF_ACCESS_TOKEN environment variables were not set.');
512503
});
513504
});
505+
506+
describe('Utils.validateOidcSupported', () => {
507+
// eslint-disable-next-line @typescript-eslint/typedef
508+
const runTest = (cliVersion: string, jfrogCredentials: JfrogCredentials, shouldThrow: boolean, errorMessage: string): void => {
509+
jest.spyOn(core, 'getInput').mockImplementation((name: string): string => {
510+
if (name === Utils.CLI_VERSION_ARG) {
511+
return cliVersion;
512+
}
513+
return '';
514+
});
515+
516+
if (shouldThrow) {
517+
expect(() => {
518+
Utils.validateOidcSupported(jfrogCredentials);
519+
}).toThrow(errorMessage);
520+
} else {
521+
expect(() => {
522+
Utils.validateOidcSupported(jfrogCredentials);
523+
}).not.toThrow();
524+
}
525+
};
526+
527+
it('should throw an error if OIDC provider is specified and version is below minimum supported', () => {
528+
const jfrogCredentials: JfrogCredentials = {
529+
jfrogUrl: 'https://example.jfrog.io',
530+
username: undefined,
531+
password: undefined,
532+
accessToken: undefined,
533+
oidcProviderName: 'github',
534+
oidcAudience: 'jfrog-github',
535+
oidcTokenId: undefined,
536+
};
537+
538+
const cliVersion: string = '2.0.0';
539+
const errorMessage: string = `OIDC provider is specified, but the JFrog CLI version ${cliVersion} does not support OIDC. Minimum required version is ${Utils.MIN_OIDC_SUPPORTED_VERSION}.`;
540+
541+
runTest(cliVersion, jfrogCredentials, true, errorMessage);
542+
});
543+
544+
it('should not throw an error if OIDC provider is specified and version is above minimum supported', () => {
545+
const jfrogCredentials: JfrogCredentials = {
546+
jfrogUrl: 'https://example.jfrog.io',
547+
username: undefined,
548+
password: undefined,
549+
accessToken: undefined,
550+
oidcProviderName: 'github',
551+
oidcAudience: 'jfrog-github',
552+
oidcTokenId: undefined,
553+
};
554+
555+
const cliVersion: string = '2.75.0';
556+
const errorMessage: string = `OIDC provider is specified, but the JFrog CLI version ${cliVersion} does not support OIDC. Minimum required version is ${Utils.MIN_OIDC_SUPPORTED_VERSION}.`;
557+
558+
runTest(cliVersion, jfrogCredentials, false, errorMessage);
559+
});
560+
561+
it('should not throw an error if OIDC provider is not specified', () => {
562+
const jfrogCredentials: JfrogCredentials = {
563+
jfrogUrl: 'https://example.jfrog.io',
564+
username: undefined,
565+
password: undefined,
566+
accessToken: undefined,
567+
oidcProviderName: undefined,
568+
oidcAudience: undefined,
569+
oidcTokenId: undefined,
570+
};
571+
572+
const cliVersion: string = '2.74.3';
573+
const errorMessage: string = `OIDC provider is specified, but the JFrog CLI version ${cliVersion} does not support OIDC. Minimum required version is ${Utils.MIN_OIDC_SUPPORTED_VERSION}.`;
574+
575+
runTest(cliVersion, jfrogCredentials, false, errorMessage);
576+
});
577+
});

0 commit comments

Comments
 (0)