Skip to content

Commit 7cd26ff

Browse files
committed
Auth Proxy add cli and logging
1 parent 54c7cd5 commit 7cd26ff

File tree

7 files changed

+813
-80
lines changed

7 files changed

+813
-80
lines changed

auth_proxy/LICENSE

Whitespace-only changes.

auth_proxy/cmd/root.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/kilimnik/vepiot/auth_proxy/proxy"
8+
"github.com/spf13/cobra"
9+
"github.com/spf13/viper"
10+
"go.uber.org/zap"
11+
"go.uber.org/zap/zapcore"
12+
)
13+
14+
var rootCmd = &cobra.Command{
15+
Use: "auth_proxy",
16+
Short: "Auth Proxy for Vepiot Hashicorp Plugin",
17+
Run: func(cmd *cobra.Command, args []string) {
18+
logger := ConfigureLogger(cmd)
19+
20+
host := viper.GetString("host")
21+
port := viper.GetInt("port")
22+
23+
server := &proxy.Server{
24+
Logger: logger,
25+
Host: host,
26+
Port: port,
27+
28+
TOTPs: make(map[string]*proxy.TOTP),
29+
}
30+
31+
server.Start()
32+
},
33+
}
34+
35+
func init() {
36+
cobra.OnInitialize(initConfig)
37+
38+
rootCmd.PersistentFlags().CountP("verbose", "v", "counted verbosity")
39+
rootCmd.PersistentFlags().Bool("no-color", false, "disable colored logging")
40+
rootCmd.PersistentFlags().String("log-format", "console", "logging format (console, json)")
41+
rootCmd.PersistentFlags().String("host", "0.0.0.0", "Host for Proxy Server")
42+
rootCmd.PersistentFlags().IntP("port", "p", 8080, "Port for Proxy Server")
43+
44+
viper.BindPFlag("verbose", rootCmd.PersistentFlags().Lookup("verbose"))
45+
viper.BindPFlag("no_color", rootCmd.PersistentFlags().Lookup("no-color"))
46+
viper.BindPFlag("log_format", rootCmd.PersistentFlags().Lookup("log-format"))
47+
viper.BindPFlag("host", rootCmd.PersistentFlags().Lookup("host"))
48+
viper.BindPFlag("port", rootCmd.PersistentFlags().Lookup("port"))
49+
}
50+
51+
func initConfig() {
52+
viper.SetEnvPrefix("proxy")
53+
viper.AutomaticEnv()
54+
55+
if err := viper.ReadInConfig(); err == nil {
56+
fmt.Println("Using config file:", viper.ConfigFileUsed())
57+
}
58+
}
59+
60+
func ConfigureLogger(cmd *cobra.Command) *zap.SugaredLogger {
61+
verbosity := viper.GetInt("verbose")
62+
disableColor := viper.GetBool("no_color")
63+
logFormat := viper.GetString("log_format")
64+
65+
var logLevel = zap.WarnLevel
66+
if verbosity == 1 {
67+
logLevel = zap.InfoLevel
68+
} else if verbosity > 1 {
69+
logLevel = zap.DebugLevel
70+
}
71+
72+
var encodeLevel = zapcore.CapitalColorLevelEncoder
73+
if disableColor {
74+
encodeLevel = zapcore.CapitalLevelEncoder
75+
}
76+
77+
cfg := zap.Config{
78+
Level: zap.NewAtomicLevelAt(logLevel),
79+
Development: verbosity > 0,
80+
Encoding: logFormat,
81+
EncoderConfig: zapcore.EncoderConfig{
82+
// Keys can be anything except the empty string.
83+
TimeKey: "T",
84+
LevelKey: "L",
85+
NameKey: "N",
86+
CallerKey: "C",
87+
FunctionKey: zapcore.OmitKey,
88+
MessageKey: "M",
89+
StacktraceKey: "S",
90+
LineEnding: zapcore.DefaultLineEnding,
91+
EncodeLevel: encodeLevel,
92+
EncodeTime: zapcore.ISO8601TimeEncoder,
93+
EncodeDuration: zapcore.StringDurationEncoder,
94+
EncodeCaller: zapcore.ShortCallerEncoder,
95+
},
96+
OutputPaths: []string{"stdout"},
97+
ErrorOutputPaths: []string{"stderr"},
98+
}
99+
logger, err := cfg.Build()
100+
if err != nil {
101+
panic(err)
102+
}
103+
defer logger.Sync() // flushes buffer, if any
104+
sugar := logger.Sugar()
105+
106+
sugar.Info("Logger Initialized")
107+
108+
return sugar
109+
}
110+
111+
func Execute() {
112+
err := rootCmd.Execute()
113+
if err != nil {
114+
os.Exit(1)
115+
}
116+
}

auth_proxy/go.mod

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,33 @@ go 1.18
44

55
require (
66
github.com/bufbuild/connect-go v1.4.1
7-
github.com/golang/protobuf v1.5.0
7+
github.com/golang/protobuf v1.5.2
8+
github.com/spf13/cobra v1.6.1
89
golang.org/x/net v0.4.0
910
)
1011

1112
require (
13+
github.com/fsnotify/fsnotify v1.6.0 // indirect
14+
github.com/hashicorp/hcl v1.0.0 // indirect
15+
github.com/inconshreveable/mousetrap v1.0.1 // indirect
16+
github.com/magiconair/properties v1.8.7 // indirect
17+
github.com/mitchellh/mapstructure v1.5.0 // indirect
18+
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
19+
github.com/spf13/afero v1.9.3 // indirect
20+
github.com/spf13/cast v1.5.0 // indirect
21+
github.com/spf13/jwalterweatherman v1.1.0 // indirect
22+
github.com/spf13/pflag v1.0.5 // indirect
23+
github.com/subosito/gotenv v1.4.2 // indirect
24+
go.uber.org/atomic v1.9.0 // indirect
25+
go.uber.org/multierr v1.8.0 // indirect
26+
golang.org/x/sys v0.3.0 // indirect
27+
gopkg.in/ini.v1 v1.67.0 // indirect
28+
gopkg.in/yaml.v3 v3.0.1 // indirect
29+
)
30+
31+
require (
32+
github.com/spf13/viper v1.15.0
33+
go.uber.org/zap v1.24.0
1234
golang.org/x/text v0.5.0 // indirect
1335
google.golang.org/protobuf v1.28.1
1436
)

0 commit comments

Comments
 (0)