Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(scripts): correct and prettify copy kv secrets script #15

Merged

Conversation

helenakallekleiv
Copy link
Contributor

@helenakallekleiv helenakallekleiv commented Jul 22, 2024

Original issue was that when running the script we got the following warning:

WARNING: Set-AzSubscription is not found. The most similar Azure PowerShell commands are:
        Get-AzSubscription
        Select-AzSubscription

This refers to the custom fuction Set-AzSubscription on line 97. I believe the issue was that the custom function name was too similar to an actual built-in Azure PowerShell cmdlet, which does not exist. To fix this I just changed the custom function name from Set-AzSubscription to Set-SubscriptionContext, and updated the references.

Simultaneously I did some formatting, added some comments and updated the parameter names.

Testing

  • Copying secrets from one key vault to another in the same subscription works as it should.
  • Have not yet tested copying secrets across different subscriptions, but will test this asap.
  • Have not tested the script with use of runbook.
  • Disabling/enabling firewall works as expected. Output below:
    WARNING: The network rule set has been turned off for this vault.
    WARNING: The network rule set has been turned off for this vault.
    Secret Name 'secret-1' with id 'https://kv-test-hekal.vault.azure.net:443/secrets/secret-1/84a4a19f9d4c47ebaa641ec104ed9560' already exists in destination vault
    Secret Name 'secret-2' with id 'https://kv-test-hekal.vault.azure.net:443/secrets/secret-2/612fa637e8d44b59adce25d798924442' already exists in destination vault
    Secret Name 'secret-3' with id 'https://kv-test-hekal.vault.azure.net:443/secrets/secret-3/7c229dcbd8ff4567b90eb5052d29ffd5' already exists in destination vault
    Reverting firewall settings for Source Vault to 'Deny'.
    Reverting firewall settings for Target Vault to 'Deny'.
    

@helenakallekleiv helenakallekleiv requested a review from a team as a code owner July 22, 2024 15:19
@helenakallekleiv
Copy link
Contributor Author

helenakallekleiv commented Jul 22, 2024

The linter throws the following error messages:

 RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSAvoidUsingConvertToSecureStringWi Error        Copy-AzKey 190   File 'Copy-Az
thPlainText                                      VaultSecre       KeyVaultSecre
                                                 t.ps1            t.ps1' uses C
                                                                  onvertTo-Secu
                                                                  reString with
                                                                   plaintext. T
                                                                  his will expo
                                                                  se secure inf
                                                                  ormation. Enc
                                                                  rypted standa
                                                                  rd strings sh
                                                                  ould be used
                                                                  instead.
PSUseShouldProcessForStateChangingF Warning      Copy-AzKey 96    Function 'Set
unctions                                         VaultSecre       -Subscription
                                                 t.ps1            Context' has
                                                                  verb that cou
                                                                  ld change sys
                                                                  tem state. Th
                                                                  erefore, the
                                                                  function has
                                                                  to support 'S
                                                                  houldProcess'
                                                                  .

Messages formatted for readability:

# RuleName: PSAvoidUsingConvertToSecureStringWithPlainText

File 'Copy-AzKeyVaultSecret.ps1' uses C onvertTo-SecureString with plaintext. 
This will expose secure information. Encrypted standard strings should be used instead.
#RuleName: PSUseShouldProcessForStateChangingFunctions

Function 'Set-SubscriptionContext' hasverb that could change system state.
Therefore, thefunction hasto support 'ShouldProcess'.

Copy link
Member

@hknutsen hknutsen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think structurally this looks good 👍

Even though the linter throws an error on something that was implemented before your changes, I'd look into if it's something we could implement to stop it from complaining, or worst case ignore it (and add a comment clarifying why we ignore it).

@helenakallekleiv helenakallekleiv requested a review from a team July 23, 2024 07:59
@helenakallekleiv
Copy link
Contributor Author

helenakallekleiv commented Jul 23, 2024

The linter simply outputs a warning related to the verb used in this custom function:

#RuleName: PSUseShouldProcessForStateChangingFunctions

Function 'Set-SubscriptionContext' has verb that could change system state.
Therefore, the function has to support 'ShouldProcess'.

To mitigate I will try to use a verb that supports ShouldProcess, AKA "Trial and Error".

@helenakallekleiv
Copy link
Contributor Author

helenakallekleiv commented Jul 23, 2024

Used Marshal Class to mitigate the following linter error:

# RuleName: PSAvoidUsingConvertToSecureStringWithPlainText

File 'Copy-AzKeyVaultSecret.ps1' uses ConvertTo-SecureString with plaintext. 
This will expose secure information. Encrypted standard strings should be used instead.

Not entirely familiar with the extended use of this but the linter seems happy.

Code block with the changes in question:

# Convert SecureString to PlainText (Temporarily for comparison purposes)
function Convert-SecureStringToPlainText {
param (
[Parameter(Mandatory = $true)]
[SecureString]$SecureString
)
return [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecureString))
}

References

@helenakallekleiv
Copy link
Contributor Author

The linter simply outputs a warning related to the verb used in this custom function:

#RuleName: PSUseShouldProcessForStateChangingFunctions

Function 'Set-SubscriptionContext' has verb that could change system state.
Therefore, the function has to support 'ShouldProcess'.

To mitigate I will try to use a verb that supports ShouldProcess, AKA "Trial and Error".

Changing custom function name from Set-SubscriptionContext to Select-SubscriptionContext mitigated the linter error.

@helenakallekleiv
Copy link
Contributor Author

Used Marshal Class to mitigate the following linter error:

# RuleName: PSAvoidUsingConvertToSecureStringWithPlainText

File 'Copy-AzKeyVaultSecret.ps1' uses ConvertTo-SecureString with plaintext. 
This will expose secure information. Encrypted standard strings should be used instead.

Not entirely familiar with the extended use of this but the linter seems happy.

Code block with the changes in question:

# Convert SecureString to PlainText (Temporarily for comparison purposes)
function Convert-SecureStringToPlainText {
param (
[Parameter(Mandatory = $true)]
[SecureString]$SecureString
)
return [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecureString))
}

References

* [PtrToStringAuto](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.marshal.ptrtostringauto?view=net-8.0)

* [SecureStringToBSTR](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.marshal.securestringtobstr?view=net-8.0)

Reverted these changes.

After testing a million different scenarios, we concluded that for now the workaround for handling key vault secrets as Encrypted Standard Strings (ESS) instead of secure strings converted to plaintext did not work. This is because even though the secret name and value are identical in both source and target key vaults, the ESS value changes after every time the command is being run, and the ESS value for target and source KV secret is not identical.

In conclusion, the linter can keep complaining, as we have not yet found a workaround.

@helenakallekleiv
Copy link
Contributor Author

Everything, except runbook, have been tested so this PR is ready for review.

Copy link

@vildeaar vildeaar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice :D

@helenakallekleiv helenakallekleiv merged commit ea98b98 into main Jul 25, 2024
2 of 4 checks passed
@helenakallekleiv helenakallekleiv deleted the fix-correct-and-prettify-copy-kv-secrets-script branch July 25, 2024 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants