-
Notifications
You must be signed in to change notification settings - Fork 2
/
RS_rsProcessQueue.psm1
106 lines (93 loc) · 4.78 KB
/
RS_rsProcessQueue.psm1
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
Function clearStaleRecords {
param (
[UInt32]$scavengeTime
)
### Scavenge stale records
$nodesJson = Get-Content $(Join-Path ([Environment]::GetEnvironmentVariable('defaultPath','Machine')) 'nodes.json') -Raw | ConvertFrom-Json
foreach($currentNode in $nodesJson.Nodes) {
if($currentNode.timeStamp -le (Get-Date).AddDays(-$scavengeTime)) {
$nodesJson.Nodes = $nodesJson.Nodes -notmatch $currentNode
}
}
Set-Content -Path $(Join-Path ([Environment]::GetEnvironmentVariable('defaultPath','Machine')) 'nodes.json') -Value ($nodesJson | ConvertTo-Json)
}
Function Get-TargetResource {
param (
[parameter(Mandatory = $true)][string]$queueName,
[System.UInt32]$scavengeTime
)
return @{
queueName = $queueName
scavengeTime = $scavengeTime
}
}
Function Test-TargetResource {
param (
[parameter(Mandatory = $true)][string]$queueName,
[System.UInt32]$scavengeTime
)
[Reflection.Assembly]::LoadWithPartialName("System.Messaging") | Out-Null
$q = New-Object System.Messaging.MessageQueue ".\private$\$queueName"
if( $q.GetAllMessages().Length -ne 0 ){
return $false
}
else{
### Scavenge stale records
clearStaleRecords -scavengeTime $scavengeTime
return $true
}
}
Function Set-TargetResource {
param (
[parameter(Mandatory = $true)][string]$queueName,
[System.UInt32]$scavengeTime
)
$d = Get-Content $(Join-Path ([Environment]::GetEnvironmentVariable('defaultPath','Machine')) 'secrets.json') -Raw | ConvertFrom-Json
[Reflection.Assembly]::LoadWithPartialName("System.Messaging") | Out-Null
$q = New-Object System.Messaging.MessageQueue ".\private$\$queueName"
$q.Formatter.TargetTypeNames = ,"System.String"
do {
$timeStamp = Get-Date
$msg = $q.Receive()
$msg = $msg.Body | ConvertFrom-Json
$nodeRecord = @{'NodeName' = "$($msg.Name)";'uuid' = "$($msg.uuid)";'dsc_config' = "$($msg.dsc_config)";'NetworkAdapters' = "$($msg.NetworkAdapters)";'timeStamp' = "$timeStamp"}
if($d.Shared_key -eq $msg.shared_key) {
$nodesJson = Get-Content $(Join-Path ([Environment]::GetEnvironmentVariable('defaultPath','Machine')) 'nodes.json') -Raw | ConvertFrom-Json
if($nodesJson.Nodes.uuid -notcontains $msg.uuid) {
$nodesJson.Nodes += $nodeRecord
Set-Content -Path $(Join-Path ([Environment]::GetEnvironmentVariable('defaultPath','Machine')) 'nodes.json') -Value ($nodesJson | ConvertTo-Json)
}
else {
$currentNode = $nodesJson.Nodes | ? uuid -eq $($msg.uuid)
foreach($property in $currentNode.PSObject.Properties) {
if($msg.PSObject.Properties.Name -contains $property.Name) {
($nodesJson.Nodes | ? uuid -eq $($msg.uuid)).$($property.Name) = $msg.$($property.Name)
}
($nodesJson.Nodes | ? uuid -eq $($msg.uuid)).timeStamp = "$timeStamp"
Set-Content -Path $(Join-Path ([Environment]::GetEnvironmentVariable('defaultPath','Machine')) 'nodes.json') -Value ($nodesJson | ConvertTo-Json)
}
}
$Certificate = New-Object System.Security.Cryptography.X509Certificates.X509Certificate -ArgumentList @(,[System.Convert]::fromBase64String($msg.PublicCert))
#Create Certificates Folder if it does not exist yet
$CertificatesPath = Join-Path -Path ([System.Environment]::GetEnvironmentVariable('defaultPath', 'Machine')) -ChildPath Certificates
If (!(Test-Path -Path $CertificatesPath -PathType Container)) {
New-Item -Path $CertificatesPath -Type Container
}
#build Base64 encoded PEM certificate using StringBuilder and write to file
#called <uuid>.cer
$NodeCertPath = Join-Path -Path $CertificatesPath -ChildPath "$($msg.uuid).cer"
$builder = New-Object System.Text.StringBuilder
$builder.AppendLine("-----BEGIN CERTIFICATE-----")
$builder.AppendLine([System.Convert]::ToBase64String($Certificate.Export([System.Security.Cryptography.X509Certificates.X509ContentType]::Cert)))
$builder.AppendLine("-----END CERTIFICATE-----")
$builder.ToString() | Out-File -FilePath $NodeCertPath
$store = Get-Item Cert:\LocalMachine\Root
$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]"ReadWrite")
$store.Add($Certificate)
$store.Close()
}
} while ( $q.GetAllMessages().Length -ne 0 )
### Scavenge stale records
clearStaleRecords -scavengeTime $scavengeTime
}
Export-ModuleMember -Function *-TargetResource