@@ -9,84 +9,71 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
9
9
{
10
10
public class DependabotProxy : IDisposable
11
11
{
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 ;
15
14
16
15
/// <summary>
17
16
/// The full address of the Dependabot proxy, if available.
18
17
/// </summary>
19
- internal readonly string ? Address ;
18
+ internal string Address { get ; }
20
19
/// <summary>
21
20
/// The path to the temporary file where the certificate is stored.
22
21
/// </summary>
23
- internal readonly string ? CertificatePath ;
22
+ internal string ? CertificatePath { get ; private set ; }
24
23
/// <summary>
25
24
/// The certificate used for the Dependabot proxy.
26
25
/// </summary>
27
- internal readonly X509Certificate2 ? Certificate ;
26
+ internal X509Certificate2 ? Certificate { get ; private set ; }
28
27
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 )
35
29
{
36
30
// 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 ) ;
39
33
40
34
if ( string . IsNullOrWhiteSpace ( host ) || string . IsNullOrWhiteSpace ( port ) )
41
35
{
42
36
logger . LogInfo ( "No Dependabot proxy credentials are configured." ) ;
43
- return ;
37
+ return null ;
44
38
}
45
39
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 } ") ;
48
42
49
43
// Obtain and store the proxy's certificate, if available.
50
44
var cert = Environment . GetEnvironmentVariable ( EnvironmentVariableNames . ProxyCertificate ) ;
51
45
52
- if ( string . IsNullOrWhiteSpace ( cert ) )
46
+ if ( ! string . IsNullOrWhiteSpace ( cert ) )
53
47
{
54
48
logger . LogInfo ( "No certificate configured for Dependabot proxy." ) ;
55
- return ;
56
- }
57
49
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 ) ;
60
55
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 ) ;
63
58
64
- using var writer = this . certFile . CreateText ( ) ;
65
- writer . Write ( cert ) ;
59
+ logger . LogInfo ( $ "Stored Dependabot proxy certificate at { result . CertificatePath } ") ;
66
60
67
- logger . LogInfo ( $ "Stored Dependabot proxy certificate at { this . CertificatePath } ") ;
61
+ result . Certificate = new X509Certificate2 ( result . CertificatePath ) ;
62
+ }
68
63
69
- this . Certificate = new X509Certificate2 ( this . CertificatePath ) ;
64
+ return result ;
70
65
}
71
66
72
- internal void ApplyProxy ( ILogger logger , ProcessStartInfo startInfo )
67
+ private DependabotProxy ( string host , string port )
73
68
{
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 } ";
82
72
}
83
73
84
74
public void Dispose ( )
85
75
{
86
- if ( this . Certificate != null )
87
- {
88
- this . Certificate . Dispose ( ) ;
89
- }
76
+ this . Certificate ? . Dispose ( ) ;
90
77
}
91
78
}
92
79
}
0 commit comments