public void X509AttestationCreateFromCAReferencesThrowsOnNullPrimaryCertificate() { // arrange string primaryStr = null; string secondaryStr = null; // act - assert TestAssert.Throws <ArgumentException>(() => X509Attestation.CreateFromCAReferences(primaryStr)); TestAssert.Throws <ArgumentException>(() => X509Attestation.CreateFromCAReferences(primaryStr, secondaryStr)); }
public void X509AttestationCreateFromCAReferencesSucceedOnPrimaryString() { // arrange string primary = CA_REFERENCE_STRING; // act X509Attestation attestation = X509Attestation.CreateFromCAReferences(primary); // assert Assert.IsNotNull(attestation.CAReferences.Primary); Assert.IsNull(attestation.CAReferences.Secondary); Assert.IsNull(attestation.ClientCertificates); Assert.IsNull(attestation.RootCertificates); }
public override void ExecuteCmdlet() { if (ShouldProcess(this.DpsName, DPSResources.AddEnrollment)) { ProvisioningServiceDescription provisioningServiceDescription; if (ParameterSetName.Equals(InputObjectParameterSet)) { this.ResourceGroupName = this.DpsObject.ResourceGroupName; this.DpsName = this.DpsObject.Name; provisioningServiceDescription = IotDpsUtils.ConvertObject <PSProvisioningServiceDescription, ProvisioningServiceDescription>(this.DpsObject); } else { if (ParameterSetName.Equals(ResourceIdParameterSet)) { this.ResourceGroupName = IotDpsUtils.GetResourceGroupName(this.ResourceId); this.DpsName = IotDpsUtils.GetIotDpsName(this.ResourceId); } provisioningServiceDescription = GetIotDpsResource(this.ResourceGroupName, this.DpsName); } IEnumerable <SharedAccessSignatureAuthorizationRuleAccessRightsDescription> authPolicies = this.IotDpsClient.IotDpsResource.ListKeys(this.DpsName, this.ResourceGroupName); SharedAccessSignatureAuthorizationRuleAccessRightsDescription policy = IotDpsUtils.GetPolicy(authPolicies, PSAccessRightsDescription.EnrollmentWrite); PSIotDpsConnectionString psIotDpsConnectionString = IotDpsUtils.ToPSIotDpsConnectionString(policy, provisioningServiceDescription.Properties.ServiceOperationsHostName); ProvisioningServiceClient client = ProvisioningServiceClient.CreateFromConnectionString(psIotDpsConnectionString.PrimaryConnectionString); Attestation attestation = null; TwinCollection tags = new TwinCollection(), desiredProperties = new TwinCollection(); if (this.IsParameterBound(c => c.Tag)) { tags = new TwinCollection(JsonConvert.SerializeObject(this.Tag)); } if (this.IsParameterBound(c => c.Desired)) { desiredProperties = new TwinCollection(JsonConvert.SerializeObject(this.Desired)); } switch (this.AttestationType) { case PSAttestationMechanismType.SymmetricKey: if ((this.IsParameterBound(c => c.PrimaryKey) && !this.IsParameterBound(c => c.SecondaryKey)) || (!this.IsParameterBound(c => c.PrimaryKey) && this.IsParameterBound(c => c.SecondaryKey))) { throw new ArgumentException("Please provide both primary and secondary key."); } else { attestation = new SymmetricKeyAttestation(this.PrimaryKey, this.SecondaryKey); } break; case PSAttestationMechanismType.Tpm: if (this.IsParameterBound(c => c.EndorsementKey)) { attestation = new TpmAttestation(this.EndorsementKey, this.IsParameterBound(c => c.StorageRootKey) ? this.StorageRootKey : null); } else { throw new ArgumentException("Endorsement key is requried."); } break; case PSAttestationMechanismType.X509: if (!this.IsParameterBound(c => c.PrimaryCertificate) && !this.IsParameterBound(c => c.SecondaryCertificate)) { if (!this.IsParameterBound(c => c.PrimaryCAName)) { throw new ArgumentException("Primary CA reference cannot be null or empty."); } if (this.IsParameterBound(c => c.SecondaryCAName)) { attestation = X509Attestation.CreateFromCAReferences(this.PrimaryCAName, this.SecondaryCAName); } else { attestation = X509Attestation.CreateFromCAReferences(this.PrimaryCAName); } } else if (!this.IsParameterBound(c => c.PrimaryCAName) && !this.IsParameterBound(c => c.SecondaryCAName)) { string primaryCer = string.Empty, secondaryCer = string.Empty; if (!this.IsParameterBound(c => c.PrimaryCertificate)) { throw new ArgumentException("Primary certificate cannot be null or empty."); } primaryCer = IotDpsUtils.GetCertificateString(this.PrimaryCertificate); if (this.IsParameterBound(c => c.SecondaryCertificate)) { secondaryCer = IotDpsUtils.GetCertificateString(this.PrimaryCertificate); if (this.IsParameterBound(c => c.RootCertificate)) { attestation = X509Attestation.CreateFromRootCertificates(primaryCer, secondaryCer); } else { attestation = X509Attestation.CreateFromClientCertificates(primaryCer, secondaryCer); } } else { if (this.IsParameterBound(c => c.RootCertificate)) { attestation = X509Attestation.CreateFromRootCertificates(primaryCer); } else { attestation = X509Attestation.CreateFromClientCertificates(primaryCer); } } } else { throw new ArgumentException("Please provide either CA reference or X509 certificate."); } break; default: throw new ArgumentException("Please provide valid attestation mechanism."); } IndividualEnrollment enrollment = new IndividualEnrollment(this.RegistrationId, attestation); if (this.IsParameterBound(c => c.DeviceId)) { enrollment.DeviceId = this.DeviceId; } enrollment.InitialTwinState = new TwinState(tags, desiredProperties); enrollment.Capabilities = new DeviceCapabilities() { IotEdge = this.EdgeEnabled.IsPresent }; switch (this.ReprovisionPolicy) { case PSReprovisionType.reprovisionandmigratedata: enrollment.ReprovisionPolicy = new ReprovisionPolicy() { UpdateHubAssignment = true, MigrateDeviceData = true }; break; case PSReprovisionType.reprovisionandresetdata: enrollment.ReprovisionPolicy = new ReprovisionPolicy() { UpdateHubAssignment = true, MigrateDeviceData = false }; break; case PSReprovisionType.never: enrollment.ReprovisionPolicy = new ReprovisionPolicy() { UpdateHubAssignment = false, MigrateDeviceData = false }; break; } if (this.IsParameterBound(c => c.AllocationPolicy)) { if (this.IsParameterBound(c => c.IotHubHostName)) { throw new ArgumentException("\"IotHubHostName\" is not required when allocation-policy is defined."); } if (this.AllocationPolicy.Equals(PSAllocationPolicy.Static)) { if (this.IsParameterBound(c => c.IotHub)) { if (this.IotHub.Length > 1) { throw new ArgumentException("Please provide only one hub when allocation-policy is defined as Static."); } } else { throw new ArgumentException("Please provide a hub to be assigned with device."); } } if (this.AllocationPolicy.Equals(PSAllocationPolicy.Custom)) { if (!this.IsParameterBound(c => c.WebhookUrl)) { throw new ArgumentException("Please provide an Azure function url when allocation-policy is defined as Custom."); } if (!this.IsParameterBound(c => c.ApiVersion)) { throw new ArgumentException("Please provide an Azure function api-version when allocation-policy is defined as Custom."); } enrollment.CustomAllocationDefinition = new CustomAllocationDefinition() { WebhookUrl = this.WebhookUrl, ApiVersion = this.ApiVersion }; } enrollment.AllocationPolicy = (Devices.Provisioning.Service.AllocationPolicy)Enum.Parse(typeof(Devices.Provisioning.Service.AllocationPolicy), this.AllocationPolicy.ToString()); enrollment.IotHubs = this.IotHub; } else { if (this.IsParameterBound(c => c.IotHub)) { throw new ArgumentException("Please provide allocation policy."); } if (this.IsParameterBound(c => c.IotHubHostName)) { enrollment.IotHubHostName = this.IotHubHostName; } } if (this.IsParameterBound(c => c.ProvisioningStatus)) { enrollment.ProvisioningStatus = (ProvisioningStatus)Enum.Parse(typeof(ProvisioningStatus), this.ProvisioningStatus.ToString()); } IndividualEnrollment result = client.CreateOrUpdateIndividualEnrollmentAsync(enrollment).GetAwaiter().GetResult(); this.WriteObject(IotDpsUtils.ToPSIndividualEnrollment(result)); } }
public override void ExecuteCmdlet() { if (ShouldProcess(this.DpsName, DPSResources.AddEnrollmentGroup)) { ProvisioningServiceDescription provisioningServiceDescription; if (ParameterSetName.Equals(InputObjectParameterSet)) { this.ResourceGroupName = this.DpsObject.ResourceGroupName; this.DpsName = this.DpsObject.Name; provisioningServiceDescription = IotDpsUtils.ConvertObject <PSProvisioningServiceDescription, ProvisioningServiceDescription>(this.DpsObject); } else { if (ParameterSetName.Equals(ResourceIdParameterSet)) { this.ResourceGroupName = IotDpsUtils.GetResourceGroupName(this.ResourceId); this.DpsName = IotDpsUtils.GetIotDpsName(this.ResourceId); } provisioningServiceDescription = GetIotDpsResource(this.ResourceGroupName, this.DpsName); } IEnumerable <SharedAccessSignatureAuthorizationRuleAccessRightsDescription> authPolicies = this.IotDpsClient.IotDpsResource.ListKeys(this.DpsName, this.ResourceGroupName); SharedAccessSignatureAuthorizationRuleAccessRightsDescription policy = IotDpsUtils.GetPolicy(authPolicies, PSAccessRightsDescription.EnrollmentWrite); PSIotDpsConnectionString psIotDpsConnectionString = IotDpsUtils.ToPSIotDpsConnectionString(policy, provisioningServiceDescription.Properties.ServiceOperationsHostName); ProvisioningServiceClient client = ProvisioningServiceClient.CreateFromConnectionString(psIotDpsConnectionString.PrimaryConnectionString); EnrollmentGroup enrollment = client.GetEnrollmentGroupAsync(this.Name).GetAwaiter().GetResult(); if (enrollment != null) { // Updating ProvisioningStatus if (this.IsParameterBound(c => c.ProvisioningStatus)) { enrollment.ProvisioningStatus = (ProvisioningStatus)Enum.Parse(typeof(ProvisioningStatus), this.ProvisioningStatus.ToString()); } // Updating InitialTwinState if (this.IsParameterBound(c => c.Tag) || this.IsParameterBound(c => c.Desired)) { TwinCollection tags = this.IsParameterBound(c => c.Tag) ? new TwinCollection(JsonConvert.SerializeObject(this.Tag)) : (enrollment.InitialTwinState != null ? enrollment.InitialTwinState.Tags : new TwinCollection()); TwinCollection desiredProperties = this.IsParameterBound(c => c.Desired) ? new TwinCollection(JsonConvert.SerializeObject(this.Desired)) : (enrollment.InitialTwinState != null ? enrollment.InitialTwinState.DesiredProperties : new TwinCollection()); enrollment.InitialTwinState = new TwinState(tags, desiredProperties); } // Updating Capabilities if (this.IsParameterBound(c => c.EdgeEnabled)) { enrollment.Capabilities = new DeviceCapabilities() { IotEdge = this.EdgeEnabled }; } // Updating ReprovisionPolicy if (this.IsParameterBound(c => c.ReprovisionPolicy)) { switch (this.ReprovisionPolicy) { case PSReprovisionType.reprovisionandmigratedata: enrollment.ReprovisionPolicy = new ReprovisionPolicy() { UpdateHubAssignment = true, MigrateDeviceData = true }; break; case PSReprovisionType.reprovisionandresetdata: enrollment.ReprovisionPolicy = new ReprovisionPolicy() { UpdateHubAssignment = true, MigrateDeviceData = false }; break; case PSReprovisionType.never: enrollment.ReprovisionPolicy = new ReprovisionPolicy() { UpdateHubAssignment = false, MigrateDeviceData = false }; break; } } // Updating AllocationPolicy and Hub if (this.IsParameterBound(c => c.IotHubHostName) && this.IsParameterBound(c => c.AllocationPolicy)) { throw new ArgumentException("\"IotHubHostName\" is not required when allocation-policy is defined."); } if (this.IsParameterBound(c => c.IotHubHostName) && this.IsParameterBound(c => c.IotHub)) { throw new ArgumentException("\"IotHubHostName\" is not required when IotHub is defined."); } if (this.IsParameterBound(c => c.IotHubHostName)) { enrollment.IotHubHostName = this.IotHubHostName; enrollment.CustomAllocationDefinition = null; enrollment.AllocationPolicy = null; enrollment.IotHubs = null; } if (this.IsParameterBound(c => c.AllocationPolicy)) { enrollment.AllocationPolicy = (Devices.Provisioning.Service.AllocationPolicy)Enum.Parse(typeof(Devices.Provisioning.Service.AllocationPolicy), this.AllocationPolicy.ToString()); } switch (enrollment.AllocationPolicy) { case Devices.Provisioning.Service.AllocationPolicy.Static: if (this.IsParameterBound(c => c.IotHub)) { if (this.IotHub.Length > 1) { throw new ArgumentException("Please provide only one hub when allocation-policy is defined as Static."); } enrollment.IotHubs = this.IotHub; } enrollment.CustomAllocationDefinition = null; enrollment.IotHubHostName = null; break; case Devices.Provisioning.Service.AllocationPolicy.Custom: if (enrollment.CustomAllocationDefinition == null) { if (!this.IsParameterBound(c => c.WebhookUrl)) { throw new ArgumentException("Please provide an Azure function url when allocation-policy is defined as Custom."); } if (!this.IsParameterBound(c => c.ApiVersion)) { throw new ArgumentException("Please provide an Azure function api-version when allocation-policy is defined as Custom."); } } string webhookUrl = string.Empty, apiVersion = string.Empty; webhookUrl = this.IsParameterBound(c => c.WebhookUrl) ? this.WebhookUrl : enrollment.CustomAllocationDefinition.WebhookUrl; apiVersion = this.IsParameterBound(c => c.ApiVersion) ? this.ApiVersion : enrollment.CustomAllocationDefinition.ApiVersion; enrollment.CustomAllocationDefinition = new CustomAllocationDefinition() { WebhookUrl = webhookUrl, ApiVersion = apiVersion }; enrollment.IotHubHostName = null; if (this.IsParameterBound(c => c.IotHub)) { enrollment.IotHubs = this.IotHub; } break; case Devices.Provisioning.Service.AllocationPolicy.Hashed: case Devices.Provisioning.Service.AllocationPolicy.GeoLatency: if (this.IsParameterBound(c => c.IotHub)) { enrollment.IotHubs = this.IotHub; } enrollment.CustomAllocationDefinition = null; enrollment.IotHubHostName = null; break; default: if (this.IsParameterBound(c => c.IotHub)) { throw new ArgumentException("Please provide allocation policy."); } break; } switch (enrollment.Attestation) { case SymmetricKeyAttestation attestation: if (this.IsParameterBound(c => c.PrimaryKey) || this.IsParameterBound(c => c.SecondaryKey)) { enrollment.Attestation = new SymmetricKeyAttestation( this.IsParameterBound(c => c.PrimaryKey) ? this.PrimaryKey : attestation.PrimaryKey, this.IsParameterBound(c => c.SecondaryKey) ? this.SecondaryKey : attestation.SecondaryKey ); } break; case X509Attestation attestation: bool updatedPrimaryCAName = this.IsParameterBound(c => c.PrimaryCAName); bool updatedSecondaryCAName = this.IsParameterBound(c => c.SecondaryCAName); bool updatedPrimaryCertificate = this.IsParameterBound(c => c.PrimaryCertificate); bool updatedSecondaryCertificate = this.IsParameterBound(c => c.SecondaryCertificate); if (updatedPrimaryCAName || updatedSecondaryCAName) { enrollment.Attestation = X509Attestation.CreateFromCAReferences( updatedPrimaryCAName ? this.PrimaryCAName : attestation.CAReferences.Primary, updatedSecondaryCAName ? this.SecondaryCAName : (attestation.CAReferences.Secondary ?? null) ); } else if (updatedPrimaryCertificate || updatedSecondaryCertificate) { string primaryCer = string.Empty, secondaryCer = string.Empty; if (!updatedPrimaryCertificate) { throw new ArgumentException("Primary certificate cannot be null or empty."); } primaryCer = IotDpsUtils.GetCertificateString(this.PrimaryCertificate); if (this.IsParameterBound(c => c.SecondaryCertificate)) { secondaryCer = IotDpsUtils.GetCertificateString(this.SecondaryCertificate); if (this.IsParameterBound(c => c.RootCertificate)) { enrollment.Attestation = X509Attestation.CreateFromRootCertificates(primaryCer, secondaryCer); } else { enrollment.Attestation = X509Attestation.CreateFromClientCertificates(primaryCer, secondaryCer); } } else { if (this.IsParameterBound(c => c.RootCertificate)) { enrollment.Attestation = X509Attestation.CreateFromRootCertificates(primaryCer); } else { enrollment.Attestation = X509Attestation.CreateFromClientCertificates(primaryCer); } } } break; default: break; } } else { throw new ArgumentException("The enrollment doesn't exist."); } EnrollmentGroup result = client.CreateOrUpdateEnrollmentGroupAsync(enrollment).GetAwaiter().GetResult(); this.WriteObject(IotDpsUtils.ToPSEnrollmentGroup(result)); } }