1- // Copyright 2022 Keyfactor
1+ // Copyright 2026 Keyfactor
22//
33// Licensed under the Apache License, Version 2.0 (the "License");
44// you may not use this file except in compliance with the License.
@@ -26,35 +26,48 @@ namespace Keyfactor.Extensions.Orchestrator.AlteonLoadBalancer
2626 public class AlteonLoadBalancerClient
2727 {
2828 private RestClient _restClient { get ; set ; }
29- ILogger logger = LogHandler . GetClassLogger < AlteonLoadBalancerClient > ( ) ;
29+ protected ILogger logger { get ; set ; }
3030
31- public AlteonLoadBalancerClient ( string baseUrl , string username , string password )
31+ public AlteonLoadBalancerClient ( string baseUrl , string username , string password , ILogger logger )
3232 {
33+ this . logger = logger ;
34+
3335 var options = new RestClientOptions ( baseUrl )
3436 {
35- RemoteCertificateValidationCallback = ( sender , certificate , chain , sslPolicyErrors ) => true ,
37+ RemoteCertificateValidationCallback = ( sender , certificate , chain , sslPolicyErrors ) => true , // this is to allow self-signed appliance certs
3638 Authenticator = new HttpBasicAuthenticator ( username , password )
3739 } ;
3840 _restClient = new RestClient ( options ) ;
3941 }
4042
4143 public async Task < CertificateTableEntryCollection > GetCertificates ( )
4244 {
45+ logger . MethodEntry ( ) ;
46+
4347 var request = new RestRequest ( Endpoints . CertificateRepository ) ;
48+
49+ logger . LogTrace ( $ "making request to retrieve certificates from endpoint: { request . Resource } ") ;
4450 try
4551 {
4652 var response = await _restClient . GetAsync < CertificateTableEntryCollection > ( request ) ;
4753 return response ;
4854 }
4955 catch ( Exception ex )
5056 {
57+ logger . LogError ( $ "An error occurred when attempting to retrieve the certificates from { _restClient . BuildUri ( request ) ? . ToString ( ) } ") ;
5158 logger . LogError ( ex . Message , ex ) ;
5259 throw ;
5360 }
61+ finally
62+ {
63+ logger . MethodExit ( ) ;
64+ }
5465 }
5566
5667 public async Task < CertificateTableEntryCollection > GetCertificatesById ( string id )
5768 {
69+ logger . MethodEntry ( ) ;
70+
5871 var url = $ "{ Endpoints . CertificateRepository } ?filter=ID:{ id } &filtertype=exact&props=ID,Type";
5972 var request = new RestRequest ( url ) ;
6073
@@ -72,6 +85,7 @@ public async Task<CertificateTableEntryCollection> GetCertificatesById(string id
7285 logger . LogError ( ex . Message , ex ) ;
7386 throw ;
7487 }
88+ finally { logger . MethodExit ( ) ; }
7589 }
7690
7791 public string GetCertificateContent ( string certId )
@@ -82,7 +96,7 @@ public string GetCertificateContent(string certId)
8296 request . AddQueryParameter ( "type" , "srvcrt" ) ;
8397 var fullUri = _restClient . BuildUri ( request ) ;
8498
85- logger . LogTrace ( $ "making request to get certificate to uri : { fullUri } ") ;
99+ logger . LogTrace ( $ "making request to get certificate from the endpoint : { fullUri } ") ;
86100
87101 try
88102 {
@@ -93,9 +107,11 @@ public string GetCertificateContent(string certId)
93107 }
94108 catch ( Exception ex )
95109 {
110+ logger . LogError ( $ "An error occurred when attempting to retrieve the certificate with id '{ certId } ' from { fullUri } ") ;
96111 logger . LogError ( ex . Message , ex ) ;
97112 throw ;
98113 }
114+ finally { logger . MethodExit ( ) ; }
99115 }
100116
101117 public async Task AddCertificate ( string alias , string pfxPassword , string certContents , string type , bool overwrite )
@@ -149,6 +165,7 @@ public async Task AddCertificate(string alias, string pfxPassword, string certCo
149165 internal async Task RemoveCertificate ( string alias )
150166 {
151167 logger . MethodEntry ( ) ;
168+ var url = string . Empty ;
152169
153170 var existing = ( await GetCertificatesById ( alias ) ) . SlbNewSslCfgCertsTable ;
154171 if ( existing . Count == 0 )
@@ -157,28 +174,32 @@ internal async Task RemoveCertificate(string alias)
157174 }
158175 try
159176 {
160- existing . ForEach ( c =>
177+ foreach ( var c in existing )
161178 {
162- var url = $ "{ Endpoints . CertificateRepository } /{ c . ID } /{ c . Type } ";
179+ url = $ "{ Endpoints . CertificateRepository } /{ c . ID } /{ c . Type } ";
163180 var request = new RestRequest ( url , Method . Delete ) ;
164181 var fullUri = _restClient . BuildUri ( request ) ;
165182 logger . LogTrace ( $ "making request to remove certificate to uri { fullUri } ") ;
166- var response = _restClient . DeleteAsync ( request ) . Result ;
183+ var response = await _restClient . DeleteAsync ( request ) ;
167184
168185 if ( ! response . IsSuccessful )
169186 {
170187 throw new Exception ( $ "Failed to remove certificate: { alias } ", response . ErrorException ) ;
171188 }
172- } ) ;
189+ }
173190 // apply and save changes
174191 await ApplyAndSave ( ) ;
175192 }
176193 catch ( Exception ex )
177194 {
195+ logger . LogError ( $ "An error occurred when attempting to remove the certificate with alias { alias } via endpoint: '{ url } '") ;
178196 logger . LogError ( ex . Message , ex ) ;
179197 throw ;
180198 }
181- logger . MethodExit ( ) ;
199+ finally
200+ {
201+ logger . MethodExit ( ) ;
202+ }
182203 }
183204
184205 /// <summary>
@@ -187,7 +208,7 @@ internal async Task RemoveCertificate(string alias)
187208 /// <returns></returns>
188209 internal async Task ApplyAndSave ( )
189210 {
190- logger . MethodEntry ( ) ;
211+ logger . MethodEntry ( ) ;
191212 logger . LogTrace ( $ "making requests to apply and save changes") ;
192213 try
193214 {
0 commit comments