@@ -9,84 +9,71 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
99{
1010 public class DependabotProxy : IDisposable
1111 {
12- private readonly string ? host ;
13- private readonly string ? port ;
14- private readonly FileInfo ? certFile ;
12+ private readonly string host ;
13+ private readonly string port ;
1514
1615 /// <summary>
1716 /// The full address of the Dependabot proxy, if available.
1817 /// </summary>
19- internal readonly string ? Address ;
18+ internal string Address { get ; }
2019 /// <summary>
2120 /// The path to the temporary file where the certificate is stored.
2221 /// </summary>
23- internal readonly string ? CertificatePath ;
22+ internal string ? CertificatePath { get ; private set ; }
2423 /// <summary>
2524 /// The certificate used for the Dependabot proxy.
2625 /// </summary>
27- internal readonly X509Certificate2 ? Certificate ;
26+ internal X509Certificate2 ? Certificate { get ; private set ; }
2827
29- /// <summary>
30- /// Gets a value indicating whether a Dependabot proxy is configured.
31- /// </summary>
32- internal bool IsConfigured => ! string . IsNullOrEmpty ( this . Address ) ;
33-
34- internal DependabotProxy ( ILogger logger , TemporaryDirectory tempWorkingDirectory )
28+ internal static DependabotProxy ? GetDependabotProxy ( ILogger logger , TemporaryDirectory tempWorkingDirectory )
3529 {
3630 // Obtain and store the address of the Dependabot proxy, if available.
37- this . host = Environment . GetEnvironmentVariable ( EnvironmentVariableNames . ProxyHost ) ;
38- this . port = Environment . GetEnvironmentVariable ( EnvironmentVariableNames . ProxyPort ) ;
31+ var host = Environment . GetEnvironmentVariable ( EnvironmentVariableNames . ProxyHost ) ;
32+ var port = Environment . GetEnvironmentVariable ( EnvironmentVariableNames . ProxyPort ) ;
3933
4034 if ( string . IsNullOrWhiteSpace ( host ) || string . IsNullOrWhiteSpace ( port ) )
4135 {
4236 logger . LogInfo ( "No Dependabot proxy credentials are configured." ) ;
43- return ;
37+ return null ;
4438 }
4539
46- this . Address = $ "http:// { this . host } : { this . port } " ;
47- logger . LogInfo ( $ "Dependabot proxy configured at { this . Address } ") ;
40+ var result = new DependabotProxy ( host , port ) ;
41+ logger . LogInfo ( $ "Dependabot proxy configured at { result . Address } ") ;
4842
4943 // Obtain and store the proxy's certificate, if available.
5044 var cert = Environment . GetEnvironmentVariable ( EnvironmentVariableNames . ProxyCertificate ) ;
5145
52- if ( string . IsNullOrWhiteSpace ( cert ) )
46+ if ( ! string . IsNullOrWhiteSpace ( cert ) )
5347 {
5448 logger . LogInfo ( "No certificate configured for Dependabot proxy." ) ;
55- return ;
56- }
5749
58- var certDirPath = new DirectoryInfo ( Path . Join ( tempWorkingDirectory . DirInfo . FullName , ".dependabot-proxy" ) ) ;
59- Directory . CreateDirectory ( certDirPath . FullName ) ;
50+ var certDirPath = new DirectoryInfo ( Path . Join ( tempWorkingDirectory . DirInfo . FullName , ".dependabot-proxy" ) ) ;
51+ Directory . CreateDirectory ( certDirPath . FullName ) ;
52+
53+ result . CertificatePath = Path . Join ( certDirPath . FullName , "proxy.crt" ) ;
54+ var certFile = new FileInfo ( result . CertificatePath ) ;
6055
61- this . CertificatePath = Path . Join ( certDirPath . FullName , "proxy.crt" ) ;
62- this . certFile = new FileInfo ( this . CertificatePath ) ;
56+ using var writer = certFile . CreateText ( ) ;
57+ writer . Write ( cert ) ;
6358
64- using var writer = this . certFile . CreateText ( ) ;
65- writer . Write ( cert ) ;
59+ logger . LogInfo ( $ "Stored Dependabot proxy certificate at { result . CertificatePath } ") ;
6660
67- logger . LogInfo ( $ "Stored Dependabot proxy certificate at { this . CertificatePath } ") ;
61+ result . Certificate = new X509Certificate2 ( result . CertificatePath ) ;
62+ }
6863
69- this . Certificate = new X509Certificate2 ( this . CertificatePath ) ;
64+ return result ;
7065 }
7166
72- internal void ApplyProxy ( ILogger logger , ProcessStartInfo startInfo )
67+ private DependabotProxy ( string host , string port )
7368 {
74- // If the proxy isn't configured, we have nothing to do.
75- if ( ! this . IsConfigured ) return ;
76-
77- logger . LogInfo ( $ "Setting up Dependabot proxy at { this . Address } ") ;
78-
79- startInfo . EnvironmentVariables . Add ( "HTTP_PROXY" , this . Address ) ;
80- startInfo . EnvironmentVariables . Add ( "HTTPS_PROXY" , this . Address ) ;
81- startInfo . EnvironmentVariables . Add ( "SSL_CERT_FILE" , this . certFile ? . FullName ) ;
69+ this . host = host ;
70+ this . port = port ;
71+ this . Address = $ "http://{ this . host } :{ this . port } ";
8272 }
8373
8474 public void Dispose ( )
8575 {
86- if ( this . Certificate != null )
87- {
88- this . Certificate . Dispose ( ) ;
89- }
76+ this . Certificate ? . Dispose ( ) ;
9077 }
9178 }
9279}
0 commit comments