1
1
# This script adds internal feeds required to build commits that depend on internal package sources. For instance,
2
2
# dotnet6-internal would be added automatically if dotnet6 was found in the nuget.config file. In addition also enables
3
3
# disabled internal Maestro (darc-int*) feeds.
4
- #
5
- # Optionally, this script also adds a credential entry for each of the internal feeds if supplied. This credential
6
- # is added via the standard environment variable VSS_NUGET_EXTERNAL_FEED_ENDPOINTS. See
7
- # https://github.com/microsoft/artifacts-credprovider/tree/v1.1.1?tab=readme-ov-file#environment-variables for more details
4
+ #
5
+ # Optionally, this script also adds a credential entry for each of the internal feeds if supplied.
8
6
#
9
7
# See example call for this script below.
10
8
#
11
9
# - task: PowerShell@2
12
- # displayName: Setup Internal Feeds
10
+ # displayName: Setup Private Feeds Credentials
13
11
# condition: eq(variables['Agent.OS'], 'Windows_NT')
14
12
# inputs:
15
13
# filePath: $(Build.SourcesDirectory)/eng/common/SetupNugetSources.ps1
16
- # arguments: -ConfigFile $(Build.SourcesDirectory)/NuGet.config
17
- # - task: NuGetAuthenticate@1
18
- #
14
+ # arguments: -ConfigFile $(Build.SourcesDirectory)/NuGet.config -Password $Env:Token
15
+ # env:
16
+ # Token: $(dn-bot-dnceng-artifact-feeds-rw)
17
+ #
19
18
# Note that the NuGetAuthenticate task should be called after SetupNugetSources.
20
19
# This ensures that:
21
20
# - Appropriate creds are set for the added internal feeds (if not supplied to the scrupt)
22
- # - The credential provider is installed
21
+ # - The credential provider is installed.
23
22
#
24
23
# This logic is also abstracted into enable-internal-sources.yml.
25
24
26
25
[CmdletBinding ()]
27
26
param (
28
27
[Parameter (Mandatory = $true )][string ]$ConfigFile ,
29
- [ string ] $Password
28
+ $Password
30
29
)
31
30
32
31
$ErrorActionPreference = " Stop"
@@ -35,23 +34,12 @@ Set-StrictMode -Version 2.0
35
34
36
35
. $PSScriptRoot \tools.ps1
37
36
38
- $feedEndpoints = $null
39
-
40
- # If a credential is provided, ensure that we don't overwrite the current set of
41
- # credentials that may have been provided by a previous call to the credential provider.
42
- if ($Password -and $null -ne $env: VSS_NUGET_EXTERNAL_FEED_ENDPOINTS ) {
43
- $feedEndpoints = $env: VSS_NUGET_EXTERNAL_FEED_ENDPOINTS | ConvertFrom-Json
44
- } elseif ($Password ) {
45
- $feedEndpoints = @ { endpointCredentials = @ () }
46
- }
47
-
48
37
# Add source entry to PackageSources
49
- function AddPackageSource ($sources , $SourceName , $SourceEndPoint , $pwd ) {
38
+ function AddPackageSource ($sources , $SourceName , $SourceEndPoint , $creds , $Username , $ pwd ) {
50
39
$packageSource = $sources.SelectSingleNode (" add[@key='$SourceName ']" )
51
40
52
- if ($null -eq $packageSource )
41
+ if ($packageSource -eq $null )
53
42
{
54
- Write-Host " `t Adding package source" $SourceName
55
43
$packageSource = $doc.CreateElement (" add" )
56
44
$packageSource.SetAttribute (" key" , $SourceName )
57
45
$packageSource.SetAttribute (" value" , $SourceEndPoint )
@@ -61,33 +49,63 @@ function AddPackageSource($sources, $SourceName, $SourceEndPoint, $pwd) {
61
49
Write-Host " Package source $SourceName already present."
62
50
}
63
51
64
- if ($pwd ) {
65
- $feedEndpoints.endpointCredentials = AddCredential - endpointCredentials $feedEndpoints.endpointCredentials - source $SourceEndPoint - pwd $pwd
66
- }
52
+ AddCredential - Creds $creds - Source $SourceName - Username $Username - pwd $pwd
67
53
}
68
54
69
- # Add a new feed endpoint credential
70
- function AddCredential ([ array ] $endpointCredentials , $source , $pwd ) {
71
- $endpointCredentials += @ {
72
- endpoint = $source ;
73
- password = $pwd
55
+ # Add a credential node for the specified source
56
+ function AddCredential ($creds , $source , $username , $pwd ) {
57
+ # If no cred supplied, don't do anything.
58
+ if ( ! $pwd ) {
59
+ return ;
74
60
}
75
- return $endpointCredentials
61
+
62
+ # Looks for credential configuration for the given SourceName. Create it if none is found.
63
+ $sourceElement = $creds.SelectSingleNode ($Source )
64
+ if ($sourceElement -eq $null )
65
+ {
66
+ $sourceElement = $doc.CreateElement ($Source )
67
+ $creds.AppendChild ($sourceElement ) | Out-Null
68
+ }
69
+
70
+ # Add the <Username> node to the credential if none is found.
71
+ $usernameElement = $sourceElement.SelectSingleNode (" add[@key='Username']" )
72
+ if ($usernameElement -eq $null )
73
+ {
74
+ $usernameElement = $doc.CreateElement (" add" )
75
+ $usernameElement.SetAttribute (" key" , " Username" )
76
+ $sourceElement.AppendChild ($usernameElement ) | Out-Null
77
+ }
78
+ $usernameElement.SetAttribute (" value" , $Username )
79
+
80
+ # Add the <ClearTextPassword> to the credential if none is found.
81
+ # Add it as a clear text because there is no support for encrypted ones in non-windows .Net SDKs.
82
+ # -> https://github.com/NuGet/Home/issues/5526
83
+ $passwordElement = $sourceElement.SelectSingleNode (" add[@key='ClearTextPassword']" )
84
+ if ($passwordElement -eq $null )
85
+ {
86
+ $passwordElement = $doc.CreateElement (" add" )
87
+ $passwordElement.SetAttribute (" key" , " ClearTextPassword" )
88
+ $sourceElement.AppendChild ($passwordElement ) | Out-Null
89
+ }
90
+
91
+ $passwordElement.SetAttribute (" value" , $pwd )
76
92
}
77
93
78
- function InsertMaestroInternalFeedCredentials ($Sources , $pwd ) {
79
- $maestroInternalSources = $Sources.SelectNodes (" add[contains(@key,'darc-int')]" )
94
+ function InsertMaestroPrivateFeedCredentials ($Sources , $Creds , $Username , $pwd ) {
95
+ $maestroPrivateSources = $Sources.SelectNodes (" add[contains(@key,'darc-int')]" )
80
96
81
- ForEach ($PackageSource in $maestroInternalSources ) {
82
- Write-Host " `t Adding credential for Maestro's feed:" $PackageSource.Key
83
- $feedEndpoints.endpointCredentials = AddCredential - endpointCredentials $feedEndpoints.endpointCredentials - source $PackageSource.value - pwd $pwd
97
+ Write-Host " Inserting credentials for $ ( $maestroPrivateSources.Count ) Maestro's private feeds."
98
+
99
+ ForEach ($PackageSource in $maestroPrivateSources ) {
100
+ Write-Host " `t Inserting credential for Maestro's feed:" $PackageSource.Key
101
+ AddCredential - Creds $creds - Source $PackageSource.Key - Username $Username - pwd $pwd
84
102
}
85
103
}
86
104
87
- function EnableInternalPackageSources ($DisabledPackageSources ) {
88
- $maestroInternalSources = $DisabledPackageSources.SelectNodes (" add[contains(@key,'darc-int')]" )
89
- ForEach ($DisabledPackageSource in $maestroInternalSources ) {
90
- Write-Host " `t Ensuring internal source '$ ( $DisabledPackageSource.key ) ' is enabled by deleting it from disabledPackageSource"
105
+ function EnablePrivatePackageSources ($DisabledPackageSources ) {
106
+ $maestroPrivateSources = $DisabledPackageSources.SelectNodes (" add[contains(@key,'darc-int')]" )
107
+ ForEach ($DisabledPackageSource in $maestroPrivateSources ) {
108
+ Write-Host " `t Ensuring private source '$ ( $DisabledPackageSource.key ) ' is enabled by deleting it from disabledPackageSource"
91
109
# Due to https://github.com/NuGet/Home/issues/10291, we must actually remove the disabled entries
92
110
$DisabledPackageSources.RemoveChild ($DisabledPackageSource )
93
111
}
@@ -105,46 +123,49 @@ $doc.Load($filename)
105
123
106
124
# Get reference to <PackageSources> or create one if none exist already
107
125
$sources = $doc.DocumentElement.SelectSingleNode (" packageSources" )
108
- if ($null -eq $sources ) {
126
+ if ($sources -eq $null ) {
109
127
$sources = $doc.CreateElement (" packageSources" )
110
128
$doc.DocumentElement.AppendChild ($sources ) | Out-Null
111
129
}
112
130
131
+ $creds = $null
132
+ if ($Password ) {
133
+ # Looks for a <PackageSourceCredentials> node. Create it if none is found.
134
+ $creds = $doc.DocumentElement.SelectSingleNode (" packageSourceCredentials" )
135
+ if ($creds -eq $null ) {
136
+ $creds = $doc.CreateElement (" packageSourceCredentials" )
137
+ $doc.DocumentElement.AppendChild ($creds ) | Out-Null
138
+ }
139
+ }
140
+
113
141
# Check for disabledPackageSources; we'll enable any darc-int ones we find there
114
142
$disabledSources = $doc.DocumentElement.SelectSingleNode (" disabledPackageSources" )
115
- if ($null -ne $disabledSources ) {
143
+ if ($disabledSources -ne $null ) {
116
144
Write-Host " Checking for any darc-int disabled package sources in the disabledPackageSources node"
117
- EnableInternalPackageSources - DisabledPackageSources $disabledSources
145
+ EnablePrivatePackageSources - DisabledPackageSources $disabledSources
118
146
}
119
147
120
- if ($Password ) {
121
- InsertMaestroInternalFeedCredentials - Sources $sources - pwd $Password
122
- }
148
+ $userName = " dn-bot"
149
+
150
+ # Insert credential nodes for Maestro's private feeds
151
+ InsertMaestroPrivateFeedCredentials - Sources $sources - Creds $creds - Username $userName - pwd $Password
123
152
124
153
# 3.1 uses a different feed url format so it's handled differently here
125
154
$dotnet31Source = $sources.SelectSingleNode (" add[@key='dotnet3.1']" )
126
- if ($null -ne $dotnet31Source ) {
127
- AddPackageSource - Sources $sources - SourceName " dotnet3.1-internal" - SourceEndPoint " https://pkgs.dev.azure.com/dnceng/_packaging/dotnet3.1-internal/nuget/v3/index.json " - pwd $Password
128
- AddPackageSource - Sources $sources - SourceName " dotnet3.1-internal-transport" - SourceEndPoint " https://pkgs.dev.azure.com/dnceng/_packaging/dotnet3.1-internal-transport/nuget/v3/index.json " - pwd $Password
155
+ if ($dotnet31Source -ne $null ) {
156
+ AddPackageSource - Sources $sources - SourceName " dotnet3.1-internal" - SourceEndPoint " https://pkgs.dev.azure.com/dnceng/_packaging/dotnet3.1-internal/nuget/v2 " - Creds $creds - Username $userName - pwd $Password
157
+ AddPackageSource - Sources $sources - SourceName " dotnet3.1-internal-transport" - SourceEndPoint " https://pkgs.dev.azure.com/dnceng/_packaging/dotnet3.1-internal-transport/nuget/v2 " - Creds $creds - Username $userName - pwd $Password
129
158
}
130
159
131
160
$dotnetVersions = @ (' 5' , ' 6' , ' 7' , ' 8' )
132
161
133
162
foreach ($dotnetVersion in $dotnetVersions ) {
134
163
$feedPrefix = " dotnet" + $dotnetVersion ;
135
164
$dotnetSource = $sources.SelectSingleNode (" add[@key='$feedPrefix ']" )
136
- if ($dotnetSource ) {
137
- AddPackageSource - Sources $sources - SourceName " $feedPrefix -internal" - SourceEndPoint " https://pkgs.dev.azure.com/dnceng/internal/_packaging/$feedprefix -internal/nuget/v3/index.json " - pwd $Password
138
- AddPackageSource - Sources $sources - SourceName " $feedPrefix -internal-transport" - SourceEndPoint " https://pkgs.dev.azure.com/dnceng/internal/_packaging/$feedPrefix -internal-transport/nuget/v3/index.json " - pwd $Password
165
+ if ($dotnetSource -ne $null ) {
166
+ AddPackageSource - Sources $sources - SourceName " $feedPrefix -internal" - SourceEndPoint " https://pkgs.dev.azure.com/dnceng/internal/_packaging/$feedPrefix -internal/nuget/v2 " - Creds $creds - Username $userName - pwd $Password
167
+ AddPackageSource - Sources $sources - SourceName " $feedPrefix -internal-transport" - SourceEndPoint " https://pkgs.dev.azure.com/dnceng/internal/_packaging/$feedPrefix -internal-transport/nuget/v2 " - Creds $creds - Username $userName - pwd $Password
139
168
}
140
169
}
141
170
142
171
$doc.Save ($filename )
143
-
144
- # If any credentials were added or altered, update the VSS_NUGET_EXTERNAL_FEED_ENDPOINTS environment variable
145
- if ($null -ne $feedEndpoints ) {
146
- # ci is set to true so vso logging commands will be used.
147
- $ci = $true
148
- Write-PipelineSetVariable - Name ' VSS_NUGET_EXTERNAL_FEED_ENDPOINTS' - Value $ ($feedEndpoints | ConvertTo-Json ) - IsMultiJobVariable $false
149
- Write-PipelineSetVariable - Name ' NUGET_CREDENTIALPROVIDER_SESSIONTOKENCACHE_ENABLED' - Value " False" - IsMultiJobVariable $false
150
- }
0 commit comments