WebApplicationProxyDSC.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
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
enum Ensure {
    Absent;
    Present;
}

function ConfigureWAP {
    <#
    Function to configure the Web Application Proxy service and connect it to ADFS
    #>

    param(
        [Parameter(Mandatory = $true)]
        [System.Management.Automation.PSCredential] 
        [System.Management.Automation.Credential()]$Credential,
        [Parameter(Mandatory = $true)]
        [string] $CertificateIdentifier,
        [Parameter(Mandatory = $true)]
        [string] $Certificate,
        [Parameter(Mandatory = $true)]
        [string] $FederationServiceName
    )

    $CmdletName = $PSCmdlet.MyInvocation.MyCommand.Name;

    Write-Verbose -Message ('Entering function {0}' -f $CmdletName);


        if ($CertificateIdentifier.ToLower() -eq 'subject')
        {

            if ($Certificate.ToLower().Substring(0,3) -ne 'cn=')
            {
               $CertSubject = "cn=" + $Certificate.ToLower()
            }
            else
            {
                $CertSubject = $Certificate.ToLower()
            }
            $CertificateThumbprint = Get-ChildItem -Path cert:\LocalMachine\My\ |
            where-Object {$_.Subject.ToLower() -eq $CertSubject} |
            Select-Object -ExpandProperty Thumbprint

        }
        else
        {
            $CertificateThumbprint = $Certificate
        }

    Install-WebApplicationProxy -CertificateThumbprint $CertificateThumbprint -FederationServiceName $FederationServiceName -FederationServiceTrustCredential $Credential
}

[DscResource()]
class WapConfiguration
{
    ### Determines whether or not the WAP Config should exist.
    [DscProperty()]
    [Ensure] $Ensure;

    <#
    The FederationServiceName property is the name of the Active Directory Federation Services (ADFS) service. For example: adfs-service.contoso.com.
    #>

    [DscProperty(key)]
    [string] $FederationServiceName;

    <#
    The Credential property is a PSCredential that represents the username/password of an Active Directory user account that is a member of
    the Domain Administrators security group. This account will be used to add a new proxy to Active Directory Federation Services (ADFS).
    #>

    [DscProperty(Mandatory)]
    [pscredential] $Credential;

    <#
    The CertificateIdentifier property can be either 'Subject' or 'Thumbprint' and indicates what the contents of the 'Certificate' property contains.
    #>

    [DscProperty(Mandatory)]
    [string] $CertificateIdentifier;

    <#
    The Certificate property is either the Subject Name of the certificate or the thumbprint of the certificate, located in the local computer's certificate store, that will be bound to the
    Active Directory Federation Service (ADFS) farm.
    #>

    [DscProperty(Mandatory)]
    [string] $Certificate;

    [WapConfiguration] Get()
    {
        Write-Verbose -Message 'Starting retrieving Web Applucation Proxy configuration.';

        try {
            $WapConfiguration= Get-WebApplicationProxyConfiguration -ErrorAction Stop;
        }
        catch {
            Write-Verbose -Message ('Error occurred while retrieving Web Application Proxy configuration: {0}' -f $global:Error[0].Exception.Message);
        }

        Write-Verbose -Message 'Finished retrieving Web Applucation Proxy configuration.';
        return $this;

    }

    [void] Set()
    {
        ### If WAP shoud be present, then go ahead and configure it.
        if ($this.Ensure -eq [Ensure]::Present) {
            try{
                $WapConfiguration = Get-WebApplicationProxyConfiguration -ErrorAction Stop;
            }
            catch {
                $WapConfiguration = $false
            }

            if (!$WapConfiguration) {
                Write-Verbose -Message 'Configuring Web Application Proxy.';
                $WapSettings = @{
                    Credential = $this.Credential;
                    CertificateIdentifier = $this.CertificateIdentifier;
                    Certificate = $this.Certificate;
                    FederationServiceName = $this.FederationServiceName;
                };
                ConfigureWAP @WapSettings;
            }

            if ($WapConfiguration) {
                #Nothing we can do to reconfigure the service here either, so do nothing
            }
        }

        if ($this.Ensure -eq [Ensure]::Absent) {
            # It is not actually possible to unconfigure WAP, so we do nothing

        }

        return;
    }

    [bool] Test()
    {
        # Assume compliance by default
        $Compliant = $true;


        Write-Verbose -Message 'Testing for presence of Web Application Proxy.';

        try {
            $WapConfiguration = Get-WebApplicationProxyConfiguration -ErrorAction Stop;
        }
        catch {
            $Compliant = $false;
            return $Compliant;
        }

        if ($this.Ensure -eq 'Present') {
            Write-Verbose -Message 'Checking for correct ADFS service configuration.';
            
            if (-not($WapConfiguration.ADFSUrl.ToLower() -contains $this.FederationServiceName.ToLower())) {
                Write-Verbose -Message 'ADFS Service Name doesn''t match the desired state.';
                $Compliant = $false;
            }
        }

        if ($this.Ensure -eq 'Absent') {
            Write-Verbose -Message 'Checking for absence of WAP Configuration.';
            if ($WapConfiguration) {
                Write-Verbose -Message
                $Compliant = $false;
            }
        }

        return $Compliant;
    }

}