Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new parameter to KubeloginInstallerV0 to authorize request to github to avoid request limits on unauthorized requests #20685

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions Tasks/KubeloginInstallerV0/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 0,
"Minor": 247,
"Patch": 0
"Minor": 249,
"Patch": 5
},
"demands": [],
"satisfies": [
Expand All @@ -30,6 +30,14 @@
"label": "kubelogin version",
"defaultValue": "latest",
"helpMarkDown": "The version of kubelogin to use"
},
{
"name": "gitHubConnection",
"type": "connectedService:github:OAuth,OAuth2,PersonalAccessToken,InstallationToken,Token",
"label": "GitHub Connection",
"defaultValue": "",
"required": false,
"helpMarkDown": "A GitHub connection is needed to prevent anonymous requests limits to the Github API for [Azure/kubelogin](https://github.com/azure/kubelogin) from impacting the installation. Leaving this empty may cause failures if the request limit is reached. This connection does not require ANY permissions."
}
],
"execution": {
Expand Down Expand Up @@ -58,4 +66,4 @@
"SucceedMsg": "Kubelogin has been successfully installed",
"Info_KubeloginDownloading": "Downloading kubelogin"
}
}
}
4 changes: 2 additions & 2 deletions Tasks/KubeloginInstallerV0/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"author": "Microsoft Corporation",
"version": {
"Major": 0,
"Minor": 247,
"Patch": 0
"Minor": 249,
"Patch": 5
},
"demands": [],
"satisfies": [
Expand Down
32 changes: 32 additions & 0 deletions Tasks/KubeloginInstallerV0/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export async function getLatestVersionTag(): Promise<string> {
request.method = 'GET';
request.headers = request.headers || {};
request.headers['User-Agent'] = userAgent;
const token = getGithubEndPointToken();
if (token) {
request.headers['Authorization'] = 'token ' + token;
}

const response = await webClient.sendRequest(request);
return response.body['tag_name'];
Expand Down Expand Up @@ -84,6 +88,10 @@ export async function getKubeloginRelease(version: string = 'latest', platform?:
request.method = 'GET';
request.headers = request.headers || {};
request.headers['User-Agent'] = userAgent;
const token = getGithubEndPointToken();
if (token) {
request.headers['Authorization'] = 'token ' + token;
}

const response = await webClient.sendRequest(request);

Expand Down Expand Up @@ -134,6 +142,30 @@ export async function unzipRelease(zipPath: string): Promise<string> {
}
}

function getGithubEndPointToken(): string {
const githubEndpoint = taskLib.getInput("gitHubConnection", false);
const githubEndpointObject = taskLib.getEndpointAuthorization(githubEndpoint, true);
let githubEndpointToken: string = null;

if (!!githubEndpointObject) {
taskLib.debug("Endpoint scheme: " + githubEndpointObject.scheme);

if (githubEndpointObject.scheme === 'PersonalAccessToken') {
githubEndpointToken = githubEndpointObject.parameters.accessToken
} else if (githubEndpointObject.scheme === 'OAuth'){
// scheme: 'OAuth'
githubEndpointToken = githubEndpointObject.parameters.AccessToken
} else if (githubEndpointObject.scheme === 'Token'){
// scheme: 'Token'
githubEndpointToken = githubEndpointObject.parameters.AccessToken
} else if (githubEndpointObject.scheme) {
throw new Error(taskLib.loc("InvalidEndpointAuthScheme", githubEndpointObject.scheme));
}
}

return githubEndpointToken;
}

export function getKubeloginPath(inputPath: string, fileName: string): string | undefined {
const files: string[] = fs.readdirSync(inputPath);
for (const file of files) {
Expand Down