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

Adding Authorization for Github api call In KubeLoginInstallerV0 #20725

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
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
71 changes: 39 additions & 32 deletions Tasks/KubeloginInstallerV0/package-lock.json

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

10 changes: 9 additions & 1 deletion Tasks/KubeloginInstallerV0/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"author": "Microsoft Corporation",
"version": {
"Major": 0,
"Minor": 247,
"Minor": 250,
"Patch": 0
},
"demands": [],
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
10 changes: 9 additions & 1 deletion Tasks/KubeloginInstallerV0/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"author": "Microsoft Corporation",
"version": {
"Major": 0,
"Minor": 247,
"Minor": 250,
"Patch": 0
},
"demands": [],
Expand All @@ -30,6 +30,14 @@
"label": "ms-resource:loc.input.label.kubeloginVersion",
"defaultValue": "latest",
"helpMarkDown": "ms-resource:loc.input.help.kubeloginVersion"
},
{
"name": "gitHubConnection",
"type": "connectedService:github:OAuth,OAuth2,PersonalAccessToken,InstallationToken,Token",
"label": "ms-resource:loc.input.label.gitHubConnection",
"defaultValue": "",
"required": false,
"helpMarkDown": "ms-resource:loc.input.help.gitHubConnection"
}
],
"execution": {
Expand Down
39 changes: 39 additions & 0 deletions Tasks/KubeloginInstallerV0/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,23 @@ export function isLatestVersion(version: string): boolean {
return v === 'latest' || v === '*' || v === '';
}

function addAuthorizationHeaderIfEnabled(request: webClient.WebRequest): void {
if (taskLib.getBoolFeatureFlag('USE_AUTHORIZATION_FOR_API_CALL')) {
const token = getGithubEndPointToken();
if (token) {
request.headers['Authorization'] = 'token ' + token;
}
}
}

export async function getLatestVersionTag(): Promise<string> {
let request = new webClient.WebRequest();
request.uri = 'https://api.github.com/repos/' + KUBELOGIN_REPO_OWNER + '/' + KUBELOGIN_REPO + '/releases/latest';
request.method = 'GET';
request.headers = request.headers || {};
request.headers['User-Agent'] = userAgent;

addAuthorizationHeaderIfEnabled(request);
const response = await webClient.sendRequest(request);
return response.body['tag_name'];
}
Expand Down Expand Up @@ -85,6 +95,7 @@ export async function getKubeloginRelease(version: string = 'latest', platform?:
request.headers = request.headers || {};
request.headers['User-Agent'] = userAgent;

addAuthorizationHeaderIfEnabled(request);
const response = await webClient.sendRequest(request);

const releaseUrl: string =
Expand Down Expand Up @@ -134,6 +145,34 @@ 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) {
throw new Error(taskLib.loc("Failed to retrieve GitHub endpoint object."));
}
taskLib.debug("Endpoint scheme: " + githubEndpointObject.scheme);

switch (githubEndpointObject.scheme) {
case 'PersonalAccessToken':
githubEndpointToken = githubEndpointObject.parameters.accessToken;
break;
case 'OAuth':
githubEndpointToken = githubEndpointObject.parameters.accessToken;
break;
case 'Token':
githubEndpointToken = githubEndpointObject.parameters.accessToken;
break;
default:
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
4 changes: 2 additions & 2 deletions _generated/KubeloginInstallerV0.versionmap.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Default|0.247.0
Node20_229_3|0.247.1
Default|0.250.0
Node20_229_3|0.250.1
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"loc.instanceNameFormat": "Install Kubelogin $(kubeloginVersion)",
"loc.input.label.kubeloginVersion": "kubelogin version",
"loc.input.help.kubeloginVersion": "The version of kubelogin to use",
"loc.input.label.gitHubConnection": "GitHub Connection",
"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.",
"loc.messages.Info_VerifyKubeloginInstallation": "Verifying kubelogin installation...",
"loc.messages.Info_ResolvedToolFromCache": "Resolved from tool cache: %s",
"loc.messages.Info_UsingToolPath": "Using tool path: %s",
Expand Down
71 changes: 39 additions & 32 deletions _generated/KubeloginInstallerV0/package-lock.json

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

14 changes: 11 additions & 3 deletions _generated/KubeloginInstallerV0/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"author": "Microsoft Corporation",
"version": {
"Major": 0,
"Minor": 247,
"Minor": 250,
"Patch": 0
},
"demands": [],
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 @@ -59,7 +67,7 @@
"Info_KubeloginDownloading": "Downloading kubelogin"
},
"_buildConfigMapping": {
"Default": "0.247.0",
"Node20_229_3": "0.247.1"
"Default": "0.250.0",
"Node20_229_3": "0.250.1"
}
}
14 changes: 11 additions & 3 deletions _generated/KubeloginInstallerV0/task.loc.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"author": "Microsoft Corporation",
"version": {
"Major": 0,
"Minor": 247,
"Minor": 250,
"Patch": 0
},
"demands": [],
Expand All @@ -30,6 +30,14 @@
"label": "ms-resource:loc.input.label.kubeloginVersion",
"defaultValue": "latest",
"helpMarkDown": "ms-resource:loc.input.help.kubeloginVersion"
},
{
"name": "gitHubConnection",
"type": "connectedService:github:OAuth,OAuth2,PersonalAccessToken,InstallationToken,Token",
"label": "ms-resource:loc.input.label.gitHubConnection",
"defaultValue": "",
"required": false,
"helpMarkDown": "ms-resource:loc.input.help.gitHubConnection"
}
],
"execution": {
Expand Down Expand Up @@ -59,7 +67,7 @@
"Info_KubeloginDownloading": "ms-resource:loc.messages.Info_KubeloginDownloading"
},
"_buildConfigMapping": {
"Default": "0.247.0",
"Node20_229_3": "0.247.1"
"Default": "0.250.0",
"Node20_229_3": "0.250.1"
}
}
Loading