Skip to content

Commit

Permalink
Merged PR 797501: Set env variables to edgelet uri and use DeviceClie…
Browse files Browse the repository at this point in the history
…ntFactory in EdgeHub

- Set env variables to edgelet details
- Use DeviceClientFactory to create EdgeHub connection
  • Loading branch information
ancaantochi committed May 2, 2018
1 parent 1ebb45f commit a9b99dd
Show file tree
Hide file tree
Showing 50 changed files with 749 additions and 379 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Microsoft. All rights reserved.

namespace Microsoft.Azure.Devices.Edge.Agent.Core
{
public class ConnectionStringCredentials : ICredentials
{
public ConnectionStringCredentials(string connectionString)
{
this.CredentialsType = CredentialType.ConnectionString;
this.ConnectionString = connectionString;
}

public string ConnectionString { get; }

public CredentialType CredentialsType { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ public static class Constants

public const string ModuleIdentityEdgeManagedByValue = "IotEdge";

public const string EdgeletUriVariableName = "IOTEDGE_IOTEDGEDURI";

public const string EdgeletVersionVariableName = "IOTEDGE_IOTEDGEDVERSION";

public const string IotHubHostnameVariableName = "IOTEDGE_IOTHUBHOSTNAME";

public const string GatewayHostnameVariableName = "IOTEDGE_GATEWAYHOSTNAME";

public const string DeviceIdVariableName = "IOTEDGE_DEVICEID";

public const string ModuleIdVariableName = "IOTEDGE_MODULEID";

public const string EdgeletAuthSchemeVariableName = "IOTEDGE_AUTHSCHEME";

public static class Labels
{
public const string Version = "net.azure-devices.edge.version";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Microsoft. All rights reserved.

namespace Microsoft.Azure.Devices.Edge.Agent.Core
{
public enum CredentialType
{
None,
ConnectionString,
IdentityProviderService
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) Microsoft. All rights reserved.

namespace Microsoft.Azure.Devices.Edge.Agent.Core
{
public interface ICredentials
{
CredentialType CredentialsType { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

namespace Microsoft.Azure.Devices.Edge.Agent.Core
{
using System;

public interface IModuleIdentity : IEquatable<IModuleIdentity>
public interface IModuleIdentity
{
string Name { get; }
string IotHubHostname { get; }

string GatewayHostname { get; }

string DeviceId { get; }

string ModuleId { get; }

string ConnectionString { get; }
ICredentials Credentials { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft. All rights reserved.

namespace Microsoft.Azure.Devices.Edge.Agent.Core
{
using Microsoft.Azure.Devices.Edge.Util;

public class IdentityProviderServiceCredentials : ICredentials
{
const string DefaultAuthScheme = "sasToken";

public IdentityProviderServiceCredentials(string providerUri) : this(providerUri, DefaultAuthScheme)
{
}

public IdentityProviderServiceCredentials(string providerUri, string authScheme) : this(providerUri, authScheme, Option.None<string>())
{
}

public IdentityProviderServiceCredentials(string providerUri, string authScheme, Option<string> providerVersion)
{
this.CredentialsType = CredentialType.IdentityProviderService;
this.ProviderUri = Preconditions.CheckNonWhiteSpace(providerUri, nameof(providerUri));
this.AuthScheme = Preconditions.CheckNonWhiteSpace(authScheme, nameof(authScheme));
this.Version = Preconditions.CheckNotNull(providerVersion, nameof(providerVersion));
}

public string ProviderUri { get; }

public Option<string> Version { get; }

public string AuthScheme { get; }

public CredentialType CredentialsType { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright (c) Microsoft. All rights reserved.

namespace Microsoft.Azure.Devices.Edge.Agent.Core
{
using System;
using System.Text;
using Microsoft.Azure.Devices.Edge.Util;

public class ModuleConnectionString
{
const string HostNamePropertyName = "HostName";
const string GatewayHostNamePropertyName = "GatewayHostName";
const string DeviceIdPropertyName = "DeviceId";
const string ModuleIdPropertyname = "ModuleId";
const string SharedAccessKeyPropertyName = "SharedAccessKey";
const char ValuePairDelimiter = ';';

readonly string sasKey;

ModuleConnectionString(string iotHubHostName, string gatewayHostName, string deviceId, string moduleId, string sasKey)
{
this.IotHubHostName = Preconditions.CheckNonWhiteSpace(iotHubHostName, nameof(iotHubHostName));
this.GatewayHostName = gatewayHostName;
this.DeviceId = Preconditions.CheckNonWhiteSpace(deviceId, nameof(deviceId));
this.ModuleId = Preconditions.CheckNonWhiteSpace(moduleId, nameof(moduleId));
this.sasKey = sasKey;
}

public string IotHubHostName { get; }

public string DeviceId { get; }

public string ModuleId { get; }

public string GatewayHostName { get; }

public override string ToString()
{
if (string.IsNullOrEmpty(this.ModuleId))
{
throw new ArgumentException("Required parameter moduleId has not been set");
}

var connectionString = new StringBuilder();
this.AppendIfNotEmpty(connectionString, HostNamePropertyName, this.IotHubHostName);
this.AppendIfNotEmpty(connectionString, DeviceIdPropertyName, this.DeviceId);
this.AppendIfNotEmpty(connectionString, ModuleIdPropertyname, this.ModuleId);
this.AppendIfNotEmpty(connectionString, SharedAccessKeyPropertyName, this.sasKey);
this.AppendIfNotEmpty(connectionString, GatewayHostNamePropertyName, this.GatewayHostName);
return connectionString.ToString();
}

void AppendIfNotEmpty(StringBuilder stringBuilder, string propertyName, string propertyValue)
{
if (!string.IsNullOrEmpty(propertyValue))
{
if (stringBuilder.Length > 0)
{
stringBuilder.Append(ValuePairDelimiter);
}

stringBuilder.Append($"{propertyName}={propertyValue}");
}
}

public static implicit operator string(ModuleConnectionString moduleConnectionStringBuilder) => moduleConnectionStringBuilder.ToString();

public class ModuleConnectionStringBuilder
{
readonly string iotHubHostName;
readonly string deviceId;
string moduleId;
string gatewayHostname;
string sasKey;

public ModuleConnectionStringBuilder(string iotHubHostName, string deviceId)
{
this.iotHubHostName = Preconditions.CheckNonWhiteSpace(iotHubHostName, nameof(iotHubHostName));
this.deviceId = Preconditions.CheckNonWhiteSpace(deviceId, nameof(deviceId));
}

public ModuleConnectionStringBuilder WithModuleId(string moduleId)
{
this.moduleId = Preconditions.CheckNonWhiteSpace(moduleId, nameof(moduleId));
return this;
}

public ModuleConnectionStringBuilder WithGatewayHostName(string gatewayHostName)
{
this.gatewayHostname = Preconditions.CheckNonWhiteSpace(gatewayHostName, nameof(gatewayHostName));
return this;
}

public ModuleConnectionStringBuilder WithSharedAccessKey(string sasKey)
{
this.sasKey = Preconditions.CheckNonWhiteSpace(sasKey, nameof(sasKey));
return this;
}

public ModuleConnectionString Build() => new ModuleConnectionString(this.iotHubHostName, this.gatewayHostname, this.deviceId, this.moduleId, this.sasKey);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,23 @@ namespace Microsoft.Azure.Devices.Edge.Agent.Core

public class ModuleIdentity : IModuleIdentity
{
public ModuleIdentity(string moduleId, string connectionString)
public ModuleIdentity(string iotHubHostname, string gatewayHostname, string deviceId, string moduleId, ICredentials credentials)
{
this.ConnectionString = Preconditions.CheckNotNull(connectionString, nameof(connectionString));
this.Name = Preconditions.CheckNotNull(moduleId, nameof(moduleId));
this.IotHubHostname = Preconditions.CheckNonWhiteSpace(iotHubHostname, nameof(this.IotHubHostname));
this.GatewayHostname = gatewayHostname;
this.DeviceId = Preconditions.CheckNonWhiteSpace(deviceId, nameof(this.DeviceId));
this.ModuleId = Preconditions.CheckNonWhiteSpace(moduleId, nameof(this.ModuleId));
this.Credentials = Preconditions.CheckNotNull(credentials, nameof(this.Credentials));
}

public string Name { get; }
public string IotHubHostname { get; }

public string ConnectionString { get; }
public string GatewayHostname { get; }

public override bool Equals(object obj) => this.Equals(obj as ModuleIdentity);
public string DeviceId { get; }

public bool Equals(IModuleIdentity other)
{
if (ReferenceEquals(null, other))
return false;
if (ReferenceEquals(this, other))
return true;

return string.Equals(this.Name, other.Name)
&& string.Equals(this.ConnectionString, other.ConnectionString);
}
public string ModuleId { get; }

public override int GetHashCode()
{
unchecked
{
return ((this.Name != null ? this.Name.GetHashCode() : 0) * 397) ^ (this.ConnectionString != null ? this.ConnectionString.GetHashCode() : 0);
}
}
public ICredentials Credentials { get; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) Microsoft. All rights reserved.

namespace Microsoft.Azure.Devices.Edge.Agent.Core
{
using Microsoft.Azure.Devices.Edge.Util;

public class ModuleIdentityProviderServiceBuilder
{
readonly string iotHubHostName;
readonly string deviceId;
readonly string gatewayHostname;

public ModuleIdentityProviderServiceBuilder(string iotHubHostName, string deviceId, string gatewayHostname)
{
this.iotHubHostName = Preconditions.CheckNonWhiteSpace(iotHubHostName, nameof(iotHubHostName));
this.deviceId = Preconditions.CheckNonWhiteSpace(deviceId, nameof(deviceId));
this.gatewayHostname = gatewayHostname;
}

public IModuleIdentity Create(string moduleId, string providerUri)
{
Preconditions.CheckNonWhiteSpace(moduleId, nameof(moduleId));
Preconditions.CheckNonWhiteSpace(providerUri, nameof(providerUri));

ICredentials credentials = new IdentityProviderServiceCredentials(providerUri);
return new ModuleIdentity(this.iotHubHostName, this.gatewayHostname, this.deviceId, moduleId, credentials);
}

public IModuleIdentity Create(string moduleId, string providerUri, string authScheme)
{
Preconditions.CheckNonWhiteSpace(moduleId, nameof(moduleId));
Preconditions.CheckNonWhiteSpace(providerUri, nameof(providerUri));
Preconditions.CheckNonWhiteSpace(authScheme, nameof(authScheme));

ICredentials credentials = new IdentityProviderServiceCredentials(providerUri, authScheme);
return new ModuleIdentity(this.iotHubHostName, this.gatewayHostname, this.deviceId, moduleId, credentials);
}
}
}
Loading

0 comments on commit a9b99dd

Please sign in to comment.