/
EnvironmentSettings.cs
300 lines (257 loc) · 16.9 KB
/
EnvironmentSettings.cs
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright © 2011 - Present RealDimensions Software, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
namespace chocolatey.infrastructure.app.configuration
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Security.Principal;
using System.Text;
using adapters;
using logging;
using nuget;
using Environment = adapters.Environment;
public static class EnvironmentSettings
{
private const string SET_ENVIRONMENT_METHOD = "SetEnvironment";
private static Lazy<IEnvironment> _environmentInitializer = new Lazy<IEnvironment>(() => new Environment());
[EditorBrowsable(EditorBrowsableState.Never)]
public static void initialize_with(Lazy<IEnvironment> environment)
{
_environmentInitializer = environment;
}
private static IEnvironment Environment
{
get { return _environmentInitializer.Value; }
}
public static void reset_environment_variables(ChocolateyConfiguration config)
{
Environment.SetEnvironmentVariable(ApplicationParameters.Environment.ChocolateyPackageInstallLocation, null);
Environment.SetEnvironmentVariable(ApplicationParameters.Environment.ChocolateyPackageInstallerType, null);
Environment.SetEnvironmentVariable(ApplicationParameters.Environment.ChocolateyPackageExitCode, null);
Environment.SetEnvironmentVariable(ApplicationParameters.Environment.ChocolateyIgnoreChecksums, null);
Environment.SetEnvironmentVariable(ApplicationParameters.Environment.ChocolateyAllowEmptyChecksums, null);
Environment.SetEnvironmentVariable(ApplicationParameters.Environment.ChocolateyAllowEmptyChecksumsSecure, null);
Environment.SetEnvironmentVariable(ApplicationParameters.Environment.ChocolateyPowerShellHost, null);
Environment.SetEnvironmentVariable(ApplicationParameters.Environment.ChocolateyForce, null);
Environment.SetEnvironmentVariable("chocolateyProxyLocation", null);
Environment.SetEnvironmentVariable("chocolateyProxyBypassList", null);
Environment.SetEnvironmentVariable("chocolateyProxyBypassOnLocal", null);
}
public static void set_environment_variables(ChocolateyConfiguration config)
{
reset_environment_variables(config);
Environment.SetEnvironmentVariable(ApplicationParameters.ChocolateyInstallEnvironmentVariableName, ApplicationParameters.InstallLocation);
Environment.SetEnvironmentVariable("CHOCOLATEY_VERSION", config.Information.ChocolateyVersion);
Environment.SetEnvironmentVariable("CHOCOLATEY_VERSION_PRODUCT", config.Information.ChocolateyProductVersion);
Environment.SetEnvironmentVariable("OS_PLATFORM", config.Information.PlatformType.get_description_or_value());
Environment.SetEnvironmentVariable("OS_VERSION", config.Information.PlatformVersion.to_string());
Environment.SetEnvironmentVariable("OS_NAME", config.Information.PlatformName.to_string());
// experimental until we know if this value returns correctly based on the OS and not the current process.
Environment.SetEnvironmentVariable("OS_IS64BIT", config.Information.Is64Bit ? "true" : "false");
Environment.SetEnvironmentVariable("IS_ADMIN", config.Information.IsUserAdministrator ? "true" : "false");
Environment.SetEnvironmentVariable("IS_PROCESSELEVATED", config.Information.IsProcessElevated ? "true" : "false");
Environment.SetEnvironmentVariable("TEMP", config.CacheLocation);
Environment.SetEnvironmentVariable("TMP", config.CacheLocation);
if (config.Debug) Environment.SetEnvironmentVariable("ChocolateyEnvironmentDebug", "true");
if (config.Verbose) Environment.SetEnvironmentVariable("ChocolateyEnvironmentVerbose", "true");
if (!config.Features.ChecksumFiles) Environment.SetEnvironmentVariable(ApplicationParameters.Environment.ChocolateyIgnoreChecksums, "true");
if (config.Features.AllowEmptyChecksums) Environment.SetEnvironmentVariable(ApplicationParameters.Environment.ChocolateyAllowEmptyChecksums, "true");
if (config.Features.AllowEmptyChecksumsSecure) Environment.SetEnvironmentVariable(ApplicationParameters.Environment.ChocolateyAllowEmptyChecksumsSecure, "true");
if (config.Features.ScriptsCheckLastExitCode) Environment.SetEnvironmentVariable(ApplicationParameters.Environment.ChocolateyCheckLastExitCode, "true");
Environment.SetEnvironmentVariable("chocolateyRequestTimeout", config.WebRequestTimeoutSeconds.to_string() + "000");
Environment.SetEnvironmentVariable("chocolateyResponseTimeout", config.CommandExecutionTimeoutSeconds.to_string() + "000");
if (!string.IsNullOrWhiteSpace(config.Proxy.Location))
{
var proxyCreds = string.Empty;
if (!string.IsNullOrWhiteSpace(config.Proxy.User) &&
!string.IsNullOrWhiteSpace(config.Proxy.EncryptedPassword)
)
{
proxyCreds = "{0}:{1}@".format_with(config.Proxy.User, NugetEncryptionUtility.DecryptString(config.Proxy.EncryptedPassword));
Environment.SetEnvironmentVariable("chocolateyProxyUser", config.Proxy.User);
Environment.SetEnvironmentVariable("chocolateyProxyPassword", NugetEncryptionUtility.DecryptString(config.Proxy.EncryptedPassword));
}
Environment.SetEnvironmentVariable("http_proxy", "{0}{1}".format_with(proxyCreds, config.Proxy.Location));
Environment.SetEnvironmentVariable("https_proxy", "{0}{1}".format_with(proxyCreds, config.Proxy.Location));
Environment.SetEnvironmentVariable("chocolateyProxyLocation", config.Proxy.Location);
if (!string.IsNullOrWhiteSpace(config.Proxy.BypassList))
{
Environment.SetEnvironmentVariable("chocolateyProxyBypassList", config.Proxy.BypassList);
Environment.SetEnvironmentVariable("no_proxy", config.Proxy.BypassList);
}
if (config.Proxy.BypassOnLocal) Environment.SetEnvironmentVariable("chocolateyProxyBypassOnLocal", "true");
}
if (config.Features.UsePowerShellHost) Environment.SetEnvironmentVariable(ApplicationParameters.Environment.ChocolateyPowerShellHost, "true");
if (config.Force) Environment.SetEnvironmentVariable(ApplicationParameters.Environment.ChocolateyForce, "true");
set_licensed_environment(config);
}
private static void set_licensed_environment(ChocolateyConfiguration config)
{
if (!config.Information.IsLicensedVersion) return;
Environment.SetEnvironmentVariable("ChocolateyLicenseType", config.Information.LicenseType);
var licenseAssembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.GetName().Name.is_equal_to("chocolatey.licensed"));
if (licenseAssembly != null)
{
Type licensedEnvironmentSettings = licenseAssembly.GetType(ApplicationParameters.LicensedEnvironmentSettings, throwOnError: false, ignoreCase: true);
if (licensedEnvironmentSettings == null)
{
if (config.RegularOutput) "chocolatey".Log().Warn(
ChocolateyLoggers.Important, @"Unable to set licensed environment settings. Please upgrade to a newer
licensed version (choco upgrade chocolatey.extension).");
return;
}
try
{
object componentClass = Activator.CreateInstance(licensedEnvironmentSettings);
licensedEnvironmentSettings.InvokeMember(
SET_ENVIRONMENT_METHOD,
BindingFlags.InvokeMethod,
null,
componentClass,
new Object[] { config }
);
}
catch (Exception ex)
{
var isDebug = ApplicationParameters.is_debug_mode_cli_primitive();
if (config.Debug) isDebug = true;
var message = isDebug ? ex.ToString() : ex.Message;
if (isDebug && ex.InnerException != null)
{
message += "{0}{1}".format_with(Environment.NewLine, ex.ToString());
}
"chocolatey".Log().Error(
ChocolateyLoggers.Important,
@"Error when setting environment for '{0}':{1} {2}".format_with(
licensedEnvironmentSettings.FullName,
Environment.NewLine,
message
));
}
}
}
/// <summary>
/// Refreshes the current environment values with the updated values,
/// even if updated outside of the current process.
/// </summary>
/// <remarks>
/// This does not remove environment variables, but will ensure all updates are shown.
/// To see actual update with removed variables, one will need to restart a shell.
/// </remarks>
public static void update_environment_variables()
{
// grab original values
var originalEnvironmentVariables = convert_to_case_insensitive_dictionary(Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Process));
var userName = originalEnvironmentVariables[ApplicationParameters.Environment.Username].to_string();
var architecture = originalEnvironmentVariables[ApplicationParameters.Environment.ProcessorArchitecture].to_string();
var originalPath = originalEnvironmentVariables[ApplicationParameters.Environment.Path]
.to_string()
.Split(new[] { ApplicationParameters.Environment.EnvironmentSeparator }, StringSplitOptions.RemoveEmptyEntries);
var originalPathExt = originalEnvironmentVariables[ApplicationParameters.Environment.PathExtensions]
.to_string()
.Split(new[] { ApplicationParameters.Environment.EnvironmentSeparator }, StringSplitOptions.RemoveEmptyEntries);
var originalPsModulePath = originalEnvironmentVariables[ApplicationParameters.Environment.PsModulePath]
.to_string()
.Split(new[] { ApplicationParameters.Environment.EnvironmentSeparator }, StringSplitOptions.RemoveEmptyEntries);
// get updated values from the registry
var machineVariables = convert_to_case_insensitive_dictionary(Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Machine));
var userVariables = convert_to_case_insensitive_dictionary(Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User));
// refresh current values with updated values, mathine first
refresh_environment_variables(machineVariables);
//if the user is SYSTEM, we should not even look at user Variables
var setUserEnvironmentVariables = true;
try
{
var userIdentity = WindowsIdentity.GetCurrent();
if (userIdentity != null && userIdentity.User == ApplicationParameters.LocalSystemSid)
{
setUserEnvironmentVariables = false;
}
}
catch (Exception ex)
{
"chocolatey".Log().Debug("Unable to determine current user to determine if LocalSystem account (to skip user env vars).{0} Reported error: {1}".format_with(Environment.NewLine, ex.Message));
}
if (setUserEnvironmentVariables) refresh_environment_variables(userVariables);
// restore process overridden variables
if (originalEnvironmentVariables.Contains(ApplicationParameters.Environment.Username)) Environment.SetEnvironmentVariable(ApplicationParameters.Environment.Username, userName);
if (originalEnvironmentVariables.Contains(ApplicationParameters.Environment.ProcessorArchitecture)) Environment.SetEnvironmentVariable(ApplicationParameters.Environment.ProcessorArchitecture, architecture);
// combine environment values that append together
var updatedPath = "{0};{1};".format_with(
machineVariables[ApplicationParameters.Environment.Path].to_string(),
userVariables[ApplicationParameters.Environment.Path].to_string()
).Replace(";;", ";");
var updatedPathExt = "{0};{1};".format_with(
machineVariables[ApplicationParameters.Environment.PathExtensions].to_string(),
userVariables[ApplicationParameters.Environment.PathExtensions].to_string()
).Replace(";;", ";");
var updatedPsModulePath = "{0};{1};".format_with(
userVariables[ApplicationParameters.Environment.PsModulePath].to_string(),
machineVariables[ApplicationParameters.Environment.PsModulePath].to_string()
).Replace(";;", ";");
// add back in process items
updatedPath += gather_process_only_items(updatedPath, originalPath);
updatedPathExt += gather_process_only_items(updatedPathExt, originalPathExt);
updatedPsModulePath = "{0};{1}".format_with(gather_process_only_items(updatedPsModulePath, originalPsModulePath),updatedPsModulePath);
if (!updatedPsModulePath.contains(ApplicationParameters.PowerShellModulePathProcessProgramFiles))
{
updatedPsModulePath = "{0};{1}".format_with(ApplicationParameters.PowerShellModulePathProcessProgramFiles, updatedPsModulePath).Replace(";;", ";");
}
if (!updatedPsModulePath.contains(ApplicationParameters.PowerShellModulePathProcessDocuments))
{
updatedPsModulePath = "{0};{1}".format_with(ApplicationParameters.PowerShellModulePathProcessDocuments, updatedPsModulePath).Replace(";;", ";");
}
if (updatedPsModulePath.StartsWith(";"))
{
updatedPsModulePath = updatedPsModulePath.Remove(0, 1);
}
Environment.SetEnvironmentVariable(ApplicationParameters.Environment.Path, updatedPath);
Environment.SetEnvironmentVariable(ApplicationParameters.Environment.PathExtensions, updatedPathExt);
Environment.SetEnvironmentVariable(ApplicationParameters.Environment.PsModulePath, updatedPsModulePath);
}
private static IDictionary convert_to_case_insensitive_dictionary(IDictionary originalDictionary)
{
if (originalDictionary == null) return new Hashtable(new Dictionary<string, string>(), StringComparer.OrdinalIgnoreCase);
return new Hashtable(originalDictionary, StringComparer.OrdinalIgnoreCase);
}
private static void refresh_environment_variables(IDictionary environmentVariables)
{
foreach (DictionaryEntry variable in environmentVariables.or_empty_list_if_null())
{
Environment.SetEnvironmentVariable(variable.Key.to_string(), variable.Value.to_string());
}
}
private static string gather_process_only_items(string currentValues, IEnumerable<string> originalValues)
{
var additionalItems = new StringBuilder();
var items = currentValues.Split(
new[] { ApplicationParameters.Environment.EnvironmentSeparator },
StringSplitOptions.RemoveEmptyEntries
);
foreach (string originalValue in originalValues.or_empty_list_if_null())
{
if (!items.Contains(originalValue, StringComparer.InvariantCultureIgnoreCase))
{
additionalItems.AppendFormat("{0};", originalValue);
}
}
return additionalItems.to_string();
}
}
}