Skip to content

Commit

Permalink
Merge pull request #222 from PlagueHO/Issue-212
Browse files Browse the repository at this point in the history
Improve Database Id Parameter Validation on *-CosmosDbDatabase functions - Fixes #212
  • Loading branch information
PlagueHO committed Nov 3, 2018
2 parents 08b89b5 + b291389 commit ab201df
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,11 @@
AzureRM.NetCore modules from being installed.
- Improved validation on Name and ResourceGroupName parameters on
`*-CosmosDBAccount*` functions - fixes [Issue #211](https://github.com/PlagueHO/CosmosDB/issues/211).
- Improved validation on Account parameter on `*-CosmosDBDatabase*` functions.
- Improved validation on Account and ResourceGroupName parameter on
`New-CosmosDbContext` function.
- Improved validation on Database Id parameter on
`*-CosmosDBDatabase*` functions - fixes [Issue #212](https://github.com/PlagueHO/CosmosDB/issues/212).

## 2.1.12.137

Expand Down
3 changes: 2 additions & 1 deletion src/en-US/CosmosDB.strings.psd1
Expand Up @@ -35,5 +35,6 @@ ConvertFrom-StringData -StringData @'
ShouldRemoveAzureCosmosDBAccount = Remove the Azure Cosmos DB account '{0}' in resource group '{1}'
GettingAzureCosmosDBAccountConnectionStringWarning = The Get-CosmosDbAccountConnectionString function does not currently work due to an issue with the Microsoft/DocumentDB provider. The connection strings will not be returned.
AccountNameInvalid = The Account Name '{0}' is invalid. The name can contain only lowercase letters, numbers and the '-' character, and must be between 3 and 31 characters.
ResourceGroupNameInvalid = The Resource Group Name '{0}' is invalid. AccountNameInvalid = The Account Name '{0}' is invalid. The name can contain only lowercase letters, numbers and the '-' character, and must be between 3 and 31 characters.
ResourceGroupNameInvalid = The Resource Group Name '{0}' is invalid. Resource group names only allow alphanumeric characters, periods, underscores, hyphens and parenthesis, cannot end in a period and must be 90 characters or less.
DatabaseIdInvalid = The Database Id '{0}' is invalid. A Database Id must not contain characters '\','/','#','?' or '=', end with a space and must be 255 characters or less.
'@
25 changes: 25 additions & 0 deletions src/lib/databases/Assert-CosmosDbDatabaseIdValid.ps1
@@ -0,0 +1,25 @@
<#
.SYNOPSIS
Helper function that asserts a Cosmos DB Database Id is valid.
#>
function Assert-CosmosDbDatabaseIdValid
{

[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.DatabaseIdInvalid -f $Id)
}

return $true
}
4 changes: 2 additions & 2 deletions src/lib/databases/Get-CosmosDbDatabase.ps1
Expand Up @@ -12,7 +12,7 @@ function Get-CosmosDbDatabase
$Context,

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

Expand All @@ -27,7 +27,7 @@ function Get-CosmosDbDatabase
$KeyType = 'master',

[Parameter()]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbDatabaseIdValid -Id $_ })]
[System.String]
$Id
)
Expand Down
2 changes: 1 addition & 1 deletion src/lib/databases/Get-CosmosDbDatabaseResourcePath.ps1
Expand Up @@ -6,7 +6,7 @@ function Get-CosmosDbDatabaseResourcePath
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbDatabaseIdValid -Id $_ })]
[System.String]
$Id
)
Expand Down
4 changes: 2 additions & 2 deletions src/lib/databases/New-CosmosDbDatabase.ps1
Expand Up @@ -12,7 +12,7 @@ function New-CosmosDbDatabase
$Context,

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

Expand All @@ -27,7 +27,7 @@ function New-CosmosDbDatabase
$KeyType = 'master',

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbDatabaseIdValid -Id $_ })]
[System.String]
$Id
)
Expand Down
4 changes: 2 additions & 2 deletions src/lib/databases/Remove-CosmosDbDatabase.ps1
Expand Up @@ -11,7 +11,7 @@ function Remove-CosmosDbDatabase
$Context,

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

Expand All @@ -26,7 +26,7 @@ function Remove-CosmosDbDatabase
$KeyType = 'master',

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbDatabaseIdValid -Id $_ })]
[System.String]
$Id
)
Expand Down
6 changes: 3 additions & 3 deletions src/lib/utils/New-CosmosDbContext.ps1
Expand Up @@ -9,12 +9,12 @@ function New-CosmosDbContext
[Parameter(Mandatory = $true, ParameterSetName = 'Account')]
[Parameter(Mandatory = $true, ParameterSetName = 'Token')]
[Parameter(Mandatory = $true, ParameterSetName = 'AzureAccount')]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbAccountNameValid -Name $_ })]
[System.String]
$Account,

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

Expand All @@ -31,7 +31,7 @@ function New-CosmosDbContext

[Alias("ResourceGroup")]
[Parameter(Mandatory = $true, ParameterSetName = 'AzureAccount')]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbResourceGroupNameValid -ResourceGroupName $_ })]
[System.String]
$ResourceGroupName,

Expand Down
44 changes: 44 additions & 0 deletions test/Unit/CosmosDB.databases.Tests.ps1
Expand Up @@ -70,6 +70,50 @@ InModuleScope CosmosDB {
Content = $script:testJsonSingle
}

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

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

Context 'When called with a 256 character Id' {
It 'Should throw expected exception' {
{
Assert-CosmosDbDatabaseIdValid -Id ('a' * 256)
} | Should -Throw ($LocalizedData.DatabaseIdInvalid -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-CosmosDbDatabaseIdValid -Id $Id
} | Should -Throw ($LocalizedData.DatabaseIdInvalid -f $Id)
}
}

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

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

0 comments on commit ab201df

Please sign in to comment.