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 10 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
42 changes: 42 additions & 0 deletions Tasks/KubeloginInstallerV0/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ export async function getLatestVersionTag(): Promise<string> {
request.headers = request.headers || {};
request.headers['User-Agent'] = userAgent;

if (taskLib.getBoolFeatureFlag('USE_AUTHORIZATION_FOR_API_CALL')){
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 +90,12 @@ export async function getKubeloginRelease(version: string = 'latest', platform?:
request.method = 'GET';
request.headers = request.headers || {};
request.headers['User-Agent'] = userAgent;
if (taskLib.getBoolFeatureFlag('USE_AUTHORIZATION_FOR_API_CALL')){
const token = getGithubEndPointToken();
if (token) {
request.headers['Authorization'] = 'token ' + token;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's extract explicit utility method to reduce code duplication

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented the utlity code

}
}

const response = await webClient.sendRequest(request);

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

function getGithubEndPointToken(): string {
try{
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));
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's reduce code nesting by inverting if (!!githubEndpointObject) {

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used switch block


return githubEndpointToken;
}
catch(error)
{
throw new Error(taskLib.loc("Failed to retrieve GitHub endpoint token:", error.message));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's create less generic exception type here.

}
}

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
Loading