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()); }
static string WritePatternMatching(CalamariVariableDictionary 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 if (variables.IsSensitive(variableName)) { builder.AppendFormat(" | \"{0}\" -> {1} |> Some", EncodeValue(variableName), EncryptVariable(variableValue)); } else { builder.AppendFormat(" | \"{0}\" -> \"{1}\" |> decode |> Some", EncodeValue(variableName), EncodeValue(variableValue)); } builder.Append(Environment.NewLine); } builder.Append(" | _ -> None"); return(builder.ToString()); }
static IEnumerable <string> GetVariableSwitchConditions(CalamariVariableDictionary variables) { return(variables.GetNames().Select(variable => { var variableValue = variables.IsSensitive(variable) ? DecryptValueCommand(variables.Get(variable)) : string.Format("decode_servicemessagevalue \"{0}\"", EncodeValue(variables.Get(variable))); return string.Format(" \"{1}\"){0} {2} ;;{0}", Environment.NewLine, EncodeValue(variable), variableValue); })); }
private VariableDictionary AddEnvironmentVariables() { var variables = new CalamariVariableDictionary(); 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); }
static IEnumerable <string> GetVariables(CalamariVariableDictionary variables) { return(variables.GetNames().Select(variable => { var variableValue = variables.IsSensitive(variable) ? DecryptValueCommand(variables.Get(variable)) : $"decode(\"{EncodeValue(variables.Get(variable))}\")"; return $"decode(\"{EncodeValue(variable)}\") : {variableValue}"; })); }
static void WriteVariableDictionary(CalamariVariableDictionary 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))) { var variableValue = variables.IsSensitive(variableName) ? EncryptVariable(variables.Get(variableName)) : EncodeValue(variables.Get(variableName)); output.Append("$OctopusParameters[").Append(EncodeValue(variableName)).Append("] = ").AppendLine(variableValue); } }
static string WriteVariableDictionary(CalamariVariableDictionary variables) { var builder = new StringBuilder(); foreach (var variable in variables.GetNames()) { var variableValue = variables.IsSensitive(variable) ? EncryptVariable(variables.Get(variable)) : EncodeValue(variables.Get(variable)); builder.Append("\t\t\tthis[").Append(EncodeValue(variable)).Append("] = ").Append(variableValue).AppendLine(";"); } return(builder.ToString()); }
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) ); }