private void SignRequest(IRequest request, AbstractAWSSigner signer, RequestMetrics metrics) { // Check if request should be signed if (credentials is AnonymousAWSCredentials) { return; } metrics.StartEvent(RequestMetrics.Metric.CredentialsRequestTime); using (ImmutableCredentials immutableCredentials = credentials.GetCredentials()) { metrics.StopEvent(RequestMetrics.Metric.CredentialsRequestTime); if (immutableCredentials.UseToken) { ClientProtocol protocol = DetermineProtocol(signer); switch (protocol) { case ClientProtocol.QueryStringProtocol: request.Parameters["SecurityToken"] = immutableCredentials.Token; break; case ClientProtocol.RestProtocol: request.Headers["x-amz-security-token"] = immutableCredentials.Token; break; default: throw new InvalidDataException("Cannot determine protocol"); } } signer.Sign(request, this.config, immutableCredentials.AccessKey, immutableCredentials.ClearSecretKey, immutableCredentials.SecureSecretKey); } }
private void SignRequest(IRequest request, AbstractAWSSigner signer) { using (ImmutableCredentials immutableCredentials = credentials.GetCredentials()) { ValidateAuthentication(immutableCredentials); if (immutableCredentials.UseToken) { ClientProtocol protocol = DetermineProtocol(signer); switch (protocol) { case ClientProtocol.QueryStringProtocol: request.Parameters["SecurityToken"] = immutableCredentials.Token; break; case ClientProtocol.RestProtocol: request.Headers["x-amz-security-token"] = immutableCredentials.Token; break; default: throw new InvalidDataException("Cannot determine protocol"); } } signer.Sign(request, this.config, immutableCredentials.AccessKey, immutableCredentials.ClearSecretKey, immutableCredentials.SecureSecretKey); } }
/// <summary> /// Allows to mock pipeline to run tests for custom PipelineHandler /// </summary> /// <param name="pipelineHandler">Custom handler to test</param> /// <param name="request">Request to use</param> /// <param name="marshaller">Marshaller to use</param> /// <param name="config">ClientConfig to use</param> /// <param name="signer">Signer to use</param> /// <returns></returns> public static IRequest RunMockRequest(IPipelineHandler pipelineHandler, AmazonWebServiceRequest request, IMarshaller <IRequest, AmazonWebServiceRequest> marshaller, ClientConfig config, AbstractAWSSigner signer) { var pipeline = new RuntimePipeline(new List <IPipelineHandler> { new NoopPipelineHandler(), new Signer(), new EndpointResolver(), pipelineHandler, new Marshaller(), }); var requestContext = new RequestContext(config.LogMetrics, signer) { ClientConfig = config, Marshaller = marshaller, OriginalRequest = request, Unmarshaller = null, IsAsync = false, ImmutableCredentials = new ImmutableCredentials("access key", "secret", "token") }; var executionContext = new ExecutionContext( requestContext, new ResponseContext() ); pipeline.InvokeSync(executionContext); return(requestContext.Request); }
protected IAsyncExecutionContext CreateAsyncTestContext(AbstractAWSSigner signer) { var putObjectRequest = new PutObjectRequest { Key = "Test", BucketName = "Test", ContentBody = "Test Content" }; var requestContext = new AsyncRequestContext(true, signer == null ? new NullSigner() : signer) { OriginalRequest = putObjectRequest, Request = new PutObjectRequestMarshaller().Marshall(putObjectRequest), Unmarshaller = PutObjectResponseUnmarshaller.Instance, ClientConfig = new AmazonS3Config() }; requestContext.Request.Endpoint = new Uri("https://s3.amazonaws.com"); var putObjectResponse = MockWebResponse.CreateFromResource("PutObjectResponse.txt") as HttpWebResponse; return(new Amazon.Runtime.Internal.AsyncExecutionContext( requestContext, new AsyncResponseContext { HttpResponse = new HttpWebRequestResponseData(putObjectResponse) } )); }
public RequestContext(bool enableMetrics, AbstractAWSSigner clientSigner) { if (clientSigner == null) { throw new ArgumentNullException("clientSigner"); } this.clientSigner = clientSigner; this.Metrics = new RequestMetrics(); this.Metrics.IsEnabled = enableMetrics; }
private static ClientProtocol DetermineProtocol(AbstractAWSSigner signer) { if (signer is AWS3Signer) { return(ClientProtocol.RestProtocol); } if (signer is QueryStringSigner) { return(ClientProtocol.QueryStringProtocol); } return(ClientProtocol.Unknown); }
/// <summary> /// This method makes the actual web request and marshalls the response body or error returned from the service. /// For some error response a retry will be attempted after an exponential pause. /// </summary> /// <typeparam name="X">The user facing request type.</typeparam> /// <typeparam name="Y">The user facing response type.</typeparam> /// <param name="request">The wrapper around the user facing request.</param> /// <param name="signer">The type of signer to use for the request.</param> /// <param name="unmarshaller">The object used to unmarshall the response body.</param> /// <returns>The response object for the request</returns> internal Y Invoke <X, Y>(IRequest <X> request, AbstractAWSSigner signer, IResponseUnmarshaller <Y, UnmarshallerContext> unmarshaller) { Type requestType = typeof(X); string requestName = requestType.Name; this.logger.DebugFormat("Starting request {0} at {1}", requestName, this.config.ServiceURL); request.Endpoint = new Uri(this.config.ServiceURL); signer.Sign(request, this.config, this.awsAccessKeyId, this.clearAwsSecretAccessKey, this.awsSecretAccessKey); string queryString = AWSSDKUtils.GetParametersAsString(request.Parameters); byte[] requestData = Encoding.UTF8.GetBytes(queryString); this.logger.DebugFormat("Request body's content [{0}] with size {1}", queryString, requestData.Length); int retries = 0; while (true) { retries++; try { HttpWebRequest webRequest = this.ConfigureWebRequest <X>(request, requestData); try { Y result; DateTime requestSent = DateTime.UtcNow; using (HttpWebResponse httpResponse = webRequest.GetResponse() as HttpWebResponse) { DateTime responseReceived = DateTime.UtcNow; this.logger.InfoFormat("Received response for {0} with status code {1} in {2} ms.", requestName, httpResponse.StatusCode, (responseReceived - requestSent).TotalMilliseconds); XmlTextReader reader = new XmlTextReader(new StreamReader(httpResponse.GetResponseStream())); UnmarshallerContext context = new UnmarshallerContext(reader); result = unmarshaller.Unmarshall(context); } return(result); } catch (WebException we) { processWebException <X, Y>(requestName, we, webRequest, unmarshaller, request, retries); } } catch (Exception e) { this.logger.Error(string.Format("Error configuring web request {0} to {1}.", requestName, request.Endpoint.ToString()), e); throw; } } }
protected AmazonServiceClient(AWSCredentials credentials, ClientConfig config) { if (config.DisableLogging) { _logger = Logger.EmptyLogger; } else { _logger = Logger.GetLogger(GetType()); } config.Validate(); Config = config; Credentials = credentials; Signer = CreateSigner(); Initialize(); BuildRuntimePipeline(); }
protected virtual IExecutionContext CreateTestContext(AbstractAWSSigner signer, ResponseUnmarshaller responseUnmarshaller, ClientConfig config) { var putObjectRequest = new PutObjectRequest { Key = "TestKey", BucketName = "TestBucket", ContentBody = "Test Content" }; if (responseUnmarshaller == null) { responseUnmarshaller = PutObjectResponseUnmarshaller.Instance; } var requestContext = new RequestContext(true, signer == null ? new NullSigner() : signer) { OriginalRequest = putObjectRequest, Request = new PutObjectRequestMarshaller().Marshall(putObjectRequest), Unmarshaller = responseUnmarshaller, ClientConfig = config ?? new AmazonS3Config { RegionEndpoint = RegionEndpoint.USEast1 } }; requestContext.Request.Endpoint = new Uri("https://s3.amazonaws.com"); var putObjectResponse = MockWebResponse.CreateFromResource("PutObjectResponse.txt") as HttpWebResponse; return(new Amazon.Runtime.Internal.ExecutionContext(requestContext, new ResponseContext { HttpResponse = new HttpWebRequestResponseData(putObjectResponse) })); }
public AsyncExecutionContext(bool enableMetrics, AbstractAWSSigner clientSigner) { this.RequestContext = new AsyncRequestContext(enableMetrics, clientSigner); this.ResponseContext = new AsyncResponseContext(); }
public AsyncRequestContext(bool enableMetrics, AbstractAWSSigner clientSigner) : base(enableMetrics, clientSigner) { }
protected void Invoke <T, R>(R request, AmazonServiceCallback callback, object state, IMarshaller <T, R> marshaller, ResponseUnmarshaller unmarshaller, AbstractAWSSigner signer) where T : IRequest where R : AmazonWebServiceRequest { AsyncResult result = null; try { ProcessPreRequestHandlers(request); IRequest irequest = marshaller.Marshall(request); result = new AsyncResult(irequest, request, callback, state, signer, unmarshaller); Invoke(result); } catch (Exception e) { AmazonLogging.LogError(AmazonLogging.AmazonLoggingLevel.Errors, "Runtime", e.Message); result.HandleException(e); result.IsCompleted = true; } //return result; }
protected virtual IExecutionContext CreateTestContext(AbstractAWSSigner signer, ResponseUnmarshaller responseUnmarshaller) { return(CreateTestContext(signer, responseUnmarshaller, null)); }
protected virtual IExecutionContext CreateTestContext(AbstractAWSSigner signer) { return(CreateTestContext(signer, null)); }
internal WebRequestState(IRequest request, ResponseUnmarshaller unmarshaller, AbstractAWSSigner signer) { this.Request = request; this.Unmarshaller = unmarshaller; this.Signer = signer; this._stopWatch = Stopwatch.StartNew(); this._stopWatch.Start(); this.Metrics = new RequestMetrics(); }
// Constructor internal AsyncResult(IRequest request, AsyncCallback callback, object state, bool completeSynchronized, AbstractAWSSigner signer, ResponseUnmarshaller unmarshaller) { this.Request = request; this.Callback = callback; this.State = state; this.CompletedSynchronously = completeSynchronized; this.Signer = signer; this.Unmarshaller = unmarshaller; this.RequestName = request.OriginalRequest.GetType().Name; this._lockObj = new object(); }
protected void Invoke <T, R, A>(R request, Action <A> callback, IMarshaller <T, R> marshaller, ResponseUnmarshaller unmarshaller, AbstractAWSSigner signer) where T : IRequest where R : AmazonWebServiceRequest where A : class { /* * AsyncResult result = null; * try * { * * ProcessPreRequestHandlers(request); * * IRequest irequest = marshaller.Marshall(request); * result = new AsyncResult(irequest, signer, unmarshaller); * result.VoidCallback = delegate * { * if (callback != null) * callback(result.FinalResponse as A); * }; * result.FinalResponseType = typeof(A); * Invoke(result); * } * catch (Exception e) * { * AmazonLogging.LogError(AmazonLogging.AmazonLoggingLevel.Errors, "Runtime", e.Message); * if (result.Exception == null) * { * result.Exception = e; * } * result.IsCompleted = true; * * endAsync(result); * } */ }
/// <summary> /// This method makes the actual web request and marshalls the response body or error returned from the service. /// For some error response a retry will be attempted after an exponential pause. /// </summary> /// <typeparam name="X">The user facing request type.</typeparam> /// <typeparam name="Y">The user facing response type.</typeparam> /// <param name="request">The wrapper around the user facing request.</param> /// <param name="signer">The type of signer to use for the request.</param> /// <param name="unmarshaller">The object used to unmarshall the response body.</param> /// <returns>The response object for the request</returns> internal Y Invoke <X, Y>(IRequest <X> request, AbstractAWSSigner signer, IResponseUnmarshaller <Y, UnmarshallerContext> unmarshaller) where X : AmazonWebServiceRequest { Type requestType = typeof(X); string requestName = requestType.Name; request.Endpoint = new Uri(this.config.ServiceURL); request.Headers["User-Agent"] = this.config.UserAgent; request.Headers["Content-Type"] = AWSSDKUtils.UrlEncodedContent; ProcessRequestHandlers(request); this.logger.DebugFormat("Starting request {0} at {1}", requestName, this.config.ServiceURL); signer.Sign(request, this.config, this.awsAccessKeyId, this.clearAwsSecretAccessKey, this.awsSecretAccessKey); string queryString = AWSSDKUtils.GetParametersAsString(request.Parameters); byte[] requestData = Encoding.UTF8.GetBytes(queryString); this.logger.DebugFormat("Request body's content [{0}] with size {1}", queryString, requestData.Length); int retries = 0; while (true) { retries++; try { HttpWebRequest webRequest = this.ConfigureWebRequest <X>(request, requestData); try { Y result; DateTime requestSent = DateTime.UtcNow; using (HttpWebResponse httpResponse = webRequest.GetResponse() as HttpWebResponse) { DateTime responseReceived = DateTime.UtcNow; this.logger.InfoFormat("Received response for {0} with status code {1} in {2} ms.", requestName, httpResponse.StatusCode, (responseReceived - requestSent).TotalMilliseconds); XmlTextReader reader; // Using NOSTREAM is the less effcient way of dealing with the response body but it is helpful // for debug purposes to see the entire xml body coming back from the server. #if NOSTREAM string responseBody = new StreamReader(httpResponse.GetResponseStream()).ReadToEnd();; reader = new XmlTextReader(new StringReader(responseBody)); #else reader = new XmlTextReader(new StreamReader(httpResponse.GetResponseStream())); #endif UnmarshallerContext context = new UnmarshallerContext(reader); result = unmarshaller.Unmarshall(context); } return(result); } catch (WebException we) { processWebException <X, Y>(requestName, we, webRequest, unmarshaller, request, retries); } } catch (IOException e) { if (this.isInnerExceptionThreadAbort(e)) { throw; } this.logger.Error(string.Format("IOException making request {0} to {1}.", requestName, request.Endpoint.ToString()), e); if (retries > this.config.MaxErrorRetry) { throw; } else { this.logger.Error(string.Format("IOException making request {0} to {1}. Attempting retry {2}.", requestName, request.Endpoint.ToString(), retries), e); } } catch (Exception e) { this.logger.Error(string.Format("Error configuring web request {0} to {1}.", requestName, request.Endpoint.ToString()), e); throw; } } }
protected Task <Res> Invoke <T, Req, Res>(Req request, IMarshaller <T, Req> marshaller, ResponseUnmarshaller unmarshaller, AbstractAWSSigner signer, CancellationToken cancellationToken = default(CancellationToken)) where T : IRequest where Req : AmazonWebServiceRequest where Res : AmazonWebServiceResponse { ProcessPreRequestHandlers(request); IRequest irequest = marshaller.Marshall(request); WebRequestState state = new WebRequestState(irequest, unmarshaller, signer); state.Metrics.StartEvent(Metric.ClientExecuteTime); irequest.Endpoint = DetermineEndpoint(irequest); if (Config.LogMetrics) { state.Metrics.IsEnabled = true; state.Metrics.AddProperty(Metric.ServiceName, irequest.ServiceName); state.Metrics.AddProperty(Metric.ServiceEndpoint, irequest.Endpoint); state.Metrics.AddProperty(Metric.MethodName, irequest.RequestName); } ConfigureRequest(state); return(InvokeHelper <Res>(state, cancellationToken)); }
protected IAsyncResult Invoke <T, R>(R request, AsyncCallback callback, object state, bool synchronized, IMarshaller <T, R> marshaller, ResponseUnmarshaller unmarshaller, AbstractAWSSigner signer) where T : IRequest where R : AmazonWebServiceRequest { ProcessPreRequestHandlers(request); IRequest irequest = marshaller.Marshall(request); AsyncResult result = new AsyncResult(irequest, callback, state, synchronized, signer, unmarshaller); Invoke(result); return(result); }
// Constructor internal AsyncResult(IRequest request, AmazonWebServiceRequest originalRequest, AmazonServiceCallback callback, object state, AbstractAWSSigner signer, ResponseUnmarshaller unmarshaller) { this.Request = request; this.Callback = callback; //this.State = state; //this.CompletedSynchronously = completeSynchronized; this.Signer = signer; this.Unmarshaller = unmarshaller; if (request != null) { this.RequestName = request.OriginalRequest.GetType().Name; } this.Metrics = new RequestMetrics(); //this._lockObj = new object(); this._stopWatch = Stopwatch.StartNew(); this._stopWatch.Start(); this.ServiceResult = new AmazonServiceResult(originalRequest, state); }