Ejemplo n.º 1
0
        public void TestUpdatableTokens()
        {
            var updatingToken   = new UpdatingSasToken(new SasToken(token: "Token 1", "Storage Account 1", "Resource Path 1"));
            var originalSecrets = new RetrievedSecrets(
                new Dictionary <string, Secret>()
            {
                ["Secret 1"] = new PlainTextSecret("Secret Value 1"),
                ["Secret 2"] = updatingToken
            });

            var context = new OperationContext(new Context(Logger));

            using var secretsExposer = InterProcessSecretsCommunicator.Expose(context, originalSecrets);

            using var readSecrets = InterProcessSecretsCommunicator.ReadExposedSecrets(context, pollingIntervalInSeconds: 10_000);

            AssertSecretsAreEqual(originalSecrets, readSecrets);

            int tokenUpdated = 0;

            ((UpdatingSasToken)readSecrets.Secrets["Secret 2"]).TokenUpdated += (sender, token) =>
            {
                tokenUpdated++;
            };

            // Updating the token
            updatingToken.UpdateToken(new SasToken("1", "2", "3"));

            readSecrets.RefreshSecrets(context);
            AssertSecretsAreEqual(originalSecrets, readSecrets);

            Assert.Equal(1, tokenUpdated); // An event should be raised

            // Updating token once again
            updatingToken.UpdateToken(new SasToken("2", "2", "3"));

            readSecrets.RefreshSecrets(context);

            AssertSecretsAreEqual(originalSecrets, readSecrets);

            Assert.Equal(2, tokenUpdated); // An event should be raised
        }
        private AzureBlobStorageCredentials CreateAzureBlobCredentialsFromSasToken(string secretName, UpdatingSasToken updatingSasToken)
        {
            var storageCredentials = new StorageCredentials(sasToken: updatingSasToken.Token.Token);

            updatingSasToken.TokenUpdated += (_, sasToken) =>
            {
                _logger.Debug($"Updating SAS token for Azure Storage secret {secretName}");
                storageCredentials.UpdateSASToken(sasToken.Token);
            };

            // The account name should never actually be updated, so its OK to take it from the initial token
            var azureCredentials = new AzureBlobStorageCredentials(storageCredentials, updatingSasToken.Token.StorageAccount);

            return(azureCredentials);
        }
        private static AzureBlobStorageCredentials CreateAzureBlobCredentialsFromSasToken(UpdatingSasToken updatingSasToken)
        {
            var storageCredentials = new StorageCredentials(sasToken: updatingSasToken.Token.Token);

            updatingSasToken.TokenUpdated += (token, sasToken) =>
            {
                storageCredentials.UpdateSASToken(sasToken.Token);
            };

            // The account name should never actually be updated, so its OK to take it from the initial token
            var azureCredentials = new AzureBlobStorageCredentials(storageCredentials, updatingSasToken.Token.StorageAccount);

            return(azureCredentials);
        }