Exemplo n.º 1
0
 public KeyRotationHelper(IDAL dal, IKeyVaultConnection keyVaultConnection, IConfiguration configuration, ILogger <KeyRotationHelper> logger)
 {
     this.dal = dal;
     this.keyVaultConnection = keyVaultConnection;
     this.configuration      = configuration;
     this.logger             = logger;
     RetryCosmosPolicy       = GetCosmosRetryPolicy();
 }
Exemplo n.º 2
0
        /// <summary>
        /// Run the app
        /// </summary>
        /// <param name="keyvaultName">Keyvault Name</param>
        /// <param name="authType">Authentication Type</param>
        /// <param name="logLevel">Log Level</param>
        /// <param name="dryRun">Dry Run flag</param>
        /// <returns>status</returns>
        public static async Task <int> RunApp(string keyvaultName, AuthenticationType authType, LogLevel logLevel, bool dryRun)
        {
            // validate keyvaultName and convert to URL
            if (!KeyVaultHelper.BuildKeyVaultConnectionString(keyvaultName, out string kvUrl))
            {
                return(-1);
            }

            try
            {
                // setup ctl c handler
                ctCancel = SetupCtlCHandler();

                AppLogLevel = logLevel;

                // build the host
                host = await BuildHost(kvUrl, authType).ConfigureAwait(false);

                if (host == null)
                {
                    return(-1);
                }

                // don't start the web server
                if (dryRun)
                {
                    return(DoDryRun(kvUrl, authType));
                }

                // log startup messages
                LogStartup();

                // verify key vault access
                IKeyVaultConnection kvConnection = host.Services.GetService <IKeyVaultConnection>();
                Task <Microsoft.Azure.KeyVault.Models.SecretBundle> secret = kvConnection.Client.GetSecretAsync(kvConnection.Address, Constants.CosmosDatabase);

                // start the webserver
                Task w = host.RunAsync();

                // this doesn't return except on ctl-c
                await w.ConfigureAwait(false);

                // use this line instead if you want to re-read the Cosmos connection info on a timer
                // await RunKeyRotationCheck(ctCancel, Constants.KeyVaultChangeCheckSeconds).ConfigureAwait(false);

                // if not cancelled, app exit -1
                return(ctCancel.IsCancellationRequested ? 0 : -1);
            }
            catch (Exception ex)
            {
                // end app on error
                if (logger != null)
                {
                    logger.LogError($"Exception: {ex}");
                }
                else
                {
                    Console.WriteLine($"{ex}\nError in Main() {ex.Message}");
                }

                return(-1);
            }
        }