-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
93 lines (81 loc) · 2.43 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
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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strconv"
helpers "github.com/ww-tech/aws-sts-proxy/helpers"
)
var (
assumeRole = os.Getenv("EKS_ASSUME_ROLE")
externalID = os.Getenv("EXTERNAL_ID")
stringRequirement = helpers.GetEnv("STRING_REQUIREMENT", "")
port = helpers.GetEnv("PORT", "8080")
hc = helpers.GetEnv("HEALTHCHECK", "/hc")
)
func main() {
if assumeRole == "" || externalID == "" {
fmt.Println("Must export EKS_ASSUME_ROLE")
fmt.Println("Must export EXTERNAL_ID")
os.Exit(1)
}
helper := helpers.Helper{
AssumeRole: assumeRole,
ExternalID: externalID,
StringRequirement: stringRequirement,
}
http.HandleFunc(hc, func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok"))
})
http.HandleFunc("/sts/token", func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("Authorization")
role := r.URL.Query().Get("RoleArn")
durationString := r.URL.Query().Get("Duration")
externalID := r.URL.Query().Get("ExternalID")
if durationString == "" {
durationString = "60"
}
duration, err := strconv.ParseInt(durationString, 10, 64)
if err != nil {
fmt.Println("ERROR => " + err.Error())
errorHandler(w, r, http.StatusUnauthorized)
return
}
stsSession, err := helper.GetSTSToken(token, role, duration, externalID)
if err != nil {
fmt.Println("ERROR => " + err.Error())
errorHandler(w, r, http.StatusUnauthorized)
return
}
stsJSON, err := json.Marshal(stsSession)
if err != nil {
fmt.Println("ERROR => " + err.Error())
errorHandler(w, r, http.StatusUnauthorized)
return
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, string(stsJSON))
})
log.Printf("listening on http://%s/", "0.0.0.0:"+port)
log.Fatal(http.ListenAndServe("0.0.0.0:"+port, nil))
}
func errorHandler(w http.ResponseWriter, r *http.Request, status int) {
var b map[string]string
var err error
w.WriteHeader(status)
if status == http.StatusNotFound {
err = json.Unmarshal([]byte(`{"error": "Custom 404"}`), &b)
} else if status == http.StatusTeapot {
err = json.Unmarshal([]byte(`{"error": "Something went wrong"}`), &b)
} else if status == http.StatusUnauthorized {
err = json.Unmarshal([]byte(`{"error": "Not Authorized"}`), &b)
} else {
err = json.Unmarshal([]byte(`{"error": "Bad Request"}`), &b)
}
if err != nil {
fmt.Println("ERROR => ", err)
}
json.NewEncoder(w).Encode(b)
}