/// <summary> /// Setup a new scheduled renewal /// </summary> /// </summary> /// <param name="runLevel"></param> /// <param name="steps"></param> /// <param name="tempRenewal"></param> /// <returns></returns> internal async Task SetupRenewal(RunLevel runLevel, Steps steps = Steps.All, Renewal?tempRenewal = null) { if (_args.Test) { runLevel |= RunLevel.Test; } if (_args.Force) { runLevel |= RunLevel.IgnoreCache; } _log.Information(LogType.All, "Running in mode: {runLevel}", runLevel); if (tempRenewal == null) { tempRenewal = Renewal.Create(_args.Id, _passwordGenerator); } using var configScope = _scopeBuilder.Configuration(_container, tempRenewal, runLevel); // Choose the target plugin if (steps.HasFlag(Steps.Target)) { var targetOptions = await SetupTarget(configScope, runLevel); if (targetOptions == null) { return; } tempRenewal.TargetPluginOptions = targetOptions; } // Generate initial target using var targetScope = _scopeBuilder.Target(_container, tempRenewal, runLevel); var initialTarget = targetScope.Resolve <Target>(); if (initialTarget is INull) { _exceptionHandler.HandleException(message: $"Source plugin {tempRenewal.TargetPluginOptions.Name} was unable to generate the certificate parameters."); return; } if (!initialTarget.IsValid(_log)) { _exceptionHandler.HandleException(message: $"Source plugin {tempRenewal.TargetPluginOptions.Name} generated invalid certificate parameters"); return; } _log.Information("Source generated using plugin {name}: {target}", tempRenewal.TargetPluginOptions.Name, initialTarget); // Setup the friendly name var ask = runLevel.HasFlag(RunLevel.Advanced | RunLevel.Interactive) && steps.HasFlag(Steps.Target); await SetupFriendlyName(tempRenewal, initialTarget, ask); // Choose the validation plugin if (steps.HasFlag(Steps.Validation)) { var validationOptions = await SetupValidation(targetScope, initialTarget, runLevel); if (validationOptions == null) { return; } tempRenewal.ValidationPluginOptions = validationOptions; } // Choose the order plugin if (steps.HasFlag(Steps.Order)) { tempRenewal.OrderPluginOptions = await SetupOrder(targetScope, runLevel); if (tempRenewal.OrderPluginOptions == null) { return; } } // Choose the CSR plugin if (initialTarget.UserCsrBytes != null) { tempRenewal.CsrPluginOptions = null; } else if (steps.HasFlag(Steps.Csr)) { tempRenewal.CsrPluginOptions = await SetupCsr(configScope, runLevel); if (tempRenewal.CsrPluginOptions == null) { return; } } // Choose store plugin(s) if (steps.HasFlag(Steps.Store)) { var store = await SetupStore(configScope, runLevel); if (store != null) { tempRenewal.StorePluginOptions = store; } else { return; } } // Choose installation plugin(s) if (steps.HasFlag(Steps.Installation)) { var install = await SetupInstallation(configScope, runLevel, tempRenewal, initialTarget); if (install != null) { tempRenewal.InstallationPluginOptions = install; } else { return; } } // Try to run for the first time var renewal = await CreateRenewal(tempRenewal, runLevel); retry: var result = await _renewalExecution.HandleRenewal(renewal, runLevel); if (result.Abort) { _log.Information($"Create certificate cancelled"); } else if (result.Success != true) { if (runLevel.HasFlag(RunLevel.Interactive) && await _input.PromptYesNo("Create certificate failed, retry?", false)) { goto retry; } if (!renewal.New && runLevel.HasFlag(RunLevel.Interactive) && await _input.PromptYesNo("Save these new settings anyway?", false)) { _renewalStore.Save(renewal, result); } _exceptionHandler.HandleException(message: $"Create certificate failed: {string.Join("\n\t- ", result.ErrorMessages)}"); } else { try { _renewalStore.Save(renewal, result); await _notification.NotifyCreated(renewal, _log.Lines); } catch (Exception ex) { _exceptionHandler.HandleException(ex); } } }
/// <summary> /// Setup a new scheduled renewal /// </summary> /// <param name="runLevel"></param> internal async Task SetupRenewal(RunLevel runLevel, Renewal?tempRenewal = null) { if (_args.Test) { runLevel |= RunLevel.Test; } if (_args.Force) { runLevel |= RunLevel.IgnoreCache; } _log.Information(LogType.All, "Running in mode: {runLevel}", runLevel); if (tempRenewal == null) { tempRenewal = Renewal.Create(_args.Id, _settings.ScheduledTask.RenewalDays, _passwordGenerator); } using var configScope = _scopeBuilder.Configuration(_container, tempRenewal, runLevel); // Choose target plugin var targetPluginOptionsFactory = configScope.Resolve <ITargetPluginOptionsFactory>(); if (targetPluginOptionsFactory is INull) { _exceptionHandler.HandleException(message: $"No source plugin could be selected"); return; } var(targetPluginDisabled, targetPluginDisabledReason) = targetPluginOptionsFactory.Disabled; if (targetPluginDisabled) { _exceptionHandler.HandleException(message: $"Source plugin {targetPluginOptionsFactory.Name} is not available. {targetPluginDisabledReason}"); return; } var targetPluginOptions = runLevel.HasFlag(RunLevel.Unattended) ? await targetPluginOptionsFactory.Default() : await targetPluginOptionsFactory.Aquire(_input, runLevel); if (targetPluginOptions == null) { _exceptionHandler.HandleException(message: $"Source plugin {targetPluginOptionsFactory.Name} aborted or failed"); return; } tempRenewal.TargetPluginOptions = targetPluginOptions; // Generate Target and validation plugin choice using var targetScope = _scopeBuilder.Target(_container, tempRenewal, runLevel); var initialTarget = targetScope.Resolve <Target>(); if (initialTarget is INull) { _exceptionHandler.HandleException(message: $"Source plugin {targetPluginOptionsFactory.Name} was unable to generate a target"); return; } if (!initialTarget.IsValid(_log)) { _exceptionHandler.HandleException(message: $"Source plugin {targetPluginOptionsFactory.Name} generated an invalid target"); return; } _log.Information("Source generated using plugin {name}: {target}", targetPluginOptions.Name, initialTarget); // Choose FriendlyName if (!string.IsNullOrEmpty(_args.FriendlyName)) { tempRenewal.FriendlyName = _args.FriendlyName; } else if (runLevel.HasFlag(RunLevel.Advanced | RunLevel.Interactive)) { var alt = await _input.RequestString($"Suggested friendly name '{initialTarget.FriendlyName}', press <Enter> to accept or type an alternative"); if (!string.IsNullOrEmpty(alt)) { tempRenewal.FriendlyName = alt; } } tempRenewal.LastFriendlyName = tempRenewal.FriendlyName ?? initialTarget.FriendlyName; // Choose validation plugin var validationPluginOptionsFactory = targetScope.Resolve <IValidationPluginOptionsFactory>(); if (validationPluginOptionsFactory is INull) { _exceptionHandler.HandleException(message: $"No validation plugin could be selected"); return; } // Configure validation try { var validationOptions = runLevel.HasFlag(RunLevel.Unattended) ? await validationPluginOptionsFactory.Default(initialTarget) : await validationPluginOptionsFactory.Aquire(initialTarget, _input, runLevel); if (validationOptions == null) { _exceptionHandler.HandleException(message: $"Validation plugin {validationPluginOptionsFactory.Name} was unable to generate options"); return; } tempRenewal.ValidationPluginOptions = validationOptions; } catch (Exception ex) { _exceptionHandler.HandleException(ex, $"Validation plugin {validationPluginOptionsFactory.Name} aborted or failed"); return; } // Choose order plugin var orderPluginOptionsFactory = targetScope.Resolve <IOrderPluginOptionsFactory>(); if (orderPluginOptionsFactory is INull) { _exceptionHandler.HandleException(message: $"No order plugin could be selected"); return; } // Configure order try { var orderOptions = runLevel.HasFlag(RunLevel.Unattended) ? await orderPluginOptionsFactory.Default() : await orderPluginOptionsFactory.Aquire(_input, runLevel); if (orderOptions == null) { _exceptionHandler.HandleException(message: $"Order plugin {orderPluginOptionsFactory.Name} was unable to generate options"); return; } tempRenewal.OrderPluginOptions = orderOptions; } catch (Exception ex) { _exceptionHandler.HandleException(ex, $"Order plugin {orderPluginOptionsFactory.Name} aborted or failed"); return; } // Choose CSR plugin if (initialTarget.CsrBytes == null) { var csrPluginOptionsFactory = configScope.Resolve <ICsrPluginOptionsFactory>(); if (csrPluginOptionsFactory is INull) { _exceptionHandler.HandleException(message: $"No CSR plugin could be selected"); return; } // Configure CSR try { var csrOptions = runLevel.HasFlag(RunLevel.Unattended) ? await csrPluginOptionsFactory.Default() : await csrPluginOptionsFactory.Aquire(_input, runLevel); if (csrOptions == null) { _exceptionHandler.HandleException(message: $"CSR plugin {csrPluginOptionsFactory.Name} was unable to generate options"); return; } tempRenewal.CsrPluginOptions = csrOptions; } catch (Exception ex) { _exceptionHandler.HandleException(ex, $"CSR plugin {csrPluginOptionsFactory.Name} aborted or failed"); return; } } // Choose and configure store plugins var resolver = configScope.Resolve <IResolver>(); var storePluginOptionsFactories = new List <IStorePluginOptionsFactory>(); try { while (true) { var storePluginOptionsFactory = await resolver.GetStorePlugin(configScope, storePluginOptionsFactories); if (storePluginOptionsFactory == null) { _exceptionHandler.HandleException(message: $"Store could not be selected"); return; } StorePluginOptions?storeOptions; try { storeOptions = runLevel.HasFlag(RunLevel.Unattended) ? await storePluginOptionsFactory.Default() : await storePluginOptionsFactory.Aquire(_input, runLevel); } catch (Exception ex) { _exceptionHandler.HandleException(ex, $"Store plugin {storePluginOptionsFactory.Name} aborted or failed"); return; } if (storeOptions == null) { _exceptionHandler.HandleException(message: $"Store plugin {storePluginOptionsFactory.Name} was unable to generate options"); return; } var isNull = storePluginOptionsFactory is NullStoreOptionsFactory; if (!isNull || storePluginOptionsFactories.Count == 0) { tempRenewal.StorePluginOptions.Add(storeOptions); storePluginOptionsFactories.Add(storePluginOptionsFactory); } if (isNull) { break; } } } catch (Exception ex) { _exceptionHandler.HandleException(ex, "Invalid selection of store plugins"); return; } // Choose and configure installation plugins var installationPluginFactories = new List <IInstallationPluginOptionsFactory>(); try { while (true) { var installationPluginOptionsFactory = await resolver.GetInstallationPlugin(configScope, tempRenewal.StorePluginOptions.Select(x => x.Instance), installationPluginFactories); if (installationPluginOptionsFactory == null) { _exceptionHandler.HandleException(message: $"Installation plugin could not be selected"); return; } InstallationPluginOptions installOptions; try { installOptions = runLevel.HasFlag(RunLevel.Unattended) ? await installationPluginOptionsFactory.Default(initialTarget) : await installationPluginOptionsFactory.Aquire(initialTarget, _input, runLevel); } catch (Exception ex) { _exceptionHandler.HandleException(ex, $"Installation plugin {installationPluginOptionsFactory.Name} aborted or failed"); return; } if (installOptions == null) { _exceptionHandler.HandleException(message: $"Installation plugin {installationPluginOptionsFactory.Name} was unable to generate options"); return; } var isNull = installationPluginOptionsFactory is NullInstallationOptionsFactory; if (!isNull || installationPluginFactories.Count == 0) { tempRenewal.InstallationPluginOptions.Add(installOptions); installationPluginFactories.Add(installationPluginOptionsFactory); } if (isNull) { break; } } } catch (Exception ex) { _exceptionHandler.HandleException(ex, "Invalid selection of installation plugins"); return; } // Try to run for the first time var renewal = await CreateRenewal(tempRenewal, runLevel); retry: var result = await _renewalExecution.HandleRenewal(renewal, runLevel); if (result.Abort) { _exceptionHandler.HandleException(message: $"Create certificate cancelled"); } else if (!result.Success) { if (runLevel.HasFlag(RunLevel.Interactive) && await _input.PromptYesNo("Create certificate failed, retry?", false)) { goto retry; } if (!renewal.New && await _input.PromptYesNo("Save these new settings anyway?", false)) { _renewalStore.Save(renewal, result); } _exceptionHandler.HandleException(message: $"Create certificate failed: {string.Join("\n\t- ", result.ErrorMessages)}"); } else { try { _renewalStore.Save(renewal, result); await _notification.NotifyCreated(renewal, _log.Lines); } catch (Exception ex) { _exceptionHandler.HandleException(ex); } } }
/// <summary> /// Setup a new scheduled renewal /// </summary> /// <param name="runLevel"></param> internal async Task SetupRenewal(RunLevel runLevel) { if (_args.Test) { runLevel |= RunLevel.Test; } if (_args.Force) { runLevel |= RunLevel.IgnoreCache; } _log.Information(LogType.All, "Running in mode: {runLevel}", runLevel); var tempRenewal = Renewal.Create(_args.Id, _settings.ScheduledTask.RenewalDays, _passwordGenerator); using var configScope = _scopeBuilder.Configuration(_container, tempRenewal, runLevel); // Choose target plugin var targetPluginOptionsFactory = configScope.Resolve <ITargetPluginOptionsFactory>(); if (targetPluginOptionsFactory is INull) { _exceptionHandler.HandleException(message: $"No target plugin could be selected"); return; } if (targetPluginOptionsFactory.Disabled) { _exceptionHandler.HandleException(message: $"Target plugin {targetPluginOptionsFactory.Name} is not available to the current user, try running as administrator"); return; } var targetPluginOptions = runLevel.HasFlag(RunLevel.Unattended) ? await targetPluginOptionsFactory.Default() : await targetPluginOptionsFactory.Aquire(_input, runLevel); if (targetPluginOptions == null) { _exceptionHandler.HandleException(message: $"Target plugin {targetPluginOptionsFactory.Name} aborted or failed"); return; } tempRenewal.TargetPluginOptions = targetPluginOptions; // Generate Target and validation plugin choice Target initialTarget = null; IValidationPluginOptionsFactory validationPluginOptionsFactory = null; using (var targetScope = _scopeBuilder.Target(_container, tempRenewal, runLevel)) { initialTarget = targetScope.Resolve <Target>(); if (initialTarget == null) { _exceptionHandler.HandleException(message: $"Target plugin {targetPluginOptionsFactory.Name} was unable to generate a target"); return; } if (!initialTarget.IsValid(_log)) { _exceptionHandler.HandleException(message: $"Target plugin {targetPluginOptionsFactory.Name} generated an invalid target"); return; } _log.Information("Target generated using plugin {name}: {target}", targetPluginOptions.Name, initialTarget); // Choose FriendlyName if (runLevel.HasFlag(RunLevel.Advanced) && runLevel.HasFlag(RunLevel.Interactive) && string.IsNullOrEmpty(_args.FriendlyName)) { var alt = await _input.RequestString($"Suggested FriendlyName is '{initialTarget.FriendlyName}', press enter to accept or type an alternative"); if (!string.IsNullOrEmpty(alt)) { tempRenewal.FriendlyName = alt; } } tempRenewal.LastFriendlyName = initialTarget.FriendlyName; // Choose validation plugin validationPluginOptionsFactory = targetScope.Resolve <IValidationPluginOptionsFactory>(); if (validationPluginOptionsFactory is INull) { _exceptionHandler.HandleException(message: $"No validation plugin could be selected"); return; } } // Configure validation try { var validationOptions = runLevel.HasFlag(RunLevel.Unattended) ? await validationPluginOptionsFactory.Default(initialTarget) : await validationPluginOptionsFactory.Aquire(initialTarget, _input, runLevel); if (validationOptions == null) { _exceptionHandler.HandleException(message: $"Validation plugin {validationPluginOptionsFactory.Name} was unable to generate options"); return; } tempRenewal.ValidationPluginOptions = validationOptions; } catch (Exception ex) { _exceptionHandler.HandleException(ex, $"Validation plugin {validationPluginOptionsFactory.Name} aborted or failed"); return; } // Choose CSR plugin if (initialTarget.CsrBytes == null) { var csrPluginOptionsFactory = configScope.Resolve <ICsrPluginOptionsFactory>(); if (csrPluginOptionsFactory is INull) { _exceptionHandler.HandleException(message: $"No CSR plugin could be selected"); return; } // Configure CSR try { var csrOptions = runLevel.HasFlag(RunLevel.Unattended) ? await csrPluginOptionsFactory.Default() : await csrPluginOptionsFactory.Aquire(_input, runLevel); if (csrOptions == null) { _exceptionHandler.HandleException(message: $"CSR plugin {csrPluginOptionsFactory.Name} was unable to generate options"); return; } tempRenewal.CsrPluginOptions = csrOptions; } catch (Exception ex) { _exceptionHandler.HandleException(ex, $"CSR plugin {csrPluginOptionsFactory.Name} aborted or failed"); return; } } // Choose and configure store plugins var resolver = configScope.Resolve <IResolver>(); var storePluginOptionsFactories = new List <IStorePluginOptionsFactory>(); try { while (true) { var storePluginOptionsFactory = await resolver.GetStorePlugin(configScope, storePluginOptionsFactories); if (storePluginOptionsFactory == null) { _exceptionHandler.HandleException(message: $"Store could not be selected"); } if (storePluginOptionsFactory is NullStoreOptionsFactory) { if (storePluginOptionsFactories.Count == 0) { throw new Exception(); } break; } StorePluginOptions storeOptions; try { storeOptions = runLevel.HasFlag(RunLevel.Unattended) ? await storePluginOptionsFactory.Default() : await storePluginOptionsFactory.Aquire(_input, runLevel); } catch (Exception ex) { _exceptionHandler.HandleException(ex, $"Store plugin {storePluginOptionsFactory.Name} aborted or failed"); return; } if (storeOptions == null) { _exceptionHandler.HandleException(message: $"Store plugin {storePluginOptionsFactory.Name} was unable to generate options"); return; } tempRenewal.StorePluginOptions.Add(storeOptions); storePluginOptionsFactories.Add(storePluginOptionsFactory); } } catch (Exception ex) { _exceptionHandler.HandleException(ex, "Invalid selection of store plugins"); return; } // Choose and configure installation plugins var installationPluginFactories = new List <IInstallationPluginOptionsFactory>(); try { while (true) { var installationPluginFactory = await resolver.GetInstallationPlugin(configScope, tempRenewal.StorePluginOptions.Select(x => x.Instance), installationPluginFactories); if (installationPluginFactory == null) { _exceptionHandler.HandleException(message: $"Installation plugin could not be selected"); } InstallationPluginOptions installOptions; try { installOptions = runLevel.HasFlag(RunLevel.Unattended) ? await installationPluginFactory.Default(initialTarget) : await installationPluginFactory.Aquire(initialTarget, _input, runLevel); } catch (Exception ex) { _exceptionHandler.HandleException(ex, $"Installation plugin {installationPluginFactory.Name} aborted or failed"); return; } if (installOptions == null) { _exceptionHandler.HandleException(message: $"Installation plugin {installationPluginFactory.Name} was unable to generate options"); return; } if (installationPluginFactory is NullInstallationOptionsFactory) { if (installationPluginFactories.Count == 0) { tempRenewal.InstallationPluginOptions.Add(installOptions); installationPluginFactories.Add(installationPluginFactory); } break; } tempRenewal.InstallationPluginOptions.Add(installOptions); installationPluginFactories.Add(installationPluginFactory); } } catch (Exception ex) { _exceptionHandler.HandleException(ex, "Invalid selection of installation plugins"); return; } // Try to run for the first time var renewal = await CreateRenewal(tempRenewal, runLevel); var result = await _renewalExecution.Renew(renewal, runLevel); if (!result.Success) { _exceptionHandler.HandleException(message: $"Create certificate failed: {result.ErrorMessage}"); } else { _renewalStore.Save(renewal, result); } }