Example #1
0
        public async Task FilenameFileSecretTest(SecretResolver secretResolver)
        {
            var secret = await secretResolver.ResolveSecretAsync(config.FilenameFileSecret);

            Assert.Equal("C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", secret);
            Assert.Equal("TestString", config.TextValue);
        }
Example #2
0
        public async Task ImplicitEnvironmentBasedSecretTest(SecretResolver secretResolver)
        {
            var secret = await secretResolver.ResolveSecretAsync(config.ImplicitEnvironmentSecret);

            Assert.Equal("PleaseDontTellAnyone", secret);
            Assert.Equal("TestString", config.TextValue);
        }
Example #3
0
        public async Task InitializeAsync()
        {
            Settings        = config.For <CosmosDbConfiguration>("CosmosDB");
            _secretResolver = new SecretResolver();

            //Notes:
            // if using an azure instance to run the tests, make sure you set the environment variable before you start visual studio
            // Ex: CMD C:\> setx COSMOSDB_KEY ABCDEFGASD==
            // On CosmosDB, make sure you create the collection 'SampleEntity' in the database defined in the config 'Stacks'
            // To overrride the appsettings values, set the environment variable using SectionName__PropertyName. i.e: CosmosDB__DatabaseAccountUri
            // Note the use of a double _ between the section and the property name

            if (Environment.GetEnvironmentVariable(Settings.SecurityKeySecret.Identifier) == null)
            {
                //if localhost and running in visual studio
                if (Settings.DatabaseAccountUri.Contains("localhost", StringComparison.InvariantCultureIgnoreCase) &&
                    Environment.GetEnvironmentVariable("VisualStudioEdition") != null)
                {
                    Environment.SetEnvironmentVariable(Settings.SecurityKeySecret.Identifier, this.Settings.PrimaryKey);
                }
                else
                {
                    throw new ArgumentNullException(
                              $"The environment variable '{Settings.SecurityKeySecret.Identifier}' has not been set");
                }
            }

            string cosmosDbKey = await _secretResolver.ResolveSecretAsync(Settings.SecurityKeySecret);

            CosmosClient = new CosmosClient(Settings.DatabaseAccountUri, cosmosDbKey);
            await CosmosClient.CreateDatabaseIfNotExistsAsync(Settings.DatabaseName);

            Database database = CosmosClient.GetDatabase(Settings.DatabaseName);
            await database.CreateContainerIfNotExistsAsync(nameof(SampleEntity), $"/{nameof(SampleEntity.OwnerId)}");
        }
Example #4
0
        public async Task FullPathFileSecretTest(SecretResolver secretResolver)
        {
            var tmpFile = "secretfile.txt";

            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                Directory.CreateDirectory(config.WindowsTempFolder);
                tmpFile = Path.Combine(config.WindowsTempFolder, tmpFile);
            }
            else
            {
                Directory.CreateDirectory(config.LinuxTempFolder);
                tmpFile = Path.Combine(config.LinuxTempFolder, tmpFile);
            }

            File.Copy("secretfile.txt", tmpFile, true);

            try
            {
                var secret = string.Empty;

                if (Environment.OSVersion.Platform == PlatformID.Win32NT)
                {
                    secret = await secretResolver.ResolveSecretAsync(config.WindowsFullPathFileSecret);
                }
                else
                {
                    secret = await secretResolver.ResolveSecretAsync(config.LinuxFullPathFileSecret);
                }

                Assert.Equal("C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", secret);
                Assert.Equal("TestString", config.TextValue);
            }
            finally
            {
                File.Delete(tmpFile);
            }
        }
Example #5
0
        public async Task RelativeFilenameFileSecretTest(SecretResolver secretResolver)
        {
            var tmpFile = "subfolder/secretfile.txt";

            Directory.CreateDirectory("subfolder");

            File.Copy("secretfile.txt", tmpFile, true);

            try
            {
                var secret = await secretResolver.ResolveSecretAsync(config.RelativeFilenameFileSecret);

                Assert.Equal("C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", secret);
            }
            finally
            {
                File.Delete(tmpFile);
            }
        }
Example #6
0
        public async Task NonExistentOptionalFileSecretTest(SecretResolver secretResolver)
        {
            var secret = await secretResolver.ResolveSecretAsync(config.NonExistentOptionalFileSecret);

            Assert.Null(secret);
        }
Example #7
0
 public async Task NonExistentRequiredFileSecretTest(SecretResolver secretResolver)
 {
     await Assert.ThrowsAsync <SecretNotFoundException>(async() => await secretResolver.ResolveSecretAsync(config.NonExistentRequiredFileSecret));
 }