-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
156 lines (130 loc) · 3.54 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"context"
"fmt"
"net/http"
"sync"
"time"
"github.com/gorilla/websocket"
"github.com/julienschmidt/httprouter"
"github.com/paralus/paralus/pkg/audit"
authv3 "github.com/paralus/paralus/pkg/auth/v3"
logv2 "github.com/paralus/paralus/pkg/log"
sentryrpcv2 "github.com/paralus/paralus/proto/rpc/sentry"
systemrpc "github.com/paralus/paralus/proto/rpc/system"
userrpc "github.com/paralus/paralus/proto/rpc/user"
"github.com/paralus/prompt/debug"
"github.com/spf13/viper"
"github.com/urfave/negroni"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
)
const (
apiPortEnv = "API_PORT"
sentryAddrEnv = "SENTRY_ADDR"
tmpPathEnv = "TEMP_PATH"
devEnv = "DEV"
kubectlBinEnv = "KUBECTL_BIN"
auditFileEnv = "AUDIT_LOG_FILE"
usernameEnv = "USER_NAME"
)
var (
apiPort int
sentryAddr string
tmpPath string
dev bool
kubectlBin string
auditFile string
sp sentryrpcv2.SentryPool
pp systemrpc.SystemPool
ugp userrpc.UGPool
_log = logv2.GetLogger()
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
Subprotocols: []string{"binary"},
}
func setup() {
viper.SetDefault(apiPortEnv, 7009)
viper.SetDefault(sentryAddrEnv, "localhost:10000")
viper.SetDefault(tmpPathEnv, "/tmp")
viper.SetDefault(devEnv, true)
viper.SetDefault(kubectlBinEnv, "/usr/local/bin/kubectl")
viper.SetDefault(auditFileEnv, "/var/log/ztka-prompt/audit.log")
viper.SetDefault(usernameEnv, "")
viper.BindEnv(apiPortEnv)
viper.BindEnv(sentryAddrEnv)
viper.BindEnv(tmpPathEnv)
viper.BindEnv(devEnv)
viper.BindEnv(kubectlBinEnv)
viper.BindEnv(auditFileEnv)
viper.BindEnv(usernameEnv)
apiPort = viper.GetInt(apiPortEnv)
sentryAddr = viper.GetString(sentryAddrEnv)
tmpPath = viper.GetString(tmpPathEnv)
dev = viper.GetBool(devEnv)
kubectlBin = viper.GetString(kubectlBinEnv)
auditFile = viper.GetString(auditFileEnv)
sp = sentryrpcv2.NewSentryPool(sentryAddr, 10)
pp = systemrpc.NewSystemPool(sentryAddr, 10)
ugp = userrpc.NewUGPool(sentryAddr, 10)
}
func runAPI(wg *sync.WaitGroup, ctx context.Context) {
defer wg.Done()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
ao := audit.AuditOptions{
LogPath: auditFile,
MaxSizeMB: 1,
MaxBackups: 10, // Should we let sidecar do rotation?
MaxAgeDays: 10, // Make these configurable via env
}
auditLogger := audit.GetAuditLogger(&ao)
dh := debug.NewDebugHandler(sp, pp, ugp, tmpPath, kubectlBin, auditLogger)
r := httprouter.New()
r.Handle("GET", "/v2/debug/prompt/project/:project/cluster/:cluster_name", dh)
n := negroni.New(
negroni.NewRecovery(),
)
if !dev {
o := authv3.Option{}
n.Use(authv3.NewRemoteAuthMiddleware(auditLogger, sentryAddr, o))
}
n.UseHandler(r)
s := http.Server{
Addr: fmt.Sprintf(":%d", apiPort),
Handler: n,
}
go func() {
_log.Infow("starting debug prompt server", "port", apiPort)
err := s.ListenAndServe()
if err != nil {
if err == http.ErrServerClosed {
_log.Infow("debug prompt server shutdown")
return
}
_log.Fatalw("unable to debug prompt server", "error", err)
}
}()
// cleanup unused system sessions cachedir
pctx, pcancel := context.WithCancel(context.Background())
defer pcancel()
go debug.PruneCacheDirs(pctx, tmpPath)
<-ctx.Done()
_log.Infow("shutting down debug prompt server")
ctx, cancel = context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
s.Shutdown(ctx)
}
func run() {
ctx := signals.SetupSignalHandler()
var wg sync.WaitGroup
wg.Add(1)
go runAPI(&wg, ctx)
<-ctx.Done()
wg.Wait()
}
func main() {
setup()
run()
}