1
1
using System ;
2
- using System . Diagnostics ;
2
+ using System . Collections . Generic ;
3
3
using System . IO ;
4
4
using System . Security . Cryptography . X509Certificates ;
5
5
using Semmle . Util ;
6
6
using Semmle . Util . Logging ;
7
+ using Newtonsoft . Json ;
7
8
8
9
namespace Semmle . Extraction . CSharp . DependencyFetching
9
10
{
10
11
public class DependabotProxy : IDisposable
11
12
{
13
+ /// <summary>
14
+ /// Represents configurations for package registries.
15
+ /// </summary>
16
+ /// <param name="Type">The type of package registry.</param>
17
+ /// <param name="URL">The URL of the package registry.</param>
18
+ public record class RegistryConfig ( string Type , string URL ) ;
19
+
12
20
private readonly string host ;
13
21
private readonly string port ;
14
22
@@ -17,6 +25,10 @@ public class DependabotProxy : IDisposable
17
25
/// </summary>
18
26
internal string Address { get ; }
19
27
/// <summary>
28
+ /// The URLs of package registries that are configured for the proxy.
29
+ /// </summary>
30
+ internal HashSet < string > RegistryURLs { get ; }
31
+ /// <summary>
20
32
/// The path to the temporary file where the certificate is stored.
21
33
/// </summary>
22
34
internal string ? CertificatePath { get ; private set ; }
@@ -67,6 +79,39 @@ public class DependabotProxy : IDisposable
67
79
result . Certificate = X509Certificate2 . CreateFromPem ( cert ) ;
68
80
}
69
81
82
+ // Try to obtain the list of private registry URLs.
83
+ var registryURLs = Environment . GetEnvironmentVariable ( EnvironmentVariableNames . ProxyURLs ) ;
84
+
85
+ if ( ! string . IsNullOrWhiteSpace ( registryURLs ) )
86
+ {
87
+ try
88
+ {
89
+ // The value of the environment variable should be a JSON array of objects, such as:
90
+ // [ { "type": "nuget_feed", "url": "https://nuget.pkg.github.com/org/index.json" } ]
91
+ var array = JsonConvert . DeserializeObject < List < RegistryConfig > > ( registryURLs ) ;
92
+ if ( array is not null )
93
+ {
94
+ foreach ( RegistryConfig config in array )
95
+ {
96
+ // The array contains all configured private registries, not just ones for C#.
97
+ // We ignore the non-C# ones here.
98
+ if ( ! config . Type . Equals ( "nuget_feed" ) )
99
+ {
100
+ logger . LogDebug ( $ "Ignoring registry at '{ config . URL } ' since it is not of type 'nuget_feed'.") ;
101
+ continue ;
102
+ }
103
+
104
+ logger . LogInfo ( $ "Found private registry at '{ config . URL } '") ;
105
+ result . RegistryURLs . Add ( config . URL ) ;
106
+ }
107
+ }
108
+ }
109
+ catch ( JsonException ex )
110
+ {
111
+ logger . LogError ( $ "Unable to parse '{ EnvironmentVariableNames . ProxyURLs } ': { ex . Message } ") ;
112
+ }
113
+ }
114
+
70
115
return result ;
71
116
}
72
117
@@ -75,6 +120,7 @@ private DependabotProxy(string host, string port)
75
120
this . host = host ;
76
121
this . port = port ;
77
122
this . Address = $ "http://{ this . host } :{ this . port } ";
123
+ this . RegistryURLs = new HashSet < string > ( ) ;
78
124
}
79
125
80
126
public void Dispose ( )
0 commit comments