Skip to content

Commit

Permalink
Updating ability to ignore the environment variable for a proxy for e…
Browse files Browse the repository at this point in the history
…dge cases
  • Loading branch information
peter-murray authored Oct 14, 2024
1 parent 0b50fc9 commit 68e8b5e
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 24 deletions.
2 changes: 1 addition & 1 deletion dist/main/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/main/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/post/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/post/index.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/actions/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ async function run() {
, applicationId = getRequiredInputValue('application_id')
, githubApiBaseUrl = core.getInput('github_api_base_url')
, httpsProxy = core.getInput('https_proxy')
, ignoreProxy = core.getBooleanInput('ignore_environment_proxy')
;
app = await createApplication(privateKey, applicationId, githubApiBaseUrl, undefined, httpsProxy);
app = await createApplication(privateKey, applicationId, githubApiBaseUrl, undefined, httpsProxy, ignoreProxy);
} catch(err) {
fail(err, 'Failed to initialize GitHub Application connection using provided id and private key');
return;
Expand Down
5 changes: 3 additions & 2 deletions src/actions/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ async function revokeToken() {

const baseUrl = core.getInput('github_api_base_url');
const proxy = core.getInput('https_proxy');
const ignoreProxy = core.getBooleanInput('ignore_environment_proxy');

const revoked = await revokeAccessToken(token, baseUrl, proxy);
const revoked = await revokeAccessToken(token, baseUrl, proxy, ignoreProxy);
if (revoked) {
core.info(` token has been revoked.`)
core.info(` token has been revoked.`);
} else {
throw new Error('Failed to revoke the application token, see logs for more information.');
}
Expand Down
47 changes: 30 additions & 17 deletions src/github-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@ import * as github from '@actions/github';
import * as jwt from 'jsonwebtoken';
import { PrivateKey } from './private-key.js';

export async function createApplication (privateKey: string, applicationId: string, baseApiUrl?: string, timeout?: number, proxy?: string): Promise<GitHubApplication> {
export async function createApplication (
privateKey: string,
applicationId: string,
baseApiUrl?: string,
timeout?: number,
proxy?: string,
ignoreEnvironmentProxy: boolean = false
): Promise<GitHubApplication> {
const app = new GitHubApplication(privateKey, applicationId, baseApiUrl);
await app.connect(timeout, proxy);
await app.connect(timeout, proxy, ignoreEnvironmentProxy);
return app;
}

export async function revokeAccessToken(token: string, baseUrl?: string, proxy?: string) {
export async function revokeAccessToken(token: string, baseUrl?: string, proxy?: string, ignoreEnvironmentProxy: boolean = false) {
// The token being provided is the one to be invalidated
const client = getOctokit(token, baseUrl, proxy);
const client = getOctokit(token, baseUrl, proxy, ignoreEnvironmentProxy);

try {
const resp = await client.rest.apps.revokeInstallationAccessToken();
Expand Down Expand Up @@ -61,7 +68,7 @@ export class GitHubApplication {
this._githubApiUrl = baseApiUrl;
}

async connect(validSeconds: number = 60, proxy?: string): Promise<GitHubApplicationMetadata> {
async connect(validSeconds: number = 60, proxy?: string, ignoreEnvironmentProxy: boolean = false): Promise<GitHubApplicationMetadata> {
const self = this
, secondsNow = Math.floor(Date.now() / 1000)
, expireInSeconds = validSeconds
Expand All @@ -74,7 +81,7 @@ export class GitHubApplication {
};

const token = jwt.sign(payload, this.privateKey, { algorithm: 'RS256' });
this._client = getOctokit(token, this._githubApiUrl, proxy);
this._client = getOctokit(token, this._githubApiUrl, proxy, ignoreEnvironmentProxy);

core.debug(`Attempting to fetch GitHub Application for the provided credentials...`);
try {
Expand Down Expand Up @@ -209,13 +216,13 @@ export class GitHubApplication {
}
}

function getOctokit(token: string, baseApiUrl?: string, proxy?: string, noProxy?: string) {
function getOctokit(token: string, baseApiUrl?: string, proxy?: string, ignoreEnvironmentProxy?: boolean) {
const baseUrl = getApiBaseUrl(baseApiUrl);

const octokitOptions = {
baseUrl: baseUrl,
request: {
agent: getProxyAgent(baseUrl, proxy, noProxy),
agent: getProxyAgent(baseUrl, proxy, ignoreEnvironmentProxy),
timeout: 5000
},
};
Expand All @@ -236,12 +243,12 @@ function _validateVariableValue(variableName: string, value?: string) {
return result;
}

function getProxyAgent(baseUrl: string, proxy?: string, noProxy?: string) {
function getProxyAgent(baseUrl: string, proxy?: string, ignoreEnvironmentProxy?: boolean) {
if (proxy) {
// User has an explict proxy set, use it
core.info(`explicit proxy specified as '${proxy}'`);

//TODO check fof explict exclusion on no_proxy
//TODO check for explict exclusion on no_proxy?
return new HttpsProxyAgent(proxy);
} else {
// When loading from the environment, also respect no_proxy settings
Expand All @@ -254,14 +261,20 @@ function getProxyAgent(baseUrl: string, proxy?: string, noProxy?: string) {
if (envProxy) {
core.info(`environment proxy specified as '${envProxy}'`);

const noProxy = process.env.no_proxy || process.env.NO_PROXY;
if (noProxy) {
core.info(`environment no_proxy set as '${noProxy}'`);
if (proxyExcluded(noProxy, baseUrl)) {
core.info(`environment proxy excluded from no_proxy settings`);
} else {
core.info(`using proxy '${envProxy}' for GitHub API calls`)
if (ignoreEnvironmentProxy) {
core.info(`Action has been configured to ignore environment proxy set. Not using the proxy from the environment and going direct for GitHub API calls...`);
} else {
const noProxy = process.env.no_proxy || process.env.NO_PROXY;
if (!noProxy) {
return new HttpsProxyAgent(envProxy);
} else {
core.info(`environment no_proxy set as '${noProxy}'`);
if (proxyExcluded(noProxy, baseUrl)) {
core.info(`environment proxy excluded from no_proxy settings`);
} else {
core.info(`using proxy '${envProxy}' for GitHub API calls`)
return new HttpsProxyAgent(envProxy);
}
}
}
}
Expand Down

0 comments on commit 68e8b5e

Please sign in to comment.