Skip to content

Commit

Permalink
prepare for new kolide-event-handler
Browse files Browse the repository at this point in the history
Co-authored-by: Thomas Krampl <[email protected]>
  • Loading branch information
sechmann and thokra-nav committed Jun 14, 2024
1 parent 2c23238 commit ebc20c0
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 51 deletions.
4 changes: 2 additions & 2 deletions cmd/apiserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,9 @@ func run(log *logrus.Entry, cfg config.Config) error {
}

updateDevice := func(event *kolidepb.DeviceEvent) error {
device, err := kolide.LookupDevice(ctx, db, event)
device, err := db.ReadDeviceByExternalID(ctx, event.GetExternalID())
if err != nil {
return err
return fmt.Errorf("read device with external_id=%v: %w", event.GetExternalID(), err)
}

changed := false
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/jackmordaunt/icns/v2 v2.2.6
github.com/kelseyhightower/envconfig v1.4.0
github.com/lestrrat-go/jwx v1.2.29
github.com/nais/kolide-event-handler v0.0.0-20240614075259-de023eff2206
github.com/nais/kolide-event-handler v0.0.0-20240614084216-95ac8998fd8f
github.com/nirasan/go-oauth-pkce-code-verifier v0.0.0-20170819232839-0fbfe93532da
github.com/prometheus/client_golang v1.17.0
github.com/sirupsen/logrus v1.9.3
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -512,10 +512,8 @@ github.com/moricho/tparallel v0.3.1 h1:fQKD4U1wRMAYNngDonW5XupoB/ZGJHdpzrWqgyg9k
github.com/moricho/tparallel v0.3.1/go.mod h1:leENX2cUv7Sv2qDgdi0D0fCftN8fRC67Bcn8pqzeYNI=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/nais/kolide-event-handler v0.0.0-20240613124908-c26ee6800776 h1:W5G0sRZnAOvr7Gt6Su3BUpf0dqVotuokB1Z1sKUPYQI=
github.com/nais/kolide-event-handler v0.0.0-20240613124908-c26ee6800776/go.mod h1:1Ta1n1Q+EtH7EIHuU3/svAkCQ8AWk9/qfs6nBj0lhxE=
github.com/nais/kolide-event-handler v0.0.0-20240614075259-de023eff2206 h1:AT0hiUcqyJyfP48JVgtiGXeIbsuKOdpyKBn1jgSpsFA=
github.com/nais/kolide-event-handler v0.0.0-20240614075259-de023eff2206/go.mod h1:7Dl7mqto/Jb4Ng8NntCAsnnYCX2clIuOUd6X7b5s+7o=
github.com/nais/kolide-event-handler v0.0.0-20240614084216-95ac8998fd8f h1:AuoUs1nEs0XUyQjwgwDygPC4zE5+EvQGkMNr5TfmPlw=
github.com/nais/kolide-event-handler v0.0.0-20240614084216-95ac8998fd8f/go.mod h1:1Ta1n1Q+EtH7EIHuU3/svAkCQ8AWk9/qfs6nBj0lhxE=
github.com/nakabonne/nestif v0.3.1 h1:wm28nZjhQY5HyYPx+weN3Q65k6ilSBxDb8v5S81B81U=
github.com/nakabonne/nestif v0.3.1/go.mod h1:9EtoZochLn5iUprVDmDjqGKPofoUEBL8U4Ngq6aY7OE=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
Expand Down
13 changes: 13 additions & 0 deletions internal/apiserver/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,19 @@ func (db *ApiServerDB) ReadDeviceById(ctx context.Context, deviceID int64) (*pb.
return sqlcDeviceToPbDevice(*device)
}

func (db *ApiServerDB) ReadDeviceByExternalID(ctx context.Context, externalID string) (*pb.Device, error) {
id := sql.NullString{
String: externalID,
Valid: true,
}
device, err := db.queries.GetDeviceByExternalID(ctx, id)
if err != nil {
return nil, err
}

return sqlcDeviceToPbDevice(*device)
}

func (db *ApiServerDB) ReadGateways(ctx context.Context) ([]*pb.Gateway, error) {
rows, err := db.queries.GetGateways(ctx)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/apiserver/database/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type APIServer interface {
AddDevice(ctx context.Context, device *pb.Device) error
ReadDevice(ctx context.Context, publicKey string) (*pb.Device, error)
ReadDeviceById(ctx context.Context, deviceID int64) (*pb.Device, error)
ReadDeviceByExternalID(ctx context.Context, externalID string) (*pb.Device, error)
ReadGateways(ctx context.Context) ([]*pb.Gateway, error)
ReadGateway(ctx context.Context, name string) (*pb.Gateway, error)
ReadDeviceBySerialPlatform(ctx context.Context, serial string, platform string) (*pb.Device, error)
Expand Down
59 changes: 59 additions & 0 deletions internal/apiserver/database/mock_api_server.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions internal/apiserver/database/queries/devices.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ SELECT * FROM devices ORDER BY id;
-- name: GetDeviceByPublicKey :one
SELECT * FROM devices WHERE public_key = @public_key;

-- name: GetDeviceByExternalID :one
SELECT * FROM devices WHERE external_id = @external_id;

-- name: GetDeviceByID :one
SELECT * FROM devices WHERE id = @id;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP INDEX devies_external_id;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE UNIQUE INDEX devies_external_id ON devices ( external_id );
26 changes: 13 additions & 13 deletions internal/apiserver/kolide/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,37 @@ import (
"testing"
"time"

kolideclient "github.com/nais/kolide-event-handler/pkg/kolide"

"github.com/nais/device/internal/apiserver/kolide"
"github.com/nais/device/internal/pb"
"github.com/stretchr/testify/assert"
)

func TestCheck(t *testing.T) {
tagTests := []struct {
tags []string
severity kolideclient.Severity
severity pb.Severity
duration time.Duration
}{
{[]string{}, kolideclient.SeverityWarning, kolideclient.DurationWarning},
{[]string{"foo", "bar"}, kolideclient.SeverityWarning, kolideclient.DurationWarning},
{[]string{"foo", "notice"}, kolideclient.SeverityNotice, kolideclient.DurationNotice},
{[]string{"warning", "notice", "danger"}, kolideclient.SeverityDanger, kolideclient.DurationDanger},
{[]string{"notice"}, kolideclient.SeverityNotice, kolideclient.DurationNotice},
{[]string{"warning"}, kolideclient.SeverityWarning, kolideclient.DurationWarning},
{[]string{"danger"}, kolideclient.SeverityDanger, kolideclient.DurationDanger},
{[]string{"critical"}, kolideclient.SeverityCritical, kolideclient.DurationCritical},
{[]string{}, pb.Severity_Warning, kolide.DurationWarning},
{[]string{"foo", "bar"}, pb.Severity_Warning, kolide.DurationWarning},
{[]string{"foo", "notice"}, pb.Severity_Notice, kolide.DurationNotice},
{[]string{"warning", "notice", "danger"}, pb.Severity_Danger, kolide.DurationDanger},
{[]string{"notice"}, pb.Severity_Notice, kolide.DurationNotice},
{[]string{"warning"}, pb.Severity_Warning, kolide.DurationWarning},
{[]string{"danger"}, pb.Severity_Danger, kolide.DurationDanger},
{[]string{"critical"}, pb.Severity_Critical, kolide.DurationCritical},
}

for _, tt := range tagTests {
t.Run(strings.Join(tt.tags, ", "), func(t *testing.T) {
check := kolideclient.Check{
check := kolide.Check{
Tags: tt.tags,
}

severity := check.Severity()

assert.Equal(t, tt.severity, severity)
assert.Equal(t, tt.duration, severity.GraceTime())
assert.Equal(t, tt.duration, kolide.GraceTime(severity))
})
}
}
31 changes: 0 additions & 31 deletions internal/apiserver/kolide/event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@ package kolide
import (
"context"
"crypto/tls"
"fmt"
"strings"
"time"

"github.com/nais/device/internal/apiserver/database"
"github.com/nais/device/internal/pb"

kolidepb "github.com/nais/kolide-event-handler/pkg/pb"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
Expand Down Expand Up @@ -90,29 +85,3 @@ func DeviceEventStreamer(ctx context.Context, log *logrus.Entry, grpcAddress, gr

return ctx.Err()
}

func LookupDevice(ctx context.Context, db database.APIServer, event *kolidepb.DeviceEvent) (*pb.Device, error) {
platform := func(platform string) string {
switch strings.ToLower(platform) {
case "darwin":
return "darwin"
case "windows":
return "windows"
default:
return "linux"
}
}

p := platform(event.GetPlatform())

device, err := db.ReadDeviceBySerialPlatform(ctx, event.GetSerial(), p)
if err != nil {
return nil, fmt.Errorf("read device with serial=%s platform=%s: %w", event.GetSerial(), p, err)
}

if device.ExternalID == "" {
device.ExternalID = event.GetExternalID()
}

return device, nil
}
10 changes: 10 additions & 0 deletions internal/apiserver/sqlc/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions internal/apiserver/sqlc/devices.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions internal/apiserver/sqlc/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ebc20c0

Please sign in to comment.