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

Improved validation on User Defined Function Id parameter on *-CosmosDBUser* functions #229

Merged
merged 1 commit into from
Nov 4, 2018
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
- Improved validation on Account parameter on `*-CosmosDBUserDefinedFunction*` functions.
- Improved validation on Database parameter on `*-CosmosDBUserDefinedFunction*` functions.
- Improved validation on Collection parameter on `*-CosmosDBUserDefinedFunction*` functions.
- Improved validation on User Defined Function Id parameter on
`*-CosmosDBUser*` functions - fixes [Issue #217](https://github.com/PlagueHO/CosmosDB/issues/217).
- Improved validation on Account parameter on `*-CosmosDBUser*` functions.
- Improved validation on Database parameter on `*-CosmosDBUser*` functions.

## 2.1.12.137

Expand Down
1 change: 1 addition & 0 deletions src/en-US/CosmosDB.strings.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ ConvertFrom-StringData -StringData @'
StoredProcedureIdInvalid = The Stored Procedure Id '{0}' is invalid. A Stored Procedure Id must not contain characters '\','/','#' or '?', end with a space or be longer than 255 characters.
TriggerIdInvalid = The Trigger Id '{0}' is invalid. A Trigger Id must not contain characters '\','/','#' or '?', end with a space or be longer than 255 characters.
UserDefinedFunctionIdInvalid = The User Defined Function Id '{0}' is invalid. A User Defined Function Id must not contain characters '\','/','#' or '?', end with a space or be longer than 255 characters.
UserIdInvalid = The User Id '{0}' is invalid. A User Id must not contain characters '\','/','#' or '?', end with a space or be longer than 255 characters.
'@
25 changes: 25 additions & 0 deletions src/lib/users/Assert-CosmosDbUserIdValid.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<#
.SYNOPSIS
Helper function that asserts a Cosmos DB User Id is valid.
#>
function Assert-CosmosDbUserIdValid
{

[CmdletBinding()]
[OutputType([System.Boolean])]
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[System.String]
$Id
)

$matches = [regex]::Match($Id,"[^\\/#?]{1,255}(?<!\s)")
if ($matches.value -ne $Id)
{
Throw $($LocalizedData.UserIdInvalid -f $Id)
}

return $true
}
6 changes: 3 additions & 3 deletions src/lib/users/Get-CosmosDbUser.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ function Get-CosmosDbUser
$Context,

[Parameter(Mandatory = $true, ParameterSetName = 'Account')]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbAccountNameValid -Name $_ })]
[System.String]
$Account,

[Parameter()]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbDatabaseIdValid -Id $_ })]
[System.String]
$Database,

Expand All @@ -32,7 +32,7 @@ function Get-CosmosDbUser
$KeyType = 'master',

[Parameter()]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbUserIdValid -Id $_ })]
[System.String]
$Id
)
Expand Down
2 changes: 2 additions & 0 deletions src/lib/users/Get-CosmosDbUserResourcePath.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ function Get-CosmosDbUserResourcePath
param
(
[Parameter(Mandatory = $true)]
[ValidateScript({ Assert-CosmosDbDatabaseIdValid -Id $_ })]
[System.String]
$Database,

[Parameter(Mandatory = $true)]
[ValidateScript({ Assert-CosmosDbUserIdValid -Id $_ })]
[System.String]
$Id
)
Expand Down
6 changes: 3 additions & 3 deletions src/lib/users/New-CosmosDbUser.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ function New-CosmosDbUser
$Context,

[Parameter(Mandatory = $true, ParameterSetName = 'Account')]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbAccountNameValid -Name $_ })]
[System.String]
$Account,

[Parameter()]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbDatabaseIdValid -Id $_ })]
[System.String]
$Database,

Expand All @@ -32,7 +32,7 @@ function New-CosmosDbUser
$KeyType = 'master',

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbUserIdValid -Id $_ })]
[System.String]
$Id
)
Expand Down
6 changes: 3 additions & 3 deletions src/lib/users/Remove-CosmosDbUser.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ function Remove-CosmosDbUser
$Context,

[Parameter(Mandatory = $true, ParameterSetName = 'Account')]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbAccountNameValid -Name $_ })]
[System.String]
$Account,

[Parameter()]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbDatabaseIdValid -Id $_ })]
[System.String]
$Database,

Expand All @@ -31,7 +31,7 @@ function Remove-CosmosDbUser
$KeyType = 'master',

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbUserIdValid -Id $_ })]
[System.String]
$Id
)
Expand Down
8 changes: 4 additions & 4 deletions src/lib/users/Set-CosmosDbUser.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ function Set-CosmosDbUser
$Context,

[Parameter(Mandatory = $true, ParameterSetName = 'Account')]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbAccountNameValid -Name $_ })]
[System.String]
$Account,

[Parameter()]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbDatabaseIdValid -Id $_ })]
[System.String]
$Database,

Expand All @@ -32,12 +32,12 @@ function Set-CosmosDbUser
$KeyType = 'master',

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbUserIdValid -Id $_ })]
[System.String]
$Id,

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbUserIdValid -Id $_ })]
[System.String]
$NewId
)
Expand Down
44 changes: 44 additions & 0 deletions test/Unit/CosmosDB.users.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,50 @@ InModuleScope CosmosDB {
Content = $script:testJsonSingle
}

Describe 'Assert-CosmosDbUserIdValid' -Tag 'Unit' {
It 'Should exist' {
{ Get-Command -Name Assert-CosmosDbUserIdValid -ErrorAction Stop } | Should -Not -Throw
}

Context 'When called with a valid Id' {
It 'Should return $true' {
Assert-CosmosDbUserIdValid -Id 'This is a valid user ID..._-99!' | Should -Be $true
}
}

Context 'When called with a 256 character Id' {
It 'Should throw expected exception' {
{
Assert-CosmosDbUserIdValid -Id ('a' * 256)
} | Should -Throw ($LocalizedData.UserIdInvalid -f ('a' * 256))
}
}

Context 'When called with an Id containing invalid characters' {
$testCases = @{ Id = 'a\b' }, @{ Id = 'a/b' }, @{ Id = 'a#b' }, @{ Id = 'a?b' }

It 'Should throw expected exception when called with "<Id>"' -TestCases $testCases {
param
(
[System.String]
$Id
)

{
Assert-CosmosDbUserIdValid -Id $Id
} | Should -Throw ($LocalizedData.UserIdInvalid -f $Id)
}
}

Context 'When called with an Id ending with a space' {
It 'Should throw expected exception' {
{
Assert-CosmosDbUserIdValid -Id ('a ')
} | Should -Throw ($LocalizedData.UserIdInvalid -f ('a '))
}
}
}

Describe 'Get-CosmosDbUserResourcePath' -Tag 'Unit' {
It 'Should exist' {
{ Get-Command -Name Get-CosmosDbUserResourcePath -ErrorAction Stop } | Should -Not -Throw
Expand Down