2020-02-23 14:36:57 +00:00
|
|
|
package arn
|
|
|
|
|
|
|
|
import (
|
2021-09-02 10:03:56 +00:00
|
|
|
"fmt"
|
2020-02-23 14:36:57 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws/arn"
|
|
|
|
)
|
|
|
|
|
2021-09-02 10:03:56 +00:00
|
|
|
var supportedServiceARN = []string{
|
|
|
|
"s3",
|
|
|
|
"s3-outposts",
|
|
|
|
"s3-object-lambda",
|
|
|
|
}
|
|
|
|
|
|
|
|
func isSupportedServiceARN(service string) bool {
|
|
|
|
for _, name := range supportedServiceARN {
|
|
|
|
if name == service {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-02-23 14:36:57 +00:00
|
|
|
// Resource provides the interfaces abstracting ARNs of specific resource
|
|
|
|
// types.
|
|
|
|
type Resource interface {
|
|
|
|
GetARN() arn.ARN
|
|
|
|
String() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceParser provides the function for parsing an ARN's resource
|
|
|
|
// component into a typed resource.
|
|
|
|
type ResourceParser func(arn.ARN) (Resource, error)
|
|
|
|
|
|
|
|
// ParseResource parses an AWS ARN into a typed resource for the S3 API.
|
|
|
|
func ParseResource(s string, resParser ResourceParser) (resARN Resource, err error) {
|
|
|
|
a, err := arn.Parse(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(a.Partition) == 0 {
|
2021-09-02 10:03:56 +00:00
|
|
|
return nil, InvalidARNError{ARN: a, Reason: "partition not set"}
|
2020-02-23 14:36:57 +00:00
|
|
|
}
|
2021-09-02 10:03:56 +00:00
|
|
|
|
|
|
|
if !isSupportedServiceARN(a.Service) {
|
|
|
|
return nil, InvalidARNError{ARN: a, Reason: "service is not supported"}
|
2020-02-23 14:36:57 +00:00
|
|
|
}
|
2021-09-02 10:03:56 +00:00
|
|
|
|
|
|
|
if strings.HasPrefix(a.Region, "fips-") || strings.HasSuffix(a.Region, "-fips") {
|
|
|
|
return nil, InvalidARNError{ARN: a, Reason: "FIPS region not allowed in ARN"}
|
|
|
|
}
|
|
|
|
|
2020-02-23 14:36:57 +00:00
|
|
|
if len(a.Resource) == 0 {
|
2021-09-02 10:03:56 +00:00
|
|
|
return nil, InvalidARNError{ARN: a, Reason: "resource not set"}
|
2020-02-23 14:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resParser(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SplitResource splits the resource components by the ARN resource delimiters.
|
|
|
|
func SplitResource(v string) []string {
|
|
|
|
var parts []string
|
|
|
|
var offset int
|
|
|
|
|
|
|
|
for offset <= len(v) {
|
|
|
|
idx := strings.IndexAny(v[offset:], "/:")
|
|
|
|
if idx < 0 {
|
|
|
|
parts = append(parts, v[offset:])
|
|
|
|
break
|
|
|
|
}
|
|
|
|
parts = append(parts, v[offset:idx+offset])
|
|
|
|
offset += idx + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return parts
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsARN returns whether the given string is an ARN
|
|
|
|
func IsARN(s string) bool {
|
|
|
|
return arn.IsARN(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvalidARNError provides the error for an invalid ARN error.
|
|
|
|
type InvalidARNError struct {
|
|
|
|
ARN arn.ARN
|
|
|
|
Reason string
|
|
|
|
}
|
|
|
|
|
2021-09-02 10:03:56 +00:00
|
|
|
// Error returns a string denoting the occurred InvalidARNError
|
2020-02-23 14:36:57 +00:00
|
|
|
func (e InvalidARNError) Error() string {
|
2021-09-02 10:03:56 +00:00
|
|
|
return fmt.Sprintf("invalid Amazon %s ARN, %s, %s", e.ARN.Service, e.Reason, e.ARN.String())
|
2020-02-23 14:36:57 +00:00
|
|
|
}
|