public CalamariVariableDictionary(string storageFilePath, string sensitiveFilePath, string sensitiveFilePassword)
        {
            var fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem();

            if (!string.IsNullOrEmpty(storageFilePath))
            {
                if (!fileSystem.FileExists(storageFilePath))
                    throw new CommandException("Could not find variables file: " + storageFilePath);

                var nonSensitiveVariables =  new VariableDictionary(storageFilePath);
                nonSensitiveVariables.GetNames().ForEach(name => Set(name, nonSensitiveVariables.GetRaw(name)));
            }

            if (!string.IsNullOrEmpty(sensitiveFilePath))
            {
                var rawVariables = string.IsNullOrWhiteSpace(sensitiveFilePassword)
                    ? fileSystem.ReadFile(sensitiveFilePath)
                    : Decrypt(fileSystem.ReadAllBytes(sensitiveFilePath), sensitiveFilePassword);


                try
                {
                    var sensitiveVariables = JsonConvert.DeserializeObject<Dictionary<string, string>>(rawVariables);
                    foreach (var variable in sensitiveVariables)
                    {
                        SetSensitive(variable.Key, variable.Value);
                    }
                }
                catch (JsonReaderException)
                {
                    throw new CommandException("Unable to parse sensitive-variables as valid JSON.");
                }
            }
        }
Exemple #2
0
        public CalamariVariableDictionary(string storageFilePath, List <string> sensitiveFilePaths, string sensitiveFilePassword, string outputVariablesFilePath = null, string outputVariablesFilePassword = null)
        {
            var fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem();

            if (!string.IsNullOrEmpty(storageFilePath))
            {
                if (!fileSystem.FileExists(storageFilePath))
                {
                    throw new CommandException("Could not find variables file: " + storageFilePath);
                }

                var nonSensitiveVariables = new VariableDictionary(storageFilePath);
                nonSensitiveVariables.GetNames().ForEach(name => Set(name, nonSensitiveVariables.GetRaw(name)));
            }

            if (sensitiveFilePaths.Any())
            {
                foreach (var sensitiveFilePath in sensitiveFilePaths)
                {
                    if (string.IsNullOrEmpty(sensitiveFilePath))
                    {
                        continue;
                    }

                    var rawVariables = string.IsNullOrWhiteSpace(sensitiveFilePassword)
                        ? fileSystem.ReadFile(sensitiveFilePath)
                        : Decrypt(fileSystem.ReadAllBytes(sensitiveFilePath), sensitiveFilePassword);

                    try
                    {
                        var sensitiveVariables = JsonConvert.DeserializeObject <Dictionary <string, string> >(rawVariables);
                        foreach (var variable in sensitiveVariables)
                        {
                            SetSensitive(variable.Key, variable.Value);
                        }
                    }
                    catch (JsonReaderException)
                    {
                        throw new CommandException("Unable to parse sensitive-variables as valid JSON.");
                    }
                }
            }

            if (!string.IsNullOrEmpty(outputVariablesFilePath))
            {
                var rawVariables = DecryptWithMachineKey(fileSystem.ReadFile(outputVariablesFilePath), outputVariablesFilePassword);
                try
                {
                    var outputVariables = JsonConvert.DeserializeObject <Dictionary <string, string> >(rawVariables);
                    foreach (var variable in outputVariables)
                    {
                        Set(variable.Key, variable.Value);
                    }
                }
                catch (JsonReaderException)
                {
                    throw new CommandException("Unable to parse output variables as valid JSON.");
                }
            }
        }
        private VariableDictionary AddEnvironmentVariables()
        {
            var variables = new VariableDictionary();
            var convention = new ContributeEnvironmentVariablesConvention();
            convention.Install(new RunningDeployment("C:\\Package.nupkg", variables));

            Assert.That(variables.GetNames().Count, Is.GreaterThan(3));
            Assert.That(variables.GetRaw(SpecialVariables.Tentacle.Agent.InstanceName), Is.EqualTo("#{env:TentacleInstanceName}"));
            return variables;
        }
Exemple #4
0
        public void ShouldAddEnvironmentVariables()
        {
            var variables  = new VariableDictionary();
            var convention = new ContributeEnvironmentVariablesConvention();

            convention.Install(new RunningDeployment("C:\\Package.nupkg", variables));

            Assert.That(variables.GetNames().Count, Is.GreaterThan(3));
            Assert.That(variables.Evaluate("My OS is #{env:OS}"), Is.StringStarting("My OS is Windows"));
            Assert.That(variables.GetRaw(SpecialVariables.Tentacle.Agent.InstanceName), Is.EqualTo("#{env:TentacleInstanceName}"));
        }
Exemple #5
0
        public CalamariVariableDictionary(string storageFilePath, string sensitiveFilePath, string sensitiveFilePassword, string base64Variables = null)
        {
            var fileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem();

            if (!string.IsNullOrEmpty(storageFilePath))
            {
                if (!fileSystem.FileExists(storageFilePath))
                {
                    throw new CommandException("Could not find variables file: " + storageFilePath);
                }

                var nonSensitiveVariables = new VariableDictionary(storageFilePath);
                nonSensitiveVariables.GetNames().ForEach(name => Set(name, nonSensitiveVariables.GetRaw(name)));
            }

            if (!string.IsNullOrEmpty(sensitiveFilePath))
            {
                var rawVariables = string.IsNullOrWhiteSpace(sensitiveFilePassword)
                    ? fileSystem.ReadFile(sensitiveFilePath)
                    : Decrypt(fileSystem.ReadAllBytes(sensitiveFilePath), sensitiveFilePassword);

                try
                {
                    var sensitiveVariables = JsonConvert.DeserializeObject <Dictionary <string, string> >(rawVariables);
                    foreach (var variable in sensitiveVariables)
                    {
                        SetSensitive(variable.Key, variable.Value);
                    }
                }
                catch (JsonReaderException)
                {
                    throw new CommandException("Unable to parse sensitive-variables as valid JSON.");
                }
            }

            if (!string.IsNullOrEmpty(base64Variables))
            {
                try
                {
                    var json      = Encoding.UTF8.GetString(Convert.FromBase64String(base64Variables));
                    var variables = JsonConvert.DeserializeObject <Dictionary <string, string> >(json);
                    foreach (var variable in variables)
                    {
                        Set(variable.Key, variable.Value);
                    }
                }
                catch (JsonReaderException)
                {
                    throw new CommandException("Unable to parse jsonVariables as valid JSON.");
                }
            }
        }
        private static string ToString(this VariableDictionary variables, Func <string, bool> nameFilter, bool useRawValue)
        {
            var text = new StringBuilder();

            foreach (var name in variables.GetNames())
            {
                if (!nameFilter(name))
                {
                    continue;
                }

                text.AppendFormat("[{0}] = '{1}'", name, useRawValue ? variables.GetRaw(name) : variables.Get(name));
                text.AppendLine();
            }

            return(text.ToString());
        }
Exemple #7
0
        public void HowToUseTheDictionary()
        {
            var variables = new VariableDictionary();

            variables.Set("Foo", "Bar");
            variables.Set("IsFamous", "True");
            variables.Set("FriendCount", "99");
            variables.Set("InstallPath", "C:\\#{Directory}");
            variables.Set("Directory", "MyDirectory");

            variables.Get("InstallPath").Should().Be("C:\\MyDirectory");
            variables.GetRaw("InstallPath").Should().Be("C:\\#{Directory}");
            variables.GetFlag("IsFamous").Should().Be(true);
            variables.GetFlag("IsInfamous").Should().Be(false);
            variables.GetFlag("IsInfamous", true).Should().Be(true);
            variables.GetInt32("FriendCount").Should().Be(99);
            variables.GetInt32("FollowerCount").Should().Be(null);
        }
Exemple #8
0
        public void HowToUseTheDictionary()
        {
            var variables = new VariableDictionary();

            variables.Set("Foo", "Bar");
            variables.Set("IsFamous", "True");
            variables.Set("FriendCount", "99");
            variables.Set("InstallPath", "C:\\#{Directory}");
            variables.Set("Directory", "MyDirectory");

            Assert.That(variables.Get("InstallPath"), Is.EqualTo("C:\\MyDirectory"));
            Assert.That(variables.GetRaw("InstallPath"), Is.EqualTo("C:\\#{Directory}"));
            Assert.That(variables.GetFlag("IsFamous"), Is.EqualTo(true));
            Assert.That(variables.GetFlag("IsInfamous"), Is.EqualTo(false));
            Assert.That(variables.GetFlag("IsInfamous", true), Is.EqualTo(true));
            Assert.That(variables.GetInt32("FriendCount"), Is.EqualTo(99));
            Assert.That(variables.GetInt32("FollowerCount"), Is.EqualTo(null));
        }
Exemple #9
0
        public void UseDictionaryWithCollectionInitializer()
        {
            var variables = new VariableDictionary
            {
                { "Foo", "Bar" },
                { "IsFamous", "True" },
                { "FriendCount", "99" },
                { "InstallPath", "C:\\#{Directory}" },
                { "Directory", "MyDirectory" }
            };


            variables.Get("InstallPath").Should().Be("C:\\MyDirectory");
            variables.GetRaw("InstallPath").Should().Be("C:\\#{Directory}");
            variables.GetFlag("IsFamous").Should().Be(true);
            variables.GetFlag("IsInfamous").Should().Be(false);
            variables.GetFlag("IsInfamous", true).Should().Be(true);
            variables.GetInt32("FriendCount").Should().Be(99);
            variables.GetInt32("FollowerCount").Should().Be(null);
        }
Exemple #10
0
        internal static ICollection <PrivateKeyAccessRule> GetPrivateKeyAccessRules(VariableDictionary variables)
        {
            // The private-key access-rules are stored as escaped JSON. However, they may contain nested
            // variables (for example the user-name may be an Octopus variable) which may not be escaped,
            // causing JSON parsing to fail.

            // So, we get the raw text
            var raw = variables.GetRaw(SpecialVariables.Action.Certificate.PrivateKeyAccessRules);

            if (string.IsNullOrWhiteSpace(raw))
            {
                return(new List <PrivateKeyAccessRule>());
            }

            // Unescape it (we only care about backslashes)
            var unescaped = raw.Replace(@"\\", @"\");
            // Perform variable-substitution and re-escape
            var escapedAndSubstituted = variables.Evaluate(unescaped).Replace(@"\", @"\\");

            return(PrivateKeyAccessRule.FromJson(escapedAndSubstituted));
        }
Exemple #11
0
        public void DocumentationIntroduction()
        {
            var variables = new VariableDictionary();

            variables.Set("Server", "Web01");
            variables.Set("Port", "10933");
            variables.Set("Url", "http://#{Server | ToLower}:#{Port}");
            variables.Set("Protocol", "#{Proto}");

            var url  = variables.Get("Url");
            var raw  = variables.GetRaw("Url");
            var eval = variables.Evaluate("#{Url}/foo");

            string error;

            variables.Get("Protocol", out error);

            url.Should().Be("http://web01:10933");
            raw.Should().Be("http://#{Server | ToLower}:#{Port}");
            eval.Should().Be("http://web01:10933/foo");
            error.Should().NotBeNull();
        }
Exemple #12
0
        public void DocumentationIntroduction()
        {
            var variables = new VariableDictionary();

            variables.Set("Server", "Web01");
            variables.Set("Port", "10933");
            variables.Set("Url", "http://#{Server | ToLower}:#{Port}");
            variables.Set("Protocol", "#{Proto}");

            var url  = variables.Get("Url");
            var raw  = variables.GetRaw("Url");
            var eval = variables.Evaluate("#{Url}/foo");

            string error;

            variables.Get("Protocol", out error);

            Assert.AreEqual("http://web01:10933", url);
            Assert.AreEqual("http://#{Server | ToLower}:#{Port}", raw);
            Assert.AreEqual("http://web01:10933/foo", eval);
            Assert.IsNotNull(error);
        }
 public static void MergeWith(this VariableDictionary variables, VariableDictionary other)
 {
     other.GetNames().ForEach(name => variables.Set(name, other.GetRaw(name)));
 }
Exemple #14
0
 public void Merge(VariableDictionary other)
 {
     other.GetNames().ForEach(name => Set(name, other.GetRaw(name)));
 }