static string DeclareLocalVariables(CalamariVariableDictionary variables)
        {
            var output = new StringBuilder();

            foreach (var variableName in variables.GetNames().Where(name => !SpecialVariables.IsLibraryScriptModule(name)))
            {
                if (SpecialVariables.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());
        }
Example #2
0
 static void WriteVariableDictionary(VariableDictionary variables, StringBuilder output)
 {
     output.AppendLine("$OctopusParameters = New-Object 'System.Collections.Generic.Dictionary[String,String]' (,[System.StringComparer]::OrdinalIgnoreCase)");
     foreach (var variableName in variables.GetNames().Where(name => !SpecialVariables.IsLibraryScriptModule(name)))
     {
         output.Append("$OctopusParameters[").Append(EncodeValue(variableName)).Append("] = ").AppendLine(EncodeValue(variables.Get(variableName)));
     }
 }
        static (string encrypted, string iv) GetEncryptedVariablesString(CalamariVariableDictionary variables)
        {
            var sb = new StringBuilder();

            foreach (var variableName in variables.GetNames().Where(name => !SpecialVariables.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)
                );
        }