private static async Task LoadSecretSettings(
            StorageSettings storageSettings,
            AppSettings appSettings,
            KeyVaultSettings keyVaultSettings)
        {
            var store = new SecretsStore(appSettings.KeyVaultBaseUrl, keyVaultSettings.ClientId, keyVaultSettings.ClientSecret);

            storageSettings.ConnectionString = await store.GetOrLoadSettingAsync(storageSettings.ConnectionString);
        }
Beispiel #2
0
 public CommandContext(
     SecretsStore store,
     IReporter reporter,
     IConsole console)
 {
     SecretStore = store;
     Reporter    = reporter;
     Console     = console;
 }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            var certificateStore = new SecretsStore();

            services.AddSingleton <ISecretsStore>(certificateStore);
            services.AddSingleton <IX509Provider>(certificateStore);

            services.AddScoped <ISigningService>(provider => new SigningService(Environment.Test, provider.GetService <IX509Provider>()));
            services.AddScoped <IUserInfoService>(provider => new UserInfoService(Environment.Test, provider.GetService <IX509Provider>()));
        }
Beispiel #4
0
    internal int RunInternal(params string[] args)
    {
        CommandLineOptions options;

        try
        {
            options = CommandLineOptions.Parse(args, _console);
        }
        catch (CommandParsingException ex)
        {
            CreateReporter(verbose: false).Error(ex.Message);
            return(1);
        }

        if (options == null)
        {
            return(1);
        }

        if (options.IsHelp)
        {
            return(2);
        }

        var reporter = CreateReporter(options.IsVerbose);

        if (options.Command is InitCommandFactory initCmd)
        {
            initCmd.Execute(new CommandContext(null, reporter, _console), _workingDirectory);
            return(0);
        }

        string userSecretsId;

        try
        {
            userSecretsId = ResolveId(options, reporter);
        }
        catch (Exception ex) when(ex is InvalidOperationException || ex is FileNotFoundException)
        {
            reporter.Error(ex.Message);
            return(1);
        }

        var store   = new SecretsStore(userSecretsId, reporter);
        var context = new Internal.CommandContext(store, reporter, _console);

        options.Command.Execute(context);
        return(0);
    }
        public static IActionResult Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, TraceWriter log)
        {
            log.Info("SecurelyScaleCosmosDB execution started.");

            RequestData   requestData        = new RequestData(req, log);
            StringBuilder validationMessages = new StringBuilder();

            if (requestData.isNotValid(ref validationMessages))
            {
                throw new Exception(validationMessages.ToString());
            }
            log.Info("Request body valid.");
            log.Info(requestData.Description);

            if (isNotValid(endpointSecretUrl, authKeySecretUrl))
            {
                throw new Exception("Please check ProtectedEndpointUrl and ProtectedAuthKeyUrl variables are set in Application Settings");
            }
            log.Info("Application Settings read without error.");

            SecretsStore secretsStore = new SecretsStore(log);

            string endPointUrl = secretsStore.GetSecret(endpointSecretUrl);
            string authKey     = secretsStore.GetSecret(authKeySecretUrl);

            if (isNotValid(endPointUrl, authKey))
            {
                throw new Exception("Information retrieved from Key Vault is invalid");
            }
            log.Info("Sensitive data read without error.");


            var  scaler        = RequestUnitsScaleFactor.CreateRequestUnitsCompute(400, 2000, 4);
            bool scalingSucess = false;

            if (requestData.MetricName == "Max RUs Per Second" && requestData.Status == "Activated")
            {
                log.Info("Total Request Units Activated Scale Up.");
                scalingSucess = CosmosDBScaler.ScaleAccount(endPointUrl, authKey, ru => scaler.ScaleUp(ru), log);
            }
            else if (requestData.MetricName == "Max RUs Per Second" && requestData.Status == "Resolved")
            {
                log.Info("Total Request Units Resolved Scale Down.");
                scalingSucess = CosmosDBScaler.ScaleAccount(endPointUrl, authKey, ru => scaler.ScaleDown(ru), log);
            }
            return(scalingSucess
                ? (ActionResult) new OkObjectResult($"CosmosDB account scaling sucessful. See Azure Function log for details.")
                : new BadRequestObjectResult("Scaling error. See Azure Function log for details."));
        }