UrlAcls/UrlAcls.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
[DscResource()]
class UrlAcls {
    [DscProperty(Key)]
    [string]$Name
    
    [DscProperty()]
    [string[]]$Protocol
    
    [DscProperty()]
    [string]$HostName

    [DscProperty()]
    [string]$SecurityContext
    
    [DscProperty()]
    [int[]]$Port
    
    [DscProperty()]
    [string]$Path = "/"

    [DscProperty()]
    [bool]$Ensure

    [UrlAcls]Get(){
        return $this
    }
    [bool]Test(){
        $result = $true
        $urlAcls = Get-UrlAcl -Port $this.Port -HostName $this.HostName -Protocol $this.Protocol

        if($this.Port -isnot [System.Array]){
            $this.Port = @($this.Port)
        }

        foreach($p in $this.Port)
        {
            $url = $this.FormatUrl($this.Protocol, $this.HostName, $this.Port, $this.Path)

            $acl = $urlAcls | Where-Object { $_.Port -eq $p }
            
            if($null -eq $acl){
                Write-Host "No urlacl found for $url"
                $result = false;
                continue;
            }

            if($null -eq ($url.users | Where-Object { $_.Name -eq $this.SecurityContext })){
                Write-Host "SecurityContext not set on: $url"
                $result = $false
                continue;
            }
        }

        return $result;
    }
    [void]Set(){
        foreach($p in $this.Port)
        {
            $acl = Get-UrlAcl -Url (this.FormatUrl -Protocol $this.Protocol -HostName $this.HostName -Port $this.Port -Port $this.Port)

            if($null -ne $acl){
                $acl | Remove-UrlAcl
            }

            New-UrlAcl -Protocol $this.Protocol -HostName $this.HostName -Port $this.Port -Path $this.Path -SecurityContext $this.SecurityContext
        }
    }
    
    [string]FormatUrl([string]$protocol, [string]$hostName, [string]$port, [string]$path){
        return "${protocol}://${hostName}:$port$path"
    }
}