Skip to content

Commit

Permalink
🐛 fix status cmd behind proxy (#2874)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariuskimmina authored Jan 2, 2024
1 parent cc17e64 commit d5114c5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
19 changes: 11 additions & 8 deletions apps/cnquery/cmd/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ Status sends a ping to Mondoo Platform to verify the credentials.
}
s.Upstream = upstreamStatus

latestVersion, err := cnquery.GetLatestVersion(httpClient)
if err != nil {
return cli_errors.NewCommandError(errors.Wrap(err, "failed to get latest version"), 1)
}

s.Client.LatestVersion = latestVersion

// check valid agent authentication
plugins := []ranger.ClientPlugin{}

Expand Down Expand Up @@ -139,6 +146,7 @@ type ClientStatus struct {
ServiceAccount string `json:"service_account,omitempty"`
ParentMrn string `json:"parentMrn,omitempty"`
Version string `json:"version,omitempty"`
LatestVersion string `json:"latest_version,omitempty"`
Build string `json:"build,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Platform *inventory.Platform `json:"platform,omitempty"`
Expand All @@ -162,15 +170,10 @@ func (s Status) RenderCliStatus() {
log.Info().Msg("Time:\t\t\t" + s.Client.Timestamp)
log.Info().Msg("Version:\t\t" + cnquery.GetVersion() + " (API Version: " + cnquery.APIVersion() + ")")

latestVersion, err := cnquery.GetLatestVersion()
if err != nil {
log.Warn().Err(err).Msg("failed to get latest version")
}

if latestVersion != "" {
log.Info().Msg("Latest Version:\t" + latestVersion)
if s.Client.LatestVersion != "" {
log.Info().Msg("Latest Version:\t" + s.Client.LatestVersion)

if cnquery.GetVersion() != latestVersion && cnquery.GetVersion() != "unstable" {
if cnquery.GetVersion() != s.Client.LatestVersion && cnquery.GetVersion() != "unstable" {
log.Warn().Msg("A newer version is available")
}
}
Expand Down
8 changes: 4 additions & 4 deletions cnquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ type Release struct {
var cnqueryLatestReleaseUrl = "https://releases.mondoo.com/cnquery/latest.json?ignoreCache=1"

// GetLatestReleaseName fetches the name of the latest release from releases.mondoo.com
func GetLatestReleaseName(url string) (string, error) {
resp, err := http.Get(url)
func GetLatestReleaseName(releaseUrl string, client *http.Client) (string, error) {
resp, err := client.Get(releaseUrl)
if err != nil {
return "", fmt.Errorf("error fetching latest release: %v", err)
}
Expand All @@ -80,8 +80,8 @@ func GetLatestReleaseName(url string) (string, error) {
}

// GetLatestVersion returns the latest version available on releases.mondoo.com
func GetLatestVersion() (string, error) {
releaseName, err := GetLatestReleaseName(cnqueryLatestReleaseUrl)
func GetLatestVersion(client *http.Client) (string, error) {
releaseName, err := GetLatestReleaseName(cnqueryLatestReleaseUrl, client)
if err != nil {
return "", err
}
Expand Down
4 changes: 3 additions & 1 deletion cnquery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
package cnquery

import (
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetLatestVersion(t *testing.T) {
version, err := GetLatestVersion()
client := &http.Client{}
version, err := GetLatestVersion(client)

assert.NoError(t, err)
assert.NotNil(t, version)
Expand Down

0 comments on commit d5114c5

Please sign in to comment.