Skip to content

Commit 39024db

Browse files
Adding Authorization for Github api call In KubeLoginInstallerV0 (#20725)
* Adding Authorization for Github api call In KubeLoginInstallerV0 * Changing minor version * changing task-loc.json * correcting FF name * changing task-loc.json * modifyng tasloc fle * modifying the logic * changes in generated folder * Addng Less genric error * bump tas verson * addng warnng * addng chld error class
1 parent 2236be5 commit 39024db

File tree

15 files changed

+324
-80
lines changed

15 files changed

+324
-80
lines changed

Tasks/KubeloginInstallerV0/package-lock.json

Lines changed: 39 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tasks/KubeloginInstallerV0/task.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"author": "Microsoft Corporation",
1616
"version": {
1717
"Major": 0,
18-
"Minor": 247,
18+
"Minor": 251,
1919
"Patch": 0
2020
},
2121
"demands": [],
@@ -30,6 +30,14 @@
3030
"label": "kubelogin version",
3131
"defaultValue": "latest",
3232
"helpMarkDown": "The version of kubelogin to use"
33+
},
34+
{
35+
"name": "gitHubConnection",
36+
"type": "connectedService:github:OAuth,OAuth2,PersonalAccessToken,InstallationToken,Token",
37+
"label": "GitHub Connection",
38+
"defaultValue": "",
39+
"required": false,
40+
"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."
3341
}
3442
],
3543
"execution": {

Tasks/KubeloginInstallerV0/task.loc.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"author": "Microsoft Corporation",
1616
"version": {
1717
"Major": 0,
18-
"Minor": 247,
18+
"Minor": 251,
1919
"Patch": 0
2020
},
2121
"demands": [],
@@ -30,6 +30,14 @@
3030
"label": "ms-resource:loc.input.label.kubeloginVersion",
3131
"defaultValue": "latest",
3232
"helpMarkDown": "ms-resource:loc.input.help.kubeloginVersion"
33+
},
34+
{
35+
"name": "gitHubConnection",
36+
"type": "connectedService:github:OAuth,OAuth2,PersonalAccessToken,InstallationToken,Token",
37+
"label": "ms-resource:loc.input.label.gitHubConnection",
38+
"defaultValue": "",
39+
"required": false,
40+
"helpMarkDown": "ms-resource:loc.input.help.gitHubConnection"
3341
}
3442
],
3543
"execution": {

Tasks/KubeloginInstallerV0/utils.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,26 @@ export function isLatestVersion(version: string): boolean {
4747
return v === 'latest' || v === '*' || v === '';
4848
}
4949

50+
function addAuthorizationHeaderIfEnabled(request: webClient.WebRequest): void {
51+
if (taskLib.getBoolFeatureFlag('USE_AUTHORIZATION_FOR_API_CALL')) {
52+
const token = getGithubEndPointToken();
53+
if (token) {
54+
request.headers['Authorization'] = 'token ' + token;
55+
}
56+
else {
57+
taskLib.warning('The GitHub token is empty. API calls may fail without proper authentication');
58+
}
59+
}
60+
}
61+
5062
export async function getLatestVersionTag(): Promise<string> {
5163
let request = new webClient.WebRequest();
5264
request.uri = 'https://api.github.com/repos/' + KUBELOGIN_REPO_OWNER + '/' + KUBELOGIN_REPO + '/releases/latest';
5365
request.method = 'GET';
5466
request.headers = request.headers || {};
5567
request.headers['User-Agent'] = userAgent;
5668

69+
addAuthorizationHeaderIfEnabled(request);
5770
const response = await webClient.sendRequest(request);
5871
return response.body['tag_name'];
5972
}
@@ -85,6 +98,7 @@ export async function getKubeloginRelease(version: string = 'latest', platform?:
8598
request.headers = request.headers || {};
8699
request.headers['User-Agent'] = userAgent;
87100

101+
addAuthorizationHeaderIfEnabled(request);
88102
const response = await webClient.sendRequest(request);
89103

90104
const releaseUrl: string =
@@ -134,6 +148,50 @@ export async function unzipRelease(zipPath: string): Promise<string> {
134148
}
135149
}
136150

151+
function getGithubEndPointToken(): string {
152+
const githubEndpoint = taskLib.getInput("gitHubConnection", false);
153+
const githubEndpointObject = taskLib.getEndpointAuthorization(githubEndpoint, true);
154+
let githubEndpointToken: string = null;
155+
156+
if (!githubEndpointObject) {
157+
throw new GitHubEndpointObjectError(taskLib.loc("Failed to retrieve GitHub endpoint object."));
158+
}
159+
taskLib.debug("Endpoint scheme: " + githubEndpointObject.scheme);
160+
161+
switch (githubEndpointObject.scheme) {
162+
case 'PersonalAccessToken':
163+
githubEndpointToken = githubEndpointObject.parameters.accessToken;
164+
break;
165+
case 'OAuth':
166+
githubEndpointToken = githubEndpointObject.parameters.accessToken;
167+
break;
168+
case 'Token':
169+
githubEndpointToken = githubEndpointObject.parameters.accessToken;
170+
break;
171+
default:
172+
throw new GitHubEndpointSchemeError(
173+
taskLib.loc("InvalidEndpointAuthScheme", githubEndpointObject.scheme)
174+
);
175+
}
176+
return githubEndpointToken;
177+
}
178+
179+
class GitHubEndpointObjectError extends Error {
180+
constructor(message: string) {
181+
super(message);
182+
this.name = "GitHubEndpointObjectError";
183+
Object.setPrototypeOf(this, GitHubEndpointObjectError.prototype);
184+
}
185+
}
186+
187+
class GitHubEndpointSchemeError extends Error {
188+
constructor(message: string) {
189+
super(message);
190+
this.name = "GitHubEndpointSchemeError";
191+
Object.setPrototypeOf(this, GitHubEndpointSchemeError.prototype);
192+
}
193+
}
194+
137195
export function getKubeloginPath(inputPath: string, fileName: string): string | undefined {
138196
const files: string[] = fs.readdirSync(inputPath);
139197
for (const file of files) {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
Default|0.247.0
2-
Node20_229_3|0.247.1
1+
Default|0.251.0
2+
Node20_229_3|0.251.1

_generated/KubeloginInstallerV0/Strings/resources.resjson/en-US/resources.resjson

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
"loc.instanceNameFormat": "Install Kubelogin $(kubeloginVersion)",
66
"loc.input.label.kubeloginVersion": "kubelogin version",
77
"loc.input.help.kubeloginVersion": "The version of kubelogin to use",
8+
"loc.input.label.gitHubConnection": "GitHub Connection",
9+
"loc.input.help.gitHubConnection": "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.",
810
"loc.messages.Info_VerifyKubeloginInstallation": "Verifying kubelogin installation...",
911
"loc.messages.Info_ResolvedToolFromCache": "Resolved from tool cache: %s",
1012
"loc.messages.Info_UsingToolPath": "Using tool path: %s",

_generated/KubeloginInstallerV0/package-lock.json

Lines changed: 39 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

_generated/KubeloginInstallerV0/task.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"author": "Microsoft Corporation",
1616
"version": {
1717
"Major": 0,
18-
"Minor": 247,
18+
"Minor": 251,
1919
"Patch": 0
2020
},
2121
"demands": [],
@@ -30,6 +30,14 @@
3030
"label": "kubelogin version",
3131
"defaultValue": "latest",
3232
"helpMarkDown": "The version of kubelogin to use"
33+
},
34+
{
35+
"name": "gitHubConnection",
36+
"type": "connectedService:github:OAuth,OAuth2,PersonalAccessToken,InstallationToken,Token",
37+
"label": "GitHub Connection",
38+
"defaultValue": "",
39+
"required": false,
40+
"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."
3341
}
3442
],
3543
"execution": {
@@ -59,7 +67,7 @@
5967
"Info_KubeloginDownloading": "Downloading kubelogin"
6068
},
6169
"_buildConfigMapping": {
62-
"Default": "0.247.0",
63-
"Node20_229_3": "0.247.1"
70+
"Default": "0.251.0",
71+
"Node20_229_3": "0.251.1"
6472
}
6573
}

0 commit comments

Comments
 (0)