-
Notifications
You must be signed in to change notification settings - Fork 895
Expand file tree
/
Copy pathpwsh_executor.rs
More file actions
926 lines (821 loc) · 32.5 KB
/
pwsh_executor.rs
File metadata and controls
926 lines (821 loc) · 32.5 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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
use std::{collections::HashMap, fs, process::Stdio};
use regex::Regex;
use serde_json::{json, value::RawValue};
use sqlx::types::Json;
use tokio::process::Command;
use windmill_common::client::AuthedClient;
use windmill_common::error::Error;
use windmill_common::worker::{to_raw_value, write_file, Connection};
use windmill_queue::{
append_logs, CanceledBy, MiniPulledJob, INIT_SCRIPT_PATH_PREFIX, PERIODIC_SCRIPT_PATH_PREFIX,
};
#[cfg(windows)]
use crate::SYSTEM_ROOT;
const NSJAIL_CONFIG_RUN_POWERSHELL_CONTENT: &str =
include_str!("../nsjail/run.powershell.config.proto");
lazy_static::lazy_static! {
static ref RE_POWERSHELL_IMPORTS: Regex = Regex::new(r#"^\s*Import-Module\s+(?:-Name\s+)?"?([^\s"]+)"?(?:\s+-RequiredVersion\s+"?([^\s"]+)"?)?"#).unwrap();
}
use crate::{
common::{
build_args_map, build_command_with_isolation, get_reserved_variables, read_file,
read_file_content, start_child_process, OccupancyMetrics,
},
handle_child::handle_child,
is_sandboxing_enabled, read_ee_registry, DISABLE_NUSER, HOME_ENV, NSJAIL_PATH, PATH_ENV,
POWERSHELL_CACHE_DIR, POWERSHELL_PATH, POWERSHELL_REPO_PAT, POWERSHELL_REPO_URL, PROXY_ENVS,
TZ_ENV,
};
fn val_to_pwsh_param(v: serde_json::Value) -> String {
match v {
serde_json::Value::Array(x) => format!(
"@({})",
x.into_iter()
.map(|v| val_to_pwsh_param(v))
.collect::<Vec<_>>()
.join(",")
),
serde_json::Value::Object(x) => {
let str = serde_json::to_string(&x).unwrap_or_else(|_| "{}".to_string());
let escaped = str.replace("'", "''");
format!("(ConvertFrom-Json '{escaped}')")
}
serde_json::Value::Null => "$null".to_string(),
serde_json::Value::Bool(x) => format!("${x}"),
serde_json::Value::String(x) => {
let escaped = x.replace("'", "''");
format!("'{escaped}'")
}
serde_json::Value::Number(x) => x.to_string(),
}
}
fn raw_to_pwsh_param(x: &str) -> String {
match serde_json::from_str::<serde_json::Value>(x) {
Ok(v) => val_to_pwsh_param(v),
Err(e) => {
tracing::error!("Error converting JSON to string: {:?}", e);
"$null".to_string()
}
}
}
fn generate_powershell_install_code() -> String {
r#"
$ErrorActionPreference = 'Stop'
$availableModules = Get-Module -ListAvailable
$path = '{path}'
$hasPrivateRepo = {has_private_repo}
$hasCredentials = {has_credentials}
$jobId = '{job_id}'
$privateRepoUrl = '{private_repo_url}'
$privateRepoPat = '{private_repo_pat}'
# Setup private repository if configured
$repoName = $null
$credentials = $null
if ($hasPrivateRepo) {
$repoName = "windmill-private-$jobId"
$repoUri = "$privateRepoUrl"
# Create PSCredential for authentication only if PAT is provided
if ($hasCredentials) {
$username = "token"
$patToken = ConvertTo-SecureString $privateRepoPat -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential($username, $patToken)
}
Write-Host "Registering temporary repository: $repoName"
# Remove repository if it already exists
Unregister-PSResourceRepository -Name $repoName -ErrorAction SilentlyContinue
Register-PSResourceRepository -Name $repoName -Uri $repoUri -Trusted
}
try {
$moduleRequests = @({modules})
foreach ($moduleRequest in $moduleRequests) {
$moduleName = $moduleRequest.Name
$requiredVersion = $moduleRequest.Version
# Check if module is already installed with the required version (case-insensitive)
$isInstalled = $false
if ($requiredVersion) {
$isInstalled = $availableModules | Where-Object { $_.Name -eq $moduleName -and $_.Version -eq $requiredVersion }
} else {
$isInstalled = $availableModules | Where-Object { $_.Name -eq $moduleName }
}
if (-not $isInstalled) {
$moduleFound = $false
# First try private repository if configured
if ($hasPrivateRepo) {
$findParams = @{ Name = $moduleName; Repository = $repoName; ErrorAction = 'SilentlyContinue' }
if ($credentials) { $findParams.Credential = $credentials }
if ($requiredVersion) { $findParams.Version = $requiredVersion }
$privateModule = Find-PSResource @findParams
if ($privateModule) {
$moduleFound = $true
$versionInfo = if ($requiredVersion) { " version $requiredVersion" } else { "" }
Write-Host "Found module $moduleName$versionInfo in private repository, installing from there..."
$saveParams = @{ Name = $moduleName; Path = $path; Repository = $repoName }
if ($credentials) { $saveParams.Credential = $credentials }
if ($requiredVersion) { $saveParams.Version = $requiredVersion }
Save-PSResource @saveParams
}
}
# If not found in private repo (or no private repo configured), try all repositories
if (-not $moduleFound) {
$versionInfo = if ($requiredVersion) { " version $requiredVersion" } else { "" }
Write-Host "Installing module $moduleName$versionInfo from public repositories..."
$saveParams = @{ Name = $moduleName; Path = $path; TrustRepository = $true }
if ($requiredVersion) { $saveParams.Version = $requiredVersion }
Save-PSResource @saveParams
}
} else {
$versionInfo = if ($requiredVersion) { " version $requiredVersion" } else { "" }
Write-Host "Module $moduleName$versionInfo already installed"
}
}
} finally {
if ($hasPrivateRepo) {
Write-Host "Unregistering temporary repository: $repoName"
Unregister-PSResourceRepository -Name $repoName
}
}
"#.to_string()
}
async fn scan_module_directories() -> Result<HashMap<String, String>, Error> {
let mut module_dirs = HashMap::new();
let cache_dir = std::path::Path::new(&*POWERSHELL_CACHE_DIR);
if let Ok(entries) = fs::read_dir(cache_dir) {
for entry in entries {
if let Ok(entry) = entry {
let module_path = entry.path();
if module_path.is_dir() {
if let Some(module_name) = module_path.file_name().and_then(|n| n.to_str()) {
module_dirs.insert(
module_name.to_lowercase(), // Use lowercase for case-insensitive lookup
module_path.to_string_lossy().to_string(),
);
}
}
}
}
}
Ok(module_dirs)
}
async fn get_module_versions(module_path: &str) -> Result<Vec<String>, Error> {
let mut versions = Vec::new();
// Look for version subdirectories within the module directory
if let Ok(version_entries) = fs::read_dir(module_path) {
for version_entry in version_entries {
if let Ok(version_entry) = version_entry {
let version_path = version_entry.path();
if version_path.is_dir() {
let version = version_path
.file_name()
.and_then(|n| n.to_str())
.unwrap_or_default()
.to_string();
// Check if this looks like a version (contains dots and numbers)
// and verify a module manifest (.psd1) or script (.psm1) actually exists
if version.chars().any(|c| c.is_numeric()) && version.contains('.') {
let has_module_files = fs::read_dir(&version_path)
.map(|entries| {
entries.filter_map(|e| e.ok()).any(|e| {
let name = e.file_name();
let name = name.to_string_lossy();
name.ends_with(".psd1") || name.ends_with(".psm1")
})
})
.unwrap_or(false);
if has_module_files {
versions.push(version);
}
}
}
}
}
}
// If no version subdirectories found, check if module files exist directly
// in the module directory (flat/single-version installation)
if versions.is_empty() {
let has_module_files = fs::read_dir(module_path)
.map(|entries| {
entries.filter_map(|e| e.ok()).any(|e| {
let name = e.file_name();
let name = name.to_string_lossy();
name.ends_with(".psd1") || name.ends_with(".psm1")
})
})
.unwrap_or(false);
if has_module_files {
versions.push("unknown".to_string());
}
}
Ok(versions)
}
async fn check_module_installed(
module_dirs: &HashMap<String, String>,
module_name: &str,
required_version: Option<&str>,
) -> Result<(bool, Vec<String>), Error> {
let module_key = module_name.to_lowercase();
if let Some(module_path) = module_dirs.get(&module_key) {
let versions = get_module_versions(module_path).await?;
let is_installed = match required_version {
Some(version) => versions.iter().any(|v| v == version),
None => !versions.is_empty(),
};
Ok((is_installed, versions))
} else {
Ok((false, Vec::new()))
}
}
#[derive(Debug)]
struct ModuleRequest {
name: String,
version: Option<String>,
}
#[tracing::instrument(level = "trace", skip_all)]
pub async fn handle_powershell_job(
mem_peak: &mut i32,
canceled_by: &mut Option<CanceledBy>,
job: &MiniPulledJob,
db: &Connection,
client: &AuthedClient,
parent_runnable_path: Option<String>,
content: &str,
job_dir: &str,
shared_mount: &str,
base_internal_url: &str,
worker_name: &str,
envs: HashMap<String, String>,
occupancy_metrics: &mut OccupancyMetrics,
) -> Result<Box<RawValue>, Error> {
let pwsh_args = {
let args = build_args_map(job, client, &db).await?.map(Json);
let job_args = if args.is_some() {
args.as_ref()
} else {
job.args.as_ref()
};
let parsed_sig = windmill_parser_bash::parse_powershell_sig(&content)?;
parsed_sig
.args
.iter()
.filter_map(|arg| {
let value_opt = job_args.and_then(|x| x.get(&arg.name));
// Check if this is a switch parameter (only [switch], not [bool])
let is_switch = arg
.otyp
.as_ref()
.map(|t| t.to_lowercase() == "switch")
.unwrap_or(false);
if is_switch {
// Handle switch parameters: -SwitchName or omit
if let Some(value) = value_opt {
match serde_json::from_str::<serde_json::Value>(value.get()) {
Ok(serde_json::Value::Bool(true)) => {
// Switch is enabled: just pass -SwitchName
Some(format!("-{}", arg.name))
}
Ok(serde_json::Value::Bool(false)) | _ => {
// Switch is disabled or invalid: omit the parameter
None
}
}
} else {
// No value provided, omit the switch (defaults to false)
None
}
} else {
// Regular parameter (including [bool]): format as -ParamName Value
// For [bool] parameters, this will be -ParamName $true or -ParamName $false
value_opt.map(|v| format!("-{} {}", arg.name, raw_to_pwsh_param(v.get())))
}
})
.collect::<Vec<_>>()
.join(" ")
};
// First, collect all imported modules
let mut imported_modules: Vec<(String, Option<String>)> = Vec::new();
for line in content.lines() {
for cap in RE_POWERSHELL_IMPORTS.captures_iter(line) {
let module_name = cap.get(1).unwrap().as_str().to_string();
let required_version = cap.get(2).map(|m| m.as_str().to_string());
imported_modules.push((module_name, required_version));
}
}
// Only scan the top-level cache directory if there are modules to check
let module_dirs = if !imported_modules.is_empty() {
scan_module_directories().await?
} else {
HashMap::new()
};
let mut modules_to_install: Vec<ModuleRequest> = Vec::new();
let mut logs1 = String::new();
for (module_name, required_version) in imported_modules {
// Check if this specific module is already installed, only scanning versions if needed
let (is_installed, installed_versions) =
check_module_installed(&module_dirs, &module_name, required_version.as_deref()).await?;
if !is_installed {
modules_to_install.push(ModuleRequest {
name: module_name.clone(),
version: required_version.clone(),
});
} else {
// Log what versions are actually installed
let version_info = if let Some(version) = &required_version {
format!(" version {} found in cache", version)
} else if installed_versions.len() == 1 {
format!(" (version {}) found in cache", installed_versions[0])
} else if installed_versions.len() > 1 {
format!(
" (versions: {}) found in cache",
installed_versions.join(", ")
)
} else {
" found in cache".to_string()
};
logs1.push_str(&format!("\n{}{}", module_name, version_info));
}
}
if !logs1.is_empty() {
append_logs(&job.id, &job.workspace_id, logs1, db).await;
}
if !modules_to_install.is_empty() {
let powershell_repo_url = read_ee_registry(
POWERSHELL_REPO_URL.read().await.clone(),
"powershell repo url",
&job.id,
&job.workspace_id,
db,
)
.await;
let powershell_repo_pat = read_ee_registry(
POWERSHELL_REPO_PAT.read().await.clone(),
"powershell repo pat",
&job.id,
&job.workspace_id,
db,
)
.await;
let has_private_repo = powershell_repo_url.is_some();
let has_credentials = powershell_repo_pat.is_some();
let modules_list = modules_to_install
.iter()
.map(|module_req| {
if let Some(version) = &module_req.version {
format!(
"@{{ Name = '{}'; Version = '{}' }}",
module_req.name, version
)
} else {
format!("@{{ Name = '{}'; Version = $null }}", module_req.name)
}
})
.collect::<Vec<_>>()
.join(", ");
let install_string = generate_powershell_install_code()
.replace("{path}", &*POWERSHELL_CACHE_DIR)
.replace("{job_id}", &job.id.to_string())
.replace("{has_private_repo}", &format!("${has_private_repo}"))
.replace("{has_credentials}", &format!("${has_credentials}"))
.replace(
"{private_repo_url}",
&powershell_repo_url.unwrap_or_default(),
)
.replace(
"{private_repo_pat}",
&powershell_repo_pat.unwrap_or_default(),
)
.replace("{modules}", &modules_list);
let mut cmd = Command::new(POWERSHELL_PATH.as_str());
cmd.args(&["-Command", &install_string])
.stdout(Stdio::piped())
.stderr(Stdio::piped());
let child = start_child_process(cmd, POWERSHELL_PATH.as_str(), false).await?;
handle_child(
&job.id,
db,
mem_peak,
canceled_by,
child,
false,
worker_name,
&job.workspace_id,
"powershell install",
job.timeout,
false,
&mut Some(occupancy_metrics),
None,
None,
)
.await?;
}
let mut logs2 = "".to_string();
logs2.push_str("\n\n--- POWERSHELL CODE EXECUTION ---\n");
append_logs(&job.id, &job.workspace_id, logs2, db).await;
// make sure default (only allhostsallusers) modules are loaded, disable autoload (cache can be large to explore especially on cloud) and add /tmp/windmill/cache to PSModulePath
#[cfg(unix)]
let profile = format!(
"$PSModuleAutoloadingPreference = 'None'
$PSModulePathBackup = $env:PSModulePath
$env:PSModulePath = \"$PSHome/Modules\"
Get-Module -ListAvailable | Import-Module
$env:PSModulePath = \"{}:$PSModulePathBackup\"",
*POWERSHELL_CACHE_DIR
);
#[cfg(windows)]
let profile = format!(
"$PSModuleAutoloadingPreference = 'None'
$PSModulePathBackup = $env:PSModulePath
$env:PSModulePath = \"C:\\Program Files\\PowerShell\\7\\Modules\"
Get-Module -ListAvailable | Import-Module
$env:PSModulePath = \"{};$PSModulePathBackup\"",
*POWERSHELL_CACHE_DIR
);
// NOTE: powershell error handling / termination is quite tricky compared to bash
// here we're trying to catch terminating errors and propagate the exit code
// to the caller such that the job will be marked as failed. It's up to the user
// to catch specific errors in their script not caught by the below as there is no
// generic set -eu as in bash
let strict_termination_start = "$ErrorActionPreference = 'Stop'\n\
Set-StrictMode -Version Latest\n\
try {\n";
let strict_termination_end = "\n\
} catch {\n\
Write-Output \"An error occurred:\"\n\
Write-Output $_\n\
exit 1\n\
}\n";
// make sure param() with its attributes is first
let content: String = if let Some((param_block, remaining_code)) =
windmill_parser_bash::extract_powershell_param_block_with_attributes(&content, true)
{
format!(
"{}\n{}\n{}\n{}\n{}",
param_block, profile, strict_termination_start, remaining_code, strict_termination_end
)
} else {
format!("{}\n{}", profile, content)
};
write_file(job_dir, "main.ps1", content.as_str())?;
write_file(
job_dir,
"wrapper.ps1",
&format!(
"$ErrorActionPreference = 'Stop'\n\
$pipe = New-TemporaryFile\n\
./main.ps1 {pwsh_args} 2>&1 | Tee-Object -FilePath $pipe\n\
Get-Content -Path $pipe | Select-Object -Last 1 | Set-Content -Path './result2.out'\n\
Remove-Item $pipe\n\
exit $LASTEXITCODE\n"
),
)?;
let mut reserved_variables =
get_reserved_variables(job, &client.token, db, parent_runnable_path).await?;
reserved_variables.insert("RUST_LOG".to_string(), "info".to_string());
let _ = write_file(job_dir, "result.json", "")?;
let _ = write_file(job_dir, "result.out", "")?;
let _ = write_file(job_dir, "result2.out", "")?;
// Check if this is a regular job (not init or periodic script)
// Init/periodic scripts need full system access without isolation
let is_regular_job = job
.runnable_path
.as_ref()
.map(|x| {
!x.starts_with(INIT_SCRIPT_PATH_PREFIX) && !x.starts_with(PERIODIC_SCRIPT_PATH_PREFIX)
})
.unwrap_or(true);
let nsjail = is_sandboxing_enabled() && is_regular_job;
let child = if nsjail {
let _ = write_file(
job_dir,
"run.config.proto",
&NSJAIL_CONFIG_RUN_POWERSHELL_CONTENT
.replace("{JOB_DIR}", job_dir)
.replace("{CLONE_NEWUSER}", &(!*DISABLE_NUSER).to_string())
.replace("{SHARED_MOUNT}", shared_mount)
.replace("{CACHE_DIR}", &*POWERSHELL_CACHE_DIR),
)?;
let cmd_args = vec![
"--config",
"run.config.proto",
"--",
POWERSHELL_PATH.as_str(),
"wrapper.ps1",
];
let mut cmd = Command::new(NSJAIL_PATH.as_str());
cmd.current_dir(job_dir)
.env_clear()
.envs(PROXY_ENVS.clone())
.envs(reserved_variables)
.env("TZ", TZ_ENV.as_str())
.env("PATH", PATH_ENV.as_str())
.env("BASE_INTERNAL_URL", base_internal_url)
.args(cmd_args)
.stdout(Stdio::piped())
.stderr(Stdio::piped());
start_child_process(cmd, NSJAIL_PATH.as_str(), false).await?
} else {
let cmd_args;
#[cfg(unix)]
{
cmd_args = vec!["wrapper.ps1"];
}
#[cfg(windows)]
{
cmd_args = vec![r".\wrapper.ps1"];
}
// Only apply unshare isolation for regular jobs, not init/periodic scripts
let mut cmd = if is_regular_job {
build_command_with_isolation(POWERSHELL_PATH.as_str(), &cmd_args)
} else {
let mut c = Command::new(POWERSHELL_PATH.as_str());
c.args(&cmd_args);
c
};
cmd.current_dir(job_dir)
.env_clear()
.envs(envs)
.envs(reserved_variables)
.env("TZ", TZ_ENV.as_str())
.env("PATH", PATH_ENV.as_str())
.env("BASE_INTERNAL_URL", base_internal_url)
.env("HOME", HOME_ENV.as_str())
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped());
#[cfg(windows)]
{
cmd.env("SystemRoot", SYSTEM_ROOT.as_str())
.env("WINDIR", SYSTEM_ROOT.as_str())
.env(
"LOCALAPPDATA",
std::env::var("LOCALAPPDATA")
.unwrap_or_else(|_| format!("{}\\AppData\\Local", HOME_ENV.as_str())),
)
.env(
"ProgramData",
std::env::var("ProgramData")
.unwrap_or_else(|_| String::from("C:\\ProgramData")),
)
.env(
"ProgramFiles",
std::env::var("ProgramFiles")
.unwrap_or_else(|_| String::from("C:\\Program Files")),
)
.env(
"ProgramFiles(x86)",
std::env::var("ProgramFiles(x86)")
.unwrap_or_else(|_| String::from("C:\\Program Files (x86)")),
)
.env(
"ProgramW6432",
std::env::var("ProgramW6432")
.unwrap_or_else(|_| String::from("C:\\Program Files")),
)
.env(
"TMP",
std::env::var("TMP").unwrap_or_else(|_| String::from("/tmp")),
)
.env(
"PATHEXT",
std::env::var("PATHEXT").unwrap_or_else(|_| {
String::from(".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL")
}),
)
.env("USERPROFILE", crate::USERPROFILE_ENV.as_str());
}
start_child_process(cmd, POWERSHELL_PATH.as_str(), false).await?
};
handle_child(
&job.id,
db,
mem_peak,
canceled_by,
child,
is_sandboxing_enabled(),
worker_name,
&job.workspace_id,
"powershell run",
job.timeout,
false,
&mut Some(occupancy_metrics),
None,
None,
)
.await?;
let result_json_path = format!("{job_dir}/result.json");
if let Ok(metadata) = tokio::fs::metadata(&result_json_path).await {
if metadata.len() > 0 {
return Ok(read_file(&result_json_path).await?);
}
}
let result_out_path = format!("{job_dir}/result.out");
if let Ok(metadata) = tokio::fs::metadata(&result_out_path).await {
if metadata.len() > 0 {
let result = read_file_content(&result_out_path).await?;
return Ok(to_raw_value(&json!(result)));
}
}
let result_out_path2 = format!("{job_dir}/result2.out");
if tokio::fs::metadata(&result_out_path2).await.is_ok() {
let result = read_file_content(&result_out_path2)
.await?
.trim()
.to_string();
return Ok(to_raw_value(&json!(result)));
}
Ok(to_raw_value(&json!(
"No result.out, result2.out or result.json found"
)))
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use tempfile::TempDir;
// --- RE_POWERSHELL_IMPORTS regex tests ---
fn match_import(line: &str) -> Option<(String, Option<String>)> {
RE_POWERSHELL_IMPORTS.captures(line).map(|cap| {
let name = cap.get(1).unwrap().as_str().to_string();
let version = cap.get(2).map(|m| m.as_str().to_string());
(name, version)
})
}
#[test]
fn test_import_module_basic() {
let (name, version) = match_import("Import-Module WindmillClient").unwrap();
assert_eq!(name, "WindmillClient");
assert_eq!(version, None);
}
#[test]
fn test_import_module_with_leading_whitespace() {
let (name, _) = match_import(" Import-Module WindmillClient").unwrap();
assert_eq!(name, "WindmillClient");
}
#[test]
fn test_import_module_with_tab_indent() {
let (name, _) = match_import("\tImport-Module WindmillClient").unwrap();
assert_eq!(name, "WindmillClient");
}
#[test]
fn test_import_module_with_name_flag() {
let (name, _) = match_import("Import-Module -Name WindmillClient").unwrap();
assert_eq!(name, "WindmillClient");
}
#[test]
fn test_import_module_with_required_version() {
let (name, version) =
match_import(r#"Import-Module WindmillClient -RequiredVersion "1.655.0""#).unwrap();
assert_eq!(name, "WindmillClient");
assert_eq!(version, Some("1.655.0".to_string()));
}
#[test]
fn test_import_module_quoted_name() {
let (name, _) = match_import(r#"Import-Module "WindmillClient""#).unwrap();
assert_eq!(name, "WindmillClient");
}
#[test]
fn test_import_module_name_flag_quoted_with_version() {
let (name, version) =
match_import(r#"Import-Module -Name "WindmillClient" -RequiredVersion "2.0.0""#)
.unwrap();
assert_eq!(name, "WindmillClient");
assert_eq!(version, Some("2.0.0".to_string()));
}
#[test]
fn test_import_module_indented_with_version() {
let (name, version) =
match_import(r#" Import-Module WindmillClient -RequiredVersion 1.0.0"#).unwrap();
assert_eq!(name, "WindmillClient");
assert_eq!(version, Some("1.0.0".to_string()));
}
#[test]
fn test_commented_import_not_matched() {
assert!(match_import("# Import-Module WindmillClient").is_none());
}
// --- get_module_versions / check_module_installed tests ---
#[tokio::test]
async fn test_empty_module_dir_not_installed() {
let tmp = TempDir::new().unwrap();
let module_dir = tmp.path().join("WindmillClient");
fs::create_dir(&module_dir).unwrap();
let versions = get_module_versions(module_dir.to_str().unwrap())
.await
.unwrap();
assert!(versions.is_empty(), "empty dir should have no versions");
}
#[tokio::test]
async fn test_empty_version_subdir_not_installed() {
let tmp = TempDir::new().unwrap();
let module_dir = tmp.path().join("WindmillClient");
let version_dir = module_dir.join("1.655.0");
fs::create_dir_all(&version_dir).unwrap();
let versions = get_module_versions(module_dir.to_str().unwrap())
.await
.unwrap();
assert!(
versions.is_empty(),
"version dir without .psd1/.psm1 should not count"
);
}
#[tokio::test]
async fn test_valid_versioned_module_detected() {
let tmp = TempDir::new().unwrap();
let module_dir = tmp.path().join("WindmillClient");
let version_dir = module_dir.join("1.655.0");
fs::create_dir_all(&version_dir).unwrap();
fs::write(version_dir.join("WindmillClient.psd1"), "# manifest").unwrap();
fs::write(version_dir.join("WindmillClient.psm1"), "# module").unwrap();
let versions = get_module_versions(module_dir.to_str().unwrap())
.await
.unwrap();
assert_eq!(versions, vec!["1.655.0"]);
}
#[tokio::test]
async fn test_flat_module_with_files_detected() {
let tmp = TempDir::new().unwrap();
let module_dir = tmp.path().join("MyModule");
fs::create_dir(&module_dir).unwrap();
fs::write(module_dir.join("MyModule.psm1"), "# module").unwrap();
let versions = get_module_versions(module_dir.to_str().unwrap())
.await
.unwrap();
assert_eq!(versions, vec!["unknown"]);
}
#[tokio::test]
async fn test_flat_module_without_files_not_detected() {
let tmp = TempDir::new().unwrap();
let module_dir = tmp.path().join("MyModule");
fs::create_dir(&module_dir).unwrap();
fs::write(module_dir.join("readme.txt"), "not a module").unwrap();
let versions = get_module_versions(module_dir.to_str().unwrap())
.await
.unwrap();
assert!(versions.is_empty());
}
#[tokio::test]
async fn test_check_module_installed_empty_dir_returns_false() {
let tmp = TempDir::new().unwrap();
let module_dir = tmp.path().join("WindmillClient");
fs::create_dir(&module_dir).unwrap();
let mut dirs = HashMap::new();
dirs.insert(
"windmillclient".to_string(),
module_dir.to_str().unwrap().to_string(),
);
let (installed, _) = check_module_installed(&dirs, "WindmillClient", None)
.await
.unwrap();
assert!(
!installed,
"empty module dir should not be considered installed"
);
}
#[tokio::test]
async fn test_check_module_installed_valid_module_returns_true() {
let tmp = TempDir::new().unwrap();
let module_dir = tmp.path().join("WindmillClient");
let version_dir = module_dir.join("1.655.0");
fs::create_dir_all(&version_dir).unwrap();
fs::write(version_dir.join("WindmillClient.psd1"), "# manifest").unwrap();
let mut dirs = HashMap::new();
dirs.insert(
"windmillclient".to_string(),
module_dir.to_str().unwrap().to_string(),
);
let (installed, versions) = check_module_installed(&dirs, "WindmillClient", None)
.await
.unwrap();
assert!(installed);
assert_eq!(versions, vec!["1.655.0"]);
}
#[tokio::test]
async fn test_check_module_installed_wrong_version_returns_false() {
let tmp = TempDir::new().unwrap();
let module_dir = tmp.path().join("WindmillClient");
let version_dir = module_dir.join("1.0.0");
fs::create_dir_all(&version_dir).unwrap();
fs::write(version_dir.join("WindmillClient.psd1"), "# manifest").unwrap();
let mut dirs = HashMap::new();
dirs.insert(
"windmillclient".to_string(),
module_dir.to_str().unwrap().to_string(),
);
let (installed, _) = check_module_installed(&dirs, "WindmillClient", Some("2.0.0"))
.await
.unwrap();
assert!(!installed, "wrong version should not match");
}
#[tokio::test]
async fn test_multiple_versions_detected() {
let tmp = TempDir::new().unwrap();
let module_dir = tmp.path().join("WindmillClient");
for ver in &["1.0.0", "1.655.0"] {
let version_dir = module_dir.join(ver);
fs::create_dir_all(&version_dir).unwrap();
fs::write(version_dir.join("WindmillClient.psd1"), "# manifest").unwrap();
}
let mut versions = get_module_versions(module_dir.to_str().unwrap())
.await
.unwrap();
versions.sort();
assert_eq!(versions, vec!["1.0.0", "1.655.0"]);
}
}