-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3. PerformCertificateRenewal.cs
112 lines (97 loc) · 4.54 KB
/
3. PerformCertificateRenewal.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using BSS.Logging;
using Renci.SshNet;
using System;
namespace CertBotHelper
{
internal static partial class Program
{
private static Boolean PerformCertificateRenewal(SftpClient sftpClient, ref Configuration configuration)
{
#region Prepare and Connect
try
{
if (sftpClient.Exists(configuration.RemoteCertBotPath + "cert.crt"))
{
Log.FastLog("Found cert.crt in remote CertBot directory, removing.", LogSeverity.Info, "PreRenewal");
sftpClient.DeleteFile(configuration.RemoteCertBotPath + "cert.crt");
}
if (sftpClient.Exists(configuration.RemoteCertBotPath + "chain.crt"))
{
Log.FastLog("Found chain.crt in remote CertBot directory, removing.", LogSeverity.Info, "PreRenewal");
sftpClient.DeleteFile(configuration.RemoteCertBotPath + "chain.crt");
}
if (sftpClient.Exists(configuration.RemoteCertBotPath + "fullChain.crt"))
{
Log.FastLog("Found fullChain.crt in remote CertBot directory, removing.", LogSeverity.Info, "PreRenewal");
sftpClient.DeleteFile(configuration.RemoteCertBotPath + "fullChain.crt");
}
}
catch (Exception exception)
{
Log.FastLog($"An error occurred whilst preparing the remote certificate directory\n{exception.Message}", LogSeverity.Error, "PreCheck");
return false;
}
SshClient sshClient;
try
{
sshClient = new(configuration.CertBotIP, configuration.SSHUsername, new PrivateKeyFile(configuration.SSHPrivateKeyPath));
}
catch (Exception exception)
{
Log.FastLog($"Unable to create new SshClient, invalid private key?\n{exception.Message}", LogSeverity.Critical, "SshClient");
return false;
}
try
{
sshClient.Connect();
Log.FastLog($"Connected to " + configuration.CertBotIP, LogSeverity.Info, "SshClient");
}
catch (Exception exception)
{
Log.FastLog($"Unable to connect to {configuration.CertBotIP}\n{exception.Message}", LogSeverity.Critical, "SshClient");
return false;
}
#endregion
String stdOut;
String errOut;
#region Run Command
try
{
Log.FastLog("Attempting to run renewal command on machine", LogSeverity.Info, "CertRenewal");
SshCommand sshCommand = sshClient.CreateCommand(configuration.RenewalCommand);
stdOut = sshCommand.Execute();
errOut = sshCommand.Error;
sshCommand.Dispose();
sshClient.Disconnect();
sshClient.Dispose();
}
catch (Exception exception)
{
Log.FastLog($"Failed to run renewal command on remote machine\n{exception.Message}", LogSeverity.Critical, "CertRenewal");
return false;
}
#endregion
#region Validate after Command
try
{
Boolean filesArePresent = true;
if (!sftpClient.Exists(configuration.RemoteCertBotPath + "cert.crt")) filesArePresent = false;
if (!sftpClient.Exists(configuration.RemoteCertBotPath + "chain.crt")) filesArePresent = false;
if (!sftpClient.Exists(configuration.RemoteCertBotPath + "fullChain.crt")) filesArePresent = false;
if (!filesArePresent)
{
Log.FastLog("Not all certificates were found! CertBot errOut output was:\n" + errOut, LogSeverity.Error, "PostRenewCheck");
return false;
}
Log.FastLog("All certificates present on server after renewal, proceeding with download", LogSeverity.Info, "PostRenewCheck");
}
catch (Exception exception)
{
Log.FastLog($"An error occurred whilst checking if all certificates are present on the server\n{exception.Message}", LogSeverity.Error, "PostRenewCheck");
return false;
}
#endregion
return true;
}
}
}