Skip to content

Modifications to UsePSCredentialType and AvoidUsernameAndPasswordParams rules. #455

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

Closed
wants to merge 2 commits into from

Conversation

kapilmb
Copy link

@kapilmb kapilmb commented Mar 3, 2016

No description provided.

@KirkMunro
Copy link
Contributor

I'm opposed to this change. Why are you removing the ordering enforcement. It is mandatory if you want a Credential parameter to work properly. The only way you can have a -Credential parameter that accepts both string and PSCredential types as input is if you define it as follows:

[Parameter()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Credential = [System.Management.Automation.PSCredential]::Empty

I added the default value to highlight that best practice as well; however, PSCredential must be defined above/before the Credential converter attribute so that the value passed in can be converted (from string) before PowerShell checks to make sure it is of type PSCredential.

@kilasuit
Copy link
Contributor

kilasuit commented Mar 3, 2016

I Agree with @KirkMunro as this would be essentially reverting work done in #391

@kapilmb
Copy link
Author

kapilmb commented Mar 3, 2016

@KirkMunro In #391, you mention that a function will return error if CredentialAttribute precedes PSCredential, but I am not able to reproduce the error. Consider the following.

Function Get-Credential1
{
    Param(
        [Parameter(Mandatory=$true,
           ValueFromPipelineByPropertyName=$true,
           Position=0)]
        [System.Management.Automation.CredentialAttribute()]
        [PSCredential] 
        $Credential
)   

Write-Output $Credential.GetType().FullName
Write-Output $Credential.UserName
Write-Output $Credential.Password
}

Function Get-Credential2
{
    Param(
        [Parameter(Mandatory=$true,
           ValueFromPipelineByPropertyName=$true,
           Position=0)]
        [PSCredential]      
        [System.Management.Automation.CredentialAttribute()]
        $Credential
)   

Write-Output $Credential.GetType().FullName
Write-Output $Credential.UserName
Write-Output $Credential.Password
}

Get-Credential1 -Credential "someuser" and Get-Credential2 -Credential "someuser" behave identically - a credential request window pops up which returns the credential type.

Since the order doesn't seem to make any difference why should we enforce it?

@KirkMunro
Copy link
Contributor

Have you tried that in downlevel versions of Windows running PowerShell 2, 3 and/or 4? In those versions of PowerShell, order matters. In PowerShell 5+, conversion attributes are processed before type attributes, so order doesn't matter. Since downlevel versions are still around and in use, it's better for the community if order is enforced on this rule, regardless of the version of PowerShell that is being used. Once PowerShell version 5 becomes the minimum supported version for PSScriptAnalyser, then the order enforcement can be removed without issue.

@kapilmb
Copy link
Author

kapilmb commented Mar 4, 2016

@KirkMunro You are right. On versions 3 and 4, Get-Credential1 -Credential "someuser" indeed gives an error.

As the rule is applicable to v3 and v4 but not to v5, I agree that we shouldn't remove the ordering enforcement. However, given that the rule is not applicable to v5 it will only cause noise within v5 context. My suggestion then would be to keep the ordering but change the error message stating that the warning is applicable only to v3 and v4.

@KirkMunro
Copy link
Contributor

Changing the error message is fine with me. I just didn't want the ordering enforcement work to be undone.

@kapilmb
Copy link
Author

kapilmb commented Mar 4, 2016

Closing the pull request as the issue we were addressing was not completely examined.

@kapilmb kapilmb closed this Mar 4, 2016
@kapilmb kapilmb deleted the FixCredentialUsage branch March 4, 2016 18:01
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.

4 participants