Skip to content

Commit

Permalink
Support application key
Browse files Browse the repository at this point in the history
  • Loading branch information
Sahar Bracha authored and SaharBracha committed Dec 12, 2024
1 parent 9615397 commit 8c4054f
Show file tree
Hide file tree
Showing 240 changed files with 18,834 additions and 7 deletions.
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ inputs:
custom-server-id:
description: "Custom JFrog CLI configuration server ID to use instead of the default one generated by the action."
required: false
application-key:
description: "Application key to associate packages with."
required: false
outputs:
oidc-token:
description: "JFrog OIDC token generated by the Setup JFrog CLI when setting oidc-provider-name."
Expand Down
75 changes: 72 additions & 3 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 YAML = __importStar(require("yaml"));
class Utils {
/**
* Retrieves server credentials for accessing JFrog's server
Expand Down Expand Up @@ -74,8 +75,9 @@ 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);
jfrogCredentials = yield this.getJfrogAccessTokenThroughOidcProtocol(jfrogCredentials, jsonWebToken, oidcProviderName, applicationKey);
// Set environment variable to track OIDC logins in the usage report.
core.exportVariable('JFROG_CLI_USAGE_CONFIG_OIDC', 'TRUE');
return jfrogCredentials;
Expand All @@ -85,6 +87,65 @@ class Utils {
}
});
}
/**
* Retrieves the application key from input or .jfrog configuration file.
*
* This method attempts to retrieve the application key from the GitHub Action input.
* If the input is not provided, it reads .jfrog configuration 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 appKey = core.getInput(Utils.APPLICATION_KEY);
if (appKey) {
return appKey;
}
const configFilePath = path.join(this.JFROG_CONFIG_DIR_NAME, this.JFROG_CONFIG_FILE);
try {
const config = yield this.readConfigFromFileSystem(configFilePath);
if (!config) {
console.log('Config file is empty or not found.');
return '';
}
const configObj = YAML.parse(config);
const applicationKey = configObj['application-key'];
if (!applicationKey) {
console.log('Application key is not found in the config file.');
return '';
}
console.log('Found application key: ' + applicationKey);
return applicationKey;
}
catch (error) {
console.log('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 +178,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 +197,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 @@ -798,6 +861,12 @@ 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 id to associate entities
Utils.APPLICATION_KEY = 'application-key';
// Config file directory name
Utils.JFROG_CONFIG_DIR_NAME = '.jfrog';
// Config file name
Utils.JFROG_CONFIG_FILE = '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/yaml

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

12 changes: 12 additions & 0 deletions node_modules/.package-lock.json

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

13 changes: 13 additions & 0 deletions node_modules/yaml/LICENSE

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

169 changes: 169 additions & 0 deletions node_modules/yaml/README.md

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

11 changes: 11 additions & 0 deletions node_modules/yaml/bin.mjs

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

Loading

0 comments on commit 8c4054f

Please sign in to comment.