2929using System . Text ;
3030using System . Net . Http . Headers ;
3131using Microsoft . Extensions . Logging ;
32+ using Org . BouncyCastle . Asn1 . Ocsp ;
3233
3334namespace Keyfactor . Extensions . Orchestrator . Fortigate
3435{
@@ -53,6 +54,7 @@ public class FortigateStore
5354 private static readonly string delete_certificate_api = "/api/v2/cmdb/vpn.certificate/local/" ;
5455
5556 private static readonly string cert_usage_api = "/api/v2/monitor/system/object/usage" ;
57+ private static readonly string https_usage_api = "/api/v2/cmdb/system/global" ;
5658
5759 private readonly HttpClientHandler handler = new HttpClientHandler ( )
5860 {
@@ -149,6 +151,46 @@ public Usage Usage(string alias, int qtype)
149151 }
150152 }
151153
154+ public string HttpsServerUsage ( )
155+ {
156+ logger . MethodEntry ( LogLevel . Debug ) ;
157+
158+ try
159+ {
160+ var result = GetResource ( https_usage_api , new Dictionary < String , String > ( ) ) ;
161+ return JsonConvert . DeserializeObject < FortigateResponse < HttpsUsage > > ( result ) . results . AdminServerCert ;
162+ }
163+ catch ( Exception ex )
164+ {
165+ logger . LogError ( FortigateException . FlattenExceptionMessages ( ex , $ "Error checking https bindings: ") ) ;
166+ throw ;
167+ }
168+ finally
169+ {
170+ logger . MethodExit ( LogLevel . Debug ) ;
171+ }
172+ }
173+
174+ public void UpdateHttpsServerUsage ( string alias )
175+ {
176+ logger . MethodEntry ( LogLevel . Debug ) ;
177+ HttpUsageRequest request = new HttpUsageRequest ( ) { AdminServerCert = new OriginKey ( ) { QOriginKey = alias } } ;
178+
179+ try
180+ {
181+ PutAsJson ( https_usage_api , request , new Dictionary < string , string > ( ) ) ;
182+ }
183+ catch ( Exception ex )
184+ {
185+ logger . LogError ( FortigateException . FlattenExceptionMessages ( ex , $ "Error updating https server binding: ") ) ;
186+ throw ;
187+ }
188+ finally
189+ {
190+ logger . MethodExit ( LogLevel . Debug ) ;
191+ }
192+ }
193+
152194 public void Insert ( string alias , string cert , string privateKey , bool overwrite , string password = null )
153195 {
154196 logger . MethodEntry ( LogLevel . Debug ) ;
@@ -170,9 +212,10 @@ public void Insert(string alias, string cert, string privateKey, bool overwrite,
170212
171213 //check to see if it's in use
172214 existingUsage = Usage ( alias , certItem . q_type ) ;
215+ bool existingHttpsUsage = HttpsServerUsage ( ) == alias ;
173216
174217 //if it's currently in use
175- if ( existingUsage != null && existingUsage . currently_using != null && existingUsage . currently_using . Length > 0 )
218+ if ( ( existingUsage != null && existingUsage . currently_using != null && existingUsage . currently_using . Length > 0 ) || existingHttpsUsage )
176219 {
177220 //if newAlias exists, end with error
178221 if ( byNewAlias . Length > 0 )
@@ -184,10 +227,18 @@ public void Insert(string alias, string cert, string privateKey, bool overwrite,
184227 logger . LogDebug ( "Inserting alias:" + newAlias ) ;
185228 Insert ( newAlias , cert , privateKey , password ) ;
186229
187- foreach ( var existingUsing in existingUsage . currently_using )
230+ if ( existingUsage != null && existingUsage . currently_using != null && existingUsage . currently_using . Length > 0 )
231+ {
232+ foreach ( var existingUsing in existingUsage . currently_using )
233+ {
234+ logger . LogDebug ( $ "Update binding for path/name/attribute { existingUsing . path } /{ existingUsing . name } /{ existingUsing . attribute } for new alias { newAlias } ") ;
235+ UpdateUsage ( newAlias , existingUsing . path , existingUsing . name , existingUsing . attribute ) ;
236+ }
237+ }
238+
239+ if ( existingHttpsUsage )
188240 {
189- logger . LogDebug ( $ "Update binding for path/name/attribute { existingUsing . path } /{ existingUsing . name } /{ existingUsing . attribute } for new alias { newAlias } ") ;
190- UpdateUsage ( newAlias , existingUsing . path , existingUsing . name , existingUsing . attribute ) ;
241+ UpdateHttpsServerUsage ( newAlias ) ;
191242 }
192243
193244 logger . LogDebug ( "Deleting alias:" + alias ) ;
0 commit comments