Skip to content

Commit

Permalink
Polish Go lint errors (#82)
Browse files Browse the repository at this point in the history
* Polish Go lint errors
* Fix CVE-2022-27191
* Upgrade Go to 1.18
* Revert "Fix CVE-2022-27191"
This reverts commit 8f334e6.
  • Loading branch information
mhmxs authored Apr 24, 2022
1 parent efd78f5 commit aa93511
Show file tree
Hide file tree
Showing 13 changed files with 175 additions and 97 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang@sha256:ec67c62f48ddfbca1ccaef18f9b3addccd707e1885fa28702a3954340786fcf6 as dependency
FROM golang:1.18.1 as dependency
WORKDIR /work
ADD ./go.* ./
RUN go mod download
Expand Down
3 changes: 0 additions & 3 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,6 @@ tasks:
go:integration-tests:
cmds:
- KUBECTL_CONTEXT=kind-{{.KIND_CLUSTER_NAME}} go test --tags=integration ./...
go:mod-tidy:
cmds:
- go mod tidy
example:load:
desc: load demo data
cmds:
Expand Down
29 changes: 22 additions & 7 deletions cmd/kubernetes-kms-vault/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import (
)

const (
healthPort = 8787
defaultHealthzTimeout = 20 * time.Second
hostPortFormatBase = 10

healthPort = 8787
metricsPort = "8095"
)

Expand All @@ -34,7 +37,7 @@ var (
configFilePath = flag.String("config-file-path", "./config.yaml", "Path for Vault Provider config file")
healthzPort = flag.Int("healthz-port", healthPort, "port for health check")
healthzPath = flag.String("healthz-path", "/healthz", "path for health check")
healthzTimeout = flag.Duration("healthz-timeout", 20*time.Second, "RPC timeout for health check")
healthzTimeout = flag.Duration("healthz-timeout", defaultHealthzTimeout, "RPC timeout for health check")
metricsBackend = flag.String("metrics-backend", "prometheus", "Backend used for metrics")
metricsAddress = flag.String("metrics-addr", metricsPort, "The address the metric endpoint binds to")
)
Expand All @@ -43,9 +46,11 @@ func main() {
klog.InitFlags(nil)

flag.Parse()

if *logFormatJSON {
klog.SetLogger(json.JSONLogger)
}

ctx := withShutdownSignal(context.Background())

// initialize metrics exporter
Expand All @@ -55,20 +60,24 @@ func main() {
klog.Errorln(err)
os.Exit(1)
}

klog.Fatalln("metrics service has stopped gracefully")
}()

klog.InfoS("Starting VaultEncryptionServiceServer service", "version", version.BuildVersion, "buildDate", version.BuildDate)

cfg, err := config.New(*configFilePath)
if err != nil {
klog.Errorln(err)
os.Exit(1)
}

proto, addr, err := utils.ParseEndpoint(*listenAddr)
if err != nil {
klog.Errorln(err)
os.Exit(1)
}

listener, err := net.Listen(proto, addr)
if err != nil {
klog.Errorln(err)
Expand All @@ -82,34 +91,39 @@ func main() {
s := grpc.NewServer(opts...)
kmsServer, err := server.New(ctx, cfg)
pb.RegisterKeyManagementServiceServer(s, kmsServer)

if err != nil {
klog.Errorln(fmt.Errorf("failed to listen: %w", err))
os.Exit(1)
}

klog.Infof("Listening for connections on address: %v", listener.Addr())

go func() {
err := s.Serve(listener)
if err != nil {
if err := s.Serve(listener); err != nil {
klog.Errorln(err)
os.Exit(1)
}

klog.Fatalln("GRPC service has stopped gracefully")
}()

healthz := &server.HealthZ{
Service: kmsServer,
HealthCheckURL: &url.URL{
Host: net.JoinHostPort("", strconv.FormatUint(uint64(*healthzPort), 10)),
Host: net.JoinHostPort("", strconv.FormatUint(uint64(*healthzPort), hostPortFormatBase)),
Path: *healthzPath,
},
UnixSocketPath: listener.Addr().String(),
RPCTimeout: *healthzTimeout,
}

go func() {
err := healthz.Serve()
if err != nil {
if err := healthz.Serve(); err != nil {
klog.Errorln(err)
os.Exit(1)
}

klog.Fatalln("healtz service has stopped gracefully")
}()

Expand All @@ -135,5 +149,6 @@ func withShutdownSignal(ctx context.Context) context.Context {
klog.Info("received shutdown signal")
cancel()
}()

return nctx
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/ondat/trousseau

go 1.17
go 1.18

replace github.com/ondat/trousseau => ./

Expand Down
7 changes: 5 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,22 @@ type ProviderConfig interface {
func New(cfpPath string) (ProviderConfig, error) {
klog.V(klogv).Infof("Populating AppConfig from %s", cfpPath)
viper.SetConfigType("yaml")

file, err := os.ReadFile(filepath.Clean(cfpPath))
if err != nil {
return nil, fmt.Errorf("unable to open config file %s: %w", cfpPath, err)
}

err = viper.ReadConfig(bytes.NewBuffer(file))
if err != nil {
return nil, fmt.Errorf("unable to read config file %s: %w", cfpPath, err)
}

var cfg appConfig
err = viper.Unmarshal(&cfg)
if err != nil {
if err = viper.Unmarshal(&cfg); err != nil {
return nil, fmt.Errorf("unable to unmarshal config file %s: %w", cfpPath, err)
}

return &cfg, nil
}

Expand Down
12 changes: 9 additions & 3 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ vault:

func TestMain(m *testing.M) {
setUp()

retCode := m.Run()

tearDown()

os.Exit(retCode)
}

Expand All @@ -30,22 +33,24 @@ func setUp() {
if err != nil {
log.Fatal(err)
}
defer f.Close()

_, err = f.Write(data)
f.Close()

if err != nil {
log.Fatal(err)
}
}

func tearDown() {
err := os.Remove(file)
if err != nil {
if err := os.Remove(file); err != nil {
log.Fatal(err)
}
}

func TestParseProvderInConfig(t *testing.T) {
r, err := cfg.New(file)

assert.NoError(t, err)
assert.Equal(t, "vault", r.GetProvider(), "Provider should return vault")
}
Expand All @@ -54,6 +59,7 @@ func TestParseVaultAddressInConfig(t *testing.T) {
r, err := cfg.New(file)

vaultCfg := r.GetVaultConfig()

assert.NoError(t, err)
assert.Equal(t, "http://localhost:9200", vaultCfg.Address, "Config should return vault address")
}
Expand Down
Loading

0 comments on commit aa93511

Please sign in to comment.