Beispiel #1
0
        public void Update(IVariables variables)
        {
            bool VariableNameIsNotASystemVariable(string v)
            {
                if (v.StartsWith("Octopus", StringComparison.OrdinalIgnoreCase))
                {
                    // Only include variables starting with 'Octopus'
                    // if it also has a colon (:)
                    if (v.StartsWith("Octopus:", StringComparison.OrdinalIgnoreCase))
                    {
                        return(map.ContainsKey(v));
                    }
                    else
                    {
                        return(false);
                    }
                }
                return(map.ContainsKey(v));
            }

            foreach (var name in variables.GetNames().Where(VariableNameIsNotASystemVariable))
            {
                try
                {
                    map[name](variables.Get(name));
                }
                catch (Exception e)
                {
                    Log.WarnFormat("Unable to set value for {0}. The following error occurred: {1}", name, e.Message);
                }
            }
        }
Beispiel #2
0
        public void Update(IVariables variables)
        {
            bool VariableNameIsMappedPath(string v)
            {
                if (v.StartsWith("Octopus", StringComparison.OrdinalIgnoreCase) &&
                    !v.StartsWith("Octopus:", StringComparison.OrdinalIgnoreCase))
                {
                    // Only include variables starting with 'Octopus'
                    // if it also has a colon (:)
                    return(false);
                }
                return(map.ContainsKey(v));
            }

            var replaced = 0;

            foreach (var name in variables.GetNames().Where(VariableNameIsMappedPath))
            {
                try
                {
                    log.Verbose(StructuredConfigMessages.StructureFound(name));
                    replaced++;
                    map[name](variables.Get(name));
                }
                catch (Exception e)
                {
                    log.WarnFormat("Unable to set value for {0}. The following error occurred: {1}", name, e.Message);
                }
            }

            if (replaced == 0)
            {
                log.Info(StructuredConfigMessages.NoStructuresFound);
            }
        }
        static string[] WriteScriptModules(IVariables variables, string parentDirectory, StringBuilder output)
        {
            var scriptModules = new List <string>();

            foreach (var variableName in variables.GetNames().Where(ScriptVariables.IsLibraryScriptModule))
            {
                if (ScriptVariables.GetLibraryScriptModuleLanguage(variables, variableName) == ScriptSyntax.PowerShell)
                {
                    var libraryScriptModuleName = ScriptVariables.GetLibraryScriptModuleName(variableName);
                    var name           = "Library_" + new string(libraryScriptModuleName.Where(char.IsLetterOrDigit).ToArray()) + "_" + DateTime.Now.Ticks;
                    var moduleFileName = $"{name}.psm1";
                    var moduleFilePath = Path.Combine(parentDirectory, moduleFileName);
                    Log.VerboseFormat("Writing script module '{0}' as PowerShell module {1}. This module will be automatically imported - functions will automatically be in scope.", libraryScriptModuleName, moduleFileName, name);
                    var contents = variables.Get(variableName);
                    if (contents == null)
                    {
                        throw new InvalidOperationException($"Value for variable {variableName} could not be found.");
                    }
                    CalamariFileSystem.OverwriteFile(moduleFilePath, contents, Encoding.UTF8);
                    output.AppendLine($"Import-ScriptModule '{libraryScriptModuleName.EscapeSingleQuotedString()}' '{moduleFilePath.EscapeSingleQuotedString()}'");
                    output.AppendLine();
                    scriptModules.Add(moduleFilePath);
                }
            }

            return(scriptModules.ToArray());
        }
Beispiel #4
0
        public void LogVariables()
        {
            string ToString(bool useRawValue)
            {
                var text = new StringBuilder();

                var namesToPrint = variables.GetNames().Where(name => !name.Contains("CustomScripts.")).OrderBy(name => name);

                foreach (var name in namesToPrint)
                {
                    var value = useRawValue ? variables.GetRaw(name) : variables.Get(name);
                    text.AppendLine($"[{name}] = '{value}'");
                }

                return(text.ToString());
            }

            if (variables.GetFlag(KnownVariables.PrintVariables))
            {
                log.Warn($"{KnownVariables.PrintVariables} is enabled. This should only be used for debugging problems with variables, and then disabled again for normal deployments.");
                log.Verbose("The following variables are available:" + Environment.NewLine + ToString(true));
            }

            if (variables.GetFlag(KnownVariables.PrintEvaluatedVariables))
            {
                log.Warn($"{KnownVariables.PrintEvaluatedVariables} is enabled. This should only be used for debugging problems with variables, and then disabled again for normal deployments.");
                log.Verbose("The following evaluated variables are available:" + Environment.NewLine + ToString(false));
            }
        }
Beispiel #5
0
        static string WritePatternMatching(IVariables variables)
        {
            var builder = new StringBuilder();

            foreach (var variableName in variables.GetNames())
            {
                var variableValue = variables.Get(variableName);
                if (variableValue == null)
                {
                    builder.AppendFormat("        | \"{0}\" -> Some null", EncodeValue(variableName));
                }
                else
                {
                    builder.AppendFormat("        | \"{0}\" -> {1} |> Some",
                                         EncodeValue(variableName),
                                         EncryptVariable(variableValue));
                }

                builder.Append(Environment.NewLine);
            }

            builder.Append("        | _ -> None");

            return(builder.ToString());
        }
Beispiel #6
0
        static void UpdateConfigurationSettings(XContainer configurationFile, IVariables variables)
        {
            Log.Verbose("Updating configuration settings...");
            var foundSettings = false;

            WithConfigurationSettings(configurationFile, (roleName, settingName, settingValueAttribute) =>
            {
                var setting = variables.Get(roleName + "/" + settingName) ??
                              variables.Get(roleName + "\\" + settingName) ??
                              variables.Get(settingName) ??
                              (variables.GetNames().Contains(settingName) ? "" : null);

                if (setting != null)
                {
                    foundSettings = true;
                    Log.Info("Updating setting for role {0}: {1} = {2}", roleName, settingName, setting);
                    settingValueAttribute.Value = setting;
                }
            });

            if (!foundSettings)
            {
                Log.Info("No settings that match provided variables were found.");
            }
        }
Beispiel #7
0
        static string DeclareLocalVariables(IVariables variables)
        {
            var output = new StringBuilder();

            foreach (var variableName in variables.GetNames().Where(name => !ScriptVariables.IsLibraryScriptModule(name)))
            {
                if (ScriptVariables.IsExcludedFromLocalVariables(variableName))
                {
                    continue;
                }

                // This is the way we used to fix up the identifiers - people might still rely on this behavior
                var legacyKey = new string(variableName.Where(char.IsLetterOrDigit).ToArray());

                // This is the way we should have done it
                var smartKey = new string(variableName.Where(IsValidPowerShellIdentifierChar).ToArray());

                if (legacyKey != smartKey)
                {
                    WriteVariableAssignment(output, legacyKey, variableName);
                }

                WriteVariableAssignment(output, smartKey, variableName);
            }

            return(output.ToString());
        }
Beispiel #8
0
 static IEnumerable <string> GetVariableSwitchConditions(IVariables variables)
 {
     return(variables.GetNames().Select(variable =>
     {
         var variableValue = DecryptValueCommand(variables.Get(variable));
         return string.Format("    \"{1}\"){0}   {2}   ;;{0}", Environment.NewLine, EncodeValue(variable), variableValue);
     }));
 }
Beispiel #9
0
 static IEnumerable <string> GetVariables(IVariables variables)
 {
     return(variables.GetNames().Select(variable =>
     {
         var variableValue = DecryptValueCommand(variables.Get(variable));
         return $"decode(\"{EncodeValue(variable)}\") : {variableValue}";
     }));
 }
        static IList <EncryptedVariable> EncryptVariables(IVariables variables)
        {
            return(variables.GetNames()
                   .Select(name =>
            {
                var encryptedValue = VariableEncryptor.Encrypt(variables.Get(name) ?? "");
                var raw = AesEncryption.ExtractIV(encryptedValue, out var iv);

                return new EncryptedVariable(name, Convert.ToBase64String(raw), ToHex(iv));
            }).ToList());
        }
Beispiel #11
0
        static string WriteVariableDictionary(IVariables variables)
        {
            var builder = new StringBuilder();

            foreach (var variable in variables.GetNames())
            {
                var variableValue = EncryptVariable(variables.Get(variable));
                builder.Append("\t\t\tthis[").Append(EncodeValue(variable)).Append("] = ").Append(variableValue).AppendLine(";");
            }
            return(builder.ToString());
        }
Beispiel #12
0
        public static ScriptSyntax GetLibraryScriptModuleLanguage(IVariables variables, string variableName)
        {
            var expectedName   = variableName.Replace("Octopus.Script.Module[", "Octopus.Script.Module.Language[");
            var syntaxVariable = variables.GetNames().FirstOrDefault(x => x == expectedName);

            if (syntaxVariable == null)
            {
                return(ScriptSyntax.PowerShell);
            }
            return((ScriptSyntax)Enum.Parse(typeof(ScriptSyntax), variables[syntaxVariable]));
        }
Beispiel #13
0
        static List <string> ApplyChanges(XNode doc, IVariables variables)
        {
            var changes = new List <string>();

            foreach (var variable in variables.GetNames())
            {
                changes.AddRange(
                    ReplaceAppSettingOrConnectionString(doc, "//*[local-name()='appSettings']/*[local-name()='add']", "key", variable, "value", variables).Concat(
                        ReplaceAppSettingOrConnectionString(doc, "//*[local-name()='connectionStrings']/*[local-name()='add']", "name", variable, "connectionString", variables).Concat(
                            ReplaceStonglyTypeApplicationSetting(doc, "//*[local-name()='applicationSettings']//*[local-name()='setting']", "name", variable, variables))));
            }
            return(changes);
        }
Beispiel #14
0
 static IEnumerable <string> PrepareScriptModules(IVariables variables, string workingDirectory)
 {
     foreach (var variableName in variables.GetNames().Where(ScriptVariables.IsLibraryScriptModule))
     {
         if (ScriptVariables.GetLibraryScriptModuleLanguage(variables, variableName) == ScriptSyntax.Python)
         {
             var libraryScriptModuleName = ScriptVariables.GetLibraryScriptModuleName(variableName);
             var name           = new string(libraryScriptModuleName.Where(x => char.IsLetterOrDigit(x) || x == '_').ToArray());
             var moduleFileName = $"{name}.py";
             Log.VerboseFormat("Writing script module '{0}' as python module {1}. Import this module via `import {2}`.", libraryScriptModuleName, moduleFileName, name);
             var moduleFilePath = Path.Combine(workingDirectory, moduleFileName);
             CalamariFileSystem.OverwriteFile(moduleFilePath, variables.Get(variableName), Encoding.UTF8);
             yield return(name);
         }
     }
 }
Beispiel #15
0
 static IEnumerable <string> PrepareScriptModules(IVariables variables, string workingDirectory)
 {
     foreach (var variableName in variables.GetNames().Where(SpecialVariables.IsLibraryScriptModule))
     {
         if (SpecialVariables.GetLibraryScriptModuleLanguage(variables, variableName) == ScriptSyntax.FSharp)
         {
             var libraryScriptModuleName = SpecialVariables.GetLibraryScriptModuleName(variableName);
             var name           = new string(libraryScriptModuleName.Where(char.IsLetterOrDigit).ToArray());
             var moduleFileName = $"{name}.fsx";
             var moduleFilePath = Path.Combine(workingDirectory, moduleFileName);
             Log.VerboseFormat("Writing script module '{0}' as f# module {1}. Import this module via `#load \"{1}\"`.", libraryScriptModuleName, moduleFileName, name);
             CalamariFileSystem.OverwriteFile(moduleFilePath, variables.Get(variableName), Encoding.UTF8);
             yield return(moduleFileName);
         }
     }
 }
Beispiel #16
0
 static IEnumerable <string> PrepareScriptModules(IVariables variables, string workingDirectory)
 {
     foreach (var variableName in variables.GetNames().Where(SpecialVariables.IsLibraryScriptModule))
     {
         if (SpecialVariables.GetLibraryScriptModuleLanguage(variables, variableName) == ScriptSyntax.Bash)
         {
             var libraryScriptModuleName = SpecialVariables.GetLibraryScriptModuleName(variableName);
             var name           = new string(libraryScriptModuleName.Where(char.IsLetterOrDigit).ToArray());
             var moduleFileName = $"{name}.sh";
             var moduleFilePath = Path.Combine(workingDirectory, moduleFileName);
             Log.VerboseFormat("Writing script module '{0}' as bash script {1}. Import this via `source {1}`.", libraryScriptModuleName, moduleFileName, name);
             Encoding utf8WithoutBom = new UTF8Encoding(false);
             CalamariFileSystem.OverwriteFile(moduleFilePath, variables.Get(variableName), utf8WithoutBom);
             EnsureValidUnixFile(moduleFilePath);
             yield return(moduleFilePath);
         }
     }
 }
Beispiel #17
0
        static (string encrypted, string iv) GetEncryptedVariablesString(IVariables variables)
        {
            var sb = new StringBuilder();

            foreach (var variableName in variables.GetNames().Where(name => !ScriptVariables.IsLibraryScriptModule(name)))
            {
                var value          = variables.Get(variableName);
                var encryptedValue = value == null ? "nul" : EncodeAsBase64(value); // "nul" is not a valid Base64 string
                sb.Append(EncodeAsBase64(variableName)).Append("$").AppendLine(encryptedValue);
            }

            var encrypted    = VariableEncryptor.Encrypt(sb.ToString());
            var rawEncrypted = AesEncryption.ExtractIV(encrypted, out var iv);

            return(
                Convert.ToBase64String(rawEncrypted, Base64FormattingOptions.InsertLineBreaks),
                Convert.ToBase64String(iv)
                );
        }
Beispiel #18
0
 static IEnumerable <string> PrepareScriptModules(IVariables variables, string workingDirectory)
 {
     foreach (var variableName in variables.GetNames().Where(ScriptVariables.IsLibraryScriptModule))
     {
         if (ScriptVariables.GetLibraryScriptModuleLanguage(variables, variableName) == ScriptSyntax.FSharp)
         {
             var libraryScriptModuleName = ScriptVariables.GetLibraryScriptModuleName(variableName);
             var name           = new string(libraryScriptModuleName.Where(char.IsLetterOrDigit).ToArray());
             var moduleFileName = $"{name}.fsx";
             var moduleFilePath = Path.Combine(workingDirectory, moduleFileName);
             Log.VerboseFormat("Writing script module '{0}' as f# module {1}. Import this module via `#load \"{1}\"`.", libraryScriptModuleName, moduleFileName, name);
             var contents = variables.Get(variableName);
             if (contents == null)
             {
                 throw new InvalidOperationException($"Value for variable {variableName} could not be found.");
             }
             CalamariFileSystem.OverwriteFile(moduleFilePath, contents, Encoding.UTF8);
             yield return(moduleFileName);
         }
     }
 }