2021-03-10 09:02:55 +00:00
|
|
|
[CmdletBinding()]
|
|
|
|
Param(
|
|
|
|
[Parameter()]
|
|
|
|
[string]$VaultAPIAddress,
|
|
|
|
[Parameter()]
|
|
|
|
[string]$VaultToken,
|
|
|
|
[Parameter()]
|
|
|
|
[string]$VaultPwPolicy,
|
|
|
|
[Parameter(Mandatory)]
|
2021-03-12 09:34:45 +00:00
|
|
|
[string]$VaultSecret,
|
2021-03-10 09:02:55 +00:00
|
|
|
[Parameter(Mandatory)]
|
|
|
|
[string]$Username
|
|
|
|
)
|
|
|
|
|
2021-03-10 10:32:53 +00:00
|
|
|
# Generate new password
|
2021-03-10 09:02:55 +00:00
|
|
|
$InvokeWebRequestSplat = @{
|
2021-03-12 09:34:45 +00:00
|
|
|
Uri = "$($VaultAPIAddress)/sys/policies/password/$($VaultPwPolicy)/generate"
|
|
|
|
Headers = @{'X-Vault-Token'="$VaultToken"}
|
|
|
|
UseBasicParsing = $True
|
2021-03-10 09:02:55 +00:00
|
|
|
}
|
|
|
|
$NewPassword = (Invoke-WebRequest @InvokeWebRequestSplat | ConvertFrom-Json).data.password
|
|
|
|
|
2021-03-10 10:32:53 +00:00
|
|
|
# Check for existense of secret
|
|
|
|
$Response, $ErrResponse = $Null, $Null
|
|
|
|
Try {
|
|
|
|
$InvokeWebRequestSplat = @{
|
2021-03-12 09:34:45 +00:00
|
|
|
Uri = "$($VaultAPIAddress)/secret/metadata/$($VaultSecret)"
|
2021-03-10 10:32:53 +00:00
|
|
|
Headers = @{'X-Vault-Token' = "$VaultToken"}
|
|
|
|
UseBasicParsing = $True
|
|
|
|
}
|
|
|
|
$Response = Invoke-WebRequest @InvokeWebRequestSplat
|
|
|
|
}
|
2021-03-12 09:34:45 +00:00
|
|
|
Catch [System.Net.WebException] {
|
2021-03-10 10:32:53 +00:00
|
|
|
$StreamReader = [System.IO.StreamReader]::new($_.Exception.Response.GetResponseStream())
|
|
|
|
$StreamReader.BaseStream.Position = 0
|
|
|
|
$ErrResponse = $StreamReader.ReadToEnd()
|
|
|
|
$StreamReader.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
If ([boolean]$Response) {
|
|
|
|
# Secret already exists; retrieve existing key/value pairs
|
|
|
|
$InvokeWebRequestSplat = @{
|
2021-03-12 09:34:45 +00:00
|
|
|
Uri = "$($VaultAPIAddress)/secret/data/$($VaultSecret)"
|
2021-03-10 10:32:53 +00:00
|
|
|
Headers = @{'X-Vault-Token' = "$VaultToken"}
|
|
|
|
UseBasicParsing = $True
|
|
|
|
}
|
|
|
|
$Secret = (Invoke-WebRequest @InvokeWebRequestSplat | ConvertFrom-Json).data
|
|
|
|
|
|
|
|
# Merge new password into dictionary
|
|
|
|
$AddMemberSplat = @{
|
|
|
|
MemberType = 'NoteProperty'
|
|
|
|
Name = "password.$($Username)"
|
|
|
|
Value = $NewPassword
|
|
|
|
Force = $True
|
|
|
|
}
|
|
|
|
$Secret.data | Add-Member @AddMemberSplat
|
|
|
|
|
|
|
|
# Store as new version
|
|
|
|
$InvokeWebRequestSplat = @{
|
2021-03-12 09:34:45 +00:00
|
|
|
Uri = "$($VaultAPIAddress)/secret/data/$($VaultSecret)"
|
|
|
|
Method = 'POST'
|
|
|
|
UseBasicParsing = $True
|
|
|
|
Headers = @{'X-Vault-Token'="$VaultToken"}
|
|
|
|
Body = @{
|
2021-03-10 10:32:53 +00:00
|
|
|
data = $Secret.data
|
|
|
|
} | ConvertTo-Json
|
|
|
|
}
|
2021-03-12 09:34:45 +00:00
|
|
|
Invoke-WebRequest @InvokeWebRequestSplat | Out-Null
|
2021-03-10 10:32:53 +00:00
|
|
|
}
|
|
|
|
ElseIf ([boolean]$ErrResponse) {
|
|
|
|
# Secret did not exist yet, store as new secret
|
|
|
|
$InvokeWebRequestSplat = @{
|
2021-03-12 09:34:45 +00:00
|
|
|
Uri = "$($VaultAPIAddress)/secret/data/$($VaultSecret)"
|
|
|
|
Method = 'POST'
|
|
|
|
UseBasicParsing = $True
|
|
|
|
Headers = @{'X-Vault-Token'="$VaultToken"}
|
|
|
|
Body = @{
|
2021-03-10 10:32:53 +00:00
|
|
|
data = @{
|
|
|
|
"password.$($Username)" = $NewPassword
|
|
|
|
}
|
|
|
|
} | ConvertTo-Json
|
|
|
|
}
|
2021-03-12 09:34:45 +00:00
|
|
|
Invoke-WebRequest @InvokeWebRequestSplat | Out-Null
|
2021-03-10 09:02:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Return $NewPassword
|