File: outpost_arn.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 (128 lines) | stat: -rw-r--r-- 3,814 bytes parent folder | download | duplicates (7)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package arn

import (
	"strings"

	"github.com/aws/aws-sdk-go-v2/aws/arn"
)

// OutpostARN interface that should be satisfied by outpost ARNs
type OutpostARN interface {
	Resource
	GetOutpostID() string
}

// ParseOutpostARNResource will parse a provided ARNs resource using the appropriate ARN format
// and return a specific OutpostARN type
//
// Currently supported outpost ARN formats:
// * Outpost AccessPoint ARN format:
//   - ARN format: arn:{partition}:s3-outposts:{region}:{accountId}:outpost/{outpostId}/accesspoint/{accesspointName}
//   - example: arn:aws:s3-outposts:us-west-2:012345678901:outpost/op-1234567890123456/accesspoint/myaccesspoint
//
// * Outpost Bucket ARN format:
//   - ARN format: arn:{partition}:s3-outposts:{region}:{accountId}:outpost/{outpostId}/bucket/{bucketName}
//   - example: arn:aws:s3-outposts:us-west-2:012345678901:outpost/op-1234567890123456/bucket/mybucket
//
// Other outpost ARN formats may be supported and added in the future.
func ParseOutpostARNResource(a arn.ARN, resParts []string) (OutpostARN, error) {
	if len(a.Region) == 0 {
		return nil, InvalidARNError{ARN: a, Reason: "region not set"}
	}

	if isFIPS(a.Region) {
		return nil, InvalidARNError{ARN: a, Reason: "FIPS region not allowed in ARN"}
	}

	if len(a.AccountID) == 0 {
		return nil, InvalidARNError{ARN: a, Reason: "account-id not set"}
	}

	// verify if outpost id is present and valid
	if len(resParts) == 0 || len(strings.TrimSpace(resParts[0])) == 0 {
		return nil, InvalidARNError{ARN: a, Reason: "outpost resource-id not set"}
	}

	// verify possible resource type exists
	if len(resParts) < 3 {
		return nil, InvalidARNError{
			ARN: a, Reason: "incomplete outpost resource type. Expected bucket or access-point resource to be present",
		}
	}

	// Since we know this is a OutpostARN fetch outpostID
	outpostID := strings.TrimSpace(resParts[0])

	switch resParts[1] {
	case "accesspoint":
		accesspointARN, err := ParseAccessPointResource(a, resParts[2:])
		if err != nil {
			return OutpostAccessPointARN{}, err
		}
		return OutpostAccessPointARN{
			AccessPointARN: accesspointARN,
			OutpostID:      outpostID,
		}, nil

	case "bucket":
		bucketName, err := parseBucketResource(a, resParts[2:])
		if err != nil {
			return nil, err
		}
		return OutpostBucketARN{
			ARN:        a,
			BucketName: bucketName,
			OutpostID:  outpostID,
		}, nil

	default:
		return nil, InvalidARNError{ARN: a, Reason: "unknown resource set for outpost ARN"}
	}
}

// OutpostAccessPointARN represents outpost access point ARN.
type OutpostAccessPointARN struct {
	AccessPointARN
	OutpostID string
}

// GetOutpostID returns the outpost id of outpost access point arn
func (o OutpostAccessPointARN) GetOutpostID() string {
	return o.OutpostID
}

// OutpostBucketARN represents the outpost bucket ARN.
type OutpostBucketARN struct {
	arn.ARN
	BucketName string
	OutpostID  string
}

// GetOutpostID returns the outpost id of outpost bucket arn
func (o OutpostBucketARN) GetOutpostID() string {
	return o.OutpostID
}

// GetARN retrives the base ARN from outpost bucket ARN resource
func (o OutpostBucketARN) GetARN() arn.ARN {
	return o.ARN
}

// parseBucketResource attempts to parse the ARN's bucket resource and retrieve the
// bucket resource id.
//
// parseBucketResource only parses the bucket resource id.
func parseBucketResource(a arn.ARN, resParts []string) (bucketName string, err error) {
	if len(resParts) == 0 {
		return bucketName, InvalidARNError{ARN: a, Reason: "bucket resource-id not set"}
	}
	if len(resParts) > 1 {
		return bucketName, InvalidARNError{ARN: a, Reason: "sub resource not supported"}
	}

	bucketName = strings.TrimSpace(resParts[0])
	if len(bucketName) == 0 {
		return bucketName, InvalidARNError{ARN: a, Reason: "bucket resource-id not set"}
	}
	return bucketName, err
}