-
Notifications
You must be signed in to change notification settings - Fork 62
Retrieving latest release metadata from osctrl.net in osctrl-admin
#785
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,6 +64,7 @@ coverage.out | |
|
|
||
| # Configuration | ||
| osctrl-api.json | ||
| version_data.json | ||
|
|
||
| # Go Workspace | ||
| go.work | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,62 @@ | ||
| package version | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "time" | ||
|
|
||
| "golang.org/x/mod/semver" | ||
| ) | ||
|
|
||
| const ( | ||
| // OsctrlVersion to have the version for all components | ||
| OsctrlVersion = "0.5.0" | ||
| // OsqueryVersion to have the version for osquery defined | ||
| OsqueryVersion = "5.21.0" | ||
| // VersionDataURL to have the URL to retrieve the latest version for all osctrl components | ||
| VersionDataURL = "https://stats.osctrl.net/version_data.json" | ||
| // versionDataRequestTimeout sets the max time to wait for version data retrieval. | ||
| versionDataRequestTimeout = 10 * time.Second | ||
| ) | ||
|
javuto marked this conversation as resolved.
|
||
|
|
||
| // VersionData to retrieve the latest version for all osctrl components | ||
| type VersionData struct { | ||
| LatestRelease string `json:"latestRelease"` | ||
| OsqueryVersion string `json:"osqueryVersion"` | ||
| SuggestedRelease string `json:"suggestedRelease"` | ||
| MoreInformation string `json:"moreInformation"` | ||
| } | ||
|
|
||
| // CheckSuggestedRelease to check if the current version is equal or higher than the suggested release | ||
| func CheckSuggestedRelease(suggestedRelease string) bool { | ||
| return semver.Compare("v"+OsctrlVersion, "v"+suggestedRelease) >= 0 | ||
| } | ||
|
javuto marked this conversation as resolved.
|
||
|
|
||
| func RetrieveVersionData(url string) (*VersionData, error) { | ||
| ctx, cancel := context.WithTimeout(context.Background(), versionDataRequestTimeout) | ||
| defer cancel() | ||
| req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| resp, err := http.DefaultClient.Do(req) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| defer resp.Body.Close() | ||
| if resp.StatusCode != http.StatusOK { | ||
| return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode) | ||
| } | ||
| data, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| var versionData VersionData | ||
| if err := json.Unmarshal(data, &versionData); err != nil { | ||
| return nil, err | ||
| } | ||
| return &versionData, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,122 @@ | ||
| package version | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "io" | ||
| "net/http" | ||
| "net/url" | ||
| "regexp" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestOsqueryVersion(t *testing.T) { | ||
| assert.Equal(t, "5.21.0", OsqueryVersion) | ||
| type roundTripFunc func(*http.Request) (*http.Response, error) | ||
|
|
||
| func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) { | ||
| return f(req) | ||
| } | ||
|
|
||
| func TestVersionConstantsAreValid(t *testing.T) { | ||
| semverPattern := regexp.MustCompile(`^\d+\.\d+\.\d+$`) | ||
|
|
||
| assert.Regexp(t, semverPattern, OsctrlVersion) | ||
| assert.Regexp(t, semverPattern, OsqueryVersion) | ||
| } | ||
|
|
||
| func TestVersionDataURL(t *testing.T) { | ||
| parsedURL, err := url.ParseRequestURI(VersionDataURL) | ||
|
|
||
| assert.NoError(t, err) | ||
| assert.Equal(t, "https", parsedURL.Scheme) | ||
| assert.Equal(t, "stats.osctrl.net", parsedURL.Host) | ||
| assert.Equal(t, "/version_data.json", parsedURL.Path) | ||
| } | ||
|
|
||
| func TestVersionDataJSONTags(t *testing.T) { | ||
| data := VersionData{ | ||
| OsqueryVersion: "5.21.0", | ||
| LatestRelease: "0.5.1", | ||
| SuggestedRelease: "0.5.1", | ||
| MoreInformation: "https://docs.example.com/releases/0.5.1", | ||
| } | ||
|
|
||
| encoded, err := json.Marshal(data) | ||
| assert.NoError(t, err) | ||
|
|
||
| var decoded map[string]string | ||
| err = json.Unmarshal(encoded, &decoded) | ||
| assert.NoError(t, err) | ||
|
|
||
| assert.Equal(t, data.OsqueryVersion, decoded["osqueryVersion"]) | ||
| assert.Equal(t, data.LatestRelease, decoded["latestRelease"]) | ||
| assert.Equal(t, data.SuggestedRelease, decoded["suggestedRelease"]) | ||
| assert.Equal(t, data.MoreInformation, decoded["moreInformation"]) | ||
| } | ||
|
|
||
| func TestOsctrlVersion(t *testing.T) { | ||
| assert.Equal(t, "0.5.0", OsctrlVersion) | ||
| func TestRetrieveVersionDataSuccess(t *testing.T) { | ||
| previousClient := http.DefaultClient | ||
| t.Cleanup(func() { http.DefaultClient = previousClient }) | ||
|
|
||
| http.DefaultClient = &http.Client{ | ||
| Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) { | ||
| assert.Equal(t, http.MethodGet, req.Method) | ||
| assert.Equal(t, VersionDataURL, req.URL.String()) | ||
| assert.NotNil(t, req.Context()) | ||
|
|
||
| payload := `{"latestRelease":"0.5.1","osqueryVersion":"5.21.0","suggestedRelease":"0.5.1","moreInformation":"https://docs.example.com/releases/0.5.1"}` | ||
| return &http.Response{ | ||
| StatusCode: http.StatusOK, | ||
| Body: io.NopCloser(strings.NewReader(payload)), | ||
| Header: make(http.Header), | ||
| }, nil | ||
| }), | ||
| } | ||
|
|
||
| data, err := RetrieveVersionData(VersionDataURL) | ||
| assert.NoError(t, err) | ||
| assert.NotNil(t, data) | ||
| assert.Equal(t, "0.5.1", data.LatestRelease) | ||
| assert.Equal(t, "5.21.0", data.OsqueryVersion) | ||
| assert.Equal(t, "0.5.1", data.SuggestedRelease) | ||
| assert.Equal(t, "https://docs.example.com/releases/0.5.1", data.MoreInformation) | ||
| } | ||
|
|
||
| func TestRetrieveVersionDataUnexpectedStatus(t *testing.T) { | ||
| previousClient := http.DefaultClient | ||
| t.Cleanup(func() { http.DefaultClient = previousClient }) | ||
|
|
||
| http.DefaultClient = &http.Client{ | ||
| Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) { | ||
| return &http.Response{ | ||
| StatusCode: http.StatusBadGateway, | ||
| Body: io.NopCloser(strings.NewReader("bad gateway")), | ||
| Header: make(http.Header), | ||
| }, nil | ||
| }), | ||
| } | ||
|
|
||
| data, err := RetrieveVersionData(VersionDataURL) | ||
| assert.Nil(t, data) | ||
| assert.EqualError(t, err, "unexpected status code: 502") | ||
| } | ||
|
|
||
| func TestRetrieveVersionDataInvalidJSON(t *testing.T) { | ||
| previousClient := http.DefaultClient | ||
| t.Cleanup(func() { http.DefaultClient = previousClient }) | ||
|
|
||
| http.DefaultClient = &http.Client{ | ||
| Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) { | ||
| return &http.Response{ | ||
| StatusCode: http.StatusOK, | ||
| Body: io.NopCloser(strings.NewReader("{")), | ||
| Header: make(http.Header), | ||
| }, nil | ||
| }), | ||
| } | ||
|
|
||
| data, err := RetrieveVersionData(VersionDataURL) | ||
| assert.Nil(t, data) | ||
| assert.Error(t, err) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition is inverted relative to the message:
CheckSuggestedReleasereturns true when the current version is equal/newer than the suggested release, but the log says “A newer version ... is available”. Either invert theifcondition or changeCheckSuggestedReleasesemantics/name so thattruemeans an update is available.