Skip to content

Commit

Permalink
Merge pull request #25 from Seagate/FMW-71949_slog
Browse files Browse the repository at this point in the history
slog and minimize configuration
  • Loading branch information
jskazinski authored Oct 16, 2024
2 parents fc82b82 + e859afe commit 65caa95
Show file tree
Hide file tree
Showing 24 changed files with 533 additions and 1,560 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ build
vendor
/pykmip-server/server.log
/pykmip-server/server.db
cmd/kms/kms
kms
30 changes: 29 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
.PHONY : all build builddir install ppkmip kmipgen lint vet clean fmt generate test cover up down docker bash fish tidy update tools pykmip-server gen-certs

SHELL = bash
BUILD_FLAGS =
TEST_FLAGS =
COMPOSE ?= docker-compose
APP_NAME := kms

help:
@echo ""
@echo "-----------------------------------------------------------------------------------"
@echo "make all - remove all"
@echo "make build - build a local executable"
@echo "make install - install the executable"
@echo "make clean - remove build files"
@echo "make tidy - go mod tidy"
@echo "make update - go get -u ./..."
@echo "make lint - golangci-lint run"
@echo "make vet - go vet ./..."
@echo "make fmt - gofumpt -w -l ."
@echo "make kms - build $(APP_NAME)"
@echo "make installkms - install $(APP_NAME)"
@echo ""

all: fmt build up test lint

Expand All @@ -26,8 +45,12 @@ kmipgen:
lint:
golangci-lint run

vet:
go vet ./...

clean:
rm -rf build/*
rm -rf $(APP_NAME)

fmt:
gofumpt -w -l .
Expand Down Expand Up @@ -100,5 +123,10 @@ pykmip-server: up
gen-certs:
openssl req -x509 -newkey rsa:4096 -keyout pykmip-server/server.key -out pykmip-server/server.cert -days 3650 -nodes -subj '/CN=localhost'

.PHONY: all build builddir run artifacts vet lint clean fmt test testall testreport up down pull builder runc ci bash fish image prep vendor.update vendor.ensure tools buildtools migratetool db.migrate
kms: cmd/$(APP_NAME)/main.go
@echo "Build local $(APP_NAME)..."
go build -o $(APP_NAME) -ldflags "-X main.buildTime=`date -u '+%Y-%m-%dT%H:%M:%S'`" ./cmd/$(APP_NAME)/main.go
ls -lh $(APP_NAME)

installkms:
install ./$(APP_NAME) /usr/local/bin
1 change: 0 additions & 1 deletion cmd/kmipgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,6 @@ func genCode(s *Specifications) (string, error) {
template.Must(tmpl.New("mask").Parse(maskTmpl))

err = tmpl.Execute(buf, in)

if err != nil {
return "", merry.Prepend(err, "executing template")
}
Expand Down
7 changes: 7 additions & 0 deletions cmd/kms/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# [1.3.0] (2024-10-15)

### Features
- Switch to using the standard package `slog`
- Reduce `kmipapi.ConfigurationSettings` to minimal required settings, remove tls.Conn pointer
- Modify all interfaces to pass `connection *tls.Conn`

# [1.2.0] (2022-10-10)

### Features
Expand Down
30 changes: 0 additions & 30 deletions cmd/kms/Makefile

This file was deleted.

38 changes: 27 additions & 11 deletions cmd/kms/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,26 @@ package main
import (
"bufio"
"context"
"crypto/tls"
"flag"
"fmt"
"log/slog"
"os"

"github.com/Seagate/kmip-go/pkg/common"
"github.com/Seagate/kmip-go/src/handlers"
"github.com/Seagate/kmip-go/src/kmipapi"
"k8s.io/klog/v2"
)

const version string = "1.2.0"
const version string = "1.3.0"

// This variable is filled in during the linker step - -ldflags "-X main.buildTime=`date -u '+%Y-%m-%dT%H:%M:%S'`"
var buildTime = ""

var programLevel = new(slog.LevelVar) // Info by default

// This variable is used to store the TLS connection for an open session with a KMS server
var tlsConnection *tls.Conn = nil

// init: called once during program execution
func init() {
Expand All @@ -21,9 +31,6 @@ func init() {

// main: the main application
func main() {
klog.InitFlags(nil)
klog.EnableContextualLogging(true)

flag.Usage = func() {
_, _ = fmt.Fprintf(flag.CommandLine.Output(), "[] kms (version=%s) usage:\n", version)
_, _ = fmt.Fprintln(flag.CommandLine.Output(), "")
Expand All @@ -35,29 +42,38 @@ func main() {
}

var usage bool
var debug bool

flag.BoolVar(&usage, "h", false, "Show usage message.")
flag.BoolVar(&debug, "d", false, "Enable debug log level.")
flag.Parse()

if usage {
flag.Usage()
os.Exit(0)
}

fmt.Printf("[] kms (version=%s)\n\n", version)
// Initialize the slog logger
logger := slog.Default()
if debug {
slog.SetLogLoggerLevel(slog.LevelDebug)
}

fmt.Printf("\n[] kms (version=%s %s)\n\n", version, buildTime)

settings := kmipapi.ConfigurationSettings{
ProtocolVersionMajor: 1,
ProtocolVersionMinor: 4,
ServiceType: kmipapi.KMIP14Service,
}

ctx := context.Background()
ctx := context.WithValue(context.Background(), common.LoggerKey, logger)

// Restore any previously stored configuration settings
filename := "kms.json"
if _, err := os.Stat(filename); err == nil {
kmipapi.Restore(ctx, &settings, filename)
err := kmipapi.Restore(ctx, &settings, "")
if err != nil {
fmt.Printf("ERROR: restoring kms configuration data, error: %v", err)
os.Exit(1)
}

scanner := bufio.NewScanner(os.Stdin)
Expand All @@ -70,7 +86,7 @@ func main() {
os.Exit(0)
}
if line != "" {
handlers.Execute(ctx, &settings, line)
handlers.Execute(ctx, &tlsConnection, &settings, line)
fmt.Println("")
}
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/ppkmip/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
"os"
"strings"

_ "github.com/Seagate/kmip-go/kmip14"
_ "github.com/Seagate/kmip-go/kmip20"
"github.com/Seagate/kmip-go/ttlv"
)

Expand Down Expand Up @@ -187,6 +185,7 @@ xml format:
}

printTTLV(outFormat, raw, count)

count++
}
case FormatHex:
Expand Down
10 changes: 3 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
module github.com/Seagate/kmip-go

go 1.21
go 1.22

require (
github.com/ansel1/merry v1.8.0
github.com/fatih/color v1.17.0
github.com/gemalto/flume v0.13.1
github.com/google/uuid v1.6.0
github.com/stretchr/testify v1.9.0
golang.org/x/text v0.17.0
k8s.io/klog/v2 v2.130.1
golang.org/x/text v0.19.0
)

require (
github.com/ansel1/merry/v2 v2.2.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/sys v0.26.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 65caa95

Please sign in to comment.