-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
62 lines (50 loc) · 1.23 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
const PolicyUrl = "https://awspolicygen.s3.amazonaws.com/js/policies.js"
const PolicyFilename = "reference.json"
func Check(err error) {
if err != nil {
panic(err)
}
}
// Load the policymap, save to `reference.json` and print to stdout
func main() {
// Download the policymap
resp, err := http.Get(PolicyUrl)
Check(err)
contents, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
Check(err)
// Parse it
policyBytes := ParsePolicyJs(contents)
prettyPolicy := BytesToPrettyJson(policyBytes)
// Write and print to stdout
ioutil.WriteFile(PolicyFilename, prettyPolicy, 0644)
fmt.Printf("%s", prettyPolicy)
}
// Parses the raw .js file and returns a JSON byte string
func ParsePolicyJs(policy []byte) []byte {
var separatorIndex int
separator := []byte("{")[0]
for i, v := range policy {
if v == separator {
separatorIndex = i
break
}
}
return policy[separatorIndex:]
}
// Turn JSON bytes into a pretty printed string
func BytesToPrettyJson(jsonBytes []byte) []byte {
var jsonIface interface{}
err := json.Unmarshal(jsonBytes, &jsonIface)
Check(err)
printedBytes, err := json.MarshalIndent(jsonIface, "", " ")
Check(err)
return printedBytes
}