Exemple #1
0
        public static async Task <Option <EdgeDevice> > GetIdentityAsync(
            string deviceId,
            IotHub iotHub,
            CancellationToken token,
            bool takeOwnership = false,
            NestedEdgeConfig nestedEdgeConfig = default(NestedEdgeConfig))
        {
            Device device = await iotHub.GetDeviceIdentityAsync(deviceId, token);

            if (device != null)
            {
                if (nestedEdgeConfig != default(NestedEdgeConfig))
                {
                    await nestedEdgeConfig.ParentDeviceId.ForEachAsync(async p =>
                    {
                        Device parentDevice = await iotHub.GetDeviceIdentityAsync(p, token);
                        device.ParentScopes.Add(parentDevice.Scope);
                    });
                }

                if (!device.Capabilities.IotEdge)
                {
                    throw new InvalidOperationException($"Device '{device.Id}' exists, but is not an edge device");
                }

                return(Option.Some(new EdgeDevice(device, takeOwnership, iotHub, nestedEdgeConfig)));
            }
            else
            {
                return(Option.None <EdgeDevice>());
            }
        }
Exemple #2
0
        public static async Task <EdgeDevice> GetOrCreateIdentityAsync(
            string deviceId,
            IotHub iotHub,
            CancellationToken token)
        {
            Device device = await iotHub.GetDeviceIdentityAsync(deviceId, token);

            if (device != null)
            {
                if (!device.Capabilities.IotEdge)
                {
                    throw new InvalidOperationException($"Device '{device.Id}' exists, but is not an edge device");
                }

                Log.Information(
                    "Device '{Device}' already exists on hub '{IotHub}'",
                    device.Id,
                    iotHub.Hostname);
                return(new EdgeDevice(device, false, iotHub));
            }
            else
            {
                return(await CreateIdentityAsync(deviceId, iotHub, token));
            }
        }
Exemple #3
0
 EdgeDevice(Device device, bool owned, IotHub iotHub, NestedEdgeConfig nestedEdgeConfig)
 {
     this.device           = device;
     this.iotHub           = iotHub;
     this.owned            = owned;
     this.nestedEdgeConfig = nestedEdgeConfig;
 }
Exemple #4
0
 LeafDevice(Device device, DeviceClient client, IotHub iotHub)
 {
     this.client    = client;
     this.device    = device;
     this.iotHub    = iotHub;
     this.messageId = Guid.NewGuid().ToString();
 }
Exemple #5
0
        public EdgeRuntime(string deviceId, Option <string> agentImage, Option <string> hubImage, Option <Uri> proxy, IEnumerable <Registry> registries, bool optimizeForPerformance, IotHub iotHub)
        {
            this.agentImage             = agentImage;
            this.hubImage               = hubImage;
            this.iotHub                 = iotHub;
            this.optimizeForPerformance = optimizeForPerformance;
            this.proxy      = proxy;
            this.registries = registries;

            this.DeviceId = deviceId;
        }
Exemple #6
0
        static async Task <LeafDevice> CreateWithSasAsync(
            string leafDeviceId,
            Option <string> parentId,
            IotHub iotHub,
            ITransportSettings transport,
            string edgeHostname,
            CancellationToken token,
            ClientOptions options,
            bool nestedEdge)
        {
            Device leaf = new Device(leafDeviceId)
            {
                Authentication = new AuthenticationMechanism
                {
                    Type = AuthenticationType.Sas
                }
            };

            await parentId.ForEachAsync(
                async p =>
            {
                Device edge = await GetEdgeDeviceIdentityAsync(p, iotHub, token);
                leaf.Scope  = edge.Scope;
            });

            // @To Remove this is a hack to be able to create lea. See PBI: 9171870
            string hostname = iotHub.Hostname;

            if (nestedEdge)
            {
                hostname = edgeHostname;
            }

            leaf = await iotHub.CreateDeviceIdentityAsync(leaf, token);

            return(await DeleteIdentityIfFailedAsync(
                       leaf,
                       iotHub,
                       token,
                       () =>
            {
                string connectionString =
                    $"HostName={hostname};" +
                    $"DeviceId={leaf.Id};" +
                    $"SharedAccessKey={leaf.Authentication.SymmetricKey.PrimaryKey};" +
                    $"GatewayHostName={edgeHostname}";

                return CreateLeafDeviceAsync(
                    leaf,
                    () => DeviceClient.CreateFromConnectionString(connectionString, new[] { transport }, options),
                    iotHub,
                    token);
            }));
        }
Exemple #7
0
 public static Task <EdgeDevice> CreateIdentityAsync(
     string deviceId,
     IotHub iotHub,
     CancellationToken token)
 {
     return(Profiler.Run(
                $"Creating edge device '{deviceId}' on hub '{iotHub.Hostname}'",
                async() =>
     {
         Device device = await iotHub.CreateEdgeDeviceIdentityAsync(deviceId, token);
         return new EdgeDevice(device, true, iotHub);
     }));
 }
Exemple #8
0
        static async Task <LeafDevice> CreateWithCaCertAsync(
            string leafDeviceId,
            string parentId,
            CertificateAuthority ca,
            IotHub iotHub,
            ITransportSettings transport,
            string edgeHostname,
            CancellationToken token,
            ClientOptions options)
        {
            Device edge = await GetEdgeDeviceIdentityAsync(parentId, iotHub, token);

            Device leaf = new Device(leafDeviceId)
            {
                Authentication = new AuthenticationMechanism
                {
                    Type = AuthenticationType.CertificateAuthority
                },
                Scope = edge.Scope
            };

            leaf = await iotHub.CreateDeviceIdentityAsync(leaf, token);

            return(await DeleteIdentityIfFailedAsync(
                       leaf,
                       iotHub,
                       token,
                       async() =>
            {
                IdCertificates certFiles = await ca.GenerateIdentityCertificatesAsync(leafDeviceId, token);

                (X509Certificate2 leafCert, IEnumerable <X509Certificate2> trustedCerts) =
                    CertificateHelper.GetServerCertificateAndChainFromFile(certFiles.CertificatePath, certFiles.KeyPath);
                // .NET runtime requires that we install the chain of CA certs, otherwise it can't
                // provide them to a server during authentication.
                OsPlatform.Current.InstallTrustedCertificates(trustedCerts);

                return await CreateLeafDeviceAsync(
                    leaf,
                    () => DeviceClient.Create(
                        iotHub.Hostname,
                        edgeHostname,
                        new DeviceAuthenticationWithX509Certificate(leaf.Id, leafCert),
                        new[] { transport },
                        options),
                    iotHub,
                    token);
            }));
Exemple #9
0
            public NestedEdgeConfig(IotHub iotHub, bool isNestedEdge, Option <string> parentDeviceId, Option <string> parentHostname, Option <string> deviceHostname)
            {
                this.IsNestedEdge = isNestedEdge;

                // Since the Nested Edge Identity of the Edge Device is not managed by the E2E test framework,
                // the value needs be passed in from the pipeline. Thus, it cannot be empty.
                if (this.IsNestedEdge)
                {
                    parentDeviceId.Expect <ArgumentException>(() => throw new ArgumentException($"Expected {nameof(parentDeviceId)} for the Nested Edge"));
                }

                this.ParentDeviceId = parentDeviceId;

                this.ParentHostname = parentHostname.GetOrElse(iotHub.Hostname);

                this.DeviceHostname = deviceHostname.GetOrElse(Dns.GetHostName().ToLower());
            }
Exemple #10
0
        public static async Task <EdgeDevice> GetOrCreateIdentityAsync(
            string deviceId,
            IotHub iotHub,
            CancellationToken token)
        {
            Option <EdgeDevice> device = await GetIdentityAsync(deviceId, iotHub, token);

            return(await device.Match(
                       d =>
            {
                Log.Information(
                    "Device '{Device}' already exists on hub '{IotHub}'",
                    d.Id,
                    iotHub.Hostname);
                return Task.FromResult(d);
            },
                       () => CreateIdentityAsync(deviceId, iotHub, token)));
        }
        public static async Task <Option <EdgeDevice> > GetIdentityAsync(
            string deviceId,
            IotHub iotHub,
            CancellationToken token)
        {
            Device device = await iotHub.GetDeviceIdentityAsync(deviceId, token);

            if (device != null)
            {
                if (!device.Capabilities.IotEdge)
                {
                    throw new InvalidOperationException($"Device '{device.Id}' exists, but is not an edge device");
                }

                return(Option.Some(new EdgeDevice(device, false, iotHub)));
            }
            else
            {
                return(Option.None <EdgeDevice>());
            }
        }
Exemple #12
0
        public static Task <EdgeDevice> CreateIdentityAsync(
            string deviceId,
            IotHub iotHub,
            AuthenticationType authType,
            X509Thumbprint x509Thumbprint,
            CancellationToken token)
        {
            if (authType == AuthenticationType.SelfSigned && x509Thumbprint == null)
            {
                throw new ArgumentException("A device created with self-signed mechanism must provide an x509 thumbprint.");
            }

            return(Profiler.Run(
                       async() =>
            {
                Device device = await iotHub.CreateEdgeDeviceIdentityAsync(deviceId, authType, x509Thumbprint, token);
                return new EdgeDevice(device, true, iotHub);
            },
                       "Created edge device '{Device}' on hub '{IotHub}'",
                       deviceId,
                       iotHub.Hostname));
        }
Exemple #13
0
        public static async Task <EdgeDevice> GetOrCreateIdentityAsync(
            string deviceId,
            NestedEdgeConfig nestedEdgeConfig,
            IotHub iotHub,
            AuthenticationType authType,
            X509Thumbprint x509Thumbprint,
            CancellationToken token)
        {
            Option <EdgeDevice> device = await GetIdentityAsync(deviceId, iotHub, token, nestedEdgeConfig : nestedEdgeConfig);

            EdgeDevice edgeDevice = await device.Match(
                d =>
            {
                Log.Information(
                    "Device '{Device}' already exists on hub '{IotHub}'",
                    d.Id,
                    iotHub.Hostname);
                return(Task.FromResult(d));
            },
                () => CreateIdentityAsync(deviceId, nestedEdgeConfig, iotHub, authType, x509Thumbprint, token));

            return(edgeDevice);
        }
 public ModuleTwin(string moduleId, string deviceId, IotHub iotHub)
 {
     this.deviceId = deviceId;
     this.iotHub   = iotHub;
     this.moduleId = moduleId;
 }
Exemple #15
0
        public static Task <LeafDevice> CreateAsync(
            string leafDeviceId,
            Protocol protocol,
            AuthenticationType auth,
            Option <string> parentId,
            bool useSecondaryCertificate,
            CertificateAuthority ca,
            IotHub iotHub,
            CancellationToken token)
        {
            return(Profiler.Run(
                       async() =>
            {
                ITransportSettings transport = protocol.ToTransportSettings();
                OsPlatform.Current.InstallEdgeCertificates(ca.Certificates.TrustedCertificates, transport);

                string edgeHostname = Dns.GetHostName().ToLower();

                switch (auth)
                {
                case AuthenticationType.Sas:
                    return await CreateWithSasAsync(
                        leafDeviceId,
                        parentId,
                        iotHub,
                        transport,
                        edgeHostname,
                        token);

                case AuthenticationType.CertificateAuthority:
                    {
                        string p = parentId.Expect(() => new ArgumentException());
                        return await CreateWithCaCertAsync(
                            leafDeviceId,
                            p,
                            ca,
                            iotHub,
                            transport,
                            edgeHostname,
                            token);
                    }

                case AuthenticationType.SelfSigned:
                    {
                        string p = parentId.Expect(() => new ArgumentException());
                        return await CreateWithSelfSignedCertAsync(
                            leafDeviceId,
                            p,
                            useSecondaryCertificate,
                            ca,
                            iotHub,
                            transport,
                            edgeHostname,
                            token);
                    }

                default:
                    throw new InvalidEnumArgumentException();
                }
            },
                       "Created leaf device '{Device}' on hub '{IotHub}'",
                       leafDeviceId,
                       iotHub.Hostname));
        }
Exemple #16
0
 public EdgeConfiguration(string deviceId, string agentImage, IotHub iotHub)
 {
     this.config   = GetBaseConfig(agentImage);
     this.iotHub   = iotHub;
     this.deviceId = deviceId;
 }
Exemple #17
0
 public EdgeModule(string id, string deviceId, IotHub iotHub)
 {
     this.deviceId = deviceId;
     this.Id       = id;
     this.iotHub   = iotHub;
 }
Exemple #18
0
 public EdgeAgent(string deviceId, IotHub iotHub)
     : base("edgeAgent", deviceId, iotHub)
 {
 }
Exemple #19
0
 EdgeDevice(Device device, bool owned, IotHub iotHub)
 {
     this.device = device;
     this.iotHub = iotHub;
     this.owned  = owned;
 }
Exemple #20
0
        public static Task <LeafDevice> CreateAsync(
            string leafDeviceId,
            Protocol protocol,
            AuthenticationType auth,
            Option <string> parentId,
            bool useSecondaryCertificate,
            CertificateAuthority ca,
            IotHub iotHub,
            string edgeHostname,
            CancellationToken token,
            Option <string> modelId,
            bool nestedEdge)
        {
            ClientOptions options = new ClientOptions();

            modelId.ForEach(m => options.ModelId = m);
            return(Profiler.Run(
                       async() =>
            {
                ITransportSettings transport = protocol.ToTransportSettings();
                OsPlatform.Current.InstallCaCertificates(ca.EdgeCertificates.TrustedCertificates, transport);

                switch (auth)
                {
                case AuthenticationType.Sas:
                    return await CreateWithSasAsync(
                        leafDeviceId,
                        parentId,
                        iotHub,
                        transport,
                        edgeHostname,
                        token,
                        options,
                        nestedEdge);

                case AuthenticationType.CertificateAuthority:
                    {
                        string p = parentId.Expect(() => new ArgumentException("Missing parent ID"));
                        return await CreateWithCaCertAsync(
                            leafDeviceId,
                            p,
                            ca,
                            iotHub,
                            transport,
                            edgeHostname,
                            token,
                            options);
                    }

                case AuthenticationType.SelfSigned:
                    {
                        string p = parentId.Expect(() => new ArgumentException("Missing parent ID"));
                        return await CreateWithSelfSignedCertAsync(
                            leafDeviceId,
                            p,
                            useSecondaryCertificate,
                            ca,
                            iotHub,
                            transport,
                            edgeHostname,
                            token,
                            options);
                    }

                default:
                    throw new InvalidEnumArgumentException();
                }
            },
                       "Created leaf device '{Device}' on hub '{IotHub}'",
                       leafDeviceId,
                       iotHub.Hostname));
        }
Exemple #21
0
 public EdgeAgent(string deviceId, IotHub iotHub)
     : base(ModuleName.EdgeAgent, deviceId, iotHub)
 {
 }