From 012f5dcbd229435b8a716b60d57c58db3ab36193 Mon Sep 17 00:00:00 2001 From: Rebecca Mahany-Horton Date: Mon, 1 May 2023 09:54:06 -0400 Subject: [PATCH] Pass knapsack in to tuf autoupdater to simplify configuration (#1168) --- cmd/launcher/launcher.go | 8 +---- pkg/autoupdate/tuf/autoupdate.go | 30 ++++++----------- pkg/autoupdate/tuf/autoupdate_test.go | 46 +++++++++++++++++++++++---- 3 files changed, 50 insertions(+), 34 deletions(-) diff --git a/cmd/launcher/launcher.go b/cmd/launcher/launcher.go index f1e49cc6d..58105df65 100644 --- a/cmd/launcher/launcher.go +++ b/cmd/launcher/launcher.go @@ -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 diff --git a/pkg/autoupdate/tuf/autoupdate.go b/pkg/autoupdate/tuf/autoupdate.go index ba86a1a48..eaadd455b 100644 --- a/pkg/autoupdate/tuf/autoupdate.go +++ b/pkg/autoupdate/tuf/autoupdate.go @@ -28,7 +28,6 @@ var rootJson []byte // Configuration defaults const ( DefaultTufServer = "https://tuf.kolide.com" - defaultChannel = "stable" tufDirectoryName = "tuf" ) @@ -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(), } @@ -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 /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) } diff --git a/pkg/autoupdate/tuf/autoupdate_test.go b/pkg/autoupdate/tuf/autoupdate_test.go index 564c77d83..5c0621866 100644 --- a/pkg/autoupdate/tuf/autoupdate_test.go +++ b/pkg/autoupdate/tuf/autoupdate_test.go @@ -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" @@ -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) @@ -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") @@ -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()