/// <inheritdoc />
        public Task <RetrievedSecrets> RetrieveSecretsAsync(List <RetrieveSecretsRequest> requests, CancellationToken token)
        {
            if (_secretsCommunicationKind == CrossProcessSecretsCommunicationKind.Environment)
            {
                // Default mode for the launcher
                return(RetrieveSecretsCoreAsync(requests, token));
            }
            else if (_secretsCommunicationKind == CrossProcessSecretsCommunicationKind.EnvironmentSingleEntry)
            {
                var secretsResult = LazyInitializer.EnsureInitialized(ref _secrets, () => DeserializeFromEnvironmentVariable());

                secretsResult.ThrowIfFailure();
                return(Task.FromResult(secretsResult.Value));
            }
            else if (_secretsCommunicationKind == CrossProcessSecretsCommunicationKind.MemoryMappedFile)
            {
                // 'ReadExposedSecrets' returns a disposable object, but the secrets obtained here are long-lived.
                RetrievedSecrets secrets = InterProcessSecretsCommunicator.ReadExposedSecrets(new OperationContext(_tracingContext));
                return(Task.FromResult(secrets));
            }
            else
            {
                throw Contract.AssertFailure($"Unknown {nameof(CrossProcessSecretsCommunicationKind)}: {_secretsCommunicationKind}.");
            }
        }
Beispiel #2
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
        }