-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
115 lines (104 loc) · 2.9 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"context"
"flag"
"fmt"
"html/template"
"io"
"log"
"net/http"
"os"
"strings"
"time"
)
const metadataBase = "http://169.254.169.254/latest/meta-data/"
const defaultPortSpec = ":8080"
// AWS metadata service is very local, we can hard-timeout much sooner
const awsHTTPTimeout = 2 * time.Second
var options struct {
portspec string
}
func init() {
flag.StringVar(&options.portspec, "port", defaultPortSpec, "port to listen on for HTTP requests")
}
func addSection(ctx context.Context, w io.Writer, path string) error {
u := metadataBase + path
req, err := http.NewRequest(http.MethodGet, u, nil)
if err != nil {
return err
}
req = req.WithContext(ctx)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
_, err = fmt.Fprintf(w, "\n<h3>%s</h3>\n", template.HTMLEscapeString(path))
if err == nil {
template.HTMLEscape(w, body)
// no error return, oops
}
return err
}
func showError(w io.Writer, path string, err error) {
fmt.Fprintf(w, "\n<h3 class=\"error\">%s</h3>\n<div class=\"error errmsg\">%s</div>\n",
template.HTMLEscapeString(path), template.HTMLEscapeString(err.Error()))
}
func AddSection(ctx context.Context, w io.Writer, path string) {
if err := addSection(ctx, w, path); err != nil {
showError(w, path, err)
}
}
func rootHandle(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, "<html><head><title>AWS Info Dumper</title></head><body><h1>AWS Info Dumper</h1>\n")
childCtx, cancel := context.WithTimeout(req.Context(), awsHTTPTimeout)
defer cancel()
if p := os.Getenv("ECS_CONTAINER_METADATA_FILE"); p != "" {
io.WriteString(w, "<h2>ECS metadata from file</h2>\n")
contents, err := os.ReadFile(p)
if err != nil {
showError(w, p, err)
} else {
fmt.Fprintf(w, "\n<h3>%s</h3>\n", template.HTMLEscapeString(p))
template.HTMLEscape(w, contents)
}
} else {
io.WriteString(w, "<h2>AWS metadata service (HTTP requests)</h2>\n")
for _, section := range []string{
"hostname",
"placement/availability-zone",
"iam/info",
} {
AddSection(childCtx, w, section)
if childCtx.Err() != nil {
// any context expiration has _almost_ certainly been shown in the output of the
// AddSection error handling; there's a few nanoseconds race, so rather than
// risk aborting early without saying so, just explicitly say "hey we're done".
showError(w, "timeout", fmt.Errorf("terminated early"))
break
}
}
}
}
func parseFlagsSanely() {
envPort := os.Getenv("PORT")
if envPort != "" {
options.portspec = envPort
}
flag.Parse()
if options.portspec == "" {
options.portspec = defaultPortSpec
}
if !strings.Contains(options.portspec, ":") {
options.portspec = ":" + options.portspec
}
}
func main() {
parseFlagsSanely()
http.HandleFunc("/", rootHandle)
log.Fatal(http.ListenAndServe(options.portspec, nil))
}