File: serialize_immutable_hostname_bucket.go

package info (click to toggle)
golang-github-aws-aws-sdk-go-v2 1.24.1-2~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 554,032 kB
  • sloc: java: 15,941; makefile: 419; sh: 175
file content (77 lines) | stat: -rw-r--r-- 2,546 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package s3

import (
	"context"
	"fmt"
	"path"

	awsmiddleware "github.com/aws/aws-sdk-go-v2/aws/middleware"

	"github.com/aws/aws-sdk-go-v2/internal/endpoints/awsrulesfn"
	smithy "github.com/aws/smithy-go"
	"github.com/aws/smithy-go/encoding/httpbinding"
	"github.com/aws/smithy-go/middleware"
	smithyhttp "github.com/aws/smithy-go/transport/http"
)

// serializeImmutableHostnameBucketMiddleware handles injecting the bucket name into
// "immutable" hostnames resolved via v1 EndpointResolvers. This CANNOT be done in
// serialization, since v2 endpoint resolution requires removing the {Bucket} path
// segment from all S3 requests.
//
// This will only be done for non-ARN buckets, as the features that use those require
// virtualhost manipulation to function and we previously (pre-ep2) expected the caller
// to handle that in their resolver.
type serializeImmutableHostnameBucketMiddleware struct {
	UsePathStyle bool
}

func (*serializeImmutableHostnameBucketMiddleware) ID() string {
	return "serializeImmutableHostnameBucket"
}

func (m *serializeImmutableHostnameBucketMiddleware) HandleSerialize(
	ctx context.Context, in middleware.SerializeInput, next middleware.SerializeHandler,
) (
	out middleware.SerializeOutput, metadata middleware.Metadata, err error,
) {
	request, ok := in.Request.(*smithyhttp.Request)
	if !ok {
		return out, metadata, &smithy.SerializationError{Err: fmt.Errorf("unknown transport type %T", in.Request)}
	}

	bucket, ok := bucketFromInput(in.Parameters)
	if !ok {
		return next.HandleSerialize(ctx, in)
	}

	// a bucket being un-vhostable will also force us to use path style
	usePathStyle := m.UsePathStyle || !awsrulesfn.IsVirtualHostableS3Bucket(bucket, request.URL.Scheme != "https")

	if !smithyhttp.GetHostnameImmutable(ctx) &&
		!(awsmiddleware.GetRequiresLegacyEndpoints(ctx) && usePathStyle) {
		return next.HandleSerialize(ctx, in)
	}

	parsedBucket := awsrulesfn.ParseARN(bucket)

	// disallow ARN buckets except for MRAP arns
	if parsedBucket != nil && len(parsedBucket.Region) > 0 {
		return next.HandleSerialize(ctx, in)
	}

	request.URL.Path = path.Join(request.URL.Path, bucket)
	request.URL.RawPath = path.Join(request.URL.RawPath, httpbinding.EscapePath(bucket, true))

	return next.HandleSerialize(ctx, in)
}

func addSerializeImmutableHostnameBucketMiddleware(stack *middleware.Stack, options Options) error {
	return stack.Serialize.Insert(
		&serializeImmutableHostnameBucketMiddleware{
			UsePathStyle: options.UsePathStyle,
		},
		"OperationSerializer",
		middleware.Before,
	)
}