Skip to content

Commit

Permalink
Adding common test configuration.
Browse files Browse the repository at this point in the history
Adding Code of conduct.
E2E tests for provisioning client.
Adding logging for both prod and test code.
Updating API
Removing Dice
Changing HSM API to sync.
Fixing Android sample.
Adding SecurityClientX509
Adding ProvisioningTransportHandle delegating pipeline.
Refactoring HTTP and AMQP code.
Adding chain CertificateInstaller to support Group Enrollment.
TPM, X509 samples
  • Loading branch information
CIPop committed Nov 8, 2017
1 parent f34f291 commit 9655511
Show file tree
Hide file tree
Showing 113 changed files with 3,847 additions and 1,283 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ x64/
*.pidb
*.log
*.scc
*.bak

# Visual C++ cache files
ipch/
Expand Down Expand Up @@ -126,6 +127,7 @@ ClientBin/
*.dbmdl
*.[Pp]ublish.xml
*.pfx
*.cer
*.publishsettings
*.jar

Expand Down
1 change: 1 addition & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
2 changes: 0 additions & 2 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ try {
BuildProject provisioning\transport\http "Provisioning Transport for HTTP"
BuildProject provisioning\transport\mqtt "Provisioning Transport for MQTT"

BuildProject security\dice "SecurityClient for DICE"
BuildProject security\tpm "SecurityClient for TPM"
}

Expand All @@ -147,7 +146,6 @@ try {
RunTests provisioning\transport\http\tests "Provisioning Transport for HTTP"
RunTests provisioning\transport\mqtt\tests "Provisioning Transport for MQTT"

RunTests security\dice\tests "SecurityClient for DICE"
RunTests security\tpm\tests "SecurityClient for TPM"
}
}
Expand Down
640 changes: 640 additions & 0 deletions common/src/Logging.Common.cs

Large diffs are not rendered by default.

121 changes: 121 additions & 0 deletions common/src/device/provisioning/Logging.ProvisioningDeviceClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Diagnostics.Tracing;

namespace Microsoft.Azure.Devices.Shared
{
[EventSource(Name = "Microsoft-Azure-Devices-Provisioning-Client")]
internal sealed partial class Logging : EventSource
{
[NonEvent]
public static void RegisterAsync(
object thisOrContextObject,
string globalDeviceEndpoint,
string idScope,
object transport,
object security)
{
DebugValidateArg(thisOrContextObject);
DebugValidateArg(security);
DebugValidateArg(transport);

if (IsEnabled) Log.RegisterAsync(
IdOf(thisOrContextObject),
globalDeviceEndpoint,
idScope,
IdOf(transport),
IdOf(security));
}

[Event(RegisterAsyncId, Keywords = Keywords.Default, Level = EventLevel.Informational)]
private void RegisterAsync(
string thisOrContextObject,
string globalDeviceEndpoint,
string idScope,
string transport,
string security) =>
WriteEvent(RegisterAsyncId, thisOrContextObject, globalDeviceEndpoint, idScope, transport, security);

[NonEvent]
public static void RegisterDevice(
object thisOrContextObject,
string registrationId,
string idScope,
string attestationType,
string operationId,
TimeSpan? retryAfter,
string status)
{
DebugValidateArg(thisOrContextObject);
DebugValidateArg(attestationType);
DebugValidateArg(retryAfter);
if (IsEnabled) Log.RegisterDevice(
IdOf(thisOrContextObject),
registrationId,
idScope,
attestationType,
operationId,
(int)(retryAfter?.TotalSeconds),
status);
}

[Event(RegisterDeviceId, Keywords = Keywords.Default, Level = EventLevel.Informational)]
private void RegisterDevice(
string thisOrContextObject,
string registrationId,
string idScope,
string attestationType,
string operationId,
int retryAfterSeconds,
string status) =>
WriteEvent(
RegisterDeviceId,
thisOrContextObject,
registrationId,
idScope,
attestationType,
operationId,
retryAfterSeconds,
status);

[NonEvent]
public static void OperationStatusLookup(
object thisOrContextObject,
string registrationId,
string operationId,
TimeSpan? retryAfter,
string status,
int attempts)
{
DebugValidateArg(thisOrContextObject);
DebugValidateArg(retryAfter);

if (IsEnabled) Log.OperationStatusLookup(
IdOf(thisOrContextObject),
registrationId,
operationId,
(int)(retryAfter?.TotalSeconds),
status,
attempts);
}

[Event(OperationStatusLookupId, Keywords = Keywords.Default, Level = EventLevel.Informational)]
private void OperationStatusLookup(
string thisOrContextObject,
string registrationId,
string operationId,
int retryAfterSeconds,
string status,
int attempts) =>
WriteEvent(
OperationStatusLookupId,
thisOrContextObject,
registrationId,
operationId,
retryAfterSeconds,
status,
attempts);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ namespace Microsoft.Azure.Devices.Provisioning.Client
internal class ClientApiVersionHelper
{
public const string ApiVersionName = "api-version";
public const string ApiVersion = "2017-08-31-preview";

public const string ClientVersionName = "client-version";
public const string ClientVersion = "test-client-version";
// TODO: Change this to GA version when the service is ready.
public const string ApiVersion = "2017-08-31-preview";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System;
using System.Diagnostics.CodeAnalysis;

namespace Microsoft.Azure.Devices.Provisioning.Client.Transport.Http.Models
namespace Microsoft.Azure.Devices.Provisioning.Client.Transport.Models
{
/// <summary>
/// Device registration result.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Newtonsoft.Json;
using System;
using System.Diagnostics.CodeAnalysis;

namespace Microsoft.Azure.Devices.Provisioning.Client.Transport.Http.Models
namespace Microsoft.Azure.Devices.Provisioning.Client.Transport.Models
{
/// <summary>
/// Registration operation status.
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1812", Justification = "Used by the JSon parser.")]
internal partial class RegistrationOperationStatus
{
public const string OperationStatusAssigned = "assigned";
public const string OperationStatusAssigning = "assigning";
public const string OperationStatusUnassigned = "unassigned";

/// <summary>
/// Initializes a new instance of the RegistrationOperationStatus
/// class.
Expand Down Expand Up @@ -66,5 +71,9 @@ public RegistrationOperationStatus(
[JsonProperty(PropertyName = "registrationStatus")]
public DeviceRegistrationResult RegistrationStatus { get; set; }

/// <summary>
/// Gets or sets the Retry-After header.
/// </summary>
public TimeSpan? RetryAfter { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Newtonsoft.Json;
using System.Diagnostics.CodeAnalysis;

namespace Microsoft.Azure.Devices.Provisioning.Client.Transport.Http.Models
namespace Microsoft.Azure.Devices.Provisioning.Client.Transport.Models
{
/// <summary>
/// TPM registration result.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System;
using System.Diagnostics.CodeAnalysis;

namespace Microsoft.Azure.Devices.Provisioning.Client.Transport.Http.Models
namespace Microsoft.Azure.Devices.Provisioning.Client.Transport.Models
{
/// <summary>
/// X509 certificate info.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Newtonsoft.Json;
using System.Diagnostics.CodeAnalysis;

namespace Microsoft.Azure.Devices.Provisioning.Client.Transport.Http.Models
namespace Microsoft.Azure.Devices.Provisioning.Client.Transport.Models
{
/// <summary>
/// X509 registration result.
Expand Down
21 changes: 21 additions & 0 deletions common/test/Configuration.IoTHub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Security.Cryptography.X509Certificates;

namespace Microsoft.Azure.Devices.E2ETests
{
public partial class Configuration
{
public static partial class IoTHub
{
public static string ConnectionString => GetValue("IOTHUB_CONN_STRING_CSHARP");

public static string ConsumerGroup => GetValue("IOTHUB_EVENTHUB_CONSUMER_GROUP", "$Default");

public static X509Certificate2 GetCertificateWithPrivateKey()
=> GetBase64EncodedCertificate("IOTHUB_X509_PFX_CERTIFICATE");
}
}
}
35 changes: 35 additions & 0 deletions common/test/Configuration.Provisioning.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Security.Cryptography.X509Certificates;

namespace Microsoft.Azure.Devices.E2ETests
{
public partial class Configuration
{
public static partial class Provisioning
{
public const string CertificatePassword = "testcertificate";

public static string GlobalDeviceEndpoint =>
GetValue("DPS_GLOBALDEVICEENDPOINT", "global.azure-devices-provisioning.net");

public static string IdScope => GetValue("DPS_IDSCOPE");

public static string TpmDeviceRegistrationId => GetValue("DPS_TPM_REGISTRATIONID");

public static string TpmDeviceId => GetValue("DPS_TPM_DEVICEID");

// To generate use Powershell: [System.Convert]::ToBase64String( (Get-Content .\certificate.pfx -Encoding Byte) )
public static X509Certificate2 GetIndividualEnrollmentCertificate()
=> GetBase64EncodedCertificate("DPS_INDIVIDUALX509_PFX_CERTIFICATE", CertificatePassword);

public static X509Certificate2 GetGroupEnrollmentCertificate()
=> GetBase64EncodedCertificate("DPS_GROUPX509_PFX_CERTIFICATE", CertificatePassword);

public static X509Certificate2Collection GetGroupEnrollmentChain()
=> GetBase64EncodedCertificateCollection("DPS_GROUPX509_CERTIFICATE_CHAIN");
}
}
}
76 changes: 76 additions & 0 deletions common/test/Configuration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Security.Cryptography.X509Certificates;

namespace Microsoft.Azure.Devices.E2ETests
{
public partial class Configuration
{
private static string GetValue(string envName, string defaultValue=null)
{
string envValue = Environment.GetEnvironmentVariable(envName);

if (string.IsNullOrWhiteSpace(envValue))
{
return defaultValue;
}

return Environment.ExpandEnvironmentVariables(envValue);
}

private static Uri GetUriValue(string envName, Uri defaultValue=null)
{
string envValue = GetValue(envName, null);

if (envValue == null)
{
return defaultValue;
}

return new Uri(envValue);
}

// To generate environment variables value use
// [Convert]::ToBase64String((Get-Content myFileName -Encoding Byte)).

private static X509Certificate2 GetBase64EncodedCertificate(string envName, string password=null, string defaultValue=null)
{
string certBase64 = GetValue(envName, null);

if (certBase64 == null)
{
certBase64 = defaultValue;
}

Byte[] buff = Convert.FromBase64String(certBase64);

if (password == null)
{
return new X509Certificate2(buff);
}
else
{
return new X509Certificate2(buff, password);
}
}

private static X509Certificate2Collection GetBase64EncodedCertificateCollection(
string envName,
string defaultValue = null)
{
string certBase64 = GetValue(envName, null);

if (certBase64 == null)
{
certBase64 = defaultValue;
}

Byte[] buff = Convert.FromBase64String(certBase64);
var collection = new X509Certificate2Collection();
collection.Import(buff);
return collection;
}
}
}
Loading

0 comments on commit 9655511

Please sign in to comment.