private static async void ProcessDesiredCertificateConfiguration(
            DeviceManagementClient client,
            string connectionString,
            string containerName,
            Message.CertificateConfiguration certificateConfiguration)
        {
            await IoTDMClient.CertificateManagement.DownloadCertificates(client, connectionString, containerName, certificateConfiguration);

            var request = new Message.SetCertificateConfigurationRequest(certificateConfiguration);

            client._systemConfiguratorProxy.SendCommandAsync(request);
        }
Exemple #2
0
        // IClientPropertyHandler
        public async Task <CommandStatus> OnDesiredPropertyChange(JToken desiredValue)
        {
            if (!(desiredValue is JObject))
            {
                throw new Error(ErrorCodes.INVALID_DESIRED_JSON_VALUE, "Invalid json value type for the " + PropertySectionName + " node.");
            }

            CertificatesDataContract.DesiredProperties desiredProperties = CertificatesDataContract.DesiredProperties.FromJsonObject((JObject)desiredValue);

            await IoTDMClient.CertificateManagement.DownloadCertificates(_systemConfiguratorProxy, _connectionString, desiredProperties);

            Message.CertificateConfiguration certificateConfiguration = new Message.CertificateConfiguration();
            certificateConfiguration.certificateStore_CA_System                 = DesiredToString(desiredProperties.certificateStore_CA_System);
            certificateConfiguration.certificateStore_My_System                 = DesiredToString(desiredProperties.certificateStore_My_System);
            certificateConfiguration.certificateStore_My_User                   = DesiredToString(desiredProperties.certificateStore_My_User);
            certificateConfiguration.certificateStore_Root_System               = DesiredToString(desiredProperties.certificateStore_Root_System);
            certificateConfiguration.rootCATrustedCertificates_CA               = DesiredToString(desiredProperties.rootCATrustedCertificates_CA);
            certificateConfiguration.rootCATrustedCertificates_Root             = DesiredToString(desiredProperties.rootCATrustedCertificates_Root);
            certificateConfiguration.rootCATrustedCertificates_TrustedPeople    = DesiredToString(desiredProperties.rootCATrustedCertificates_TrustedPeople);
            certificateConfiguration.rootCATrustedCertificates_TrustedPublisher = DesiredToString(desiredProperties.rootCATrustedCertificates_TrustedPublisher);

            var request = new Message.SetCertificateConfigurationRequest(certificateConfiguration);
            await _systemConfiguratorProxy.SendCommandAsync(request);

            JObject reportedProperties = await GetReportedPropertyAsync();

            Debug.WriteLine("-- Reporting Certificates -------------------------------------");
            Debug.WriteLine(reportedProperties.ToString());
            Debug.WriteLine("-- Reporting Certificates Done --------------------------------");

            // Because the section contains a list, we need to reset the parent to allow for removals...
            await _deviceManagementClient.ReportPropertiesAsync(PropertySectionName, new JValue("refreshing"));

            // Report the updated list...
            await _deviceManagementClient.ReportPropertiesAsync(PropertySectionName, reportedProperties);

            return(CommandStatus.Committed);
        }
        public void ProcessDeviceManagementProperties(TwinCollection desiredProperties)
        {
            // ToDo: We should not throw here. All problems need to be logged.
            Message.CertificateConfiguration certificateConfiguration = null;

            foreach (KeyValuePair <string, object> dp in desiredProperties)
            {
                if (dp.Key == "microsoft" && dp.Value is JObject)
                {
                    JToken managementNode;
                    if ((dp.Value as JObject).TryGetValue("management", out managementNode))
                    {
                        foreach (var managementProperty in managementNode.Children().OfType <JProperty>())
                        {
                            switch (managementProperty.Name)
                            {
                            case "initialProvisioning":
                                if (managementProperty.Value.Type == JTokenType.Object)
                                {
                                    Debug.WriteLine("initialProvisioning = " + managementProperty.Value.ToString());
                                    var provBlobInfo = JsonConvert.DeserializeObject <IoTDMClient.ProvisionBlobInfo>(managementProperty.Value.ToString());
                                    provBlobInfo.ProvisionPkgsAsync(this);
                                }
                                break;

                            case "scheduledReboot":
                                if (managementProperty.Value.Type == JTokenType.Object)
                                {
                                    Debug.WriteLine("scheduledReboot = " + managementProperty.Value.ToString());

                                    JObject subProperties = (JObject)managementProperty.Value;

                                    var request = new Message.SetRebootInfoRequest();

                                    DateTime singleRebootTime = DateTime.Parse(subProperties.Property("singleRebootTime").Value.ToString());
                                    request.singleRebootTime = singleRebootTime.ToString("yyyy-MM-ddTHH:mm:ssZ");

                                    DateTime dailyRebootTime = DateTime.Parse(subProperties.Property("dailyRebootTime").Value.ToString());
                                    request.dailyRebootTime = dailyRebootTime.ToString("yyyy-MM-ddTHH:mm:ssZ");

                                    this._systemConfiguratorProxy.SendCommandAsync(request);
                                }
                                break;

                            case "externalStorage":
                                if (managementProperty.Value.Type == JTokenType.Object)
                                {
                                    Debug.WriteLine("externalStorage = " + managementProperty.Value.ToString());

                                    JObject subProperties = (JObject)managementProperty.Value;

                                    _externalStorage.connectionString = (string)subProperties.Property("connectionString").Value;
                                    _externalStorage.containerName    = (string)subProperties.Property("container").Value;
                                }
                                break;

                            case "certificates":
                                if (managementProperty.Value.Type == JTokenType.Object)
                                {
                                    // Capture the configuration here.
                                    // To apply the configuration we need to wait until externalStorage has been configured too.
                                    Debug.WriteLine("CertificateConfiguration = " + managementProperty.Value.ToString());
                                    certificateConfiguration = JsonConvert.DeserializeObject <CertificateConfiguration>(managementProperty.Value.ToString());
                                }
                                break;

                            case "timeInfo":
                                if (managementProperty.Value.Type == JTokenType.Object)
                                {
                                    Debug.WriteLine("timeInfo = " + managementProperty.Value.ToString());

                                    // Default JsonConvert Deserializing changes ISO8601 date fields to "mm/dd/yyyy hh:mm:ss".
                                    // We need to preserve the ISO8601 since that's the format SystemConfigurator understands.
                                    // Because of that, we are not using:
                                    // Message.SetTimeInfo requestInfo = JsonConvert.DeserializeObject<Message.SetTimeInfo>(fieldsJson);

                                    Message.SetTimeInfoRequest request = new Message.SetTimeInfoRequest();
                                    JObject subProperties = (JObject)managementProperty.Value;
                                    request.ntpServer            = (string)subProperties.Property("ntpServer").Value;
                                    request.timeZoneBias         = (int)subProperties.Property("timeZoneBias").Value;
                                    request.timeZoneDaylightBias = (int)subProperties.Property("timeZoneDaylightBias").Value;
                                    DateTime daylightDate = DateTime.Parse(subProperties.Property("timeZoneDaylightDate").Value.ToString());
                                    request.timeZoneDaylightDate = daylightDate.ToString("yyyy-MM-ddTHH:mm:ssZ");
                                    request.timeZoneDaylightName = (string)subProperties.Property("timeZoneDaylightName").Value;
                                    request.timeZoneStandardBias = (int)subProperties.Property("timeZoneStandardBias").Value;
                                    DateTime standardDate = DateTime.Parse(subProperties.Property("timeZoneStandardDate").Value.ToString());
                                    request.timeZoneStandardDate = standardDate.ToString("yyyy-MM-ddTHH:mm:ssZ");
                                    request.timeZoneStandardName = (string)subProperties.Property("timeZoneStandardName").Value;

                                    this._systemConfiguratorProxy.SendCommandAsync(request);
                                }
                                break;

                            case "windowsUpdatePolicy":
                                if (managementProperty.Value.Type == JTokenType.Object)
                                {
                                    Debug.WriteLine("windowsUpdatePolicy = " + managementProperty.Value.ToString());
                                    var configuration = JsonConvert.DeserializeObject <WindowsUpdatePolicyConfiguration>(managementProperty.Value.ToString());
                                    this._systemConfiguratorProxy.SendCommandAsync(new SetWindowsUpdatePolicyRequest(configuration));
                                }
                                break;

                            case "windowsUpdates":
                                if (managementProperty.Value.Type == JTokenType.Object)
                                {
                                    Debug.WriteLine("windowsUpdates = " + managementProperty.Value.ToString());
                                    var configuration = JsonConvert.DeserializeObject <SetWindowsUpdatesConfiguration>(managementProperty.Value.ToString());
                                    this._systemConfiguratorProxy.SendCommandAsync(new SetWindowsUpdatesRequest(configuration));
                                }
                                break;

                            default:
                                // Not supported
                                break;
                            }
                        }
                    }
                }
            }

            // Need to keep this until externalStorage is processed.
            // ToDo: The client does not get a full copy of the device twin when it first connects! (regression?)
            //       This means that the externalStorage might not get set when the machine connects.
            if (!String.IsNullOrEmpty(_externalStorage.connectionString) &&
                !String.IsNullOrEmpty(_externalStorage.containerName) &&
                certificateConfiguration != null)
            {
                ProcessDesiredCertificateConfiguration(this, _externalStorage.connectionString, _externalStorage.containerName, certificateConfiguration);
            }
        }