Skip to content

Commit

Permalink
Pass knapsack in to tuf autoupdater to simplify configuration (#1168)
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaMahany authored May 1, 2023
1 parent a68cf73 commit 012f5dc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 34 deletions.
8 changes: 1 addition & 7 deletions cmd/launcher/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,17 +363,11 @@ func runLauncher(ctx context.Context, cancel func(), opts *launcher.Options) err
mirrorClient := http.DefaultClient
mirrorClient.Timeout = 5 * time.Minute // gives us extra time to avoid a timeout on download
tufAutoupdater, err := tuf.NewTufAutoupdater(
k.TufServerURL(),
opts.RootDirectory,
k.UpdateDirectory(),
k,
metadataClient,
k.MirrorServerURL(),
mirrorClient,
k.AutoupdateErrorsStore(),
extension,
tuf.WithLogger(logger),
tuf.WithChannel(k.UpdateChannel()),
tuf.WithUpdateCheckInterval(k.AutoupdateInterval()),
)
if err != nil {
// Log the error, but don't return it -- the new TUF autoupdater is not critical yet
Expand Down
30 changes: 9 additions & 21 deletions pkg/autoupdate/tuf/autoupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ var rootJson []byte
// Configuration defaults
const (
DefaultTufServer = "https://tuf.kolide.com"
defaultChannel = "stable"
tufDirectoryName = "tuf"
)

Expand Down Expand Up @@ -70,25 +69,13 @@ func WithLogger(logger log.Logger) TufAutoupdaterOption {
}
}

func WithChannel(channel string) TufAutoupdaterOption {
return func(ta *TufAutoupdater) {
ta.channel = channel
}
}

func WithUpdateCheckInterval(checkInterval time.Duration) TufAutoupdaterOption {
return func(ta *TufAutoupdater) {
ta.checkInterval = checkInterval
}
}

func NewTufAutoupdater(metadataUrl, rootDirectory string, updateDirectory string, metadataHttpClient *http.Client,
mirrorUrl string, mirrorHttpClient *http.Client, store types.KVStore, osquerier querier, opts ...TufAutoupdaterOption) (*TufAutoupdater, error) {
func NewTufAutoupdater(k types.Knapsack, metadataHttpClient *http.Client,
mirrorHttpClient *http.Client, osquerier querier, opts ...TufAutoupdaterOption) (*TufAutoupdater, error) {
ta := &TufAutoupdater{
channel: defaultChannel,
channel: k.UpdateChannel(),
interrupt: make(chan struct{}),
checkInterval: 60 * time.Second,
store: store,
checkInterval: k.AutoupdateInterval(),
store: k.AutoupdateErrorsStore(),
logger: log.NewNopLogger(),
}

Expand All @@ -97,16 +84,17 @@ func NewTufAutoupdater(metadataUrl, rootDirectory string, updateDirectory string
}

var err error
ta.metadataClient, err = initMetadataClient(rootDirectory, metadataUrl, metadataHttpClient)
ta.metadataClient, err = initMetadataClient(k.RootDirectory(), k.TufServerURL(), metadataHttpClient)
if err != nil {
return nil, fmt.Errorf("could not init metadata client: %w", err)
}

// If the update directory wasn't set by a flag, use the default location of <launcher root>/updates.
updateDirectory := k.UpdateDirectory()
if updateDirectory == "" {
updateDirectory = filepath.Join(rootDirectory, "updates")
updateDirectory = filepath.Join(k.RootDirectory(), "updates")
}
ta.libraryManager, err = newUpdateLibraryManager(mirrorUrl, mirrorHttpClient, updateDirectory, osquerier, ta.logger)
ta.libraryManager, err = newUpdateLibraryManager(k.MirrorServerURL(), mirrorHttpClient, updateDirectory, osquerier, ta.logger)
if err != nil {
return nil, fmt.Errorf("could not init update library manager: %w", err)
}
Expand Down
46 changes: 40 additions & 6 deletions pkg/autoupdate/tuf/autoupdate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
"time"

"github.com/go-kit/kit/log"
localservermocks "github.com/kolide/launcher/ee/localserver/mocks"
"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"
"github.com/kolide/launcher/pkg/threadsafebuffer"
"github.com/stretchr/testify/require"
"github.com/theupdateframework/go-tuf"
Expand All @@ -31,10 +31,21 @@ func TestNewTufAutoupdater(t *testing.T) {

testRootDir := t.TempDir()
s := setupStorage(t)

_, err := NewTufAutoupdater("https://example.com", testRootDir, "", http.DefaultClient, "https://example.com", http.DefaultClient, s, localservermocks.NewQuerier(t))
mockKnapsack := typesmocks.NewKnapsack(t)
mockKnapsack.On("RootDirectory").Return(testRootDir)
mockKnapsack.On("UpdateChannel").Return("nightly")
mockKnapsack.On("AutoupdateInterval").Return(60 * time.Second)
mockKnapsack.On("AutoupdateErrorsStore").Return(s)
mockKnapsack.On("TufServerURL").Return("https://example.com")
mockKnapsack.On("UpdateDirectory").Return("")
mockKnapsack.On("MirrorServerURL").Return("https://example.com")

_, err := NewTufAutoupdater(mockKnapsack, http.DefaultClient, http.DefaultClient, newMockQuerier(t))
require.NoError(t, err, "could not initialize new TUF autoupdater")

// Confirm we pulled all config items as expected
mockKnapsack.AssertExpectations(t)

// Confirm that the TUF directory we expose is the one that we created
exposedRootDir := LocalTufDirectory(testRootDir)

Expand All @@ -57,11 +68,22 @@ func TestExecute(t *testing.T) {
testReleaseVersion := "1.2.3"
tufServerUrl, rootJson := initLocalTufServer(t, testReleaseVersion)
s := setupStorage(t)
mockKnapsack := typesmocks.NewKnapsack(t)
mockKnapsack.On("RootDirectory").Return(testRootDir)
mockKnapsack.On("UpdateChannel").Return("nightly")
mockKnapsack.On("AutoupdateInterval").Return(60 * time.Second)
mockKnapsack.On("AutoupdateErrorsStore").Return(s)
mockKnapsack.On("TufServerURL").Return(tufServerUrl)
mockKnapsack.On("UpdateDirectory").Return("")
mockKnapsack.On("MirrorServerURL").Return("https://example.com")

// Set up autoupdater
autoupdater, err := NewTufAutoupdater(tufServerUrl, testRootDir, "", http.DefaultClient, tufServerUrl, http.DefaultClient, s, localservermocks.NewQuerier(t))
autoupdater, err := NewTufAutoupdater(mockKnapsack, http.DefaultClient, http.DefaultClient, newMockQuerier(t))
require.NoError(t, err, "could not initialize new TUF autoupdater")

// Confirm we pulled all config items as expected
mockKnapsack.AssertExpectations(t)

// Update the metadata client with our test root JSON
require.NoError(t, autoupdater.metadataClient.Init(rootJson), "could not initialize metadata client with test root JSON")

Expand Down Expand Up @@ -122,9 +144,21 @@ func Test_storeError(t *testing.T) {
w.WriteHeader(http.StatusInternalServerError)
}))
defer testTufServer.Close()

autoupdater, err := NewTufAutoupdater(testTufServer.URL, testRootDir, "", http.DefaultClient, testTufServer.URL, http.DefaultClient, setupStorage(t), localservermocks.NewQuerier(t))
mockKnapsack := typesmocks.NewKnapsack(t)
mockKnapsack.On("RootDirectory").Return(testRootDir)
mockKnapsack.On("UpdateChannel").Return("nightly")
mockKnapsack.On("AutoupdateInterval").Return(60 * time.Second)
mockKnapsack.On("AutoupdateErrorsStore").Return(setupStorage(t))
mockKnapsack.On("TufServerURL").Return(testTufServer.URL)
mockKnapsack.On("UpdateDirectory").Return("")
mockKnapsack.On("MirrorServerURL").Return("https://example.com")

autoupdater, err := NewTufAutoupdater(mockKnapsack, http.DefaultClient, http.DefaultClient, newMockQuerier(t))
require.NoError(t, err, "could not initialize new TUF autoupdater")

// Confirm we pulled all config items as expected
mockKnapsack.AssertExpectations(t)

mockLibraryManager := NewMocklibrarian(t)
autoupdater.libraryManager = mockLibraryManager
mockLibraryManager.On("TidyLibrary").Return().Once()
Expand Down

0 comments on commit 012f5dc

Please sign in to comment.