Skip to content

Commit f346e38

Browse files
committed
C#: Add DependabotProxy class
1 parent b407a5a commit f346e38

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.IO;
3+
using Semmle.Util;
4+
5+
namespace Semmle.Extraction.CSharp.DependencyFetching
6+
{
7+
internal class DependabotProxy
8+
{
9+
private readonly string? host;
10+
private readonly string? port;
11+
private readonly FileInfo? certFile;
12+
13+
/// <summary>
14+
/// The full address of the Dependabot proxy, if available.
15+
/// </summary>
16+
internal readonly string? Address;
17+
18+
/// <summary>
19+
/// Gets a value indicating whether a Dependabot proxy is configured.
20+
/// </summary>
21+
internal bool IsConfigured => !string.IsNullOrEmpty(this.Address);
22+
23+
internal DependabotProxy(TemporaryDirectory tempWorkingDirectory)
24+
{
25+
// Obtain and store the address of the Dependabot proxy, if available.
26+
this.host = Environment.GetEnvironmentVariable(EnvironmentVariableNames.ProxyHost);
27+
this.port = Environment.GetEnvironmentVariable(EnvironmentVariableNames.ProxyPort);
28+
29+
if (string.IsNullOrWhiteSpace(host) || string.IsNullOrWhiteSpace(port))
30+
{
31+
return;
32+
}
33+
34+
this.Address = $"http://{this.host}:{this.port}";
35+
36+
// Obtain and store the proxy's certificate, if available.
37+
var cert = Environment.GetEnvironmentVariable(EnvironmentVariableNames.ProxyCertificate);
38+
39+
if (string.IsNullOrWhiteSpace(cert))
40+
{
41+
return;
42+
}
43+
44+
var certDirPath = new DirectoryInfo(Path.Join(tempWorkingDirectory.DirInfo.FullName, ".dependabot-proxy"));
45+
Directory.CreateDirectory(certDirPath.FullName);
46+
47+
this.certFile = new FileInfo(Path.Join(certDirPath.FullName, "proxy.crt"));
48+
49+
using var writer = this.certFile.CreateText();
50+
writer.Write(cert);
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)