Skip to content

Commit

Permalink
Replace logger with slogger in powereventwatcher, debug server, compa…
Browse files Browse the repository at this point in the history
…ctdb, and keyidentifier
  • Loading branch information
RebeccaMahany committed Feb 26, 2024
1 parent 73d57b3 commit 0a76d94
Show file tree
Hide file tree
Showing 18 changed files with 172 additions and 121 deletions.
4 changes: 2 additions & 2 deletions cmd/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func runLauncher(ctx context.Context, cancel func(), multiSlogger, systemMultiSl
}

debugAddrPath := filepath.Join(rootDirectory, "debug_addr")
debug.AttachDebugHandler(debugAddrPath, logger)
debug.AttachDebugHandler(debugAddrPath, slogger)
defer os.Remove(debugAddrPath)

// open the database for storing launcher data, we do it here
Expand Down Expand Up @@ -281,7 +281,7 @@ func runLauncher(ctx context.Context, cancel func(), multiSlogger, systemMultiSl
// For now, remediation is not performed -- we only log the hardware change.
agent.DetectAndRemediateHardwareChange(ctx, k)

powerEventWatcher, err := powereventwatcher.New(ctx, k, log.With(logger, "component", "power_event_watcher"))
powerEventWatcher, err := powereventwatcher.New(ctx, k, slogger)
if err != nil {
slogger.Log(ctx, slog.LevelDebug,
"could not init power event watcher",
Expand Down
20 changes: 16 additions & 4 deletions cmd/launcher/run_compactdb.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package main

import (
"context"
"errors"
"log/slog"
"os"
"path/filepath"

"github.com/go-kit/kit/log/level"
"github.com/kolide/kit/logutil"
"github.com/kolide/launcher/ee/agent"
"github.com/kolide/launcher/pkg/launcher"
"github.com/kolide/launcher/pkg/log/multislogger"
)

func runCompactDb(args []string) error {
Expand All @@ -21,7 +23,14 @@ func runCompactDb(args []string) error {
}

// relevel
logger := logutil.NewServerLogger(opts.Debug)
slogLevel := slog.LevelInfo
if opts.Debug {
slogLevel = slog.LevelDebug
}

slogger := multislogger.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: slogLevel,
})).Logger

boltPath := filepath.Join(opts.RootDirectory, "launcher.db")

Expand All @@ -30,7 +39,10 @@ func runCompactDb(args []string) error {
return err
}

level.Info(logger).Log("msg", "Done compacting. Safe to remove old db", "path", oldDbPath)
slogger.Log(context.TODO(), slog.LevelInfo,
"done compacting, safe to remove old db",
"path", oldDbPath,
)

return nil
}
4 changes: 2 additions & 2 deletions ee/keyidentifier/examples/key-identifier/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"os"

"github.com/kolide/kit/logutil"
"github.com/kolide/launcher/ee/keyidentifier"
"github.com/kolide/launcher/pkg/log/multislogger"
)

func main() {
Expand All @@ -18,7 +18,7 @@ func main() {
}

func testIdentifyFile(path string) error {
kIdentifer, _ := keyidentifier.New(keyidentifier.WithLogger(logutil.NewCLILogger(true)))
kIdentifer, _ := keyidentifier.New(keyidentifier.WithSlogger(multislogger.New().Logger))

ki, err := kIdentifer.IdentifyFile(path)
if err != nil {
Expand Down
53 changes: 27 additions & 26 deletions ee/keyidentifier/keyidentifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package keyidentifier

import (
"bytes"
"context"
"crypto/ecdsa"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"log/slog"
"os"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/kolide/launcher/pkg/log/multislogger"

"golang.org/x/crypto/ssh"
)
Expand All @@ -29,35 +30,35 @@ type KeyInfo struct {
}

// keyidentifier attempts to identify a key. It uses a set of
// herusitics to try to guiess what kind, what size, and whether or
// heuristics to try to guiess what kind, what size, and whether or
// not it's encrypted.
type KeyIdentifier struct {
logger log.Logger
slogger *slog.Logger
}

type Option func(*KeyIdentifier)

func WithLogger(logger log.Logger) Option {
return func(kIdentifer *KeyIdentifier) {
kIdentifer.logger = logger
func WithSlogger(slogger *slog.Logger) Option {
return func(kIdentifier *KeyIdentifier) {
kIdentifier.slogger = slogger.With("component", "key_identifier")
}
}

func New(opts ...Option) (*KeyIdentifier, error) {
kIdentifer := &KeyIdentifier{
logger: log.NewNopLogger(),
kIdentifier := &KeyIdentifier{
slogger: multislogger.NewNopLogger(),
}

for _, opt := range opts {
opt(kIdentifer)
opt(kIdentifier)
}

return kIdentifer, nil
return kIdentifier, nil
}

func (kIdentifer *KeyIdentifier) IdentifyFile(path string) (*KeyInfo, error) {
level.Debug(kIdentifer.logger).Log(
"msg", "starting a key identification",
func (kIdentifier *KeyIdentifier) IdentifyFile(path string) (*KeyInfo, error) {
kIdentifier.slogger.Log(context.TODO(), slog.LevelDebug,
"starting a key identification",
"file", path,
)

Expand All @@ -66,7 +67,7 @@ func (kIdentifer *KeyIdentifier) IdentifyFile(path string) (*KeyInfo, error) {
return nil, fmt.Errorf("reading file %s: %w", path, err)
}

ki, err := kIdentifer.Identify(keyBytes)
ki, err := kIdentifier.Identify(keyBytes)
if err != nil {
return nil, fmt.Errorf("identifying key: %w", err)
}
Expand All @@ -77,9 +78,9 @@ func (kIdentifer *KeyIdentifier) IdentifyFile(path string) (*KeyInfo, error) {
// Identify uses a manually curated set of heuristics to determine
// what kind of key something is. Generally speaking, we consider `err
// == nil` as success, and throw away errors as unparsable keys.
func (kIdentifer *KeyIdentifier) Identify(keyBytes []byte) (*KeyInfo, error) {
level.Debug(kIdentifer.logger).Log(
"msg", "starting a key identification",
func (kIdentifier *KeyIdentifier) Identify(keyBytes []byte) (*KeyInfo, error) {
kIdentifier.slogger.Log(context.TODO(), slog.LevelDebug,
"starting a key identification",
"file", "<bytestream>",
)

Expand All @@ -94,7 +95,7 @@ func (kIdentifer *KeyIdentifier) Identify(keyBytes []byte) (*KeyInfo, error) {
}

// If nothing else fits. treat it like a pem
if ki, err := kIdentifer.attemptPem(keyBytes); err == nil {
if ki, err := kIdentifier.attemptPem(keyBytes); err == nil {
return ki, nil
}

Expand All @@ -105,7 +106,7 @@ func (kIdentifer *KeyIdentifier) Identify(keyBytes []byte) (*KeyInfo, error) {
// attemptPem tries to decode the pem, and then work with the key. It's
// based on code from x/crypto's ssh.ParseRawPrivateKey, but more
// flexible in handling encryption and formats.
func (kIdentifer *KeyIdentifier) attemptPem(keyBytes []byte) (*KeyInfo, error) {
func (kIdentifier *KeyIdentifier) attemptPem(keyBytes []byte) (*KeyInfo, error) {
ki := &KeyInfo{
Format: "",
Parser: "attemptPem",
Expand All @@ -118,8 +119,8 @@ func (kIdentifer *KeyIdentifier) attemptPem(keyBytes []byte) (*KeyInfo, error) {

ki.Encrypted = boolPtr(pemEncryptedBlock(block))

level.Debug(kIdentifer.logger).Log(
"msg", "pem decoded",
kIdentifier.slogger.Log(context.TODO(), slog.LevelDebug,

Check failure on line 122 in ee/keyidentifier/keyidentifier.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

keys should be written in snake_case (sloglint)

Check failure on line 122 in ee/keyidentifier/keyidentifier.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

keys should be written in snake_case (sloglint)

Check failure on line 122 in ee/keyidentifier/keyidentifier.go

View workflow job for this annotation

GitHub Actions / lint (windows-latest)

keys should be written in snake_case (sloglint)

Check failure on line 122 in ee/keyidentifier/keyidentifier.go

View workflow job for this annotation

GitHub Actions / lint (windows-latest)

keys should be written in snake_case (sloglint)

Check failure on line 122 in ee/keyidentifier/keyidentifier.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

keys should be written in snake_case (sloglint)

Check failure on line 122 in ee/keyidentifier/keyidentifier.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

keys should be written in snake_case (sloglint)
"pem decoded",
"block type", block.Type,
)

Expand Down Expand Up @@ -156,8 +157,8 @@ func (kIdentifer *KeyIdentifier) attemptPem(keyBytes []byte) (*KeyInfo, error) {
if key, err := x509.ParseECPrivateKey(block.Bytes); err == nil {
ki.Bits = key.PublicKey.Curve.Params().BitSize
} else {
level.Debug(kIdentifer.logger).Log(
"msg", "x509.ParseECPrivateKey failed to parse",
kIdentifier.slogger.Log(context.TODO(), slog.LevelDebug,
"x509.ParseECPrivateKey failed to parse",
"err", err,
)
}
Expand All @@ -179,8 +180,8 @@ func (kIdentifer *KeyIdentifier) attemptPem(keyBytes []byte) (*KeyInfo, error) {
}

// Unmatched. return what we have
level.Debug(kIdentifer.logger).Log(
"msg", "pem failed to match block type",
kIdentifier.slogger.Log(context.TODO(), slog.LevelDebug,
"pem failed to match block type",
"type", block.Type,
)
return ki, nil
Expand Down
4 changes: 2 additions & 2 deletions ee/keyidentifier/keyidentifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
"testing"

"github.com/kolide/kit/logutil"
"github.com/kolide/launcher/pkg/log/multislogger"
"github.com/stretchr/testify/require"
)

Expand All @@ -21,7 +21,7 @@ type spec struct {
func TestIdentifyFiles(t *testing.T) {
t.Parallel()

kIdentifier, err := New(WithLogger(logutil.NewCLILogger(true)))
kIdentifier, err := New(WithSlogger(multislogger.NewNopLogger()))
require.NoError(t, err)

testFiles, err := filepath.Glob("testdata/specs/*.json")
Expand Down
4 changes: 2 additions & 2 deletions ee/powereventwatcher/power_event_watcher_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ package powereventwatcher

import (
"context"
"log/slog"

"github.com/go-kit/kit/log"
"github.com/kolide/launcher/ee/agent/types"
"github.com/kolide/launcher/pkg/traces"
)
Expand All @@ -16,7 +16,7 @@ type noOpPowerEventWatcher struct {
interrupted bool
}

func New(ctx context.Context, _ types.Knapsack, _ log.Logger) (*noOpPowerEventWatcher, error) {
func New(ctx context.Context, _ types.Knapsack, _ *slog.Logger) (*noOpPowerEventWatcher, error) {
_, span := traces.StartSpan(ctx)
defer span.End()

Expand Down
4 changes: 2 additions & 2 deletions ee/powereventwatcher/power_event_watcher_other_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
"testing"
"time"

"github.com/go-kit/kit/log"
typesmocks "github.com/kolide/launcher/ee/agent/types/mocks"
"github.com/kolide/launcher/pkg/log/multislogger"
"github.com/stretchr/testify/require"
)

func TestInterrupt_Multiple(t *testing.T) {
t.Parallel()

p, err := New(context.TODO(), typesmocks.NewKnapsack(t), log.NewNopLogger())
p, err := New(context.TODO(), typesmocks.NewKnapsack(t), multislogger.NewNopLogger())
require.NoError(t, err)

// Start and then interrupt
Expand Down
Loading

0 comments on commit 0a76d94

Please sign in to comment.