/
aws.go
124 lines (104 loc) · 3 KB
/
aws.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
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
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"regexp"
"sort"
"strings"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
func AWSGenerate(ctx context.Context) error {
fmt.Println("running aws generation...")
if err := awsGenerateIAM(ctx); err != nil {
return err
}
fmt.Println("aws generation completed...")
return nil
}
func awsGenerateIAM(ctx context.Context) error {
response, err := http.Get("https://awspolicygen.s3.amazonaws.com/js/policies.js")
if err != nil {
return err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code: %d", response.StatusCode)
}
body, err := io.ReadAll(response.Body)
if err != nil {
return err
}
body = []byte(strings.TrimPrefix(string(body), "app.PolicyEditorConfig="))
type Service struct {
StringPrefix string `json:"StringPrefix"`
Actions []string `json:"Actions"`
ARNFormat string `json:"ARNFormat"`
ARNRegex string `json:"ARNRegex"`
ConditionKeys []string `json:"conditionKeys"`
HasResource bool `json:"HasResource"`
}
type Policies struct {
ServiceMap map[string]Service `json:"serviceMap"`
}
policies := Policies{}
if err := json.Unmarshal(body, &policies); err != nil {
return err
}
actionsOutput := bytes.Buffer{}
actionsOutput.WriteString("// THIS FILE IS GENERATED VIA MAGE. DO NOT EDIT MANUALLY!\n\n")
counter := len(policies.ServiceMap)
keys := []string{}
for k := range policies.ServiceMap {
keys = append(keys, k)
}
sort.Slice(keys, func(i, j int) bool {
return keys[i] < keys[j]
})
for _, k := range keys {
formattedService := k
toReplace := []string{
"(",
")",
"-",
".",
"Amazon",
"AWS",
}
for _, t := range toReplace {
formattedService = strings.ReplaceAll(formattedService, t, "")
}
if formattedService[0] == '_' {
formattedService = formattedService[1:]
}
// We have to remove spaces after we removed all the other characters and converted it to PascalCase.
formattedService = cases.Title(language.AmericanEnglish).String(strings.ToLower(formattedService))
formattedService = strings.ReplaceAll(formattedService, " ", "")
actionsOutput.WriteString(fmt.Sprintf("export enum %s {\n", formattedService))
for _, a := range policies.ServiceMap[k].Actions {
exp, err := regexp.Compile("([A-Z])")
if err != nil {
return err
}
// Do some formatting to turn this 'BatchDetectDominantLanguage' into
// 'BATCH_DETECT_DOMINANT_LANGUAGE = "comprehend:BatchDetectDominantLanguage",'.
formattedKey := strings.ToUpper(strings.TrimSpace(string(exp.ReplaceAll([]byte(a), []byte("_$1"))))[1:])
actionsOutput.WriteString(fmt.Sprintf(" %s = \"%s\",\n", formattedKey, a))
}
if counter == 1 {
actionsOutput.WriteString("}\n")
} else {
actionsOutput.WriteString("}\n\n")
}
counter--
}
if err := os.WriteFile("./packages/aws/iam/action/action.ts", actionsOutput.Bytes(), 0o744); err != nil {
return err
}
return nil
}