コード例 #1
0
        private static void SetEnvironmentVariablesFromCodeCakeBuilderKeyVault(SafeCakeLog logger, CakeContext context)
        {
            string filePath = "CodeCakeBuilder/CodeCakeBuilderKeyVault.txt";

            if (System.IO.File.Exists(filePath))
            {
                logger.Information("Reading environment variables from CodeCakeBuilderKeyVault.txt file.");
                string key = context.InteractiveEnvironmentVariable("CODECAKEBUILDER_SECRET_KEY", setCache: true);
                try
                {
                    if (key != null)
                    {
                        var envVars = KeyVault.DecryptValues(System.IO.File.ReadAllText(filePath), key);
                        foreach (var e in envVars)
                        {
                            if (Environment.GetEnvironmentVariable(e.Key) == null)
                            {
                                logger.Information($"Environment variable '{e.Key}' set from key vault.");
                                Environment.SetEnvironmentVariable(e.Key, e.Value);
                            }
                            else
                            {
                                logger.Information($"Environment variable '{e.Key}' is already defined. Value from Key Vault is ignored.");
                            }
                        }
                    }
                    else
                    {
                        logger.Warning($"Environment variable CODECAKEBUILDER_SECRET_KEY is not set. Cannot open the Key Vault.");
                    }
                }
                catch (Exception ex)
                {
                    logger.Warning($"Error while reading key vault values: {ex.Message}.");
                }
            }
            else
            {
                logger.Information("No CodeCakeBuilder/CodeCakeBuilderKeyVault.txt file found.");
            }
        }