Exemple #1
0
        public async Task Configure(string connectionString, string image, string hostname)
        {
            Console.WriteLine($"Setting up iotedged with container registry '{this.credentials.Match(c => c.Address, () => "<none>")}'");

            const string  YamlPath = "/etc/iotedge/config.yaml";
            Task <string> text     = File.ReadAllTextAsync(YamlPath);

            var doc = new YamlDocument(await text);

            doc.Replace("provisioning.device_connection_string", connectionString);
            doc.Replace("agent.config.image", image);
            doc.Replace("hostname", hostname);

            foreach (RegistryCredentials c in this.credentials)
            {
                doc.Replace("agent.config.auth.serveraddress", c.Address);
                doc.Replace("agent.config.auth.username", c.User);
                doc.Replace("agent.config.auth.password", c.Password);
            }

            string result = doc.ToString();

            FileAttributes attr = 0;

            if (File.Exists(YamlPath))
            {
                attr = File.GetAttributes(YamlPath);
                File.SetAttributes(YamlPath, attr & ~FileAttributes.ReadOnly);
            }

            await File.WriteAllTextAsync(YamlPath, result);

            if (attr != 0)
            {
                File.SetAttributes(YamlPath, attr);
            }
        }
Exemple #2
0
        public async Task Configure(string connectionString, string image, string hostname, string deviceCaCert, string deviceCaPk, string deviceCaCerts, LogLevel runtimeLogLevel)
        {
            Console.WriteLine($"Setting up iotedged with agent image '{image}'");

            const string  YamlPath = "/etc/iotedge/config.yaml";
            Task <string> text     = File.ReadAllTextAsync(YamlPath);

            var doc = new YamlDocument(await text);

            doc.ReplaceOrAdd("provisioning.device_connection_string", connectionString);
            doc.ReplaceOrAdd("agent.config.image", image);
            doc.ReplaceOrAdd("hostname", hostname);

            foreach (RegistryCredentials c in this.credentials)
            {
                doc.ReplaceOrAdd("agent.config.auth.serveraddress", c.Address);
                doc.ReplaceOrAdd("agent.config.auth.username", c.User);
                doc.ReplaceOrAdd("agent.config.auth.password", c.Password);
            }

            doc.ReplaceOrAdd("agent.env.RuntimeLogLevel", runtimeLogLevel.ToString());

            if (this.httpUris.HasValue)
            {
                HttpUris uris = this.httpUris.OrDefault();
                doc.ReplaceOrAdd("connect.management_uri", uris.ConnectManagement);
                doc.ReplaceOrAdd("connect.workload_uri", uris.ConnectWorkload);
                doc.ReplaceOrAdd("listen.management_uri", uris.ListenManagement);
                doc.ReplaceOrAdd("listen.workload_uri", uris.ListenWorkload);
            }
            else
            {
                doc.ReplaceOrAdd("connect.management_uri", "unix:///var/run/iotedge/mgmt.sock");
                doc.ReplaceOrAdd("connect.workload_uri", "unix:///var/run/iotedge/workload.sock");
                doc.ReplaceOrAdd("listen.management_uri", "fd://iotedge.mgmt.socket");
                doc.ReplaceOrAdd("listen.workload_uri", "fd://iotedge.socket");
            }

            if (!string.IsNullOrEmpty(deviceCaCert) && !string.IsNullOrEmpty(deviceCaPk) && !string.IsNullOrEmpty(deviceCaCerts))
            {
                doc.ReplaceOrAdd("certificates.device_ca_cert", deviceCaCert);
                doc.ReplaceOrAdd("certificates.device_ca_pk", deviceCaPk);
                doc.ReplaceOrAdd("certificates.trusted_ca_certs", deviceCaCerts);
            }

            this.proxy.ForEach(proxy => doc.ReplaceOrAdd("agent.env.https_proxy", proxy));

            this.upstreamProtocol.ForEach(upstreamProtocol => doc.ReplaceOrAdd("agent.env.UpstreamProtocol", upstreamProtocol.ToString()));

            string result = doc.ToString();


            FileAttributes attr = 0;

            if (File.Exists(YamlPath))
            {
                attr = File.GetAttributes(YamlPath);
                File.SetAttributes(YamlPath, attr & ~FileAttributes.ReadOnly);
            }

            await File.WriteAllTextAsync(YamlPath, result);

            if (attr != 0)
            {
                File.SetAttributes(YamlPath, attr);
            }
        }
        public async Task Configure(DeviceProvisioningMethod method, Option <string> agentImage, string hostname, string deviceCaCert, string deviceCaPk, string deviceCaCerts, LogLevel runtimeLogLevel)
        {
            agentImage.ForEach(
                image =>
            {
                Console.WriteLine($"Setting up iotedged with agent image {image}");
            },
                () =>
            {
                Console.WriteLine("Setting up iotedged with agent image 1.0");
            });

            const string  YamlPath = "/etc/iotedge/config.yaml";
            Task <string> text     = File.ReadAllTextAsync(YamlPath);
            var           doc      = new YamlDocument(await text);

            method.ManualConnectionString.Match(
                cs =>
            {
                doc.ReplaceOrAdd("provisioning.device_connection_string", cs);
                return(string.Empty);
            },
                () =>
            {
                doc.Remove("provisioning.device_connection_string");
                return(string.Empty);
            });

            method.Dps.ForEach(
                dps =>
            {
                doc.ReplaceOrAdd("provisioning.source", "dps");
                doc.ReplaceOrAdd("provisioning.global_endpoint", dps.EndPoint);
                doc.ReplaceOrAdd("provisioning.scope_id", dps.ScopeId);
                switch (dps.AttestationType)
                {
                case DPSAttestationType.SymmetricKey:
                    doc.ReplaceOrAdd("provisioning.attestation.method", "symmetric_key");
                    doc.ReplaceOrAdd("provisioning.attestation.symmetric_key", dps.SymmetricKey.Expect(() => new ArgumentException("Expected symmetric key")));
                    break;

                case DPSAttestationType.X509:
                    var certUri = new Uri(dps.DeviceIdentityCertificate.Expect(() => new ArgumentException("Expected path to identity certificate")));
                    var keyUri  = new Uri(dps.DeviceIdentityPrivateKey.Expect(() => new ArgumentException("Expected path to identity private key")));
                    doc.ReplaceOrAdd("provisioning.attestation.method", "x509");
                    doc.ReplaceOrAdd("provisioning.attestation.identity_cert", certUri.AbsoluteUri);
                    doc.ReplaceOrAdd("provisioning.attestation.identity_pk", keyUri.AbsoluteUri);
                    break;

                default:
                    doc.ReplaceOrAdd("provisioning.attestation.method", "tpm");
                    break;
                }

                dps.RegistrationId.ForEach(id => { doc.ReplaceOrAdd("provisioning.attestation.registration_id", id); });
            });

            agentImage.ForEach(image =>
            {
                doc.ReplaceOrAdd("agent.config.image", image);
            });

            doc.ReplaceOrAdd("hostname", hostname);

            foreach (RegistryCredentials c in this.credentials)
            {
                doc.ReplaceOrAdd("agent.config.auth.serveraddress", c.Address);
                doc.ReplaceOrAdd("agent.config.auth.username", c.User);
                doc.ReplaceOrAdd("agent.config.auth.password", c.Password);
            }

            doc.ReplaceOrAdd("agent.env.RuntimeLogLevel", runtimeLogLevel.ToString());

            if (this.httpUris.HasValue)
            {
                HttpUris uris = this.httpUris.OrDefault();
                doc.ReplaceOrAdd("connect.management_uri", uris.ConnectManagement);
                doc.ReplaceOrAdd("connect.workload_uri", uris.ConnectWorkload);
                doc.ReplaceOrAdd("listen.management_uri", uris.ListenManagement);
                doc.ReplaceOrAdd("listen.workload_uri", uris.ListenWorkload);
            }
            else
            {
                doc.ReplaceOrAdd("connect.management_uri", "unix:///var/run/iotedge/mgmt.sock");
                doc.ReplaceOrAdd("connect.workload_uri", "unix:///var/run/iotedge/workload.sock");
                doc.ReplaceOrAdd("listen.management_uri", "fd://iotedge.mgmt.socket");
                doc.ReplaceOrAdd("listen.workload_uri", "fd://iotedge.socket");
            }

            if (!string.IsNullOrEmpty(deviceCaCert) && !string.IsNullOrEmpty(deviceCaPk) && !string.IsNullOrEmpty(deviceCaCerts))
            {
                doc.ReplaceOrAdd("certificates.device_ca_cert", deviceCaCert);
                doc.ReplaceOrAdd("certificates.device_ca_pk", deviceCaPk);
                doc.ReplaceOrAdd("certificates.trusted_ca_certs", deviceCaCerts);
            }

            this.proxy.ForEach(proxy => doc.ReplaceOrAdd("agent.env.https_proxy", proxy));

            this.upstreamProtocol.ForEach(upstreamProtocol => doc.ReplaceOrAdd("agent.env.UpstreamProtocol", upstreamProtocol.ToString()));

            string result = doc.ToString();

            FileAttributes attr = 0;

            if (File.Exists(YamlPath))
            {
                attr = File.GetAttributes(YamlPath);
                File.SetAttributes(YamlPath, attr & ~FileAttributes.ReadOnly);
            }

            await File.WriteAllTextAsync(YamlPath, result);

            if (attr != 0)
            {
                File.SetAttributes(YamlPath, attr);
            }
        }