DSCResources/ROACommon/Src/Save-ROAManagementPortalConfiguration.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
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
function Save-ROAManagementPortalConfiguration {
<#
    .SYNOPSIS
        Writes a RES ONE Automation Management Portal web configuration file.
#>

    [CmdletBinding(SupportsShouldProcess, DefaultParameterSetName = 'WindowsAuthentication')]
    param (
        ## Path to RES ONE Automation Management Portal web configuration file
        [Parameter(Mandatory, ValueFromPipeline)]
        [System.String] $Path,

        ## RES ONE Automation database server/instance name.
        [Parameter(Mandatory)]
        [System.String] $DatabaseServer,

        ## RES ONE Automation database name.
        [Parameter(Mandatory)]
        [System.String] $DatabaseName,
        
        ## RES ONE Automation database access credential. Leave blank to use Windows Authentication for database access.
        [Parameter()]
        [System.Management.Automation.PSCredential] $Credential,

        ## RES ONE Automation API key.
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [System.String] $ApiKey,

        ## RES ONE Identity Broker server Uri.
        [Parameter(Mandatory, ParameterSetName = 'IdentityBroker')]
        [System.String] $IdentityBrokerUrl,

        ## RES ONE Identity Broker application Uri.
        [Parameter(Mandatory, ParameterSetName = 'IdentityBroker')]
        [System.String] $ApplicationUrl,

        ## RES ONE Identity Broker client Id.
        [Parameter(Mandatory, ParameterSetName = 'IdentityBroker')]
        [System.String] $ClientId,

        ## RES ONE Identity Broker client shared secret.
        [Parameter(Mandatory, ParameterSetName = 'IdentityBroker')]
        [System.Management.Automation.PSCredential] $ClientSecret
    )

    $webConsoleConfigTemplate = @'
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<webConsoleConfiguration>
    <managementService>
        <#ManagementServicePlaceholder#>
    </managementService>
    <#AuthenticationPlaceholder#>
    <#PublicApiPlaceholder#>
</webConsoleConfiguration>
'@


$webConsoleManagementServiceSqlAuthenticationTemplate = @'
<database type="<#DatabaseType#>" server="<#DatabaseServer#>" name="<#DatabaseName#>"
            user="<#DatabaseUser#>" password="<#DatabasePassword#>"
            useWindowsAuthentication="false" />
'@


$webConsoleManagementServiceWindowsAuthenticationTemplate = @'
<database type="<#DatabaseType#>" server="<#DatabaseServer#>" name="<#DatabaseName#>"
            useWindowsAuthentication="true" />
'@


$webConsoleSqlAuthenticationTemplate = @'
<authentication type="IdentityBroker">
        <identityServer serverUrl="<#ServerUrl#>"
            applicationUrl="<#ApplicationUrl#>" clientId="<#ClientId#>"
            clientSecret="<#ClientSecret#>" />
    </authentication>
'@


$webConsoleWindowsAuthenticationTemplate = @'
<authentication type="Automation">
        <identityServer serverUrl="" applicationUrl="/" clientId="" clientSecret="" />
    </authentication>
'@


$webConsoleApiEnabledTemplate = @'
<publicAPI enabled="true" apiKey="<#APIKey#>" />
'@


$webConsoleApiDisabledTemplate = @'
<publicAPI enabled="false" />
'@



    if ($null -ne $Credential) {

        $managementService = $webConsoleManagementServiceSqlAuthenticationTemplate;
        $managementService = $managementService.Replace('<#DatabaseUser#>', $Credential.Username);
        $managementService = $managementService.Replace('<#DatabasePassword#>', $Credential.GetNetworkCredential().Password);

        $webConsoleConfig = $webConsoleConfigTemplate.Replace('<#ManagementServicePlaceholder#>', $managementService);
    }
    else {

        $webConsoleConfig = $webConsoleConfigTemplate.Replace('<#ManagementServicePlaceholder#>', $webConsoleManagementServiceWindowsAuthenticationTemplate);
    }
    
    $webConsoleConfig = $webConsoleConfig.Replace('<#DatabaseType#>', 'MSSQL');
    $webConsoleConfig = $webConsoleConfig.Replace('<#DatabaseServer#>', $DatabaseServer);
    $webConsoleConfig = $webConsoleConfig.Replace('<#DatabaseName#>', $DatabaseName);

    if ($PSCmdlet.ParameterSetName -eq 'IdentityBroker') {

        $identityServer = $webConsoleSqlAuthenticationTemplate;
        $identityServer = $identityServer.Replace('<#ServerUrl#>', $IdentityBrokerUrl);
        $identityServer = $identityServer.Replace('<#ApplicationUrl#>', $ApplicationUrl);
        $identityServer = $identityServer.Replace('<#ClientId#>', $ClientId);
        $identityServer = $identityServer.Replace('<#ClientSecret#>', $ClientSecret.GetNetworkCredential().Password);

        $webConsoleConfig = $webConsoleConfig.Replace('<#AuthenticationPlaceholder#>', $identityServer);
    }
    else {

        $webConsoleConfig = $webConsoleConfig.Replace('<#AuthenticationPlaceholder#>', $webConsoleWindowsAuthenticationTemplate);
    }

    if ($null -ne $ApiKey) {

        $apiConfig = $webConsoleApiEnabledTemplate;
        $apiConfig = $apiConfig.Replace('<#APIKey#>', $ApiKey);

        $webConsoleConfig = $webConsoleConfig.Replace('<#PublicApiPlaceholder#>', $apiConfig);
    }
    else {
        
        $webConsoleConfig = $webConsoleConfig.Replace('<#PublicApiPlaceholder#>', $webConsoleApiDisabledTemplate);
    }

    Set-Content -Value $webConsoleConfig -Path $Path -Encoding UTF8;

} #end function