Skip to content

Modifies error messages of AvoidUsernameAndPasswordParams and UsePSCredentialType rules. #456

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

Merged
merged 4 commits into from
Mar 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Rules/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions Rules/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,16 @@
<value>One Char</value>
</data>
<data name="UsePSCredentialTypeDescription" xml:space="preserve">
<value>Checks that cmdlets that have a Credential parameter accept PSCredential with CredentialAttribute where PSCredential comes before CredentialAttribute.. This comes from the PowerShell teams best practices.</value>
<value>For PowerShell 4.0 and earlier, a parameter named Credential with type PSCredential must have a credential transformation attribute defined after the PSCredential type attribute. </value>
</data>
<data name="UsePSCredentialTypeError" xml:space="preserve">
<value>The Credential parameter in '{0}' must be of the type PSCredential with CredentialAttribute where PSCredential comes before CredentialAttribute.</value>
<value>The Credential parameter in '{0}' must be of type PSCredential. For PowerShell 4.0 and earlier, please define a credential transformation attribute, e.g. [System.Management.Automation.Credential()], after the PSCredential type attribute.</value>
</data>
<data name="UsePSCredentialTypeErrorSB" xml:space="preserve">
<value>The Credential parameter in a found script block must be of the type PSCredential with CredentialAttribute where PSCredential comes before CredentialAttribute.</value>
<value>The Credential parameter found in the script block must be of type PSCredential. For PowerShell 4.0 and earlier please define a credential transformation attribute, e.g. [System.Management.Automation.Credential()], after the PSCredential type attribute. </value>
</data>
<data name="UsePSCredentialTypeCommonName" xml:space="preserve">
<value>PSCredential</value>
<value>Use PSCredential type.</value>
</data>
<data name="ReservedCmdletCharDescription" xml:space="preserve">
<value>Checks for reserved characters in cmdlet names. These characters usually cause a parsing error. Otherwise they will generally cause runtime errors.</value>
Expand Down Expand Up @@ -511,10 +511,10 @@
<value>Avoid Using Username and Password Parameters</value>
</data>
<data name="AvoidUsernameAndPasswordParamsDescription" xml:space="preserve">
<value>Functions should only take in a credential parameter of type PSCredential with CredentialAttribute where PSCredential comes before CredentialAttribute instead of username and password parameters.</value>
<value>Functions should take in a Credential parameter of type PSCredential (with a Credential transformation attribute defined after it in PowerShell 4.0 or earlier) or set the Password parameter to type SecureString.</value>
</data>
<data name="AvoidUsernameAndPasswordParamsError" xml:space="preserve">
<value>Function '{0}' has both username and password parameters. A credential parameter of type PSCredential with a CredentialAttribute where PSCredential comes before CredentialAttribute should be used.</value>
<value>Function '{0}' has both Username and Password parameters. Either set the type of the Password parameter to SecureString or replace the Username and Password parameters with a Credential parameter of type PSCredential. If using a Credential parameter in PowerShell 4.0 or earlier, please define a credential transformation attribute after the PSCredential type attribute.</value>
</data>
<data name="AvoidUsernameAndPasswordParamsName" xml:space="preserve">
<value>AvoidUsingUserNameAndPassWordParams</value>
Expand Down
13 changes: 11 additions & 2 deletions Rules/UsePSCredentialType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
{

/// <summary>
/// UsePSCredentialType: Analyzes the ast to check that cmdlets that have a Credential parameter accept PSCredential.
/// UsePSCredentialType: Checks if a parameter named Credential is of type PSCredential. Also checks if there is a credential transformation attribute defined after the PSCredential type attribute. The order between credential transformation attribute and PSCredential type attribute is applicable only to Poweshell 4.0 and earlier.
/// </summary>
[Export(typeof(IScriptRule))]
public class UsePSCredentialType : IScriptRule
{
/// <summary>
/// AnalyzeScript: Analyzes the ast to check that cmdlets that have a Credential parameter accept PSCredential.
/// AnalyzeScript: Analyzes the ast to check if a parameter named Credential is of type PSCredential. Also checks if there is a credential transformation attribute defined after the PSCredential type attribute. The order between the credential transformation attribute and PSCredential type attribute is applicable only to Poweshell 4.0 and earlier.
/// </summary>
/// <param name="ast">The script's ast</param>
/// <param name="fileName">The script's file name</param>
Expand All @@ -39,6 +39,15 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
{
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);

var sbAst = ast as ScriptBlockAst;
if (sbAst != null
&& sbAst.ScriptRequirements != null
&& sbAst.ScriptRequirements.RequiredPSVersion != null
&& sbAst.ScriptRequirements.RequiredPSVersion.Major == 5)
{
yield break;
}

IEnumerable<Ast> funcDefAsts = ast.FindAll(testAst => testAst is FunctionDefinitionAst, true);
IEnumerable<Ast> scriptBlockAsts = ast.FindAll(testAst => testAst is ScriptBlockAst, true);

Expand Down
4 changes: 2 additions & 2 deletions Tests/Rules/AvoidUserNameAndPasswordParams.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Import-Module PSScriptAnalyzer

$violationMessage = "Function 'TestFunction' has both username and password parameters. A credential parameter of type PSCredential with a CredentialAttribute where PSCredential comes before CredentialAttribute should be used."
$violationMessage = "Function 'TestFunction' has both Username and Password parameters. Either set the type of the Password parameter to SecureString or replace the Username and Password parameters with a Credential parameter of type PSCredential. If using a Credential parameter in PowerShell 4.0 or earlier, please define a credential transformation attribute after the PSCredential type attribute."
$violationName = "PSAvoidUsingUserNameAndPasswordParams"
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
$violations = Invoke-ScriptAnalyzer $directory\AvoidUserNameAndPasswordParams.ps1 | Where-Object {$_.RuleName -eq $violationName}
Expand All @@ -13,7 +13,7 @@ Describe "AvoidUserNameAndPasswordParams" {
}

It "has the correct violation message" {
$violations[0].Message | Should Match $violationMessage
$violations[0].Message | Should Be $violationMessage
}
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/Rules/PSCredentialType.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function Credential2
[Parameter(Mandatory=$true,
ValueFromPipelineByPropertyName=$true,
Position=0)]
[System.Management.Automation.CredentialAttribute()]
[System.Management.Automation.Credential()]
[pscredential]
$Credential
)
Expand Down
4 changes: 2 additions & 2 deletions Tests/Rules/PSCredentialType.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Import-Module PSScriptAnalyzer
$violationMessage = "The Credential parameter in 'Credential' must be of the type PSCredential with CredentialAttribute where PSCredential comes before CredentialAttribute."
$violationMessage = "The Credential parameter in 'Credential' must be of type PSCredential. For PowerShell 4.0 and earlier, please define a credential transformation attribute, e.g. [System.Management.Automation.Credential()], after the PSCredential type attribute."
$violationName = "PSUsePSCredentialType"
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
$violations = Invoke-ScriptAnalyzer $directory\PSCredentialType.ps1 | Where-Object {$_.RuleName -eq $violationName}
Expand All @@ -12,7 +12,7 @@ Describe "PSCredentialType" {
}

It "has the correct description message" {
$violations[0].Message | Should Match $violationMessage
$violations[0].Message | Should Be $violationMessage
}
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/Rules/PSCredentialTypeNoViolations.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
ValueFromPipelineByPropertyName=$true,
Position=0)]
[pscredential]
[System.Management.Automation.CredentialAttribute()]
[System.Management.Automation.Credential()]
$Credential
)
}