-
Notifications
You must be signed in to change notification settings - Fork 16
Add authentication support for Linux #77
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "changes": [ | ||
| { | ||
| "type": "minor", | ||
| "comment": "Add authentication support for Linux", | ||
| "packageName": "ado-npm-auth", | ||
| "email": "dannyvv@microsoft.com", | ||
| "dependentChangeType": "patch" | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| dist | ||
| lib | ||
| lib | ||
| .bin |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,59 @@ | ||
| import { hostname } from "os"; | ||
| import { hostname, platform } from "os"; | ||
| import { AdoPatResponse, adoPat } from "../azureauth/ado.js"; | ||
| import { toBase64 } from "../utils/encoding.js"; | ||
| import { credentialProviderPat } from "./nugetCredentialProvider.js"; | ||
|
|
||
| /** | ||
| * Generates a valid ADO PAT, scoped for vso.packaging in the given ado organization, 30 minute timeout | ||
| * @returns { string } a valid ADO PAT | ||
| */ | ||
| export const generateNpmrcPat = async ( | ||
| organization: string, | ||
| feed: string, | ||
| encode = false, | ||
| azureAuthLocation?: string, | ||
| ): Promise<string> => { | ||
| const name = `${hostname()}-${organization}`; | ||
| const pat = await adoPat( | ||
| { | ||
| promptHint: `${name} .npmrc PAT`, | ||
| organization, | ||
| displayName: `${name}-npmrc-pat`, | ||
| scope: ["vso.packaging"], | ||
| timeout: "30", | ||
| output: "json", | ||
| }, | ||
| const rawToken = await getRawToken( | ||
| name, | ||
| organization, | ||
| feed, | ||
| azureAuthLocation, | ||
| ); | ||
|
|
||
| const rawToken = (pat as AdoPatResponse).token; | ||
|
|
||
| if (encode) { | ||
| return toBase64(rawToken); | ||
| } | ||
|
|
||
| return rawToken; | ||
| }; | ||
|
|
||
| async function getRawToken( | ||
| name: string, | ||
| organization: string, | ||
| feed: string, | ||
| azureAuthLocation?: string, | ||
| ): Promise<string> { | ||
| switch (platform()) { | ||
| case "win32": | ||
| case "darwin": | ||
| const pat = await adoPat( | ||
| { | ||
| promptHint: `Authenticate to ${organization} to generate a temporary token for npm`, | ||
| organization: `https://dev.azure.com/${organization}`, | ||
| displayName: name, | ||
| scope: ["vso.packaging"], | ||
| timeout: "30m", | ||
| }, | ||
| azureAuthLocation, | ||
| ); | ||
| return (pat as AdoPatResponse).token; | ||
| case "linux": | ||
| const cpPat = await credentialProviderPat(feed); | ||
| return cpPat.Password; | ||
| default: | ||
| throw new Error( | ||
| `Platform ${platform()} is not supported for ADO authentication`, | ||
| ); | ||
| } | ||
| } | ||
123 changes: 123 additions & 0 deletions
123
packages/ado-npm-auth/src/npmrc/nugetCredentialProvider.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| import os from "os"; | ||
| import fs from "fs"; | ||
| import path from "path"; | ||
| import { downloadFile } from "../utils/request.js"; | ||
| import { execProcess } from "../utils/exec.js"; | ||
|
|
||
| const CredentialProviderVersion = "1.4.1"; | ||
| const OutputDir = path.join( | ||
| __dirname, | ||
| "..", | ||
| ".bin", | ||
| "CredentialProvider.Microsoft", | ||
| "v" + CredentialProviderVersion, | ||
| ); | ||
|
|
||
| interface CredentialProviderResponse { | ||
| Username: string; | ||
| Password: string; | ||
| } | ||
|
|
||
| export async function credentialProviderPat( | ||
| registry: string, | ||
| ): Promise<CredentialProviderResponse> { | ||
| const nugetFeedUrl = toNugetUrl(registry); | ||
| const toolPath = await getCredentialProvider(); | ||
| return await invokeCredentialProvider(toolPath, nugetFeedUrl); | ||
| } | ||
|
|
||
| function toNugetUrl(registry: string): string { | ||
| if (!registry.endsWith("/npm/registry/")) { | ||
| throw new Error( | ||
| `Registry URL ${registry} is not a valid Azure Artifacts npm registry URL. Expected it to end with '/npm/registry/'`, | ||
| ); | ||
| } | ||
| return ( | ||
| "https://" + registry.replace("/npm/registry/", "/nuget/v3/index.json") | ||
| ); | ||
| } | ||
|
|
||
| async function invokeCredentialProvider( | ||
| toolPath: string, | ||
| nugetFeedUrl: string, | ||
| ): Promise<CredentialProviderResponse> { | ||
| let response = ""; | ||
| await execProcess(toolPath, ["-U", nugetFeedUrl, "-I", "-F", "Json"], { | ||
| stdio: "pipe", | ||
| processStdOut: (data: string) => { | ||
| response += data; | ||
| }, | ||
| processStdErr: (data: string) => { | ||
| console.error(data); | ||
| }, | ||
| }); | ||
| try { | ||
| let value = JSON.parse(response); | ||
| return value as CredentialProviderResponse; | ||
| } catch (error) { | ||
| throw new Error(`Failed to parse CredentialProvider output: ${response}`); | ||
| } | ||
| } | ||
|
|
||
| function tryFileExists(executable: string): string | undefined { | ||
| if (fs.existsSync(executable)) { | ||
| return executable; | ||
| } else if (fs.existsSync(executable + ".exe")) { | ||
| return executable + ".exe"; | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| async function getCredentialProvider(): Promise<string> { | ||
| let toolPath = tryFileExists( | ||
| path.join( | ||
| os.homedir(), | ||
| ".nuget", | ||
| "plugins", | ||
| "netcore", | ||
| "CredentialProvider.Microsoft", | ||
| "CredentialProvider.Microsoft", | ||
| ), | ||
| ); | ||
| if (toolPath) { | ||
| return toolPath; | ||
| } | ||
|
|
||
| const downloadedFilePath = path.join( | ||
| OutputDir, | ||
| "plugins", | ||
| "netcore", | ||
| "CredentialProvider.Microsoft", | ||
| "CredentialProvider.Microsoft", | ||
| ); | ||
| toolPath = tryFileExists(downloadedFilePath); | ||
| if (toolPath) { | ||
| return toolPath; | ||
| } | ||
|
|
||
| await downloadCredentialProvider(); | ||
| toolPath = tryFileExists(downloadedFilePath); | ||
| if (toolPath) { | ||
| fs.chmodSync(toolPath, 0o755); | ||
| } else { | ||
| throw new Error( | ||
| `CredentialProvider was not found at expected path after download: ${toolPath}`, | ||
| ); | ||
| } | ||
|
|
||
| return toolPath; | ||
| } | ||
|
|
||
| async function downloadCredentialProvider(): Promise<void> { | ||
| const downloadUrl = `https://github.com/microsoft/artifacts-credprovider/releases/download/v${CredentialProviderVersion}/Microsoft.Net8.${os.platform()}-${os.arch()}.NuGet.CredentialProvider.tar.gz`; | ||
| const downloadPath = path.join( | ||
| OutputDir, | ||
| "CredentialProvider.Microsoft.tar.gz", | ||
| ); | ||
|
|
||
| console.log(`🌐 Downloading ${downloadUrl}`); | ||
| await downloadFile(downloadUrl, downloadPath); | ||
| await execProcess("tar", ["-xzf", downloadPath, "-C", OutputDir], { | ||
| stdio: "inherit", | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,56 @@ | ||
| import { exec as _exec } from "node:child_process"; | ||
| import { exec as _exec, spawn } from "node:child_process"; | ||
| import { promisify } from "node:util"; | ||
|
|
||
| export const exec = promisify(_exec); | ||
|
|
||
| /** | ||
| * Executes a command as a child process. | ||
| * @param tool The command to run. | ||
| * @param args The arguments to pass to the command. | ||
| * @param options Options for the child process. | ||
| * @returns A promise that resolves when the process exits. | ||
| */ | ||
| export function execProcess( | ||
| tool: string, | ||
| args: string[], | ||
| options?: { | ||
| cwd?: string; | ||
| env?: { [key: string]: string }; | ||
| stdio?: "pipe" | "inherit" | "ignore"; | ||
| shell?: boolean | string; | ||
| processStdOut?: (data: string) => void; | ||
| processStdErr?: (data: string) => void; | ||
| }, | ||
| ): Promise<void> { | ||
| return new Promise((resolve, reject) => { | ||
| const cwd = options?.cwd || process.cwd(); | ||
| console.log(`🚀 Launching [${tool} ${args.join(" ")}] in ${cwd}`); | ||
| const result = spawn(tool, args, { | ||
| cwd: cwd, | ||
| env: options?.env || process.env, | ||
| stdio: options?.stdio || "inherit", | ||
| shell: options?.shell || false, | ||
| }); | ||
|
|
||
| if (options?.stdio === "pipe") { | ||
| result.stdout?.setEncoding("utf8"); | ||
| result.stdout?.on("data", function (data: Buffer) { | ||
| const strData = data.toString("utf8"); | ||
| options?.processStdOut?.(strData); | ||
| }); | ||
|
|
||
| result.stderr?.setEncoding("utf8"); | ||
| result.stderr?.on("data", function (data: Buffer) { | ||
| const strData = data.toString("utf8"); | ||
| options?.processStdErr?.(strData); | ||
| }); | ||
| } | ||
| result.on("exit", (code) => { | ||
| if (code == 0) { | ||
| resolve(); | ||
| } else { | ||
| reject(new Error(`Process ${tool} exited with code ${code}`)); | ||
| } | ||
| }); | ||
| }); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.