Ejemplo n.º 1
0
        /// <summary>
        /// Sets handlers that return reasonable default values.
        /// </summary>
        /// <param name="server">The assistant erver.</param>
        private void SetDefaultHandlers(ProfileServer server)
        {
            server.GetProfileValueHandler = (request, name) => ProfileHandlerResult.Create($"{name}-profile");

            server.GetSecretPasswordHandler =
                (request, name, vault, masterPassword) =>
            {
                var sb = new StringBuilder();

                sb.Append(name);

                if (vault != null)
                {
                    sb.AppendWithSeparator(vault, "-");
                }

                if (masterPassword != null)
                {
                    sb.AppendWithSeparator(masterPassword, "-");
                }

                sb.Append("-password");

                return(ProfileHandlerResult.Create(sb.ToString()));
            };

            server.GetSecretValueHandler =
                (request, name, vault, masterPassword) =>
            {
                var sb = new StringBuilder();

                sb.Append(name);

                if (vault != null)
                {
                    sb.AppendWithSeparator(vault, "-");
                }

                if (masterPassword != null)
                {
                    sb.AppendWithSeparator(masterPassword, "-");
                }

                sb.Append("-secret");

                return(ProfileHandlerResult.Create(sb.ToString()));
            };

            server.CallHandler =
                request =>
            {
                // We're just going to echo the value of the "command" argument.

                return(ProfileHandlerResult.Create(request.Args["command"]));
            };
        }
Ejemplo n.º 2
0
        public void ProfileReferences()
        {
            // Verify that [IProfileClient] integration works by starting a profile
            // server, injecting an [IProfileClient] implementation and then verifying
            // that secret passwords, secret values, and profile values can be
            // resolved.

            var pipeName = Guid.NewGuid().ToString("d");
            var server   = new ProfileServer(pipeName);

            server.GetProfileValueHandler =
                (request, name) =>
            {
                if (name == "missing")
                {
                    return(ProfileHandlerResult.CreateError(request, ProfileStatus.NotFound, $"[{name}] variable not found."));
                }

                return(ProfileHandlerResult.Create($"{name}-profile"));
            };

            server.GetSecretPasswordHandler =
                (request, name, vault, masterPassword) =>
            {
                if (name == "missing")
                {
                    return(ProfileHandlerResult.CreateError(request, ProfileStatus.NotFound, $"[{name}] variable not found."));
                }

                if (vault == null)
                {
                    return(ProfileHandlerResult.Create($"{name}-password"));
                }
                else
                {
                    return(ProfileHandlerResult.Create($"{name}-password-{vault}"));
                }
            };

            server.GetSecretValueHandler =
                (request, name, vault, masterPassword) =>
            {
                if (name == "missing")
                {
                    return(ProfileHandlerResult.CreateError(request, ProfileStatus.NotFound, $"[{name}] variable not found."));
                }

                if (vault == null)
                {
                    return(ProfileHandlerResult.Create($"{name}-secret"));
                }
                else
                {
                    return(ProfileHandlerResult.Create($"{name}-secret-{vault}"));
                }
            };

            server.Start();

            try
            {
                var client = new ProfileClient(pipeName);

                NeonHelper.ServiceContainer.AddSingleton <IProfileClient>(client);

                //-------------------------------------------------------------
                // Verify secret passwords

                var source = "TEST = $<<<password:test>>>";
                var output = new PreprocessReader(source).ReadToEnd().Trim();

                Assert.Equal("TEST = test-password", output);

                source = "TEST = $<<<password:test:vault>>>";
                output = new PreprocessReader(source).ReadToEnd().Trim();

                Assert.Equal("TEST = test-password-vault", output);

                Assert.Throws <ProfileException>(() => new PreprocessReader("TEST = $<<<password:missing>>>").ReadToEnd());

                //-------------------------------------------------------------
                // Verify secret values

                source = "TEST = $<<<secret:test>>>";
                output = new PreprocessReader(source).ReadToEnd().Trim();

                Assert.Equal("TEST = test-secret", output);

                source = "TEST = $<<<secret:test:vault>>>";
                output = new PreprocessReader(source).ReadToEnd().Trim();

                Assert.Equal("TEST = test-secret-vault", output);

                Assert.Throws <ProfileException>(() => new PreprocessReader("TEST = $<<<secret:missing>>>").ReadToEnd());

                //-------------------------------------------------------------
                // Verify secret values targeting a specific property.

                source = "TEST = $<<<secret:test[field]>>>";
                output = new PreprocessReader(source).ReadToEnd().Trim();

                Assert.Equal("TEST = test[field]-secret", output);

                source = "TEST = $<<<secret:test[field]:vault>>>";
                output = new PreprocessReader(source).ReadToEnd().Trim();

                Assert.Equal("TEST = test[field]-secret-vault", output);

                Assert.Throws <ProfileException>(() => new PreprocessReader("TEST = $<<<secret:missing>>>").ReadToEnd());

                //-------------------------------------------------------------
                // Verify profile values

                source = "TEST = $<<<profile:test>>>";
                output = new PreprocessReader(source).ReadToEnd().Trim();

                Assert.Equal("TEST = test-profile", output);

                Assert.Throws <ProfileException>(() => new PreprocessReader("TEST = $<<<profile:missing>>>").ReadToEnd());
            }
            finally
            {
                NeonHelper.ServiceContainer.Remove(NeonHelper.ServiceContainer.Single(service => service.ServiceType == typeof(IProfileClient)));

                server.Dispose();
            }
        }