예제 #1
0
        private static void ProcessPreRequestHandlers(IExecutionContext executionContext)
        {
            var request = executionContext.RequestContext.OriginalRequest;

            // For CreateAccessPoint, the bucket needs to be extracted from the Arn before
            // it is added to the request payload. Setting outpost id so that the header isn't
            // lost in the case of an outpost request
            var createAccessPointRequest = request as CreateAccessPointRequest;
            Arn arn;

            if (createAccessPointRequest != null && createAccessPointRequest.IsSetBucket() &&
                Arn.TryParse(createAccessPointRequest.Bucket, out arn))
            {
                IS3Resource s3Resource = null;
                if (arn.IsOutpostArn())
                {
                    s3Resource = arn.ParseOutpost();
                }
                if (s3Resource != null)
                {
                    createAccessPointRequest.OutpostId = ((S3OutpostResource)s3Resource).OutpostId;
                    createAccessPointRequest.Bucket    = s3Resource.Name;
                    createAccessPointRequest.AccountId = createAccessPointRequest.AccountId ?? arn.AccountId;
                }
            }
        }
        public static void ProcessRequestHandlers(IExecutionContext executionContext)
        {
            var    request = executionContext.RequestContext.Request;
            var    config  = executionContext.RequestContext.ClientConfig;
            string nonArnOutpostId;
            Arn    s3Arn;

            if (S3ArnUtils.RequestContainsArn(request, out s3Arn))
            {
                IS3Resource s3Resource = null;
                if (s3Arn.IsOutpostArn())
                {
                    if (!s3Arn.IsValidService())
                    {
                        throw new AmazonClientException($"Invalid ARN: {s3Arn.ToString()}, not S3 Outposts ARN");
                    }
                    s3Resource = s3Arn.ParseOutpost();
                    request.Headers[HeaderKeys.XAmzOutpostId] = ((S3OutpostResource)s3Resource).OutpostId;
                }
                if (s3Resource != null)
                {
                    s3Resource.ValidateArnWithClientConfig(config);
                    request.Endpoint = s3Resource.GetEndpoint(config);
                    request.UseSigV4 = true;
                    request.CanonicalResourcePrefix    = string.Concat("/", s3Resource.FullResourceName);
                    request.OverrideSigningServiceName = s3Arn.Service;
                    // The access point arn can be using a region different from the configured region for the service client.
                    // If so be sure to set the authentication region so the signer will use the correct region.
                    request.AuthenticationRegion = s3Arn.Region;
                    request.Headers[HeaderKeys.XAmzAccountId] = s3Arn.AccountId;
                    // replace the ARNs in the resource path or query params with the extracted name of the resource
                    // These methods assume that there is max 1 Arn in the PathResources or Parameters
                    S3ArnUtils.ReplacePathResourceArns(request.PathResources, s3Resource.Name);
                    S3ArnUtils.ReplacePathResourceArns(request.Parameters, s3Resource.Name);
                }
            }
            else if (S3ArnUtils.DoesRequestHaveOutpostId(request.OriginalRequest, out nonArnOutpostId))
            {
                if (!S3ArnUtils.IsValidOutpostId(nonArnOutpostId))
                {
                    throw new AmazonClientException($"Invalid outpost ID. ID must contain only alphanumeric characters and dashes");
                }
                request.OverrideSigningServiceName = S3ArnUtils.S3OutpostsService;
                request.Endpoint = S3ArnUtils.GetNonStandardOutpostIdEndpoint(config);
                request.Headers[HeaderKeys.XAmzOutpostId] = nonArnOutpostId;
            }
        }
        public static void ProcessRequestHandlers(IExecutionContext executionContext)
        {
            var request = executionContext.RequestContext.Request;
            var config  = executionContext.RequestContext.ClientConfig;

            //If a ServiceURL is set the config ClientRegion should be null. Under this case
            //the region needs to be determined from the ServiceURL.
            RegionEndpoint useRegion = config.RegionEndpoint;

            if (useRegion == null && !string.IsNullOrEmpty(config.ServiceURL))
            {
                var regionName = AWSSDKUtils.DetermineRegion(config.ServiceURL);
                useRegion = RegionEndpoint.GetBySystemName(regionName);
            }

            string nonArnOutpostId;
            Arn    s3Arn;

            if (S3ArnUtils.RequestContainsArn(request, out s3Arn))
            {
                IS3Resource s3Resource = null;
                if (!s3Arn.HasValidAccountId())
                {
                    throw new AmazonAccountIdException();
                }
                if (s3Arn.IsOutpostArn())
                {
                    if (!s3Arn.IsValidService())
                    {
                        throw new AmazonClientException($"Invalid ARN: {s3Arn.ToString()}, not S3 Outposts ARN");
                    }
                    s3Resource = s3Arn.ParseOutpost(config as AmazonS3ControlConfig);

                    request.Headers[HeaderKeys.XAmzOutpostId] = ((S3OutpostResource)s3Resource).OutpostId;
                }
                if (s3Resource != null)
                {
                    s3Resource.ValidateArnWithClientConfig(config, useRegion);
                    if (string.IsNullOrEmpty(config.ServiceURL))
                    {
                        request.Endpoint = s3Resource.GetEndpoint(config);
                    }
                    else
                    {
                        request.Endpoint = new Uri(config.ServiceURL);
                    }
                    request.SignatureVersion           = SignatureVersion.SigV4;
                    request.CanonicalResourcePrefix    = string.Concat("/", s3Resource.FullResourceName);
                    request.OverrideSigningServiceName = s3Arn.Service;
                    // The access point arn can be using a region different from the configured region for the service client.
                    // If so be sure to set the authentication region so the signer will use the correct region.
                    request.AuthenticationRegion = s3Arn.Region;
                    request.Headers[HeaderKeys.XAmzAccountId] = s3Arn.AccountId;
                    // replace the ARNs in the resource path or query params with the extracted name of the resource
                    // These methods assume that there is max 1 Arn in the PathResources or Parameters
                    S3ArnUtils.ReplacePathResourceArns(request.PathResources, s3Resource.Name);
                    S3ArnUtils.ReplacePathResourceArns(request.Parameters, s3Resource.Name);
                }
            }
            else if (S3ArnUtils.DoesRequestHaveOutpostId(request.OriginalRequest, out nonArnOutpostId))
            {
                if (!S3ArnUtils.IsValidOutpostId(nonArnOutpostId))
                {
                    throw new AmazonClientException($"Invalid outpost ID. ID must contain only alphanumeric characters and dashes");
                }
                request.OverrideSigningServiceName = S3ArnUtils.S3OutpostsService;
                if (string.IsNullOrEmpty(config.ServiceURL))
                {
                    request.Endpoint = S3ArnUtils.GetNonStandardOutpostIdEndpoint(config);
                }
                else
                {
                    request.Endpoint = new Uri(config.ServiceURL);
                }
                request.Headers[HeaderKeys.XAmzOutpostId] = nonArnOutpostId;
            }
        }