|
| 1 | +### Get certificate configuration by using [AZURE-PIPELINES-TASK-LIB](https://github.com/Microsoft/azure-pipelines-task-lib) method (Min Agent Version 2.122.0) |
| 2 | + |
| 3 | +#### Node.js Lib |
| 4 | + |
| 5 | +Method for retrieve certificate settings in node.js lib |
| 6 | +``` typescript |
| 7 | +export function getHttpCertConfiguration(): CertConfiguration { |
| 8 | +} |
| 9 | +``` |
| 10 | +`CertConfiguration` has following fields |
| 11 | +```typescript |
| 12 | +export interface CertConfiguration { |
| 13 | + caFile?: string; |
| 14 | + certFile?: string; |
| 15 | + keyFile?: string; |
| 16 | + certArchiveFile?: string; |
| 17 | + passphrase?: string; |
| 18 | + } |
| 19 | +``` |
| 20 | + |
| 21 | +In the following example, we will retrieve certificate configuration information and use VSTS-Node-Api to make a Rest Api call back to VSTS/TFS service, the Rest call will use the certificates you configured in agent. |
| 22 | +```typescript |
| 23 | +// MyCertExampleTask.ts |
| 24 | +import tl = require('azure-pipelines-task-lib/task'); |
| 25 | +import api = require('vso-node-api'); |
| 26 | +import VsoBaseInterfaces = require('vso-node-api/interfaces/common/VsoBaseInterfaces'); |
| 27 | + |
| 28 | +async function run() { |
| 29 | + |
| 30 | + // get cert config |
| 31 | + let cert = tl.getHttpCertConfiguration(); |
| 32 | + |
| 33 | + // TFS server url |
| 34 | + let serverUrl = "https://mycompanytfs.com/tfs"; |
| 35 | + |
| 36 | + // Personal access token |
| 37 | + let token = "<YOUR_TOKEN_HERE>"; |
| 38 | + let authHandler = api.getPersonalAccessTokenHandler(token); |
| 39 | + |
| 40 | + // Options for VSTS-Node-Api, |
| 41 | + // this is not required if you want to send http request to the same TFS |
| 42 | + // instance the agent currently connect to. |
| 43 | + // VSTS-Node-Api will pick up certificate setting from azure-pipelines-task-lib automatically |
| 44 | + let option: VsoBaseInterfaces.IRequestOptions = { |
| 45 | + cert: { |
| 46 | + caFile: "C:\\ca.pem", |
| 47 | + certFile: "C:\\client-cert.pem", |
| 48 | + keyFile: "C:\\client-cert-key.pem", |
| 49 | + passphrase: "test123", |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + // Make a Rest call to VSTS/TFS |
| 54 | + let vsts: api.WebApi = new api.WebApi(serverUrl, authHandler, option); |
| 55 | + let connData: lim.ConnectionData = await vsts.connect(); |
| 56 | + console.log('Hello ' + connData.authenticatedUser.providerDisplayName); |
| 57 | + |
| 58 | + // You should only use the retrieved certificate config to call the TFS instance your agent current connect to or any resource within your cooperation that accept those certificates. |
| 59 | +} |
| 60 | + |
| 61 | +run(); |
| 62 | +``` |
| 63 | + |
| 64 | +#### PowerShell Lib |
| 65 | + |
| 66 | +On Windows the CA certificate needs to be installed into the `Trusted CA Store` of `Windows Certificate manager` first. |
| 67 | +So the PowerShell lib will only expose the client certificate information |
| 68 | + |
| 69 | +Method for retrieve client certificate settings in PowerShell lib |
| 70 | +``` powershell |
| 71 | +function Get-ClientCertificate { |
| 72 | + [CmdletBinding()] |
| 73 | + param() |
| 74 | +
|
| 75 | + # Return a new X509Certificate2 object to the client certificate |
| 76 | + return New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Certificate2 |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +In the following example, we will retrieve client certificate configuration information and print it out first, then we will use PowerShell lib method to get `VssHttpClient` and make a Rest Api call back to TFS service's `Project` endpoint and retrieve all team projects. The Rest call will use the client certificate you configured in agent. |
| 81 | + |
| 82 | +```powershell |
| 83 | +# retrieve cert config |
| 84 | +$cert = Get-VstsClientCertificate |
| 85 | +Write-Host $cert |
| 86 | +
|
| 87 | +# get project http client (the client will have proxy hook up by default) |
| 88 | +$projectHttpClient = Get-VstsVssHttpClient -TypeName Microsoft.TeamFoundation.Core.WebApi.ProjectHttpClient -OMDirectory "<Directory that contains required .dlls>" |
| 89 | +
|
| 90 | +# print out all team projects |
| 91 | +$projectHttpClient.GetProjects().Result |
| 92 | +``` |
0 commit comments