-
Notifications
You must be signed in to change notification settings - Fork 5
/
root.go
135 lines (117 loc) · 3.24 KB
/
root.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
125
126
127
128
129
130
131
132
133
134
135
package cmd
import (
"context"
"github.com/evalphobia/logrus_sentry"
"github.com/honeycombio/beeline-go"
"github.com/kelseyhightower/envconfig"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var clientID string
var issuerURL string
var roleARN string
const (
flagVerbose = "verbose"
successMessage = `<h1>Success!</h1><p>You are now authenticated with AWS; this temporary session
will allow you to run AWS commmands from the command line.</p><p> When running
aws-cli commands, be sure to specify your profile in one of the following ways:</p>
<code>$ aws --profile <profile-name> <command></code><br/>
<code>$ AWS_PROFILE=<profile-name> aws <command></code><br/>
<p> Feel free to <a href="#" onclick="window.close();">close this window</a> whenever</p>
`
)
type SentryEnvironment struct {
DSN string
}
func loadSentryEnv() (*SentryEnvironment, error) {
env := &SentryEnvironment{}
err := envconfig.Process("SENTRY", env)
if err != nil {
return env, errors.Wrap(err, "Unable to load all the environment variables")
}
return env, nil
}
type HoneycombEnvironment struct {
SECRET_KEY string
DATASET_NAME string `default:"aws-oidc"`
SERVICE_NAME string `default:"aws-oidc"`
}
func loadHoneycombEnv() (*HoneycombEnvironment, error) {
env := &HoneycombEnvironment{}
err := envconfig.Process("HONEYCOMB", env)
if err != nil {
return env, errors.Wrap(err, "Unable to load all the honeycomb environment variables")
}
return env, nil
}
func init() {
rootCmd.PersistentFlags().BoolP(flagVerbose, "v", false, "Use this to enable verbose mode")
}
var rootCmd = &cobra.Command{
Use: "aws-oidc",
SilenceUsage: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// parse flags
verbose, err := cmd.Flags().GetBool(flagVerbose)
if err != nil {
return errors.Wrap(err, "Missing verbose flag")
}
if verbose {
log.SetLevel(log.DebugLevel)
log.SetReportCaller(true)
}
err = configureLogrusHooks()
if err != nil {
return errors.Wrap(err, "Unable to configure Logrus Hooks")
}
err = configureHoneycombTelemetry()
if err != nil {
return errors.Wrap(err, "Unable to set up Honeycomb Telemetry")
}
return nil
},
}
func configureLogrusHooks() error {
sentryEnv, err := loadSentryEnv()
if err != nil {
return err
}
// if env var not set, ignore
if sentryEnv.DSN == "" {
log.Debug("Sentry DSN not set. Skipping Sentry Configuration")
return nil
}
sentryHook, err := logrus_sentry.NewSentryHook(sentryEnv.DSN, []log.Level{
log.PanicLevel,
log.FatalLevel,
log.ErrorLevel,
})
if err != nil {
log.Errorf("Error configuring Sentry")
return nil
}
log.AddHook(sentryHook)
return nil
}
func configureHoneycombTelemetry() error {
honeycombEnv, err := loadHoneycombEnv()
if err != nil {
return err
}
// if env var not set, ignore
if honeycombEnv.SECRET_KEY == "" {
log.Debug("Honeycomb Secret Key not set. Skipping Honeycomb Configuration")
return nil
}
beeline.Init(beeline.Config{
WriteKey: honeycombEnv.SECRET_KEY,
Dataset: honeycombEnv.DATASET_NAME,
ServiceName: honeycombEnv.SERVICE_NAME,
})
return nil
}
func Execute(ctx context.Context) error {
defer beeline.Close()
return rootCmd.ExecuteContext(ctx)
}