forked from hashicorp/aws-sdk-go-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.go
44 lines (36 loc) · 1.14 KB
/
validation.go
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
package awsbase
import (
"fmt"
"github.com/aws/aws-sdk-go/aws/endpoints"
)
// ValidateAccountID checks if the given AWS account ID is specifically allowed or forbidden.
// The allowedAccountIDs can be used as a whitelist and forbiddenAccountIDs can be used as a blacklist.
func ValidateAccountID(accountID string, allowedAccountIDs, forbiddenAccountIDs []string) error {
if len(forbiddenAccountIDs) > 0 {
for _, forbiddenAccountID := range forbiddenAccountIDs {
if accountID == forbiddenAccountID {
return fmt.Errorf("Forbidden AWS Account ID: %s", accountID)
}
}
}
if len(allowedAccountIDs) > 0 {
for _, allowedAccountID := range allowedAccountIDs {
if accountID == allowedAccountID {
return nil
}
}
return fmt.Errorf("AWS Account ID not allowed: %s", accountID)
}
return nil
}
// ValidateRegion checks if the given region is a valid AWS region.
func ValidateRegion(region string) error {
for _, partition := range endpoints.DefaultPartitions() {
for _, partitionRegion := range partition.Regions() {
if region == partitionRegion.ID() {
return nil
}
}
}
return fmt.Errorf("Invalid AWS Region: %s", region)
}