129 lines
4.1 KiB
C#
129 lines
4.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using Amazon.S3;
|
|
|
|
//for ssl certs
|
|
using System.Security.Cryptography.X509Certificates;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace rockfishCore.Util
|
|
{
|
|
public static class OpsDiagnostics
|
|
{
|
|
private const int S3_EXPECTED_FILE_COUNT = 24;
|
|
public static readonly string[] S3_EXPECTED_BACKUP_FILE_PREFIXES =
|
|
{ "biz-docker-website-backup-", "biz-letsencryptbackup-", "biz-pecklist-db-backup-", "biz-rockfish-db-backup-",
|
|
"devops-docker-le-config-backup-","forum-backup-", "mail-svn-repo-backup-" };
|
|
private const string S3_SECRET_KEY = "iNwbHr+sK+9is2wmRjIax+rdyEjLNvWKJBYr7w4txkY";
|
|
private const string S3_ACCESS_KEY = "CMPAFDNX53OWPC55HBJ4";
|
|
private const string S3_HOST_ENDPOINT = "https://nyc3.digitaloceanspaces.com";
|
|
private const string S3_BUCKET_NAME = "gztw1";
|
|
|
|
private static HttpClient Client = new HttpClient();
|
|
|
|
|
|
public static bool CheckWebsite(string url, string mustContain)
|
|
{
|
|
bool Result = false;
|
|
var Response = Client.GetAsync(url).Result;
|
|
if (Response.IsSuccessStatusCode)
|
|
{
|
|
var PageText = Response.Content.ReadAsStringAsync().Result;
|
|
if (PageText.Contains(mustContain))
|
|
{
|
|
Result = true;
|
|
}
|
|
}
|
|
return Result;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// check if an ssl cert is within 10 days of expiry
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <returns></returns>
|
|
public static bool IsSSLCertAboutToExpire(string url)
|
|
{
|
|
var expires = GetServerCertificateExpiryAsync(url).Result;
|
|
//In 10 days will we be past the expiry date
|
|
var deadline = DateTime.Now.AddDays(10);
|
|
if (expires < deadline)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
|
|
static async Task<DateTime> GetServerCertificateExpiryAsync(string url)
|
|
{
|
|
DateTime ret = DateTime.MinValue;
|
|
var httpClientHandler = new HttpClientHandler
|
|
{
|
|
ServerCertificateCustomValidationCallback = (_, cert, __, ___) =>
|
|
{
|
|
ret = cert.NotAfter;
|
|
return true;
|
|
}
|
|
};
|
|
|
|
var httpClient = new HttpClient(httpClientHandler);
|
|
await httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Head, url));
|
|
|
|
return ret;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static List<string> GetFileListFromSpacesBackupStorage()
|
|
{
|
|
AmazonS3Config ClientConfig = new AmazonS3Config();
|
|
ClientConfig.ServiceURL = S3_HOST_ENDPOINT;
|
|
IAmazonS3 s3Client = new AmazonS3Client(S3_ACCESS_KEY, S3_SECRET_KEY, ClientConfig);
|
|
var ObjectList = s3Client.ListObjectsAsync(S3_BUCKET_NAME).Result;
|
|
var FileList = ObjectList.S3Objects.Select(c => c.Key).ToList();
|
|
return FileList;
|
|
}
|
|
|
|
public static bool VerifyBackups()
|
|
{
|
|
bool ret = false;
|
|
var SpacesFileNames = GetFileListFromSpacesBackupStorage();
|
|
if (SpacesFileNames.Count != S3_EXPECTED_FILE_COUNT) return ret;
|
|
//get yesterday's date in the same format as the backup creates
|
|
var ExpectedBackupDateString = DateTime.Today.AddDays(-1).ToString("yyMMdd");
|
|
int foundMatches = 0;
|
|
foreach (string ExpectedFileName in S3_EXPECTED_BACKUP_FILE_PREFIXES)
|
|
{
|
|
foreach (string FileName in SpacesFileNames)
|
|
{
|
|
if (FileName.StartsWith(ExpectedFileName + ExpectedBackupDateString))
|
|
{
|
|
foundMatches++;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (foundMatches == S3_EXPECTED_BACKUP_FILE_PREFIXES.Length)
|
|
{
|
|
ret = true;
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
}//eoc
|
|
|
|
}//eons |