public async Task PullsSecretUsingCorrectValue([Frozen] Mock <IVaultClientFactory> mockFact, Mock <IVaultClientV1> mockV1, Mock <ISecretsEngine> mockSecrets, Mock <IKeyValueSecretsEngine> mockKV, Mock <IVaultClient> mockClient, string path, VaultProvider sut) { var mockKVV1 = new Mock <IKeyValueSecretsEngineV1>(); mockKVV1.Setup(m => m.ReadSecretAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())) .Returns <string, string, string>((_, __, ___) => Task.FromResult(new Secret <Dictionary <string, object> >() { Data = new Dictionary <string, object>() { { "value", "secret" } } })); mockKVV1.CallBase = false; mockKV.Setup(m => m.V1).Returns(() => mockKVV1.Object); mockSecrets.Setup(m => m.KeyValue).Returns(() => mockKV.Object); mockV1.Setup(m => m.Secrets).Returns(() => mockSecrets.Object); mockClient.Setup(m => m.V1).Returns(() => mockV1.Object); mockFact.Setup(m => m.GetClient()).Returns(() => mockClient.Object); var actual = await sut.GetSecret(path).ConfigureAwait(false); actual.Should().Be("secret"); }
public async Task ExceptionIsRethrown([Frozen] Mock <IVaultClientFactory> mockFact, string path, VaultProvider sut) { mockFact.Setup(m => m.GetClient()).Returns(() => throw new VaultSharp.Core.VaultApiException("exceptions are dumb")); Func <Task <string> > s = () => sut.GetSecret(path); await s.Should().ThrowExactlyAsync <VaultSharp.Core.VaultApiException>().ConfigureAwait(false); }
public async Task ExceptionWithWarnAndErrorIsRethrown([Frozen] Mock <IVaultClientFactory> mockFact, string path, VaultProvider sut) { var messageDict = new Dictionary <string, IEnumerable <string> >() { { "warnings", new List <string>() { "warning" } }, { "errors", new List <string>() { "error" } } }; mockFact.Setup(m => m.GetClient()).Returns(() => throw new VaultSharp.Core.VaultApiException(HttpStatusCode.OK, JsonConvert.SerializeObject(messageDict))); Func <Task <string> > s = () => sut.GetSecret(path); await s.Should().ThrowExactlyAsync <VaultSharp.Core.VaultApiException>().ConfigureAwait(false); }
/// <summary> /// The main entry point for the application. /// </summary> //[STAThread] static int Main(string[] args) { Params param = null; Parser.Default.ParseArguments <Params>(args) .WithParsed(opts => param = opts) .WithNotParsed(PrintErrorAndExit); Log.Logger = MakeLogger(param.Verbose); Log.Information("Vault2Git -- converting history from Vault repositories to Git"); Console.InputEncoding = System.Text.Encoding.UTF8; Configuration configuration; // First look for Config file in the current directory - allows for repository-based config files var configPath = Path.Combine(Environment.CurrentDirectory, "Vault2Git.exe.config"); if (File.Exists(configPath)) { var configFileMap = new ExeConfigurationFileMap { ExeConfigFilename = configPath }; configuration = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None); } else { // Get normal exe file config. // This is what happens by default when using ConfigurationManager.AppSettings["setting"] // to access config properties var applicationName = Environment.GetCommandLineArgs()[0]; #if !DEBUG applicationName += ".exe"; #endif configPath = Path.Combine(Environment.CurrentDirectory, applicationName); configuration = ConfigurationManager.OpenExeConfiguration(configPath); } Log.Information($"Using config file {configPath}"); // Get access to the AppSettings properties in the chosen config file param.ApplyAppConfigIfParamNotPresent((AppSettingsSection)configuration.GetSection("appSettings")); var git2VaultRepoPaths = param.Paths.ToDictionary(pair => RemoveTrailingSlash(pair.Split('~')[1]), pair => RemoveTrailingSlash(pair.Split('~')[0])); if (!git2VaultRepoPaths.Keys.All(p => param.Branches.Contains(p))) { Console.Error.WriteLine($"Config git branches ({string.Join(",", git2VaultRepoPaths.Keys)}) are not a superset of branches ({string.Join(",", param.Branches)})"); return(-2); } param.Branches = git2VaultRepoPaths.Keys; // check working folder ends with trailing slash if (param.WorkingFolder.Last() != '\\') { param.WorkingFolder += '\\'; } Log.Information(param.ToString()); var git = new GitProvider(param.WorkingFolder, param.GitCmd, param.GitDomainName, param.SkipEmptyCommits, param.IgnoreGitIgnore); var vault = new VaultProvider(param.VaultServer, param.VaultRepo, param.VaultUser, param.VaultPassword); var processor = new Processor(git, vault, param.Directories.ToList(), param.Limit, param.DoGitPushOrigin, param.SampleTimeWhenNoFullPathAvailable, param.BeginDate) { WorkingFolder = param.WorkingFolder, ForceFullFolderGet = param.ForceFullFolderGet }; var git2VaultRepoPathsSubset = new Dictionary <string, string>(); foreach (var branch in param.Branches) { git2VaultRepoPathsSubset[branch] = git2VaultRepoPaths[branch]; } if (param.RunContinuously) { var consecutiveErrorCount = 0; var cancelKeyPressed = false; Console.CancelKeyPress += delegate { cancelKeyPressed = true; Log.Information("Stop process requested"); }; var nextRun = DateTime.UtcNow; do { if (nextRun <= DateTime.UtcNow) { try { processor.Pull(git2VaultRepoPathsSubset); consecutiveErrorCount = 0; } catch (Exception e) { Log.Warning($"Exception caught while pulling in new versions from vault. Current consecutive exception count: {consecutiveErrorCount}.\n{e}"); } nextRun = DateTime.UtcNow.AddMinutes(1); Log.Information($"Next run scheduled for {nextRun:u}"); } Thread.Sleep(TimeSpan.FromSeconds(1)); } while (!cancelKeyPressed); } else { processor.Pull(git2VaultRepoPathsSubset); } if (!param.IgnoreLabels) { processor.CreateTagsFromLabels(); } return(0); }