public async Task ListModulesAsync( [FromRoute] string deviceId) { try { Events.ReceivedRequest(nameof(this.ListModulesAsync), deviceId); try { deviceId = WebUtility.UrlDecode(Preconditions.CheckNonWhiteSpace(deviceId, nameof(deviceId))); } catch (Exception ex) { Events.BadRequest(nameof(this.ListModulesAsync), ex.Message); await this.SendResponseAsync(HttpStatusCode.BadRequest, FormatErrorResponseMessage(ex.Message)); return; } if (!await this.AuthenticateAsync(deviceId, Option.None <string>(), Option.None <string>())) { await this.SendResponseAsync(HttpStatusCode.Unauthorized); return; } IEdgeHub edgeHub = await this.edgeHubGetter; IDeviceScopeIdentitiesCache identitiesCache = edgeHub.GetDeviceScopeIdentitiesCache(); Option <string> targetAuthChain = await identitiesCache.GetAuthChain(deviceId); if (!targetAuthChain.HasValue) { Events.AuthorizationFail_NoAuthChain(deviceId); await this.SendResponseAsync(HttpStatusCode.Unauthorized); return; } string edgeDeviceId = edgeHub.GetEdgeDeviceId(); var requestData = new ListModulesOnBehalfOfData($"{targetAuthChain.OrDefault()}"); RegistryApiHttpResult result = await this.apiClient.ListModulesAsync(edgeDeviceId, requestData); await this.SendResponseAsync(result.StatusCode, result.JsonContent); Events.CompleteRequest(nameof(this.ListModulesAsync), edgeDeviceId, requestData.AuthChain, result); } catch (Exception ex) { Events.InternalServerError(nameof(this.ListModulesAsync), ex); await this.SendResponseAsync(HttpStatusCode.InternalServerError, FormatErrorResponseMessage(ex.ToString())); } }
public async Task ListModulesOnBehalfOfAsync( [FromRoute] string actorDeviceId, [FromBody] ListModulesOnBehalfOfData requestData) { try { Events.ReceivedOnBehalfOfRequest(nameof(this.ListModulesOnBehalfOfAsync), actorDeviceId, Events.GetAdditionalInfo(requestData)); try { Preconditions.CheckNonWhiteSpace(actorDeviceId, nameof(actorDeviceId)); Preconditions.CheckNotNull(requestData, nameof(requestData)); Preconditions.CheckNonWhiteSpace(requestData.AuthChain, nameof(requestData.AuthChain)); } catch (Exception ex) { Events.BadRequest(nameof(this.ListModulesOnBehalfOfAsync), ex.Message); await this.SendResponseAsync(HttpStatusCode.BadRequest, FormatErrorResponseMessage(ex.ToString())); return; } actorDeviceId = WebUtility.UrlDecode(actorDeviceId); IEdgeHub edgeHub = await this.edgeHubGetter; IHttpRequestAuthenticator authenticator = await this.authenticatorGetter; Try <string> targetAuthChainTry = await AuthorizeOnBehalfOf(actorDeviceId, requestData.AuthChain, nameof(this.ListModulesOnBehalfOfAsync), this.HttpContext, edgeHub, authenticator); if (targetAuthChainTry.Success) { string targetAuthChain = targetAuthChainTry.Value; string edgeDeviceId = edgeHub.GetEdgeDeviceId(); RegistryApiHttpResult result = await this.apiClient.ListModulesAsync( edgeDeviceId, new ListModulesOnBehalfOfData($"{targetAuthChain}")); await this.SendResponseAsync(result.StatusCode, result.JsonContent); Events.CompleteRequest(nameof(this.ListModulesOnBehalfOfAsync), edgeDeviceId, targetAuthChain, result); } else { await this.SendErrorResponse(targetAuthChainTry.Exception); } } catch (Exception ex) { Events.InternalServerError(nameof(this.ListModulesOnBehalfOfAsync), ex); await this.SendResponseAsync(HttpStatusCode.InternalServerError, FormatErrorResponseMessage(ex.ToString())); } }
public async Task ListModulesOnBehalfOf_Success() { // Setup var authChainMapping = new Dictionary <string, string>() { { ChildEdgeId, AuthChainOnRoot } }; (RegistryController controller, Mock <IRegistryOnBehalfOfApiClient> registryApiClient, Mock <IHttpRequestAuthenticator> authenticator, Mock <IDeviceScopeIdentitiesCache> _) = this.TestSetup(RootEdgeId, authChainMapping); ListModulesOnBehalfOfData actualRequestData = null; var modules = new[] { new Module(ChildEdgeId, "moduleId2"), new Module(ChildEdgeId, "moduleId1") }; registryApiClient.Setup(c => c.ListModulesAsync(RootEdgeId, It.IsAny <ListModulesOnBehalfOfData>())) .Callback <string, ListModulesOnBehalfOfData>((_, data) => actualRequestData = data) .Returns(Task.FromResult(new RegistryApiHttpResult(HttpStatusCode.OK, JsonConvert.SerializeObject(modules)))); authenticator.Setup(a => a.AuthenticateAsync(ParentEdgeId, Option.Some(Constants.EdgeHubModuleId), Option.Some(AuthChainOnParent), It.IsAny <HttpContext>())) .ReturnsAsync(new HttpAuthResult(true, string.Empty)); // Act await controller.ListModulesOnBehalfOfAsync(ParentEdgeId, new ListModulesOnBehalfOfData(AuthChainOnParent)); // Verify registryApiClient.Verify(c => c.ListModulesAsync(RootEdgeId, It.IsAny <ListModulesOnBehalfOfData>()), Times.Once()); Assert.Equal(AuthChainOnRoot, actualRequestData.AuthChain); Assert.Equal((int)HttpStatusCode.OK, controller.HttpContext.Response.StatusCode); byte[] actualResponseBytes = this.GetResponseBodyBytes(controller); string actualResponseJson = Encoding.UTF8.GetString(actualResponseBytes); Module[] actualModules = JsonConvert.DeserializeObject <Module[]>(actualResponseJson); Assert.Equal(2, actualModules.Length); Assert.Equal(ChildEdgeId, actualModules[0].DeviceId); Assert.Contains(actualModules, m => m.Id.Equals("moduleId1")); Assert.Equal(ChildEdgeId, actualModules[1].DeviceId); Assert.Contains(actualModules, m => m.Id.Equals("moduleId2")); }
public static string GetAdditionalInfo(ListModulesOnBehalfOfData data) { return($"authChain={data.AuthChain}"); }
public async Task ListModulesOnBehalfOfAsync( [FromRoute] string actorDeviceId, [FromBody] ListModulesOnBehalfOfData requestData) { try { Events.ReceivedOnBehalfOfRequest(nameof(this.ListModulesOnBehalfOfAsync), actorDeviceId, Events.GetAdditionalInfo(requestData)); try { Preconditions.CheckNonWhiteSpace(actorDeviceId, nameof(actorDeviceId)); Preconditions.CheckNotNull(requestData, nameof(requestData)); Preconditions.CheckNonWhiteSpace(requestData.AuthChain, nameof(requestData.AuthChain)); } catch (Exception ex) { Events.BadRequest(nameof(this.ListModulesOnBehalfOfAsync), ex.Message); await this.SendResponseAsync(HttpStatusCode.BadRequest, FormatErrorResponseMessage(ex.ToString())); return; } actorDeviceId = WebUtility.UrlDecode(actorDeviceId); if (!AuthChainHelpers.TryGetTargetDeviceId(requestData.AuthChain, out string targetDeviceId)) { Events.InvalidRequestAuthChain(nameof(this.ListModulesOnBehalfOfAsync), requestData.AuthChain); await this.SendResponseAsync(HttpStatusCode.BadRequest, FormatErrorResponseMessage($"Invalid request auth chain {requestData.AuthChain}.")); return; } if (!await this.AuthenticateAsync(actorDeviceId, Option.Some(Constants.EdgeHubModuleId), Option.Some(requestData.AuthChain))) { await this.SendResponseAsync(HttpStatusCode.Unauthorized); return; } IEdgeHub edgeHub = await this.edgeHubGetter; IDeviceScopeIdentitiesCache identitiesCache = edgeHub.GetDeviceScopeIdentitiesCache(); Option <string> targetAuthChain = await identitiesCache.GetAuthChain(targetDeviceId); if (!targetAuthChain.HasValue) { Events.AuthorizationFail_NoAuthChain(targetDeviceId); await this.SendResponseAsync(HttpStatusCode.Unauthorized); return; } string edgeDeviceId = edgeHub.GetEdgeDeviceId(); RegistryApiHttpResult result = await this.apiClient.ListModulesAsync( edgeDeviceId, new ListModulesOnBehalfOfData($"{targetAuthChain.OrDefault()}")); await this.SendResponseAsync(result.StatusCode, result.JsonContent); Events.CompleteRequest(nameof(this.ListModulesOnBehalfOfAsync), edgeDeviceId, targetAuthChain.OrDefault(), result); } catch (Exception ex) { Events.InternalServerError(nameof(this.ListModulesOnBehalfOfAsync), ex); await this.SendResponseAsync(HttpStatusCode.InternalServerError, FormatErrorResponseMessage(ex.ToString())); } }