-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathBashScriptBootstrapper.cs
More file actions
217 lines (185 loc) · 9.69 KB
/
BashScriptBootstrapper.cs
File metadata and controls
217 lines (185 loc) · 9.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Calamari.Common.Features.Processes;
using Calamari.Common.Features.Scripts;
using Calamari.Common.FeatureToggles;
using Calamari.Common.Plumbing;
using Calamari.Common.Plumbing.Extensions;
using Calamari.Common.Plumbing.FileSystem;
using Calamari.Common.Plumbing.Logging;
using Calamari.Common.Plumbing.Variables;
namespace Calamari.Common.Features.Scripting.Bash
{
public class BashScriptBootstrapper
{
public const string WindowsNewLine = "\r\n";
public const string LinuxNewLine = "\n";
static readonly string BootstrapScriptTemplate;
static readonly string SensitiveVariablePassword = AesEncryption.RandomString(16);
static readonly AesEncryption VariableEncryptor = AesEncryption.ForScripts(SensitiveVariablePassword);
static readonly ICalamariFileSystem CalamariFileSystem = CalamariPhysicalFileSystem.GetPhysicalFileSystem();
static BashScriptBootstrapper()
{
BootstrapScriptTemplate = EmbeddedResource.ReadEmbeddedText(typeof(BashScriptBootstrapper).Namespace + ".Bootstrap.sh");
}
public static string FormatCommandArguments(string bootstrapFile)
{
var encryptionKey = ToHex(VariableEncryptor.EncryptionKey);
var commandArguments = new StringBuilder();
commandArguments.AppendFormat("\"{0}\" \"{1}\"", bootstrapFile, encryptionKey);
return commandArguments.ToString();
}
public static string PrepareConfigurationFile(string workingDirectory, IVariables variables)
{
var configurationFile = Path.Combine(workingDirectory, "Configure." + Guid.NewGuid().ToString().Substring(10) + ".sh");
var builder = new StringBuilder(BootstrapScriptTemplate);
var encryptedVariables = EncryptVariables(variables);
var variableString = GetEncryptedVariablesKvp(variables);
builder.Replace("#### VariableDeclarations ####", string.Join(LinuxNewLine, GetVariableSwitchConditions(encryptedVariables)));
builder.Replace("#### VARIABLESTRING.IV ####", variableString.iv);
builder.Replace("#### VARIABLESTRING.ENCRYPTED ####", variableString.encrypted);
using (var file = new FileStream(configurationFile, FileMode.CreateNew, FileAccess.Write))
using (var writer = new StreamWriter(file, Encoding.ASCII))
{
writer.Write(builder.Replace(WindowsNewLine, LinuxNewLine));
writer.Flush();
}
File.SetAttributes(configurationFile, FileAttributes.Hidden);
return configurationFile;
}
static string EncodeAsHex(string value)
{
var bytes = Encoding.UTF8.GetBytes(value);
return BitConverter.ToString(bytes).Replace("-", "");
}
static (string encrypted, string iv) GetEncryptedVariablesKvp(IVariables variables)
{
var sb = new StringBuilder();
foreach (var variable in variables
.Where(v => !ScriptVariables.IsLibraryScriptModule(v.Key))
.Where(v => !ScriptVariables.IsBuildInformationVariable(v.Key)))
{
var value = variable.Value ?? "nul";
sb.Append($"{EncodeAsHex(variable.Key)}").Append("$").AppendLine(EncodeAsHex(value));
}
var encrypted = VariableEncryptor.Encrypt(sb.ToString());
var rawEncrypted = AesEncryption.ExtractIV(encrypted, out var iv);
return (
Convert.ToBase64String(rawEncrypted),
ToHex(iv)
);
}
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();
}
static IEnumerable<string> GetVariableSwitchConditions(IEnumerable<EncryptedVariable> variables)
{
return variables
.Select(variable =>
{
var variableValue = $@"decrypt_variable ""{variable.EncryptedValue}"" ""{variable.Iv}""";
return string.Format(" \"{1}\"){0} {2} ;;{0}", Environment.NewLine, EncodeValue(variable.Name), variableValue);
});
}
static string ToHex(byte[] bytes)
{
return BitConverter.ToString(bytes).Replace("-", "");
}
static string EncodeValue(string value)
{
return Convert.ToBase64String(Encoding.UTF8.GetBytes(value ?? ""));
}
public static string FindBashExecutable()
{
if (CalamariEnvironment.IsRunningOnWindows)
{
var systemFolder = Environment.GetFolderPath(Environment.SpecialFolder.System);
return Path.Combine(systemFolder, "bash.exe");
}
return "bash";
}
static void EnsureValidUnixFile(string scriptFilePath)
{
var text = File.ReadAllText(scriptFilePath);
text = text.Replace(WindowsNewLine, LinuxNewLine);
File.WriteAllText(scriptFilePath, text);
}
public static (string bootstrapFile, string[] temporaryFiles) PrepareBootstrapFile(Script script, string configurationFile, string workingDirectory, IVariables variables)
{
var bootstrapFile = Path.Combine(workingDirectory, "Bootstrap." + Guid.NewGuid().ToString().Substring(10) + "." + Path.GetFileName(script.File));
var scriptModulePaths = PrepareScriptModules(variables, workingDirectory).ToArray();
using (var file = new FileStream(bootstrapFile, FileMode.CreateNew, FileAccess.Write))
using (var writer = new StreamWriter(file, Encoding.ASCII))
{
writer.NewLine = LinuxNewLine;
writer.WriteLine("#!/bin/bash");
writer.WriteLine("source \"$(pwd)/" + Path.GetFileName(configurationFile) + "\"");
writer.WriteLine("shift"); // Shift the variable decryption key out of scope of the user script and script modules (see: https://github.com/OctopusDeploy/Calamari/pull/773)
var preloadModules = variables.Get(BashScriptVariables.PreloadScriptModules);
if (!string.IsNullOrWhiteSpace(preloadModules))
PreloadScriptModules(writer, preloadModules, scriptModulePaths);
writer.WriteLine("source \"$(pwd)/" + Path.GetFileName(script.File) + "\" " + script.Parameters);
writer.Flush();
}
File.SetAttributes(bootstrapFile, FileAttributes.Hidden);
EnsureValidUnixFile(script.File);
return (bootstrapFile, scriptModulePaths);
}
static void PreloadScriptModules(StreamWriter writer, string preloadModules, string[] scriptModulePaths)
{
var modules = preloadModules.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries);
foreach (var module in modules)
{
var sanitizedName = ScriptVariables.FormatScriptName(module.Trim());
var fileName = $"{sanitizedName}.sh";
var scriptModule = scriptModulePaths.FirstOrDefault(p => string.Equals(Path.GetFileName(p), fileName, StringComparison.OrdinalIgnoreCase));
if (scriptModule != null)
{
Log.VerboseFormat("Preloading script module '{0}'.", module.Trim());
writer.WriteLine("source \"$(pwd)/" + fileName + "\"");
}
}
}
static IEnumerable<string> PrepareScriptModules(IVariables variables, string workingDirectory)
{
foreach (var variableName in variables.GetNames().Where(ScriptVariables.IsLibraryScriptModule))
if (ScriptVariables.GetLibraryScriptModuleLanguage(variables, variableName) == ScriptSyntax.Bash)
{
var libraryScriptModuleName = ScriptVariables.GetLibraryScriptModuleName(variableName);
var name = ScriptVariables.FormatScriptName(libraryScriptModuleName);
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);
var contents = variables.Get(variableName);
if (contents == null)
throw new InvalidOperationException($"Value for variable {variableName} could not be found.");
CalamariFileSystem.OverwriteFile(moduleFilePath, contents, utf8WithoutBom);
EnsureValidUnixFile(moduleFilePath);
yield return moduleFilePath;
}
}
class EncryptedVariable
{
public EncryptedVariable(string name, string encryptedValue, string iv)
{
Name = name;
EncryptedValue = encryptedValue;
Iv = iv;
}
public string Name { get; }
public string EncryptedValue { get; }
public string Iv { get; }
}
}
}