Skip to content

Commit

Permalink
Put back findRelease for now
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaMahany committed May 4, 2023
1 parent 9b89838 commit eb6832e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 53 deletions.
48 changes: 44 additions & 4 deletions pkg/autoupdate/tuf/autoupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ package tuf

import (
_ "embed"
"encoding/json"
"fmt"
"net/http"
"os"
"path/filepath"
"runtime"
"strconv"
"time"

Expand All @@ -25,9 +27,8 @@ var rootJson []byte

// Configuration defaults
const (
DefaultTufServer = "https://tuf.kolide.com"
tufDirectoryName = "tuf"
genericReleaseVersionFormat = "%s/%s/%s/release.json" // <binary>/<os>/<channel>/release.json
DefaultTufServer = "https://tuf.kolide.com"
tufDirectoryName = "tuf"
)

// Binaries handled by autoupdater
Expand Down Expand Up @@ -238,7 +239,7 @@ func (ta *TufAutoupdater) checkForUpdate() error {
// downloadUpdate will download a new release for the given binary, if available from TUF
// and not already downloaded.
func (ta *TufAutoupdater) downloadUpdate(binary autoupdatableBinary, targets data.TargetFiles) (string, error) {
release, releaseMetadata, err := findRelease(binary, targets, ta.channel)
release, releaseMetadata, err := ta.findRelease(binary, targets)
if err != nil {
return "", fmt.Errorf("could not find release: %w", err)
}
Expand All @@ -258,6 +259,45 @@ func (ta *TufAutoupdater) downloadUpdate(binary autoupdatableBinary, targets dat
return release, nil
}

// findRelease checks the latest data from TUF (in `targets`) to see whether a new release
// has been published for our channel. If it has, it returns the target for that release
// and its associated metadata.
func (ta *TufAutoupdater) findRelease(binary autoupdatableBinary, targets data.TargetFiles) (string, data.TargetFileMeta, error) {
// First, find the target that the channel release file is pointing to
var releaseTarget string
targetReleaseFile := fmt.Sprintf("%s/%s/%s/release.json", binary, runtime.GOOS, ta.channel)
for targetName, target := range targets {
if targetName != targetReleaseFile {
continue
}

// We found the release file that matches our OS and binary. Evaluate it
// to see if we're on this latest version.
var custom ReleaseFileCustomMetadata
if err := json.Unmarshal(*target.Custom, &custom); err != nil {
return "", data.TargetFileMeta{}, fmt.Errorf("could not unmarshal release file custom metadata: %w", err)
}

releaseTarget = custom.Target
break
}

if releaseTarget == "" {
return "", data.TargetFileMeta{}, fmt.Errorf("expected release file %s for binary %s to be in targets but it was not", targetReleaseFile, binary)
}

// Now, get the metadata for our release target
for targetName, target := range targets {
if targetName != releaseTarget {
continue
}

return filepath.Base(releaseTarget), target, nil
}

return "", data.TargetFileMeta{}, fmt.Errorf("could not find metadata for release target %s for binary %s", targetReleaseFile, binary)
}

// storeError saves errors that occur during the periodic check for updates, so that they
// can be queryable via the `kolide_tuf_autoupdater_errors` table.
func (ta *TufAutoupdater) storeError(autoupdateErr error) {
Expand Down
49 changes: 0 additions & 49 deletions pkg/autoupdate/tuf/tuf_util.go

This file was deleted.

0 comments on commit eb6832e

Please sign in to comment.