Skip to content

Commit 99dd892

Browse files
authored
Users/zhentan/cargo auth task (#17265)
* CargoAuthenticateV0 * Follow up PR * Verify cargo registry is from artifacts * Nit * Nit changes with comments * Resource string localization change * Bump task versions and add CargoAuthenticate * Add missing NugetInstallerV0 * Fix after master merge * Bump one more task
1 parent 36956fb commit 99dd892

File tree

60 files changed

+2139
-66
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+2139
-66
lines changed

.github/CODEOWNERS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ Tasks/CacheBetaV0/ @johnterickson
101101

102102
Tasks/CacheBetaV1/ @johnterickson
103103

104+
Tasks/CargoAuthenticateV0/ @zhenghao104 @jmyersmsft
105+
104106
Tasks/ChefV1/ @vsebesta @rvairavelu @pipeline-environment-team
105107

106108
Tasks/ChefKnifeV1/ @vsebesta @rvairavelu @pipeline-environment-team
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"loc.friendlyName": "Cargo authenticate (for task runners)",
3+
"loc.helpMarkDown": "",
4+
"loc.description": "Authentication task for the cargo client used for installing Cargo crates distribution",
5+
"loc.instanceNameFormat": "cargo Authenticate $(configFile)",
6+
"loc.input.label.configFile": "config.toml file to authenticate",
7+
"loc.input.help.configFile": "Path to the config.toml file that specifies the registries you want to work with. Select the file, not the folder e.g. \"/.cargo/config.toml\".",
8+
"loc.messages.FailedToAddAuthentication": "Failed to add authentication.",
9+
"loc.messages.ConfigTomlNotToml": "The file you selected to authenticate (%s) is not a valid .toml file.",
10+
"loc.messages.ConfigTomlDoesNotExist": "The config.toml file you selected at %s does not currently exist.",
11+
"loc.messages.ConfigTomlMissingRegistry": "The config.toml file you selected must contain registries table",
12+
"loc.messages.AuthenticatingThisConfigToml": "Adding authentication for registry listed in this config.toml file at %s",
13+
"loc.messages.AddingAuthRegistry": "Adding auth for registry: %s with token name: %s"
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import fs = require('fs');
2+
import assert = require('assert');
3+
import path = require('path');
4+
5+
describe('CargoAuthenticateV0 Suite', function () {
6+
before(() => {
7+
});
8+
9+
after(() => {
10+
});
11+
12+
it('Does a basic hello world test', function(done: MochaDone) {
13+
// TODO - add real tests
14+
done();
15+
});
16+
});

Tasks/CargoAuthenticateV0/Tests/package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "cargo-authenticate-tests",
3+
"version": "1.0.0",
4+
"description": "Azure Pipelines Cargo Authenticate V0 Task Tests",
5+
"main": "L0.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+ssh://[email protected]/Microsoft/azure-pipelines-tasks.git"
12+
},
13+
"author": "Microsoft Corporation",
14+
"license": "MIT",
15+
"bugs": {
16+
"url": "https://github.com/Microsoft/azure-pipelines-tasks/issues"
17+
},
18+
"homepage": "https://github.com/Microsoft/azure-pipelines-tasks#readme"
19+
}
20+
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import * as path from 'path';
2+
import * as tl from 'azure-pipelines-task-lib/task';
3+
import * as toml from 'toml';
4+
import * as fs from 'fs';
5+
import * as constants from './constants';
6+
import * as util from 'azure-pipelines-tasks-packaging-common/util';
7+
import * as pkgLocationUtils from 'azure-pipelines-tasks-packaging-common/locationUtilities';
8+
import * as url from 'url';
9+
10+
async function main(): Promise<void> {
11+
tl.setResourcePath(path.join(__dirname, 'task.json'));
12+
13+
try {
14+
let configtoml = tl.getInput(constants.CargoAuthenticateTaskInput.ConfigFile);
15+
if (!tl.exist(configtoml)) {
16+
throw new Error(tl.loc('ConfigTomlDoesNotExist', configtoml));
17+
}
18+
else {
19+
console.log(tl.loc("AuthenticatingThisConfigToml", configtoml));
20+
}
21+
22+
// These two formats will work
23+
// [registries]
24+
// zhentan-test = { index = "sparse+https://pkgs.dev.azure.com/codesharing-SU0/zhentan-test/_packaging/zhentan-test/Cargo/index/" }
25+
// [registries.zhentan-test1]
26+
// index = "sparse+https://pkgs.dev.azure.com/codesharing-SU0/zhentan-test/_packaging/zhentan-test/Cargo/index/"
27+
let configtomlFile = fs.readFileSync(configtoml, 'utf8');
28+
var result = toml.parse(configtomlFile);
29+
if (!result.registries)
30+
{
31+
throw new Error(tl.loc('ConfigTomlMissingRegistry'));
32+
}
33+
34+
let packagingLocation: pkgLocationUtils.PackagingLocation;
35+
try {
36+
// TODO: Change Npm to Cargo once new packaging common is deployed
37+
packagingLocation = await pkgLocationUtils.getPackagingUris(pkgLocationUtils.ProtocolType.Npm);
38+
} catch (error) {
39+
tl.debug('Unable to get packaging URIs');
40+
util.logError(error);
41+
throw error;
42+
}
43+
44+
const collectionHosts = packagingLocation.PackagingUris.map((pkgUrl: string) => {
45+
const parsedUrl = url.parse(pkgUrl);
46+
if (parsedUrl && parsedUrl.host) {
47+
return parsedUrl.host.toLowerCase();
48+
}
49+
return undefined;
50+
});
51+
52+
const localAccesstoken = `Bearer ${tl.getVariable('System.AccessToken')}`;
53+
for (let registry of Object.keys(result.registries)) {
54+
const registryUrl = url.parse(result.registries[registry].index);
55+
if(registryUrl && registryUrl.host && collectionHosts.indexOf(registryUrl.host.toLowerCase()) >= 0) {
56+
const tokenName = `CARGO_REGISTRIES_${registry.toLocaleUpperCase().replace("-", "_")}_TOKEN`;
57+
tl.debug(tl.loc('AddingAuthRegistry', registry, tokenName));
58+
tl.setVariable(tokenName, localAccesstoken);
59+
}
60+
}
61+
62+
//External endpoints
63+
}
64+
65+
catch(error) {
66+
tl.error(error);
67+
tl.setResult(tl.TaskResult.Failed, tl.loc("FailedToAddAuthentication"));
68+
return;
69+
}
70+
}
71+
72+
main();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export class CargoAuthenticateTaskInput {
2+
public static ConfigFile: string = 'configFile';
3+
}

Tasks/CargoAuthenticateV0/icon.png

2.74 KB
Loading

Tasks/CargoAuthenticateV0/icon.svg

Lines changed: 62 additions & 0 deletions
Loading
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"rm": [
3+
{
4+
"items": [
5+
"node_modules/azure-pipelines-tasks-artifacts-common/node_modules/azure-pipelines-task-lib"
6+
],
7+
"options": "-Rf"
8+
}
9+
]
10+
}

0 commit comments

Comments
 (0)