-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
80 lines (63 loc) · 1.48 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
package main
import (
"context"
"flag"
"fmt"
"io"
"log"
"os"
"strings"
"golang.org/x/oauth2/google"
)
func main() {
params := ¶ms{}
params.parse()
p, err := os.ReadFile(params.jwtfile)
if err != nil {
log.Fatalln(err)
}
ctx := context.Background()
cfg, err := google.JWTConfigFromJSON(p, params.scopes...)
if err != nil {
log.Fatalln(err)
}
cfg.Subject = params.acct
client := cfg.Client(ctx)
resp, err := client.Get(params.endpoint)
if err != nil {
log.Fatalln(err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
fmt.Println("Response status code:", resp.Status, resp.StatusCode)
fmt.Println("Body:", string(body))
}
type params struct {
jwtfile string
acct string
scopes []string
endpoint string
}
func (p *params) parse() {
flag.StringVar(&p.jwtfile, "jwtfile", "", "The credentials JWT file.")
flag.StringVar(&p.acct, "delegated_account", "", "The delegated account.")
scopes := flag.String("scopes", "", "A comma-sepparated list of scopes.")
flag.StringVar(&p.endpoint, "endpoint", "", "The endpoint to test. (Only GET endpoints)")
flag.Parse()
fillFromEnvIfEmpty(&p.jwtfile, "JWT_FILE")
fillFromEnvIfEmpty(&p.acct, "DELEGATED_ACCOUNT")
fillFromEnvIfEmpty(scopes, "SCOPES")
fillFromEnvIfEmpty(&p.endpoint, "ENDPOINT")
p.scopes = strings.Split(*scopes, ",")
}
func fillFromEnvIfEmpty(v *string, envkey string) {
if v == nil {
return
}
if *v == "" {
*v = os.Getenv(envkey)
}
}