-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.ps1
97 lines (88 loc) · 3.67 KB
/
init.ps1
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
[CmdletBinding()]
param(
)
#Requires -RunAsAdministrator
Set-StrictMode -Version 'Latest'
& {
Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath 'PSModules\Carbon' -Resolve) -Verbose:$false
Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath 'Carbon.Core' -Resolve) -Verbose:$false
}
$passwordPath = Join-Path -Path $PSScriptRoot -ChildPath 'Tests\.password'
if( -not (Test-Path -Path $passwordPath) )
{
$rng = [Security.Cryptography.RNGCryptoServiceProvider]::New()
$randomBytes = [byte[]]::New(9)
do
{
Write-Verbose -Message ('Generating random password for test accounts.')
$rng.GetBytes($randomBytes);
$password = [Convert]::ToBase64String($randomBytes)
}
# Password needs to contain uppercase letter, lowercase letter, and a number.
while( $password -cnotmatch '[A-Z]' -and $password -cnotmatch '[a-z]' -and $password -notmatch '\d' -and `
$password -notmatch ([regex]::Escape('`~!@#$%^&*()_+-=[]\{}|;'':",./<>?')) )
$password | Set-Content -Path $passwordPath
Write-Verbose -Message ('Generating IV for encrypting test account password on Linux.')
$randomBytes = [byte[]]::New(6)
$rng.GetBytes($randomBytes)
$salt = [Convert]::ToBase64String($randomBytes)
$salt | Add-Content -Path $passwordPath
}
else
{
Get-Content -Path $passwordPath -Raw | Write-Verbose
}
$password,$salt = Get-Content -Path $passwordPath -TotalCount 2
$users =
Import-LocalizedData -BaseDirectory (Join-Path -Path $PSScriptRoot -ChildPath 'Tests') -FileName 'users.psd1' |
ForEach-Object { $_['Users'] } |
ForEach-Object {
$_['Description'] = "Carbon.Core $($_['For']) test user."
[pscustomobject]$_ | Write-Output
}
foreach( $user in $users )
{
if( (Test-COperatingSystem -IsWindows) )
{
$maxLength = $user.Description.Length
if( $maxLength -gt 48 )
{
$maxLength = 48
}
$description = $user.Description.Substring(0, $maxLength)
$credential = [pscredential]::New($user.Name, (ConvertTo-SecureString $password -AsPlainText -Force))
Install-CUser -Credential $credential -Description $description -UserCannotChangePassword
}
elseif( (Test-COperatingSystem -IsMacOS) )
{
$newUid =
sudo dscl . -list /Users UniqueID |
ForEach-Object { $username,$uid = $_ -split ' +' ; return [int]$uid } |
Sort-Object |
Select-Object -Last 1
Write-Verbose " Found highest user ID ""$($newUid)""."
$newUid += 1
$username = $user.Name
Write-Verbose " Creating $($username) (uid: $($newUid))"
# Create the user account
sudo dscl . -create /Users/$username
sudo dscl . -create /Users/$username UserShell /bin/bash
sudo dscl . -create /Users/$username RealName $username
sudo dscl . -create /Users/$username UniqueID $newUid
sudo dscl . -create /Users/$username PrimaryGroupID 20
sudo dscl . -create /Users/$username NFSHomeDirectory /Users/$username
sudo dscl . -passwd /Users/$username $password
sudo createhomedir -c
}
elseif( (Test-COperatingSystem -IsLinux) )
{
$userExists = Get-Content '/etc/passwd' | Where-Object { $_ -match "^$([regex]::Escape($user.Name))\b"}
if( $userExists )
{
continue
}
Write-Verbose -Message ("Adding user ""$($user.Name)"".")
$encryptedPassword = $password | openssl passwd -stdin -salt $salt
sudo useradd -p $encryptedPassword -m $user.Name --comment $user.Description
}
}