Skip to content

Commit

Permalink
Merge pull request #220 from PlagueHO/Issue-211
Browse files Browse the repository at this point in the history
Improved validation on Name and ResourceGroupName parameters on *-CosmosDBAccount* functions - Fixes #Issue-211
  • Loading branch information
PlagueHO committed Nov 3, 2018
2 parents 82199fe + 2b85424 commit 08b89b5
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,8 @@
- Temporarily suppressed running tests using MacOS in Azure Pipelines
because the Hosted Agent has been updated with Az, preventing the
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).

## 2.1.12.137

Expand Down
2 changes: 2 additions & 0 deletions src/en-US/CosmosDB.strings.psd1
Expand Up @@ -34,4 +34,6 @@ ConvertFrom-StringData -StringData @'
ShouldUpdateAzureCosmosDBAccount = Update an Azure Cosmos DB account '{0}' in resource group '{1}'
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.
'@
25 changes: 25 additions & 0 deletions src/lib/accounts/Assert-CosmosDbAccountNameValid.ps1
@@ -0,0 +1,25 @@
<#
.SYNOPSIS
Helper function that asserts a Cosmos DB Account name is valid.
#>
function Assert-CosmosDbAccountNameValid
{

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

$matches = [regex]::Match($Name,"[A-Za-z0-9\-]{3,31}")
if ($matches.value -ne $Name)
{
Throw $($LocalizedData.AccountNameInvalid -f $Name)
}

return $true
}
25 changes: 25 additions & 0 deletions src/lib/accounts/Assert-CosmosDbResourceGroupNameValid.ps1
@@ -0,0 +1,25 @@
<#
.SYNOPSIS
Helper function that asserts a Azure Resource Group name is valid.
#>
function Assert-CosmosDbResourceGroupNameValid
{

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

$matches = [regex]::Match($ResourceGroupName,"[A-Za-z0-9_\-\.]{1,90}(?<!\.)")
if ($matches.value -ne $ResourceGroupName)
{
Throw $($LocalizedData.ResourceGroupNameInvalid -f $ResourceGroupName)
}

return $true
}
4 changes: 2 additions & 2 deletions src/lib/accounts/Get-CosmosDbAccount.ps1
Expand Up @@ -6,12 +6,12 @@ function Get-CosmosDbAccount
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbAccountNameValid -Name $_ })]
[System.String]
$Name,

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbResourceGroupNameValid -ResourceGroupName $_ })]
[System.String]
$ResourceGroupName
)
Expand Down
4 changes: 2 additions & 2 deletions src/lib/accounts/Get-CosmosDbAccountConnectionString.ps1
Expand Up @@ -6,12 +6,12 @@ function Get-CosmosDbAccountConnectionString
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbAccountNameValid -Name $_ })]
[System.String]
$Name,

[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbResourceGroupNameValid -ResourceGroupName $_ })]
[System.String]
$ResourceGroupName
)
Expand Down
4 changes: 2 additions & 2 deletions src/lib/accounts/Get-CosmosDbAccountMasterKey.ps1
Expand Up @@ -7,12 +7,12 @@ function Get-CosmosDbAccountMasterKey
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbAccountNameValid -Name $_ })]
[System.String]
$Name,

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

Expand Down
4 changes: 2 additions & 2 deletions src/lib/accounts/New-CosmosDbAccount.ps1
Expand Up @@ -8,12 +8,12 @@ function New-CosmosDbAccount
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbAccountNameValid -Name $_ })]
[System.String]
$Name,

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

Expand Down
4 changes: 2 additions & 2 deletions src/lib/accounts/New-CosmosDbAccountMasterKey.ps1
Expand Up @@ -7,12 +7,12 @@ function New-CosmosDbAccountMasterKey
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbAccountNameValid -Name $_ })]
[System.String]
$Name,

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

Expand Down
4 changes: 2 additions & 2 deletions src/lib/accounts/Remove-CosmosDbAccount.ps1
Expand Up @@ -8,12 +8,12 @@ function Remove-CosmosDbAccount
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbAccountNameValid -Name $_ })]
[System.String]
$Name,

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

Expand Down
4 changes: 2 additions & 2 deletions src/lib/accounts/Set-CosmosDbAccount.ps1
Expand Up @@ -8,12 +8,12 @@ function Set-CosmosDbAccount
param
(
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ Assert-CosmosDbAccountNameValid -Name $_ })]
[System.String]
$Name,

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

Expand Down
80 changes: 80 additions & 0 deletions test/Unit/CosmosDB.accounts.Tests.ps1
Expand Up @@ -79,6 +79,86 @@ InModuleScope CosmosDB {
$script:testKey = 'GFJqJeri2Rq910E0G7PsWoZkzowzbj23Sm9DUWFC0l0P8o16mYyuaZKN00Nbtj9F1QQnumzZKSGZwknXGERrlA=='
$script:testKeySecureString = ConvertTo-SecureString -String $script:testKey -AsPlainText -Force

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

Context 'When called with a valid name' {
It 'Should return $true' {
Assert-CosmosDbAccountNameValid -Name 'validaccountname' | Should -Be $true
}
}

Context 'When called with a 2 character name' {
It 'Should throw expected exception' {
{
Assert-CosmosDbAccountNameValid -Name ('a' * 2)
} | Should -Throw ($LocalizedData.AccountNameInvalid -f ('a' * 2))
}
}

Context 'When called with a 32 character name' {
It 'Should throw expected exception' {
{
Assert-CosmosDbAccountNameValid -Name ('a' * 32)
} | Should -Throw ($LocalizedData.AccountNameInvalid -f ('a' * 32))
}
}

Context 'When called containing an underscore' {
It 'Should throw expected exception' {
{
Assert-CosmosDbAccountNameValid -Name ('a_b')
} | Should -Throw ($LocalizedData.AccountNameInvalid -f ('a_b'))
}
}

Context 'When called containing a period' {
It 'Should throw expected exception' {
{
Assert-CosmosDbAccountNameValid -Name ('a.b')
} | Should -Throw ($LocalizedData.AccountNameInvalid -f ('a.b'))
}
}
}

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

Context 'When called with a valid resource group name' {
It 'Should return $true' {
Assert-CosmosDbResourceGroupNameValid -ResourceGroupName 'valid_resource-group.name123' | Should -Be $true
}
}

Context 'When called with a 91 character resource group name' {
It 'Should throw expected exception' {
{
Assert-CosmosDbResourceGroupNameValid -ResourceGroupName ('a' * 91)
} | Should -Throw ($LocalizedData.ResourceGroupNameInvalid -f ('a' * 91))
}
}

Context 'When called with resource group name containing an exclamation' {
It 'Should throw expected exception' {
{
Assert-CosmosDbResourceGroupNameValid -ResourceGroupName ('a!')
} | Should -Throw ($LocalizedData.ResourceGroupNameInvalid -f ('a!'))
}
}

Context 'When called with resource group name ending in a period' {
It 'Should throw expected exception' {
{
Assert-CosmosDbResourceGroupNameValid -ResourceGroupName ('a.')
} | Should -Throw ($LocalizedData.ResourceGroupNameInvalid -f ('a.'))
}
}
}

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

0 comments on commit 08b89b5

Please sign in to comment.