|
| 1 | +# ---------------------------------------------------------------------------------- |
| 2 | +# |
| 3 | +# Copyright Microsoft Corporation |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | +# ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | + |
| 16 | +############################################## |
| 17 | +## Manage IotDps Enrollment Group Cmdlets ## |
| 18 | +############################################## |
| 19 | + |
| 20 | +<# |
| 21 | +.SYNOPSIS |
| 22 | +Test Iot Hub Device Provisioning Service Enrollment Group cmdlets for CRUD operations |
| 23 | +#> |
| 24 | + |
| 25 | +function Test-AzIotDpsEnrollmentGroupLifeCycle |
| 26 | +{ |
| 27 | + $Location = Get-Location "Microsoft.Devices" "Device Provisioning Service" |
| 28 | + $IotDpsName = getAssetName |
| 29 | + $ResourceGroupName = getAssetName |
| 30 | + $IotHubName = getAssetName |
| 31 | + $hubKeyName = "ServiceKey" |
| 32 | + $CertificateKey = "MIIBiDCCAS2gAwIBAgIFWks8LR4wCgYIKoZIzj0EAwIwNjEUMBIGA1UEAwwLcmlvdGNvcmVuZXcxETAPBgNVBAoMCE1TUl9URVNUMQswCQYDVQQGEwJVUzAgFw0xNzAxMDEwMDAwMDBaGA8zNzAxMDEzMTIzNTk1OVowNjEUMBIGA1UEAwwLcmlvdGNvcmVuZXcxETAPBgNVBAoMCE1TUl9URVNUMQswCQYDVQQGEwJVUzBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABLVS6bK+QMm+HZ0247Nm+JmnERuickBXTj6rydcP3WzVQNBNpvcQ/4YVrPp60oiYRxZbsPyBtHt2UCAC00vEXy+jJjAkMA4GA1UdDwEB/wQEAwIHgDASBgNVHRMBAf8ECDAGAQH/AgECMAoGCCqGSM49BAMCA0kAMEYCIQDEjs2PoZEi/yAQNj2Vji9RthQ33HG/QdL12b1ABU5UXgIhAPJujG/c/S+7vcREWI7bQcCb31JIBDhWZbt4eyCvXZtZ" |
| 33 | + $Sku = "S1" |
| 34 | + $symEnroll = getAssetName |
| 35 | + $x509Enroll = getAssetName |
| 36 | + |
| 37 | + # Constant variable |
| 38 | + $LinkedHubName = [string]::Format("{0}.azure-devices.net",$IotHubName) |
| 39 | + $AllocationWeight = 10 |
| 40 | + |
| 41 | + # Create or Update Resource Group |
| 42 | + $resourceGroup = New-AzResourceGroup -Name $ResourceGroupName -Location $Location |
| 43 | + |
| 44 | + # Create Iot Hub Device Provisioning Service |
| 45 | + $iotDps = New-AzIoTDps -ResourceGroupName $ResourceGroupName -Name $IotDpsName -Location $Location |
| 46 | + Assert-True { $iotDps.Name -eq $IotDpsName } |
| 47 | + Assert-True { $iotDps.Properties.IotHubs.Count -eq 0 } |
| 48 | + |
| 49 | + # Create an Iot Hub |
| 50 | + $iotHub = New-AzIoTHub -Name $IotHubName -ResourceGroupName $ResourceGroupName -Location $Location -SkuName $Sku -Units 1 |
| 51 | + Assert-True { $iotHub.Name -eq $IotHubName } |
| 52 | + |
| 53 | + # Add a key to IoT Hub |
| 54 | + $hubKeys = Add-AzIoTHubKey -Name $IotHubName -ResourceGroupName $ResourceGroupName -KeyName $hubKeyName -Rights ServiceConnect |
| 55 | + Assert-True { $hubKeys.Count -gt 1 } |
| 56 | + |
| 57 | + # Get key information from IoT Hub |
| 58 | + $hubKey = Get-AzIoTHubKey -Name $IotHubName -ResourceGroupName $ResourceGroupName -KeyName $hubKeyName |
| 59 | + |
| 60 | + $HubConnectionString = [string]::Format("HostName={0};SharedAccessKeyName={1};SharedAccessKey={2}",$iotHub.Properties.HostName,$hubKey.KeyName,$hubKey.PrimaryKey) |
| 61 | + |
| 62 | + # Link an Iot Hub to an Iot Hub Device Provisioning Service |
| 63 | + $linkedHub = Add-AzIoTDpsHub -ResourceGroupName $ResourceGroupName -Name $IotDpsName -IotHubConnectionString $HubConnectionString -IotHubLocation $iotHub.Location |
| 64 | + Assert-True { $linkedHub.Count -eq 1 } |
| 65 | + Assert-True { $linkedHub.LinkedHubName -eq $iotHub.Properties.HostName } |
| 66 | + Assert-True { $linkedHub.Location -eq $iotHub.Location } |
| 67 | + |
| 68 | + # Expecting error while creating enrollment group with invalid attestation mechanism |
| 69 | + $errorMessage = "Please provide valid attestation mechanism" |
| 70 | + Assert-ThrowsContains { Add-AzIoTDeviceProvisioningServiceEnrollmentGroup -ResourceGroupName $ResourceGroupName -DpsName $IotDpsName -Name $symEnroll -AttestationType None } $errorMessage |
| 71 | + $errorMessage = '"TPM" is not a valid attestation mechanism for an enrollment group' |
| 72 | + Assert-ThrowsContains { Add-AzIoTDeviceProvisioningServiceEnrollmentGroup -ResourceGroupName $ResourceGroupName -DpsName $IotDpsName -Name $symEnroll -AttestationType Tpm } $errorMessage |
| 73 | + |
| 74 | + # Expecting error while creating enrollment group with symmetrickey attestation mechanism |
| 75 | + $errorMessage = "Please provide both primary and secondary key" |
| 76 | + Assert-ThrowsContains { Add-AzIoTDeviceProvisioningServiceEnrollmentGroup -ResourceGroupName $ResourceGroupName -DpsName $IotDpsName -Name $symEnroll -AttestationType SymmetricKey -PrimaryKey "123456" } $errorMessage |
| 77 | + |
| 78 | + # Expecting error while creating enrollment group with X509 attestation mechanism |
| 79 | + $errorMessage = "Please provide either CA reference or X509 certificate" |
| 80 | + Assert-ThrowsContains { Add-AzIoTDeviceProvisioningServiceEnrollmentGroup -ResourceGroupName $ResourceGroupName -DpsName $IotDpsName -Name $x509Enroll -AttestationType X509 -SecondaryCAName "valid-ca-name" -SecondaryCertificate "valid-certificate" } $errorMessage |
| 81 | + Assert-ThrowsContains { Add-AzIoTDeviceProvisioningServiceEnrollmentGroup -ResourceGroupName $ResourceGroupName -DpsName $IotDpsName -Name $x509Enroll -AttestationType X509 } $errorMessage |
| 82 | + $errorMessage = "Primary CA reference cannot be null or empty" |
| 83 | + Assert-ThrowsContains { Add-AzIoTDeviceProvisioningServiceEnrollmentGroup -ResourceGroupName $ResourceGroupName -DpsName $IotDpsName -Name $x509Enroll -AttestationType X509 -SecondaryCAName "valid-ca-name" } $errorMessage |
| 84 | + $errorMessage = "Primary certificate cannot be null or empty" |
| 85 | + Assert-ThrowsContains { Add-AzIoTDeviceProvisioningServiceEnrollmentGroup -ResourceGroupName $ResourceGroupName -DpsName $IotDpsName -Name $x509Enroll -AttestationType X509 -SecondaryCertificate "valid-certificate" } $errorMessage |
| 86 | + |
| 87 | + # Expecting error while creating enrollment group with allocation policy and iothubhostname both defined. |
| 88 | + $errorMessage = '"IotHubHostName" is not required when allocation-policy is defined' |
| 89 | + Assert-ThrowsContains { Add-AzIoTDeviceProvisioningServiceEnrollmentGroup -ResourceGroupName $ResourceGroupName -DpsName $IotDpsName -Name $symEnroll -AttestationType SymmetricKey -AllocationPolicy GeoLatency -IotHubHostName $LinkedHubName } $errorMessage |
| 90 | + |
| 91 | + # Expecting error while creating enrollment group with static allocation policy. |
| 92 | + $errorMessage = "Please provide only one hub when allocation-policy is defined as Static" |
| 93 | + Assert-ThrowsContains { Add-AzIoTDeviceProvisioningServiceEnrollmentGroup -ResourceGroupName $ResourceGroupName -DpsName $IotDpsName -Name $symEnroll -AttestationType SymmetricKey -AllocationPolicy Static -IotHub "hub1","hub2" } $errorMessage |
| 94 | + $errorMessage = "Please provide a hub to be assigned with device" |
| 95 | + Assert-ThrowsContains { Add-AzIoTDeviceProvisioningServiceEnrollmentGroup -ResourceGroupName $ResourceGroupName -DpsName $IotDpsName -Name $symEnroll -AttestationType SymmetricKey -AllocationPolicy Static } $errorMessage |
| 96 | + |
| 97 | + # Expecting error while creating enrollment group with custom allocation policy. |
| 98 | + $errorMessage = "Please provide an Azure function url when allocation-policy is defined as Custom" |
| 99 | + Assert-ThrowsContains { Add-AzIoTDeviceProvisioningServiceEnrollmentGroup -ResourceGroupName $ResourceGroupName -DpsName $IotDpsName -Name $symEnroll -AttestationType SymmetricKey -AllocationPolicy Custom } $errorMessage |
| 100 | + $errorMessage = "Please provide an Azure function api-version when allocation-policy is defined as Custom" |
| 101 | + Assert-ThrowsContains { Add-AzIoTDeviceProvisioningServiceEnrollmentGroup -ResourceGroupName $ResourceGroupName -DpsName $IotDpsName -Name $symEnroll -AttestationType SymmetricKey -AllocationPolicy Custom -WebhookUrl "azure-function-url" } $errorMessage |
| 102 | + |
| 103 | + # Expecting error while creating enrollment group without allocation policy. |
| 104 | + $errorMessage = "Please provide allocation policy" |
| 105 | + Assert-ThrowsContains { Add-AzIoTDeviceProvisioningServiceEnrollmentGroup -ResourceGroupName $ResourceGroupName -DpsName $IotDpsName -Name $symEnroll -AttestationType SymmetricKey -IotHub $LinkedHubName } $errorMessage |
| 106 | + |
| 107 | + # Create enrollment group with symmetrickey attestation |
| 108 | + $symEnrollment = Add-AzIoTDeviceProvisioningServiceEnrollmentGroup -ResourceGroupName $ResourceGroupName -DpsName $IotDpsName -Name $symEnroll -AttestationType SymmetricKey |
| 109 | + Assert-True { $symEnrollment.EnrollmentGroupId -eq $symEnroll } |
| 110 | + Assert-True { $symEnrollment.AllocationPolicy -eq "Hashed" } |
| 111 | + Assert-False { $symEnrollment.Capabilities.IotEdge } |
| 112 | + Assert-True { $symEnrollment.ProvisioningStatus -eq "Enabled" } |
| 113 | + Assert-True { $symEnrollment.Attestation.Type -eq "SymmetricKey" } |
| 114 | + Assert-True { $symEnrollment.ReprovisionPolicy.UpdateHubAssignment } |
| 115 | + Assert-True { $symEnrollment.ReprovisionPolicy.MigrateDeviceData } |
| 116 | + |
| 117 | + # Create enrollment group with X509 attestation |
| 118 | + $x509Enrollment = Add-AzIoTDeviceProvisioningServiceEnrollmentGroup -ResourceGroupName $ResourceGroupName -DpsName $IotDpsName -Name $x509Enroll -AttestationType X509 -PrimaryCertificate $CertificateKey -RootCertificate -IotHubHostName $LinkedHubName -ReprovisionPolicy reprovisionandresetdata -ProvisioningStatus "Disabled" |
| 119 | + Assert-True { $x509Enrollment.EnrollmentGroupId -eq $x509Enroll } |
| 120 | + Assert-True { $x509Enrollment.IotHubHostName -eq $LinkedHubName } |
| 121 | + Assert-False { $x509Enrollment.Capabilities.IotEdge } |
| 122 | + Assert-True { $x509Enrollment.ProvisioningStatus -eq "Disabled" } |
| 123 | + Assert-True { $x509Enrollment.Attestation.Type -eq "X509" } |
| 124 | + Assert-True { $x509Enrollment.ReprovisionPolicy.UpdateHubAssignment } |
| 125 | + Assert-False { $x509Enrollment.ReprovisionPolicy.MigrateDeviceData } |
| 126 | + |
| 127 | + # Get enrollment group |
| 128 | + $enrollment = Get-AzIoTDeviceProvisioningServiceEnrollmentGroup -ResourceGroupName $ResourceGroupName -DpsName $IotDpsName -Name $symEnroll |
| 129 | + Assert-True { $enrollment.EnrollmentGroupId -eq $symEnroll } |
| 130 | + |
| 131 | + # Get enrollment groups |
| 132 | + $enrollments = Get-AzIoTDeviceProvisioningServiceEnrollmentGroup -ResourceGroupName $ResourceGroupName -DpsName $IotDpsName |
| 133 | + Assert-True { $enrollments.Count -eq 2 } |
| 134 | + |
| 135 | + # Error While Updating enrollment group |
| 136 | + $errorMessage = "Not Found" |
| 137 | + Assert-ThrowsContains { Set-AzIoTDeviceProvisioningServiceEnrollmentGroup -ResourceGroupName $ResourceGroupName -DpsName $IotDpsName -Name "Invalid" } $errorMessage |
| 138 | + |
| 139 | + # Update enrollment group |
| 140 | + $tag = @{} |
| 141 | + $tag.add("environment","test") |
| 142 | + $symEnrollmentUpdated = Set-AzIoTDeviceProvisioningServiceEnrollmentGroup -ResourceGroupName $ResourceGroupName -DpsName $IotDpsName -Name $symEnroll -Tag $tag -AllocationPolicy Custom -WebhookUrl "https://www.test.test" -ApiVersion "2018-09-01-preview" |
| 143 | + Assert-True { $symEnrollmentUpdated.EnrollmentGroupId -eq $symEnroll } |
| 144 | + Assert-True { $symEnrollmentUpdated.InitialTwinState.Tags.ToJson() -eq '{"environment":"test"}' } |
| 145 | + Assert-True { $symEnrollmentUpdated.AllocationPolicy -eq "Custom" } |
| 146 | + Assert-True { $symEnrollmentUpdated.CustomAllocationDefinition.WebhookUrl -eq "https://www.test.test" } |
| 147 | + Assert-True { $symEnrollmentUpdated.CustomAllocationDefinition.ApiVersion -eq "2018-09-01-preview" } |
| 148 | + |
| 149 | + $x509EnrollmentUpdated = Set-AzIoTDeviceProvisioningServiceEnrollmentGroup -ResourceGroupName $ResourceGroupName -DpsName $IotDpsName -Name $x509Enroll -ProvisioningStatus "Enabled" |
| 150 | + Assert-True { $x509EnrollmentUpdated.EnrollmentGroupId -eq $x509Enroll } |
| 151 | + Assert-False { $x509EnrollmentUpdated.Capabilities.IotEdge } |
| 152 | + Assert-True { $x509EnrollmentUpdated.ProvisioningStatus -eq "Enabled" } |
| 153 | + Assert-True { $x509EnrollmentUpdated.Attestation.Type -eq "X509" } |
| 154 | + Assert-True { $x509EnrollmentUpdated.ReprovisionPolicy.UpdateHubAssignment } |
| 155 | + Assert-False { $x509EnrollmentUpdated.ReprovisionPolicy.MigrateDeviceData } |
| 156 | + |
| 157 | + # Remove enrollment group |
| 158 | + $result = Remove-AzIoTDPSEnrollmentGroup -ResourceGroupName $ResourceGroupName -DpsName $IotDpsName -Name $symEnroll -PassThru |
| 159 | + Assert-True { $result } |
| 160 | + $result = Remove-AzIoTDeviceProvisioningServiceEnrollmentGroup -ResourceGroupName $ResourceGroupName -DpsName $IotDpsName -PassThru |
| 161 | + Assert-True { $result } |
| 162 | + |
| 163 | + # Remove Linked Hub from Iot Hub Device Provisioning Service |
| 164 | + $result = Remove-AzIoTDpsHub -ResourceGroupName $ResourceGroupName -Name $IotDpsName -LinkedHubName $LinkedHubName -PassThru |
| 165 | + Assert-True { $result } |
| 166 | + |
| 167 | + # Remove IotHub |
| 168 | + Remove-AzIotHub -ResourceGroupName $ResourceGroupName -Name $IotHubName |
| 169 | + |
| 170 | + # Remove Resource Group |
| 171 | + Remove-AzResourceGroup -Name $ResourceGroupName -force |
| 172 | +} |
0 commit comments