Skip to content

Commit

Permalink
detach associated certificate in thing (#2151)
Browse files Browse the repository at this point in the history
* detach associated certificate in thing

* detach associated certificate in thing

* adding some tests
  • Loading branch information
ssgueye2 authored and kbeaugrand committed Jun 22, 2023
1 parent c20a6c4 commit 65b48a3
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,35 @@ public async Task<UpdateThingResponse> UpdateDevice(UpdateThingRequest device)

public async Task<DeleteThingResponse> DeleteDevice(DeleteThingRequest device)
{
//Retreive all thing princpals and detach it before deleting the thing
var principals = await this.amazonIotClient.ListThingPrincipalsAsync(new ListThingPrincipalsRequest
{
NextToken = string.Empty,
ThingName = device.ThingName
});

if (principals.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
throw new InternalServerErrorException($"Unable to retreive Thing {device.ThingName} principals due to an error in the Amazon IoT API : {principals.HttpStatusCode}");

}

foreach (var principal in principals.Principals)
{
var detachPrincipal = await this.amazonIotClient.DetachThingPrincipalAsync(new DetachThingPrincipalRequest
{
Principal = principal,
ThingName = device.ThingName
});

if (detachPrincipal.HttpStatusCode != System.Net.HttpStatusCode.OK)
{
throw new InternalServerErrorException($"Unable to detach Thing {device.ThingName} principal due to an error in the Amazon IoT API : {detachPrincipal.HttpStatusCode}");

}
}

//Delete the thing type before detaching the princiapl
var deleteResponse = await this.amazonIotClient.DeleteThingAsync(device);

if (deleteResponse.HttpStatusCode != System.Net.HttpStatusCode.OK)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace AzureIoTHub.Portal.Tests.Unit.Infrastructure.Services
{
using System.Collections.Generic;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -253,6 +254,19 @@ public async Task DeleteDeviceShouldReturnAValue()
HttpStatusCode = HttpStatusCode.OK
};

_ = this.mockAmazonIotClient.Setup(iotClient => iotClient.ListThingPrincipalsAsync(It.IsAny<ListThingPrincipalsRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new ListThingPrincipalsResponse
{
HttpStatusCode = HttpStatusCode.OK,
Principals = Fixture.Create<List<string>>()
});

_ = this.mockAmazonIotClient.Setup(iotClient => iotClient.DetachThingPrincipalAsync(It.IsAny<DetachThingPrincipalRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new DetachThingPrincipalResponse
{
HttpStatusCode = HttpStatusCode.OK
});

_ = this.mockAmazonIotClient.Setup(iotClient => iotClient.DeleteThingAsync(It.IsAny<DeleteThingRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(expected);

Expand All @@ -272,9 +286,8 @@ public Task DeleteDeviceShouldThrowInternalServerErrorIfHttpStatusCodeIsNotOK()
{
ThingName = Fixture.Create<string>(),
};

_ = this.mockAmazonIotClient.Setup(iotClient => iotClient.DeleteThingAsync(It.IsAny<DeleteThingRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new DeleteThingResponse
_ = this.mockAmazonIotClient.Setup(iotClient => iotClient.ListThingPrincipalsAsync(It.IsAny<ListThingPrincipalsRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new ListThingPrincipalsResponse
{
HttpStatusCode = HttpStatusCode.BadRequest
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
namespace AzureIoTHub.Portal.Tests.Unit.Infrastructure.Services
{
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Amazon.GreengrassV2;
using Amazon.GreengrassV2.Model;
using Amazon.IoT;
using Amazon.IoT.Model;
using Amazon.SecretsManager;
using Amazon.SecretsManager.Model;
using AutoFixture;
using AutoMapper;
using AzureIoTHub.Portal.Application.Services;
Expand Down Expand Up @@ -378,6 +380,19 @@ public async Task UpdateDeviceShouldThrowNotImplementedException()
_ = await act.Should().ThrowAsync<NotImplementedException>();
}

[Test]
public async Task CreateEdgeDeviceShouldThrowNotImplementedException()
{
// Arrange
var deviceId = Fixture.Create<string>();

// Act
var act = () => this.externalDeviceService.CreateEdgeDevice(deviceId);

// Assert
_ = await act.Should().ThrowAsync<NotImplementedException>();
}

[Test]
public async Task UpdateDeviceTwinShouldThrowNotImplementedException()
{
Expand All @@ -387,5 +402,149 @@ public async Task UpdateDeviceTwinShouldThrowNotImplementedException()
// Assert
_ = await act.Should().ThrowAsync<NotImplementedException>();
}

[Test]
public async Task GetEdgeDeviceCredentialsShouldReturnExisitingDeviceCredentials()
{
// Arrange
var device = Fixture.Create<IoTEdgeDevice>();

_ = this.mockSecretsManager.Setup(c => c.GetSecretValueAsync(It.IsAny<GetSecretValueRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new GetSecretValueResponse());

// Act
var result = this.externalDeviceService.GetEdgeDeviceCredentials(device);

// Assert
_ = result.Should().NotBeNull();

}

[Test]
public async Task GetEdgeDeviceCredentialsShouldCreateAndReturnDeviceCredentials()
{
// Arrange
var device = Fixture.Create<IoTEdgeDevice>();

_ = this.mockSecretsManager.Setup(c => c.GetSecretValueAsync(It.IsAny<GetSecretValueRequest>(), It.IsAny<CancellationToken>()))
.ThrowsAsync(new Amazon.SecretsManager.Model.ResourceNotFoundException("Resource Not found"));


_ = this.mockAmazonIot.Setup(c => c.AttachPolicyAsync(It.IsAny<AttachPolicyRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new AttachPolicyResponse());

_ = this.mockAmazonIot.Setup(c => c.CreateKeysAndCertificateAsync(true, It.IsAny<CancellationToken>()))
.ReturnsAsync(new CreateKeysAndCertificateResponse());

_ = this.mockAmazonIot.Setup(c => c.AttachThingPrincipalAsync(It.IsAny<AttachThingPrincipalRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new AttachThingPrincipalResponse());

_ = this.mockSecretsManager.Setup(c => c.CreateSecretAsync(It.IsAny<CreateSecretRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new CreateSecretResponse());

// Act
var result = this.externalDeviceService.GetEdgeDeviceCredentials(device);

// Assert
_ = result.Should().NotBeNull();

}

[Test]
public async Task GetDeviceCredentialsShouldReturnExisitingDeviceCredentials()
{
// Arrange
var deviceName = Fixture.Create<string>();

_ = this.mockSecretsManager.Setup(c => c.GetSecretValueAsync(It.IsAny<GetSecretValueRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new GetSecretValueResponse());

// Act
var result = this.externalDeviceService.GetDeviceCredentials(deviceName);

// Assert
_ = result.Should().NotBeNull();

}

[Test]
public async Task GetDeviceCredentialsShouldCreateAndReturnDeviceCredentials()
{
// Arrange
var deviceName = Fixture.Create<string>();

_ = this.mockSecretsManager.Setup(c => c.GetSecretValueAsync(It.IsAny<GetSecretValueRequest>(), It.IsAny<CancellationToken>()))
.ThrowsAsync(new Amazon.SecretsManager.Model.ResourceNotFoundException("Resource Not found"));


_ = this.mockAmazonIot.Setup(c => c.AttachPolicyAsync(It.IsAny<AttachPolicyRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new AttachPolicyResponse());

_ = this.mockAmazonIot.Setup(c => c.CreateKeysAndCertificateAsync(true, It.IsAny<CancellationToken>()))
.ReturnsAsync(new CreateKeysAndCertificateResponse());

_ = this.mockAmazonIot.Setup(c => c.AttachThingPrincipalAsync(It.IsAny<AttachThingPrincipalRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new AttachThingPrincipalResponse());

_ = this.mockSecretsManager.Setup(c => c.CreateSecretAsync(It.IsAny<CreateSecretRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new CreateSecretResponse());

// Act
var result = this.externalDeviceService.GetDeviceCredentials(deviceName);

// Assert
_ = result.Should().NotBeNull();
}

[Test]
public async Task RetriveEdgeDeviceLastDeploymentShouldRetuenLastDeployment()
{
//Arrange
var device = Fixture.Create<IoTEdgeDevice>();

_ = this.mockGreengrassV2.Setup(c => c.GetCoreDeviceAsync(It.IsAny<GetCoreDeviceRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new GetCoreDeviceResponse());

// Act
var result = this.externalDeviceService.RetrieveLastConfiguration(device);

// Assert
_ = result.Should().NotBeNull();
}

[Test]
public async Task RemoveDeviceCredentialsShouldRemoveDeviceCredentials()
{
// Arrange
var device = Fixture.Create<IoTEdgeDevice>();

_ = this.mockAmazonIot.Setup(c => c.ListThingPrincipalsAsync(It.IsAny<ListThingPrincipalsRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new ListThingPrincipalsResponse
{
Principals = Fixture.Create<List<string>>()
});

_ = this.mockAmazonIot.Setup(c => c.DetachPolicyAsync(It.IsAny<DetachPolicyRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new DetachPolicyResponse());

_ = this.mockAmazonIot.Setup(c => c.DetachThingPrincipalAsync(It.IsAny<DetachThingPrincipalRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new DetachThingPrincipalResponse());

_ = this.mockSecretsManager.Setup(c => c.DeleteSecretAsync(It.IsAny<DeleteSecretRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new DeleteSecretResponse());

_ = this.mockAmazonIot.Setup(c => c.UpdateCertificateAsync(It.IsAny<UpdateCertificateRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new UpdateCertificateResponse());

_ = this.mockAmazonIot.Setup(c => c.DeleteCertificateAsync(It.IsAny<DeleteCertificateRequest>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new DeleteCertificateResponse());

// Act
var result = this.externalDeviceService.RemoveDeviceCredentials(device);

// Assert
_ = result.Should().NotBeNull();
}

}
}

0 comments on commit 65b48a3

Please sign in to comment.