Debian Codegraph @

ansible_collections/microsoft/ad/plugins/modules/service_account.ps1

raw
1 #!powershell
2
3 # Copyright: (c) 2024, Ansible Project
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 #AnsibleRequires -CSharpUtil Ansible.Basic
7 #AnsibleRequires -PowerShell ..module_utils._ADObject
8
9 $setParams = @{
10 PropertyInfo = @(
11 [PSCustomObject]@{
12 Name = 'allowed_to_retrieve_password'
13 Option = @{
14 # The values aren't password, will satisfy sanity tests
15 no_log = $false
16 type = 'add_remove_set'
17 }
18 Attribute = 'PrincipalsAllowedToRetrieveManagedPassword'
19 DNLookup = $true
20 }
21 [PSCustomObject]@{
22 Name = 'delegates'
23 Option = @{
24 aliases = 'principals_allowed_to_delegate'
25 type = 'add_remove_set'
26 }
27 Attribute = 'PrincipalsAllowedToDelegateToAccount'
28 DNLookup = $true
29 }
30 [PSCustomObject]@{
31 Name = 'dns_hostname'
32 Option = @{ type = 'str' }
33 Attribute = 'DNSHostName'
34 }
35 [PSCustomObject]@{
36 Name = 'do_not_append_dollar_to_sam'
37 Option = @{
38 default = $false
39 type = 'bool'
40 }
41 }
42 [PSCustomObject]@{
43 Name = 'enabled'
44 Option = @{ type = 'bool' }
45 Attribute = 'Enabled'
46 }
47 [PSCustomObject]@{
48 Name = 'kerberos_encryption_types'
49 Option = @{
50 type = 'add_remove_set'
51 choices = 'aes128', 'aes256', 'des', 'rc4'
52 }
53 Attribute = 'KerberosEncryptionType'
54 CaseInsensitive = $true
55
56 New = {
57 param($Module, $ADParams, $NewParams)
58
59 $encTypes = @(
60 $Module.Params.kerberos_encryption_types.add
61 $Module.Params.kerberos_encryption_types.set
62 ) | Select-Object -Unique
63
64 $NewParams.KerberosEncryptionType = $encTypes
65 $Module.Diff.after.kerberos_encryption_types = $MencTypes
66 }
67 Set = {
68 param($Module, $ADParams, $SetParams, $ADObject)
69
70 # This is an enum value and needs custom handling for things like
71 # unsetting the values with none.
72 $rawValue = $ADObject.KerberosEncryptionType.Value
73
74 $existing = foreach ($v in [System.Enum]::GetValues($rawValue.GetType())) {
75 if ($rawValue -band $v) { $v.ToString() }
76 }
77 if ($existing -eq 'None') {
78 $existing = @()
79 }
80 $module.Diff.before.kerberos_encryption_types = $existing
81
82 $desired = $Module.Params.kerberos_encryption_types
83 $compareParams = @{
84 Existing = $existing
85 CaseInsensitive = $true
86 }
87 $res = Compare-AnsibleADIdempotentList @compareParams @desired
88 if ($res.Changed) {
89 if ($res.Value) {
90 $SetParams.KerberosEncryptionType = $res.Value -join ', '
91 }
92 else {
93 $SetParams.KerberosEncryptionType = 'None'
94 }
95 }
96 $module.Diff.after.kerberos_encryption_types = $res.Value
97 }
98 }
99 [PSCustomObject]@{
100 Name = 'outbound_auth_only'
101 Option = @{
102 default = $false
103 type = 'bool'
104 }
105 }
106 [PSCustomObject]@{
107 Name = 'sam_account_name'
108 Option = @{ type = 'str' }
109 Attribute = 'sAMAccountName'
110 # New handling is done in PostAction as New-ADServiceAccount cannot
111 # set a SAM without the $ suffix.
112 Set = {
113 param($Module, $ADParams, $SetParams, $ADObject)
114
115 # Using the -SAMAccountName parameter will automatically append
116 # '$' to the value. We want to set the provided user value
117 # which may not have the suffix so we use the raw attribute
118 # replacement method.
119 $sam = $Module.Params.sam_account_name
120 if ($sam -ne $ADObject.SAMAccountName) {
121 if (-not $SetParams.ContainsKey('Replace')) {
122 $SetParams['Replace'] = @{}
123 }
124 $SetParams.Replace['sAMAccountName'] = $sam
125 }
126
127 $module.Diff.after.sam_account_name = $sam
128 }
129 }
130 [PSCustomObject]@{
131 Name = 'spn'
132 Option = @{
133 aliases = 'spns'
134 type = 'add_remove_set'
135 }
136 Attribute = 'servicePrincipalName'
137 CaseInsensitive = $true
138 IsRawAttribute = $true
139 }
140 [PSCustomObject]@{
141 Name = 'trusted_for_delegation'
142 Option = @{ type = 'bool' }
143 Attribute = 'TrustedForDelegation'
144 }
145 [PSCustomObject]@{
146 Name = 'upn'
147 Option = @{ type = 'str' }
148 Attribute = 'userPrincipalName'
149 IsRawAttribute = $true
150 }
151 )
152 ModuleNoun = 'ADServiceAccount'
153 DefaultPath = {
154 param($Module, $ADParams)
155
156 $GUID_MANAGED_SERVICE_ACCOUNTS_CONTAINER_W = '1EB93889E40C45DF9F0C64D23BBB6237'
157 $defaultNamingContext = (Get-ADRootDSE @ADParams -Properties defaultNamingContext).defaultNamingContext
158
159 Get-ADObject @ADParams -Identity $defaultNamingContext -Properties otherWellKnownObjects |
160 Select-Object -ExpandProperty otherWellKnownObjects |
161 Where-Object { $_.StartsWith("B:32:$($GUID_MANAGED_SERVICE_ACCOUNTS_CONTAINER_W):") } |
162 ForEach-Object Substring 38
163 }
164 PreAction = {
165 param ($Module, $ADParams, $ADObject)
166
167 if ($Module.Params.outbound_auth_only) {
168 if ($Module.Params.dns_hostname) {
169 $Module.FailJson("dns_hostname can not be set when outbound_auth_only=true.")
170 }
171 elseif (-not $ADObject) {
172 # -RestrictToOutboundAuthenticationOnly is used in a parameter
173 # set where we cannot set
174 # PrincipalsAllowedToRetrieveManagedPassword. If
175 # outbound_auth_only is set, we use a temp value and unset it
176 # in the post action to simplify our code.
177 $Module.Params.dns_hostname = [Guid]::NewGuid().ToString()
178 }
179 }
180 elseif (
181 $module.Params.state -eq 'present' -and
182 -not $ADObject -and
183 -not $Module.Params.dns_hostname
184 ) {
185 $Module.FailJson('dns_hostname is required when creating a new service account.')
186 }
187
188 if (
189 $Module.Params.sam_account_name -and
190 -not $Module.Params.sam_account_name.EndsWith('$') -and
191 -not $Module.Params.do_not_append_dollar_to_sam
192 ) {
193 $Module.Params.sam_account_name = "$($Module.Params.sam_account_name)$"
194 }
195 }
196 PostAction = {
197 param($Module, $ADParams, $ADObject)
198
199 if ($ADObject) {
200 $Module.Result.sid = $ADObject.SID.Value
201
202 $setParams = @{}
203 # This should only happen when the service account was created.
204 # The code will set sam_account_name to the desired value without
205 # the '$' suffix.
206 if (
207 $Module.Params.state -eq 'present' -and
208 $Module.Params.sam_account_name -and
209 $Module.Params.do_not_append_dollar_to_sam -and
210 $Module.Params.sam_account_name -ne $ADObject.SAMAccountName
211 ) {
212 $setParams['Replace'] = @{
213 sAMAccountName = $module.Params.sam_account_name
214 }
215 }
216 if (
217 $Module.Params.state -eq 'present' -and
218 $Module.Params.outbound_auth_only
219 ) {
220 $Module.Diff.after.Remove('dns_hostname')
221 $setParams['Clear'] = 'dnsHostName'
222 }
223
224 if ($setParams.Count) {
225 $ADObject | Set-ADServiceAccount -WhatIf:$Module.CheckMode @ADParams @setParams
226 }
227 }
228 elseif ($Module.Params.state -eq 'present') {
229 # Use dummy value for check mode when creating a new user
230 $Module.Result.sid = 'S-1-5-0000'
231 }
232 }
233 }
234 Invoke-AnsibleADObject @setParams