Skip to content

Commit

Permalink
wire slogger into sortedVersionsInLibrary and log invalid version rea…
Browse files Browse the repository at this point in the history
…sons
  • Loading branch information
zackattack01 committed Sep 19, 2024
1 parent 385f4fa commit 59ab614
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
6 changes: 3 additions & 3 deletions ee/tuf/library_lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func CheckOutLatest(ctx context.Context, binary autoupdatableBinary, rootDirecto

// If we can't find the specific release version that we should be on, then just return the executable
// with the most recent version in the library
return mostRecentVersion(ctx, binary, updateDirectory, channel)
return mostRecentVersion(ctx, slogger, binary, updateDirectory, channel)
}

// findExecutable looks at our local TUF repository to find the release for our
Expand Down Expand Up @@ -270,12 +270,12 @@ func findExecutable(ctx context.Context, binary autoupdatableBinary, tufReposito

// mostRecentVersion returns the path to the most recent, valid version available in the library for the
// given binary, along with its version.
func mostRecentVersion(ctx context.Context, binary autoupdatableBinary, baseUpdateDirectory, channel string) (*BinaryUpdateInfo, error) {
func mostRecentVersion(ctx context.Context, slogger *slog.Logger, binary autoupdatableBinary, baseUpdateDirectory, channel string) (*BinaryUpdateInfo, error) {
ctx, span := traces.StartSpan(ctx)
defer span.End()

// Pull all available versions from library
validVersionsInLibrary, _, err := sortedVersionsInLibrary(ctx, binary, baseUpdateDirectory)
validVersionsInLibrary, _, err := sortedVersionsInLibrary(ctx, slogger, binary, baseUpdateDirectory)
if err != nil {
return nil, fmt.Errorf("could not get sorted versions in library for %s: %w", binary, err)
}
Expand Down
10 changes: 5 additions & 5 deletions ee/tuf/library_lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func Test_mostRecentVersion(t *testing.T) {
tufci.CopyBinary(t, secondVersionPath)
require.NoError(t, os.Chmod(secondVersionPath, 0755))

latest, err := mostRecentVersion(context.TODO(), binary, testBaseDir, "nightly")
latest, err := mostRecentVersion(context.TODO(), multislogger.NewNopLogger(), binary, testBaseDir, "nightly")
require.NoError(t, err, "did not expect error getting most recent version")
require.Equal(t, secondVersionPath, latest.Path)
require.Equal(t, secondVersion, latest.Version)
Expand Down Expand Up @@ -202,7 +202,7 @@ func Test_mostRecentVersion_DoesNotReturnInvalidExecutables(t *testing.T) {
require.NoError(t, os.MkdirAll(filepath.Dir(secondVersionPath), 0755))
os.WriteFile(secondVersionPath, []byte{}, 0755)

latest, err := mostRecentVersion(context.TODO(), binary, testBaseDir, "nightly")
latest, err := mostRecentVersion(context.TODO(), multislogger.NewNopLogger(), binary, testBaseDir, "nightly")
require.NoError(t, err, "did not expect error getting most recent version")
require.Equal(t, firstVersionPath, latest.Path)
require.Equal(t, firstVersion, latest.Version)
Expand All @@ -221,7 +221,7 @@ func Test_mostRecentVersion_ReturnsErrorOnNoUpdatesDownloaded(t *testing.T) {
// Create update directories
testBaseDir := t.TempDir()

_, err := mostRecentVersion(context.TODO(), binary, testBaseDir, "nightly")
_, err := mostRecentVersion(context.TODO(), multislogger.NewNopLogger(), binary, testBaseDir, "nightly")
require.Error(t, err, "should have returned error when there are no available updates")
})
}
Expand All @@ -239,7 +239,7 @@ func Test_mostRecentVersion_requiresLauncher_v1_4_1(t *testing.T) {
tufci.CopyBinary(t, firstVersionPath)
require.NoError(t, os.Chmod(firstVersionPath, 0755))

_, err := mostRecentVersion(context.TODO(), binaryLauncher, testBaseDir, "stable")
_, err := mostRecentVersion(context.TODO(), multislogger.NewNopLogger(), binaryLauncher, testBaseDir, "stable")
require.Error(t, err, "should not select launcher version under v1.4.1")
}

Expand All @@ -255,7 +255,7 @@ func Test_mostRecentVersion_acceptsLauncher_v1_4_1(t *testing.T) {
tufci.CopyBinary(t, firstVersionPath)
require.NoError(t, os.Chmod(firstVersionPath, 0755))

latest, err := mostRecentVersion(context.TODO(), binaryLauncher, testBaseDir, "stable")
latest, err := mostRecentVersion(context.TODO(), multislogger.NewNopLogger(), binaryLauncher, testBaseDir, "stable")
require.NoError(t, err, "should be able to select launcher version equal to v1.4.1")
require.Equal(t, firstVersionPath, latest.Path)
}
Expand Down
18 changes: 16 additions & 2 deletions ee/tuf/library_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ func (ulm *updateLibraryManager) TidyLibrary(binary autoupdatableBinary, current

const numberOfVersionsToKeep = 3

versionsInLibrary, invalidVersionsInLibrary, err := sortedVersionsInLibrary(context.Background(), binary, ulm.baseDir)
versionsInLibrary, invalidVersionsInLibrary, err := sortedVersionsInLibrary(context.Background(), ulm.slogger, binary, ulm.baseDir)
if err != nil {
ulm.slogger.Log(context.TODO(), slog.LevelWarn,
"could not get versions in library to tidy update library",
Expand Down Expand Up @@ -410,7 +410,7 @@ func (ulm *updateLibraryManager) TidyLibrary(binary autoupdatableBinary, current
// sortedVersionsInLibrary looks through the update library for the given binary to validate and sort all
// available versions. It returns a sorted list of the valid versions, a list of invalid versions, and
// an error only when unable to glob for versions.
func sortedVersionsInLibrary(ctx context.Context, binary autoupdatableBinary, baseUpdateDirectory string) ([]string, []string, error) {
func sortedVersionsInLibrary(ctx context.Context, slogger *slog.Logger, binary autoupdatableBinary, baseUpdateDirectory string) ([]string, []string, error) {
ctx, span := traces.StartSpan(ctx)
defer span.End()

Expand All @@ -425,13 +425,27 @@ func sortedVersionsInLibrary(ctx context.Context, binary autoupdatableBinary, ba
rawVersion := filepath.Base(rawVersionWithPath)
v, err := semver.NewVersion(rawVersion)
if err != nil {
slogger.Log(ctx, slog.LevelWarn,
"detected invalid binary version while parsing raw version",
"raw_version", rawVersion,
"binary", binary,
"err", err,
)

invalidVersions = append(invalidVersions, rawVersion)
continue
}

versionDir := filepath.Join(updatesDirectory(binary, baseUpdateDirectory), rawVersion)
if err := CheckExecutable(ctx, executableLocation(versionDir, binary), "--version"); err != nil {
traces.SetError(span, err)
slogger.Log(ctx, slog.LevelWarn,
"detected invalid binary version while checking executable",
"version_dir", versionDir,
"binary", binary,
"err", err,
)

invalidVersions = append(invalidVersions, rawVersion)
continue
}
Expand Down
4 changes: 2 additions & 2 deletions ee/tuf/library_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ func Test_sortedVersionsInLibrary(t *testing.T) {
}

// Get sorted versions
validVersions, invalidVersions, err := sortedVersionsInLibrary(context.TODO(), binaryLauncher, testBaseDir)
validVersions, invalidVersions, err := sortedVersionsInLibrary(context.TODO(), multislogger.NewNopLogger(), binaryLauncher, testBaseDir)
require.NoError(t, err, "expected no error on sorting versions in library")

// Confirm invalid versions are the ones we expect
Expand Down Expand Up @@ -725,7 +725,7 @@ func Test_sortedVersionsInLibrary_devBuilds(t *testing.T) {
}

// Get sorted versions
validVersions, invalidVersions, err := sortedVersionsInLibrary(context.TODO(), binaryLauncher, testBaseDir)
validVersions, invalidVersions, err := sortedVersionsInLibrary(context.TODO(), multislogger.NewNopLogger(), binaryLauncher, testBaseDir)
require.NoError(t, err, "expected no error on sorting versions in library")

// Confirm we don't have any invalid versions
Expand Down

0 comments on commit 59ab614

Please sign in to comment.