public async Task GetModule_Success() { // Setup var authChainMapping = new Dictionary <string, string>() { { ChildEdgeId, AuthChainOnParent } }; (RegistryController controller, Mock <IRegistryOnBehalfOfApiClient> registryApiClient, Mock <IHttpRequestAuthenticator> authenticator, Mock <IDeviceScopeIdentitiesCache> _) = this.TestSetup(ParentEdgeId, authChainMapping); GetModuleOnBehalfOfData actualRequestData = null; var module = new Module(ChildEdgeId, ModuleId); registryApiClient.Setup(c => c.GetModuleAsync(ParentEdgeId, It.IsAny <GetModuleOnBehalfOfData>())) .Callback <string, GetModuleOnBehalfOfData>((_, data) => actualRequestData = data) .Returns(Task.FromResult(new RegistryApiHttpResult(HttpStatusCode.OK, JsonConvert.SerializeObject(module)))); authenticator.Setup(a => a.AuthenticateAsync(ChildEdgeId, Option.None <string>(), Option.None <string>(), It.IsAny <HttpContext>())) .ReturnsAsync(new HttpAuthResult(true, string.Empty)); // Act await controller.GetModuleAsync(ChildEdgeId, ModuleId); // Verify registryApiClient.Verify(c => c.GetModuleAsync(ParentEdgeId, It.IsAny <GetModuleOnBehalfOfData>()), Times.Once()); Assert.Equal(AuthChainOnParent, actualRequestData.AuthChain); Assert.Equal(ModuleId, actualRequestData.ModuleId); Assert.Equal((int)HttpStatusCode.OK, controller.HttpContext.Response.StatusCode); byte[] actualResponseBytes = this.GetResponseBodyBytes(controller); string actualResponseJson = Encoding.UTF8.GetString(actualResponseBytes); Module actualModule = JsonConvert.DeserializeObject <Module>(actualResponseJson); Assert.Equal(ChildEdgeId, actualModule.DeviceId); Assert.Equal(ModuleId, actualModule.Id); }
public async Task GetModuleAsync( [FromRoute] string deviceId, [FromRoute] string moduleId) { try { Events.ReceivedRequest(nameof(this.GetModuleAsync), deviceId, moduleId); try { deviceId = WebUtility.UrlDecode(Preconditions.CheckNonWhiteSpace(deviceId, nameof(deviceId))); moduleId = WebUtility.UrlDecode(Preconditions.CheckNonWhiteSpace(moduleId, nameof(moduleId))); } catch (Exception ex) { Events.BadRequest(nameof(this.GetModuleAsync), ex.Message); await this.SendResponseAsync(HttpStatusCode.BadRequest, FormatErrorResponseMessage(ex.Message)); return; } IHttpRequestAuthenticator authenticator = await this.authenticatorGetter; if (!await AuthenticateAsync(deviceId, Option.None <string>(), Option.None <string>(), this.HttpContext, authenticator)) { 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 GetModuleOnBehalfOfData($"{targetAuthChain.OrDefault()}", moduleId); RegistryApiHttpResult result = await this.apiClient.GetModuleAsync(edgeDeviceId, requestData); await this.SendResponseAsync(result.StatusCode, result.JsonContent); Events.CompleteRequest(nameof(this.GetModuleAsync), edgeDeviceId, requestData.AuthChain, result); } catch (Exception ex) { Events.InternalServerError(nameof(this.GetModuleAsync), ex); await this.SendResponseAsync(HttpStatusCode.InternalServerError, FormatErrorResponseMessage(ex.ToString())); } }
public async Task GetModuleOnBehalfOfAsync( [FromRoute] string actorDeviceId, [FromBody] GetModuleOnBehalfOfData requestData) { try { Events.ReceivedOnBehalfOfRequest(nameof(this.GetModuleOnBehalfOfAsync), actorDeviceId, Events.GetAdditionalInfo(requestData)); try { Preconditions.CheckNonWhiteSpace(actorDeviceId, nameof(actorDeviceId)); Preconditions.CheckNotNull(requestData, nameof(requestData)); Preconditions.CheckNonWhiteSpace(requestData.AuthChain, nameof(requestData.AuthChain)); Preconditions.CheckNonWhiteSpace(requestData.ModuleId, nameof(requestData.ModuleId)); } catch (Exception ex) { Events.BadRequest(nameof(this.GetModuleOnBehalfOfAsync), 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.GetModuleOnBehalfOfAsync), this.HttpContext, edgeHub, authenticator); if (targetAuthChainTry.Success) { string targetAuthChain = targetAuthChainTry.Value; string edgeDeviceId = edgeHub.GetEdgeDeviceId(); RegistryApiHttpResult result = await this.apiClient.GetModuleAsync( edgeDeviceId, new GetModuleOnBehalfOfData($"{targetAuthChain}", requestData.ModuleId)); await this.SendResponseAsync(result.StatusCode, result.JsonContent); Events.CompleteRequest(nameof(this.GetModuleOnBehalfOfAsync), edgeDeviceId, targetAuthChain, result); } else { await this.SendErrorResponse(targetAuthChainTry.Exception); } } catch (Exception ex) { Events.InternalServerError(nameof(this.GetModuleOnBehalfOfAsync), ex); await this.SendResponseAsync(HttpStatusCode.InternalServerError, FormatErrorResponseMessage(ex.ToString())); } }
public static string GetAdditionalInfo(GetModuleOnBehalfOfData data) { return($"authChain={data.AuthChain}, targetModuleId={data.ModuleId}"); }
public async Task GetModuleOnBehalfOfAsync( [FromRoute] string actorDeviceId, [FromBody] GetModuleOnBehalfOfData requestData) { try { Events.ReceivedOnBehalfOfRequest(nameof(this.GetModuleOnBehalfOfAsync), actorDeviceId, Events.GetAdditionalInfo(requestData)); try { Preconditions.CheckNonWhiteSpace(actorDeviceId, nameof(actorDeviceId)); Preconditions.CheckNotNull(requestData, nameof(requestData)); Preconditions.CheckNonWhiteSpace(requestData.AuthChain, nameof(requestData.AuthChain)); Preconditions.CheckNonWhiteSpace(requestData.ModuleId, nameof(requestData.ModuleId)); } catch (Exception ex) { Events.BadRequest(nameof(this.GetModuleOnBehalfOfAsync), 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.GetModuleOnBehalfOfAsync), 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.GetModuleAsync( edgeDeviceId, new GetModuleOnBehalfOfData($"{targetAuthChain.OrDefault()}", requestData.ModuleId)); await this.SendResponseAsync(result.StatusCode, result.JsonContent); Events.CompleteRequest(nameof(this.GetModuleOnBehalfOfAsync), edgeDeviceId, targetAuthChain.OrDefault(), result); } catch (Exception ex) { Events.InternalServerError(nameof(this.GetModuleOnBehalfOfAsync), ex); await this.SendResponseAsync(HttpStatusCode.InternalServerError, FormatErrorResponseMessage(ex.ToString())); } }