Skip to content

Commit 878019a

Browse files
author
Elena Fiocca
committed
Repush local changes
1 parent c3dbef2 commit 878019a

8 files changed

Lines changed: 237 additions & 325 deletions

File tree

AxisIPCamera/Client/AxisHttpClient.cs

Lines changed: 48 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Reflection;
43
using System.IO;
54
using System.Net;
65
using System.Net.Http;
7-
using System.Runtime.ConstrainedExecution;
6+
using System.Text;
7+
using System.Xml;
8+
89
using Microsoft.Extensions.Logging;
9-
using System.Threading;
10-
using System.Threading.Tasks;
11-
using Keyfactor.Logging;
12-
using Keyfactor.Orchestrators.Extensions;
13-
using Keyfactor.Extensions.Orchestrator.AxisIPCamera.Model;
1410
using Newtonsoft.Json;
1511
using RestSharp;
1612
using RestSharp.Authenticators;
17-
using System.Xml;
13+
14+
using Keyfactor.Logging;
15+
using Keyfactor.Orchestrators.Extensions;
16+
using Keyfactor.Extensions.Orchestrator.AxisIPCamera.Model;
17+
using Keyfactor.Orchestrators.Extensions.Interfaces;
18+
using Keyfactor.Extensions.Orchestrator.AxisIPCamera.Helpers;
1819

1920
/* AxisHttpClient.cs
2021
* ---------------------------------------------------------------------------------------------------
@@ -53,112 +54,76 @@ public class AxisHttpClient
5354
private readonly RestClient _httpClient;
5455
private ILogger Logger { get; }
5556

56-
public AxisHttpClient(JobConfiguration config, CertificateStore store)
57+
public AxisHttpClient(JobConfiguration config, CertificateStore store, IPAMSecretResolver resolver)
5758
{
5859
try
5960
{
61+
var errorContext = new CertificateErrorContext();
62+
6063
Logger = LogHandler.GetClassLogger<AxisHttpClient>();
61-
Logger.MethodEntry();
62-
63-
// TODO REMOVE
64-
bool vetDevice = true;
65-
64+
Logger.LogTrace("Entered AxisHttpClient constructor.");
6665
Logger.LogTrace("Initializing Axis IP Camera HTTP Client");
6766

68-
var baseRestClientUrl =
69-
(config.UseSSL) ? $"https://{store.ClientMachine}" : $"http://{store.ClientMachine}";
70-
71-
// TODO: Need to consider onboarding of camera
67+
//TODO; REMOVE var baseRestClientUrl =
68+
//(config.UseSSL) ? $"https://{store.ClientMachine}" : $"http://{store.ClientMachine}";
69+
// NOTE: Ignoring the default config.UseSSL custom field --- we will always connect to the device via HTTPS
70+
var baseRestClientUrl = $"https://{store.ClientMachine}";
71+
7272
Logger.LogDebug($"Base HTTP Client URL: {baseRestClientUrl}");
7373

74-
// If vetting the device, initialize custom HTTP handler for onboarding of device
74+
// Initialize custom HTTP handler to validate device identity
7575
RestClientOptions options = null;
76-
if (vetDevice)
76+
Logger.LogTrace($"Adding custom SSL cert validator to the HTTP client options...");
77+
var handler = new HttpClientHandler
7778
{
78-
Logger.LogInformation($"Vet device: {vetDevice} --- Looking at custom cert validator");
79-
var handler = new HttpClientHandler
80-
{
81-
ServerCertificateCustomValidationCallback =
82-
DeviceCertValidator.GetValidator(store.StorePath, Logger)
83-
};
79+
ServerCertificateCustomValidationCallback =
80+
DeviceCertValidator.GetValidator(store.StorePath, errorContext, Logger)
81+
};
8482

85-
// Initialize HTTP client options with the base URL and custom cert validator
86-
options = new RestClientOptions(baseRestClientUrl)
87-
{
88-
ConfigureMessageHandler = _ => handler
89-
};
90-
}
91-
else
83+
// Initialize HTTP client options with the base URL and custom SSL cert validator
84+
options = new RestClientOptions(baseRestClientUrl)
9285
{
93-
// Initialize HTTP client options with the base URL
94-
options = new RestClientOptions(baseRestClientUrl);
95-
options.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
96-
}
86+
ConfigureMessageHandler = _ => handler
87+
};
9788

9889
// Add Basic Auth username and password credentials
9990
Logger.LogTrace("Adding Basic Auth Credentials to the HTTP client options...");
10091

92+
string username = PAMUtilities.ResolvePAMField(resolver, Logger, "API Username", config.ServerUsername);
93+
string password = PAMUtilities.ResolvePAMField(resolver, Logger, "API Password", config.ServerPassword);
94+
10195
// TODO: Do we want to remove this log statement in PRODUCTION?
102-
Logger.LogDebug($"Username: {config.ServerUsername}, Password: {config.ServerPassword}");
96+
Logger.LogDebug($"Username: {username}, Password: {password}");
10397
options.Authenticator = new HttpBasicAuthenticator(config.ServerUsername, config.ServerPassword);
10498

10599
// Add SSL validation
106-
Logger.LogTrace("Checking for SSL validation...");
107-
Logger.LogDebug($"Use SSL: {config.UseSSL}");
108-
109-
Logger.LogTrace("Turning off SSL validation --- FOR TESTING ONLY");
110-
111-
// TODO: Enable this flag in PRODUCTION
112-
//if (config.UseSSL)
113-
//{
114-
// TODO FOR TESTING: options.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
115-
//}
100+
Logger.LogTrace("Validating connection to the device...");
116101

117102
_httpClient = new RestClient(options);
103+
var request = new RestRequest("/"); // Initiates the TLS handshake to retrieve the server cert
104+
var response = _httpClient.Execute(request);
118105

119-
// TODO FOR TESTING
120-
if (vetDevice)
106+
// Build the list of errors to log to the console
107+
StringBuilder errorSb = new StringBuilder();
108+
if (errorContext.HasErrors)
121109
{
122-
var request = new RestRequest("/"); // Initiates the TLS handshake to retrieve the server cert
123-
var response = _httpClient.Execute(request);
124-
Logger.LogTrace($"Response status: {response.StatusCode}");
125-
126-
/*if (!response.IsSuccessful)
110+
foreach (var error in errorContext.Errors)
127111
{
128-
if (response.StatusCode == 0 && response.ErrorException != null)
129-
{
130-
// Likely caused by TLS/Cert validation failure
131-
Logger.LogError(response.ErrorException, "TLS handshake or certificate validation failed.");
132-
throw new Exception("INVALID DEVICE --- Cert chain validation failed.",
133-
response.ErrorException);
134-
}
135-
136-
Logger.LogError("Request failed. Status: {Status}, Message: {Message}",
137-
response.StatusCode, response.ErrorMessage);
138-
139-
throw new Exception("Request failed: " + response.ErrorMessage);
140-
}*/
112+
errorSb.AppendLine(error);
113+
}
114+
throw new Exception(errorSb.ToString());
141115
}
142-
116+
117+
Logger.LogTrace($"Connection to the device response status code: {response.StatusCode}");
143118

144119
Logger.LogTrace("Completed Initialization of Axis IP Camera HTTP Client");
145120

146-
Logger.MethodExit();
147-
}
148-
catch (CertificateSslException e1)
149-
{
150-
Logger.LogError("Certificate SSL validation failed: " + LogHandler.FlattenException(e1));
151-
throw;
152-
}
153-
catch (CertificateSubjectValidationException e2)
154-
{
155-
Logger.LogError("Subject validation failed: " + LogHandler.FlattenException(e2));
156-
throw;
121+
Logger.LogTrace("Leaving AxisHttpClient constructor.");
157122
}
158123
catch (Exception e)
159124
{
160-
Logger.LogError("Error in Constructor AxisRestClient(): " + LogHandler.FlattenException(e));
161-
throw;
125+
Logger.LogError("Error initializing Axis IP Camera HTTP Client: " + LogHandler.FlattenException(e));
126+
throw new Exception($"Device identity could not be verified successfully --- {e.Message}");
162127
}
163128
}
164129

@@ -988,7 +953,7 @@ public string GetCertUsageBinding(Constants.CertificateUsage certUsage)
988953
break;
989954
}
990955

991-
Logger.LogDebug($"Bound Certificate Alias: {boundCertAlias}");
956+
Logger.LogDebug($"Bound certificate alias for '{certUsageString}': {boundCertAlias}");
992957

993958
Logger.MethodExit();
994959
}

AxisIPCamera/Client/AxisSoapClient.cs

Lines changed: 0 additions & 67 deletions
This file was deleted.

AxisIPCamera/Helpers/CertificateErrorContext.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ public void Add(string error)
1212
Errors.Add(error);
1313
}
1414

15+
public void Insert(int index, string error)
16+
{
17+
Errors.Insert(index, error);
18+
}
19+
1520
public bool HasErrors => Errors.Any();
1621
}
1722
}

0 commit comments

Comments
 (0)