Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into becca/patchelf
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaMahany committed Nov 21, 2023
2 parents bb2be51 + 082b734 commit ece50ed
Show file tree
Hide file tree
Showing 41 changed files with 69 additions and 55 deletions.
2 changes: 1 addition & 1 deletion cmd/launcher/interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (

"github.com/go-kit/kit/log"
"github.com/kolide/launcher/cmd/launcher/internal"
"github.com/kolide/launcher/ee/tuf"
"github.com/kolide/launcher/pkg/agent"
"github.com/kolide/launcher/pkg/autoupdate"
"github.com/kolide/launcher/pkg/autoupdate/tuf"
"github.com/kolide/launcher/pkg/launcher"
"github.com/kolide/launcher/pkg/osquery/interactive"
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ import (
"github.com/kolide/launcher/ee/control/consumers/notificationconsumer"
desktopRunner "github.com/kolide/launcher/ee/desktop/runner"
"github.com/kolide/launcher/ee/localserver"
"github.com/kolide/launcher/ee/tuf"
"github.com/kolide/launcher/pkg/agent"
"github.com/kolide/launcher/pkg/agent/flags"
"github.com/kolide/launcher/pkg/agent/knapsack"
"github.com/kolide/launcher/pkg/agent/storage"
agentbbolt "github.com/kolide/launcher/pkg/agent/storage/bbolt"
"github.com/kolide/launcher/pkg/autoupdate"
"github.com/kolide/launcher/pkg/autoupdate/tuf"
"github.com/kolide/launcher/pkg/backoff"
"github.com/kolide/launcher/pkg/contexts/ctxlog"
"github.com/kolide/launcher/pkg/debug"
Expand Down
2 changes: 1 addition & 1 deletion cmd/launcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
"github.com/kolide/kit/env"
"github.com/kolide/kit/logutil"
"github.com/kolide/kit/version"
"github.com/kolide/launcher/ee/tuf"
"github.com/kolide/launcher/pkg/autoupdate"
"github.com/kolide/launcher/pkg/autoupdate/tuf"
"github.com/kolide/launcher/pkg/contexts/ctxlog"
"github.com/kolide/launcher/pkg/execwrapper"
"github.com/kolide/launcher/pkg/launcher"
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import (

"github.com/Masterminds/semver"
"github.com/go-kit/kit/log"
tufci "github.com/kolide/launcher/ee/tuf/ci"
"github.com/kolide/launcher/pkg/agent/storage"
storageci "github.com/kolide/launcher/pkg/agent/storage/ci"
"github.com/kolide/launcher/pkg/agent/types"
typesmocks "github.com/kolide/launcher/pkg/agent/types/mocks"
tufci "github.com/kolide/launcher/pkg/autoupdate/tuf/ci"
"github.com/kolide/launcher/pkg/threadsafebuffer"
mock "github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"

"github.com/go-kit/kit/log"
tufci "github.com/kolide/launcher/pkg/autoupdate/tuf/ci"
tufci "github.com/kolide/launcher/ee/tuf/ci"
"github.com/stretchr/testify/require"
)

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"testing"

"github.com/go-kit/kit/log"
tufci "github.com/kolide/launcher/ee/tuf/ci"
"github.com/kolide/launcher/pkg/autoupdate"
tufci "github.com/kolide/launcher/pkg/autoupdate/tuf/ci"
"github.com/stretchr/testify/require"
"github.com/theupdateframework/go-tuf/data"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"testing"

"github.com/go-kit/kit/log"
tufci "github.com/kolide/launcher/pkg/autoupdate/tuf/ci"
tufci "github.com/kolide/launcher/ee/tuf/ci"
"github.com/stretchr/testify/require"
"github.com/theupdateframework/go-tuf/data"
"golang.org/x/sys/windows"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion pkg/agent/knapsack/knapsack.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (

"github.com/go-kit/kit/log"
"github.com/kolide/kit/ulid"
"github.com/kolide/launcher/ee/tuf"
"github.com/kolide/launcher/pkg/agent/flags/keys"
"github.com/kolide/launcher/pkg/agent/storage"
"github.com/kolide/launcher/pkg/agent/types"
"github.com/kolide/launcher/pkg/autoupdate"
"github.com/kolide/launcher/pkg/autoupdate/tuf"
"github.com/kolide/launcher/pkg/log/multislogger"
"go.etcd.io/bbolt"
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/debug/checkups/tuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"runtime"

"github.com/go-kit/kit/log"
"github.com/kolide/launcher/ee/tuf"
"github.com/kolide/launcher/pkg/agent/types"
"github.com/kolide/launcher/pkg/autoupdate/tuf"
)

type (
Expand Down
2 changes: 1 addition & 1 deletion pkg/debug/checkups/tuf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"strings"
"testing"

tufci "github.com/kolide/launcher/ee/tuf/ci"
typesmocks "github.com/kolide/launcher/pkg/agent/types/mocks"
tufci "github.com/kolide/launcher/pkg/autoupdate/tuf/ci"
"github.com/stretchr/testify/require"
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/osquery/runtime/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"time"

"github.com/go-kit/kit/log/level"
"github.com/kolide/launcher/ee/tuf"
"github.com/kolide/launcher/pkg/autoupdate"
"github.com/kolide/launcher/pkg/autoupdate/tuf"
"github.com/kolide/launcher/pkg/backoff"
"github.com/kolide/launcher/pkg/contexts/ctxlog"
"github.com/kolide/launcher/pkg/osquery/runtime/history"
Expand Down
2 changes: 1 addition & 1 deletion pkg/osquery/tables/tufinfo/release_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/osquery/osquery-go/plugin/table"
"github.com/theupdateframework/go-tuf/data"

"github.com/kolide/launcher/ee/tuf"
"github.com/kolide/launcher/pkg/agent/types"
"github.com/kolide/launcher/pkg/autoupdate/tuf"
)

const tufReleaseVersionTableName = "kolide_tuf_release_version"
Expand Down
4 changes: 2 additions & 2 deletions pkg/osquery/tables/tufinfo/release_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"time"

"github.com/google/uuid"
"github.com/kolide/launcher/ee/tuf"
tufci "github.com/kolide/launcher/ee/tuf/ci"
"github.com/kolide/launcher/pkg/agent/types/mocks"
"github.com/kolide/launcher/pkg/autoupdate/tuf"
tufci "github.com/kolide/launcher/pkg/autoupdate/tuf/ci"
"github.com/osquery/osquery-go/gen/osquery"

"github.com/stretchr/testify/require"
Expand Down
90 changes: 52 additions & 38 deletions pkg/sendbuffer/sendbuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,32 @@ type sender interface {
}

var (
defaultMaxSize = 128 * 1024
defaultMaxSendSize = 8 * 1024
defaultMaxSizeBytes = 512 * 1024
defaultSendSizeBytes = 8 * 1024
)

type SendBuffer struct {
logs [][]byte
size, maxStorageSize, maxSendSize int
writeMutex, sendMutex sync.Mutex
logger log.Logger
sender sender
sendInterval time.Duration
isSending bool
logs [][]byte
size, maxStorageSizeBytes, maxSendSizeBytes int
sendMutex sync.Mutex
writeMutex sync.RWMutex
logger log.Logger
sender sender
sendInterval time.Duration
isSending bool
}

type option func(*SendBuffer)

func WithMaxStorageSize(maxSize int) option {
func WithMaxStorageSizeBytes(maxSize int) option {
return func(sb *SendBuffer) {
sb.maxStorageSize = maxSize
sb.maxStorageSizeBytes = maxSize
}
}

func WithMaxSendSize(sendSize int) option {
func WithMaxSendSizeBytes(sendSize int) option {
return func(sb *SendBuffer) {
sb.maxSendSize = sendSize
sb.maxSendSizeBytes = sendSize
}
}

Expand All @@ -59,12 +60,12 @@ func WithSendInterval(sendInterval time.Duration) option {

func New(sender sender, opts ...option) *SendBuffer {
sb := &SendBuffer{
maxStorageSize: defaultMaxSize,
maxSendSize: defaultMaxSendSize,
sender: sender,
sendInterval: 1 * time.Minute,
logger: log.NewNopLogger(),
isSending: false,
maxStorageSizeBytes: defaultMaxSizeBytes,
maxSendSizeBytes: defaultSendSizeBytes,
sender: sender,
sendInterval: 1 * time.Minute,
logger: log.NewNopLogger(),
isSending: false,
}

for _, opt := range opts {
Expand All @@ -85,27 +86,27 @@ func (sb *SendBuffer) Write(in []byte) (int, error) {
}

// if the single data piece is larger than the max send size, drop it and log
if len(in) > sb.maxSendSize {
if len(in) > sb.maxSendSizeBytes {
sb.logger.Log(
"msg", "dropped data because element greater than max send size",
"size_of_data", len(in),
"max_send_size", sb.maxSendSize,
"size_of_data_bytes", len(in),
"max_send_size_bytes", sb.maxSendSizeBytes,
"head", string(in)[0:minInt(len(in), 100)],
)
return len(in), nil
}

// if we are full, something has backed up
// purge everything
if len(in)+sb.size > sb.maxStorageSize {
if len(in)+sb.size > sb.maxStorageSizeBytes {
sb.deleteLogs(len(sb.logs))

sb.logger.Log(
"msg", "reached capacity, dropping all data and starting over",
"size_of_data", len(in),
"buffer_size", sb.size,
"size_plus_data", sb.size+len(in),
"max_size", sb.maxStorageSize,
"size_of_data_bytes", len(in),
"buffer_size_bytes", sb.size,
"size_plus_data_bytes", sb.size+len(in),
"max_size", sb.maxStorageSizeBytes,
)
}

Expand Down Expand Up @@ -161,7 +162,8 @@ func (sb *SendBuffer) sendAndPurge() error {
defer sb.sendMutex.Unlock()

toSendBuff := &bytes.Buffer{}
if err := sb.flushToWriter(toSendBuff); err != nil {
lastKey, err := sb.copyLogs(toSendBuff, sb.maxSendSizeBytes)
if err != nil {
return err
}

Expand All @@ -170,34 +172,46 @@ func (sb *SendBuffer) sendAndPurge() error {
}

if err := sb.sender.Send(toSendBuff); err != nil {
sb.logger.Log("msg", "failed to send, dropping data", "err", err)
sb.logger.Log("msg", "failed to send, will retry", "err", err)
return nil
}
// TODO: populate logs with device data (id, serial, munemo, orgid) when we
// get first set of control data with device info before shipping

// testing on a new enrollment in debug mode, log size hit 130K bytes
// before enrollment completed and was able to ship logs
// 2023-11-16
sb.writeMutex.Lock()
defer sb.writeMutex.Unlock()
sb.deleteLogs(lastKey)

return nil
}

func (sb *SendBuffer) flushToWriter(w io.Writer) error {
sb.writeMutex.Lock()
defer sb.writeMutex.Unlock()
// copyLogs writes to the provided writer, peeking at the size of each log
// before for copying and returning when the next log would exceed the maxSize,
// it's up to the caller to delete any copied logs
func (sb *SendBuffer) copyLogs(w io.Writer, maxSizeBytes int) (int, error) {
sb.writeMutex.RLock()
defer sb.writeMutex.RUnlock()

size := 0
removeDataKeysToIndex := 0
lastLogIndex := 0

for i := 0; i < len(sb.logs); i++ {
if len(sb.logs[i])+size > sb.maxSendSize {
if len(sb.logs[i])+size > maxSizeBytes {
break
}

if _, err := w.Write(sb.logs[i]); err != nil {
return err
return 0, err
}

size += len(sb.logs[i])
removeDataKeysToIndex++
lastLogIndex++
}

sb.deleteLogs(removeDataKeysToIndex)
return nil
return lastLogIndex, nil
}

func (sb *SendBuffer) deleteLogs(toIndex int) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/sendbuffer/sendbuffer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ func TestSendBuffer(t *testing.T) {

sb := New(
&testSender{lastReceived: lastReceivedData, t: t},
WithMaxStorageSize(tt.maxStorageSize),
WithMaxSendSize(tt.maxSendSize),
WithMaxStorageSizeBytes(tt.maxStorageSize),
WithMaxSendSizeBytes(tt.maxSendSize),
)

requireStoreSizeEqualsHttpBufferReportedSize(t, sb)
Expand Down Expand Up @@ -141,7 +141,7 @@ func TestSendBufferConcurrent(t *testing.T) {
testSender := &testSender{lastReceived: &bytes.Buffer{}, t: t}
sb := New(
testSender,
WithMaxSendSize(tt.maxSendSize),
WithMaxSendSizeBytes(tt.maxSendSize),
// run interval in background quickly
WithSendInterval(1*time.Millisecond),
)
Expand Down

0 comments on commit ece50ed

Please sign in to comment.