Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/jfrog/setup-jfrog-cli int…
Browse files Browse the repository at this point in the history
…o reduce_timeout
  • Loading branch information
EyalDelarea committed Dec 26, 2024
2 parents e6e125c + ac9f827 commit 3be68c0
Show file tree
Hide file tree
Showing 48 changed files with 17,919 additions and 32 deletions.
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ author: "JFrog"
inputs:
version:
description: "JFrog CLI Version"
default: "2.72.2"
default: "2.72.4"
required: false
download-repository:
description: "Remote repository in Artifactory pointing to 'https://releases.jfrog.io/artifactory/jfrog-cli'. Use this parameter in case you don't have an Internet access."
Expand Down
132 changes: 120 additions & 12 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const core_1 = require("@octokit/core");
const github = __importStar(require("@actions/github"));
const zlib_1 = require("zlib");
const util_1 = require("util");
const js_yaml_1 = require("js-yaml");
class Utils {
/**
* Retrieves server credentials for accessing JFrog's server
Expand All @@ -58,6 +59,9 @@ class Utils {
let jfrogCredentials = this.collectJfrogCredentialsFromEnvVars();
const oidcProviderName = core.getInput(Utils.OIDC_INTEGRATION_PROVIDER_NAME);
if (!oidcProviderName) {
// Set environment variable to track OIDC usage.
core.exportVariable('JFROG_CLI_USAGE_CONFIG_OIDC', '');
core.exportVariable('JFROG_CLI_USAGE_OIDC_USED', 'FALSE');
// Use JF_ENV or the credentials found in the environment variables
return jfrogCredentials;
}
Expand All @@ -74,17 +78,78 @@ class Utils {
catch (error) {
throw new Error(`Getting openID Connect JSON web token failed: ${error.message}`);
}
const applicationKey = yield this.getApplicationKey();
try {
jfrogCredentials = yield this.getJfrogAccessTokenThroughOidcProtocol(jfrogCredentials, jsonWebToken, oidcProviderName);
// Set environment variable to track OIDC logins in the usage report.
jfrogCredentials = yield this.getJfrogAccessTokenThroughOidcProtocol(jfrogCredentials, jsonWebToken, oidcProviderName, applicationKey);
// Set environment variable to track OIDC usage.
core.exportVariable('JFROG_CLI_USAGE_CONFIG_OIDC', 'TRUE');
core.exportVariable('JFROG_CLI_USAGE_OIDC_USED', 'TRUE');
return jfrogCredentials;
}
catch (error) {
throw new Error(`Exchanging JSON web token with an access token failed: ${error.message}`);
}
});
}
/**
* Retrieves the application key from .jfrog/config file.
*
* This method attempts to read config file from the file system.
* If the configuration file exists and contains the application key, it returns the key.
* If the configuration file does not exist or does not contain the application key, it returns an empty string.
*
* @returns A promise that resolves to the application key as a string.
*/
static getApplicationKey() {
return __awaiter(this, void 0, void 0, function* () {
const configFilePath = path.join(this.JF_CONFIG_DIR_NAME, this.JF_CONFIG_FILE_NAME);
try {
const config = yield this.readConfigFromFileSystem(configFilePath);
if (!config) {
console.debug('Config file is empty or not found.');
return '';
}
const configObj = (0, js_yaml_1.load)(config);
const application = configObj[this.APPLICATION_ROOT_YML];
if (!application) {
console.log('Application root is not found in the config file.');
return '';
}
const applicationKey = application[this.KEY];
if (!applicationKey) {
console.log('Application key is not found in the config file.');
return '';
}
console.debug('Found application key: ' + applicationKey);
return applicationKey;
}
catch (error) {
console.error('Error reading config:', error);
return '';
}
});
}
/**
* Reads .jfrog configuration file from file system.
*
* This method attempts to read .jfrog configuration file from the specified relative path.
* If the file exists, it reads the file content and returns it as a string.
* If the file does not exist, it returns an empty string.
*
* @param configRelativePath - The relative path to the configuration file.
* @returns A promise that resolves to the content of the configuration file as a string.
*/
static readConfigFromFileSystem(configRelativePath) {
return __awaiter(this, void 0, void 0, function* () {
core.debug(`Reading config from file system. Looking for ${configRelativePath}`);
if (!(0, fs_1.existsSync)(configRelativePath)) {
core.debug(`config.yml not found in ${configRelativePath}`);
return '';
}
core.debug(`config.yml found in ${configRelativePath}`);
return yield fs_1.promises.readFile(configRelativePath, 'utf-8');
});
}
/**
* Gathers JFrog's credentials from environment variables and delivers them in a JfrogCredentials structure
* @returns JfrogCredentials struct with all credentials found in environment variables
Expand Down Expand Up @@ -117,9 +182,10 @@ class Utils {
* @param jfrogCredentials existing JFrog credentials - url, access token, username + password
* @param jsonWebToken JWT achieved from GitHub JWT provider
* @param oidcProviderName OIDC provider name
* @param applicationKey
* @returns an access token for the requested Artifactory server
*/
static getJfrogAccessTokenThroughOidcProtocol(jfrogCredentials, jsonWebToken, oidcProviderName) {
static getJfrogAccessTokenThroughOidcProtocol(jfrogCredentials, jsonWebToken, oidcProviderName, applicationKey) {
return __awaiter(this, void 0, void 0, function* () {
// If we've reached this stage, the jfrogCredentials.jfrogUrl field should hold a non-empty value obtained from process.env.JF_URL
const exchangeUrl = jfrogCredentials.jfrogUrl.replace(/\/$/, '') + '/access/api/v1/oidc/token';
Expand All @@ -135,7 +201,8 @@ class Utils {
"provider_name": "${oidcProviderName}",
"project_key": "${projectKey}",
"gh_job_id": "${jobId}",
"gh_run_id": "${runId}"
"gh_run_id": "${runId}",
"application_key": "${applicationKey}"
}`;
const additionalHeaders = {
'Content-Type': 'application/json',
Expand Down Expand Up @@ -340,14 +407,18 @@ class Utils {
return serverId;
}
/**
* Return custom server ID if provided, or default server ID otherwise.
* Returns the custom server ID if provided, otherwise returns the default server ID.
*/
static getCustomOrDefaultServerId() {
const customServerId = this.getInputtedCustomId();
return customServerId || this.getRunDefaultServerId();
}
static getInputtedCustomId() {
let customServerId = core.getInput(Utils.CUSTOM_SERVER_ID);
if (customServerId) {
return customServerId;
}
return Utils.getRunDefaultServerId();
return undefined;
}
/**
* Return the default server ID for JFrog CLI server configuration.
Expand Down Expand Up @@ -378,6 +449,19 @@ class Utils {
if (!core.getBooleanInput(Utils.JOB_SUMMARY_DISABLE)) {
Utils.enableJobSummaries();
}
Utils.setUsageEnvVars();
}
// Set usage variables to be captured by JFrog CLI visibility metric service.
static setUsageEnvVars() {
var _a, _b, _c;
// Set the GitHub repository name or default to an empty string.
core.exportVariable('JFROG_CLI_USAGE_GIT_REPO', (_a = process.env.GITHUB_REPOSITORY) !== null && _a !== void 0 ? _a : '');
// Set the GitHub workflow name or default to an empty string.
core.exportVariable('JFROG_CLI_USAGE_JOB_ID', (_b = process.env.GITHUB_WORKFLOW) !== null && _b !== void 0 ? _b : '');
// Set the GitHub run ID or default to an empty string.
core.exportVariable('JFROG_CLI_USAGE_RUN_ID', (_c = process.env.GITHUB_RUN_ID) !== null && _c !== void 0 ? _c : '');
// Indicate if JF_GIT_TOKEN is provided as an environment variable.
core.exportVariable('JFROG_CLI_USAGE_GH_TOKEN_FOR_CODE_SCANNING_ALERTS_PROVIDED', !!process.env.JF_GIT_TOKEN);
}
/**
* Enabling job summary is done by setting the output dir for the summaries.
Expand Down Expand Up @@ -407,15 +491,26 @@ class Utils {
});
}
/**
* Removed configured JFrog CLI servers that are saved in the servers env var, and unset the env var.
* Removes configured JFrog CLI servers saved in the environment variable.
* If a custom server ID is defined, only remove the custom server ID.
*/
static removeJFrogServers() {
return __awaiter(this, void 0, void 0, function* () {
for (const serverId of Utils.getConfiguredJFrogServers()) {
core.debug(`Removing server ID: '${serverId}'...`);
yield Utils.runCli(['c', 'rm', serverId, '--quiet']);
const customServerId = this.getInputtedCustomId();
core.info(`The value of custom is: '${customServerId}'`);
if (customServerId) {
// Remove only the custom server ID
core.debug(`Removing custom server ID: '${customServerId}'...`);
yield Utils.runCli(['c', 'rm', customServerId, '--quiet']);
}
else {
// Remove all configured server IDs
for (const serverId of Utils.getConfiguredJFrogServers()) {
core.debug(`Removing server ID: '${serverId}'...`);
yield Utils.runCli(['c', 'rm', serverId, '--quiet']);
}
core.exportVariable(Utils.JFROG_CLI_SERVER_IDS_ENV_VAR, '');
}
core.exportVariable(Utils.JFROG_CLI_SERVER_IDS_ENV_VAR, '');
});
}
/**
Expand Down Expand Up @@ -726,7 +821,10 @@ class Utils {
return Utils.getMarkdownHeader() + fileContent + Utils.getMarkdownFooter();
}
static getMarkdownFooter() {
return '\n\n # \n\n The above Job Summary was generated by the <a href="https://github.com/marketplace/actions/setup-jfrog-cli"> Setup JFrog CLI GitHub Action </a>';
return `\n\n # \n\n ${this.getUsageBadge()} \n\n The above Job Summary was generated by the <a href="https://github.com/marketplace/actions/setup-jfrog-cli"> Setup JFrog CLI GitHub Action </a>`;
}
static getUsageBadge() {
return `![](${process.env.JF_URL}/ui/api/v1/u?s=1&m=1&job_id=${process.env.GITHUB_JOB}&run_id=${process.env.GITHUB_RUN_ID}&git_repo=${process.env.GITHUB_REPOSITORY})`;
}
/**
* Checks if the header image is accessible via the internet.
Expand Down Expand Up @@ -810,6 +908,16 @@ Utils.CLI_REMOTE_ARG = 'download-repository';
Utils.OIDC_AUDIENCE_ARG = 'oidc-audience';
// OpenID Connect provider_name input
Utils.OIDC_INTEGRATION_PROVIDER_NAME = 'oidc-provider-name';
// Application yaml root key
Utils.APPLICATION_ROOT_YML = 'application';
// Application Config file key, yaml should look like:
// application:
// key: <application key>
Utils.KEY = 'key';
// Config file directory name
Utils.JF_CONFIG_DIR_NAME = '.jfrog';
// Config file name
Utils.JF_CONFIG_FILE_NAME = 'config.yml';
// Disable Job Summaries feature flag
Utils.JOB_SUMMARY_DISABLE = 'disable-job-summary';
// Disable auto build info publish feature flag
Expand Down
1 change: 1 addition & 0 deletions node_modules/.bin/js-yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion node_modules/.package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3be68c0

Please sign in to comment.