@@ -18,84 +18,54 @@ public class DependabotProxy : IDependabotProxy
1818 /// <param name="URL">The URL of the package registry.</param>
1919 public record class RegistryConfig ( string Type , string URL ) ;
2020
21- private readonly string host ;
22- private readonly string port ;
23-
2421 public string Address { get ; }
2522
26- public HashSet < string > RegistryURLs { get ; }
23+ public HashSet < string > RegistryURLs { get ; } = [ ] ;
2724
2825 public string ? CertificatePath { get ; private set ; }
2926
3027 public X509Certificate2 ? Certificate { get ; private set ; }
3128
32- internal static IDependabotProxy ? GetDependabotProxy (
33- ILogger logger , IDiagnosticsWriter diagnosticsWriter , TemporaryDirectory tempWorkingDirectory )
29+ private DependabotProxy ( IDependabotProxyConfiguration config , ILogger logger , TemporaryDirectory tempWorkingDirectory )
3430 {
35- // Setting HTTP(S)_PROXY and SSL_CERT_FILE have no effect on Windows or macOS,
36- // but we would still end up using the Dependabot proxy to check for feed reachability.
37- // This would result in us discovering that the feeds are reachable, but `dotnet` would
38- // fail to connect to them. To prevent this from happening, we do not initialise an
39- // instance of `DependabotProxy` on those platforms.
40- if ( SystemBuildActions . Instance . IsWindows ( ) || SystemBuildActions . Instance . IsMacOs ( ) ) return null ;
41-
42- // Obtain and store the address of the Dependabot proxy, if available.
43- var host = Environment . GetEnvironmentVariable ( EnvironmentVariableNames . ProxyHost ) ;
44- var port = Environment . GetEnvironmentVariable ( EnvironmentVariableNames . ProxyPort ) ;
45-
46- if ( string . IsNullOrWhiteSpace ( host ) || string . IsNullOrWhiteSpace ( port ) )
47- {
48- logger . LogInfo ( "No Dependabot proxy credentials are configured." ) ;
49- return null ;
50- }
51-
52- var result = new DependabotProxy ( host , port ) ;
53- logger . LogInfo ( $ "Dependabot proxy configured at { result . Address } ") ;
31+ Address = $ "http://{ config . Host } :{ config . Port } ";
5432
55- // Obtain and store the proxy's certificate, if available.
56- var cert = Environment . GetEnvironmentVariable ( EnvironmentVariableNames . ProxyCertificate ) ;
57-
58- if ( ! string . IsNullOrWhiteSpace ( cert ) )
33+ if ( ! string . IsNullOrWhiteSpace ( config . Certificate ) )
5934 {
6035 var certDirPath = new DirectoryInfo ( Path . Join ( tempWorkingDirectory . DirInfo . FullName , ".dependabot-proxy" ) ) ;
6136 Directory . CreateDirectory ( certDirPath . FullName ) ;
6237
63- result . CertificatePath = Path . Join ( certDirPath . FullName , "proxy.crt" ) ;
64- var certFile = new FileInfo ( result . CertificatePath ) ;
38+ CertificatePath = Path . Join ( certDirPath . FullName , "proxy.crt" ) ;
39+ var certFile = new FileInfo ( CertificatePath ) ;
6540
6641 using var writer = certFile . CreateText ( ) ;
67- writer . Write ( cert ) ;
42+ writer . Write ( config . Certificate ) ;
6843 writer . Close ( ) ;
6944
70- logger . LogInfo ( $ "Stored Dependabot proxy certificate at { result . CertificatePath } ") ;
45+ logger . LogInfo ( $ "Stored Dependabot proxy certificate at { CertificatePath } ") ;
7146
72- result . Certificate = X509Certificate2 . CreateFromPem ( cert ) ;
47+ Certificate = X509Certificate2 . CreateFromPem ( config . Certificate ) ;
7348 }
7449
75- // Try to obtain the list of private registry URLs.
76- var registryURLs = Environment . GetEnvironmentVariable ( EnvironmentVariableNames . ProxyURLs ) ;
77-
78- if ( ! string . IsNullOrWhiteSpace ( registryURLs ) )
50+ if ( ! string . IsNullOrWhiteSpace ( config . RegistryURLs ) )
7951 {
8052 try
8153 {
82- // The value of the environment variable should be a JSON array of objects, such as:
83- // [ { "type": "nuget_feed", "url": "https://nuget.pkg.github.com/org/index.json" } ]
84- var array = JsonConvert . DeserializeObject < List < RegistryConfig > > ( registryURLs ) ;
54+ var array = JsonConvert . DeserializeObject < List < RegistryConfig > > ( config . RegistryURLs ) ;
8555 if ( array is not null )
8656 {
87- foreach ( RegistryConfig config in array )
57+ foreach ( RegistryConfig registry in array )
8858 {
8959 // The array contains all configured private registries, not just ones for C#.
9060 // We ignore the non-C# ones here.
91- if ( ! config . Type . Equals ( "nuget_feed" ) )
61+ if ( ! registry . Type . Equals ( "nuget_feed" ) )
9262 {
93- logger . LogDebug ( $ "Ignoring registry at '{ config . URL } ' since it is not of type 'nuget_feed'.") ;
63+ logger . LogDebug ( $ "Ignoring registry at '{ registry . URL } ' since it is not of type 'nuget_feed'.") ;
9464 continue ;
9565 }
9666
97- logger . LogInfo ( $ "Found private registry at '{ config . URL } '") ;
98- result . RegistryURLs . Add ( config . URL ) ;
67+ logger . LogInfo ( $ "Found private registry at '{ registry . URL } '") ;
68+ RegistryURLs . Add ( registry . URL ) ;
9969 }
10070 }
10171 }
@@ -104,6 +74,39 @@ public record class RegistryConfig(string Type, string URL);
10474 logger . LogError ( $ "Unable to parse '{ EnvironmentVariableNames . ProxyURLs } ': { ex . Message } ") ;
10575 }
10676 }
77+ }
78+
79+ internal static IDependabotProxy ? Make ( ILogger logger , IDiagnosticsWriter diagnosticsWriter , TemporaryDirectory tempWorkingDirectory )
80+ {
81+ // Setting HTTP(S)_PROXY and SSL_CERT_FILE have no effect on Windows or macOS,
82+ // but we would still end up using the Dependabot proxy to check for feed reachability.
83+ // This would result in us discovering that the feeds are reachable, but `dotnet` would
84+ // fail to connect to them. To prevent this from happening, we do not initialise an
85+ // instance of `DependabotProxy` on those platforms.
86+ if ( SystemBuildActions . Instance . IsWindows ( ) || SystemBuildActions . Instance . IsMacOs ( ) )
87+ {
88+ return null ;
89+ }
90+
91+ return Make ( new DependabotProxyConfiguration ( ) , logger , diagnosticsWriter , tempWorkingDirectory ) ;
92+ }
93+
94+ /// <summary>
95+ /// Creates an instance of the Dependabot proxy using the specified configuration.
96+ /// Returns null if the proxy cannot be created.
97+ /// This overload is exposed primarily to enable platform-independent unit testing.
98+ /// </summary>
99+ internal static IDependabotProxy ? Make (
100+ IDependabotProxyConfiguration proxyConfig , ILogger logger , IDiagnosticsWriter diagnosticsWriter , TemporaryDirectory tempWorkingDirectory )
101+ {
102+ if ( string . IsNullOrWhiteSpace ( proxyConfig . Host ) || string . IsNullOrWhiteSpace ( proxyConfig . Port ) )
103+ {
104+ logger . LogDebug ( "No Dependabot proxy credentials are configured." ) ;
105+ return null ;
106+ }
107+
108+ var result = new DependabotProxy ( proxyConfig , logger , tempWorkingDirectory ) ;
109+ logger . LogInfo ( $ "Dependabot proxy configured at { result . Address } ") ;
107110
108111 // Emit a diagnostic for the discovered private registries, so that it is easy
109112 // for users to see that they were picked up.
@@ -125,17 +128,9 @@ public record class RegistryConfig(string Type, string URL);
125128 return result ;
126129 }
127130
128- private DependabotProxy ( string host , string port )
129- {
130- this . host = host ;
131- this . port = port ;
132- this . Address = $ "http://{ this . host } :{ this . port } ";
133- this . RegistryURLs = new HashSet < string > ( ) ;
134- }
135-
136131 public void Dispose ( )
137132 {
138- this . Certificate ? . Dispose ( ) ;
133+ Certificate ? . Dispose ( ) ;
139134 }
140135 }
141136}
0 commit comments