Skip to content

Commit

Permalink
feat: switch to /version endpoint for validating enterprise version
Browse files Browse the repository at this point in the history
Signed-off-by: Bradley Jones <[email protected]>
  • Loading branch information
bradleyjones committed Aug 15, 2023
1 parent 033958b commit 0f987d1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
36 changes: 27 additions & 9 deletions pkg/reporter/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var enterpriseEndpoint = reportAPIPathV2
//nolint:gosec
func Post(report inventory.Report, anchoreDetails config.AnchoreInfo) error {
defer tracker.TrackFunctionTime(time.Now(), "Reporting results to Anchore for cluster: "+report.ClusterName+"")
log.Debug("Reporting results to Anchore")
log.Debug("Reporting results to Anchore using endpoint: ", enterpriseEndpoint)
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: anchoreDetails.HTTP.Insecure},
}
Expand Down Expand Up @@ -82,6 +82,18 @@ func Post(report inventory.Report, anchoreDetails config.AnchoreInfo) error {
return nil
}

type AnchoreVersion struct {
API struct {
Version string `json:"version"`
} `json:"api"`
DB struct {
SchemaVersion string `json:"schema_version"`
} `json:"db"`
Service struct {
Version string `json:"version"`
} `json:"service"`
}

// This method retrieves the API version from Anchore
// and caches the response if parsed successfully
//
Expand All @@ -97,29 +109,35 @@ func checkVersion(anchoreDetails config.AnchoreInfo) error {
}
gock.InterceptClient(client) // Required to use gock for testing custom client

resp, err := client.Get(anchoreDetails.URL)
resp, err := client.Get(anchoreDetails.URL + "/version")
if err != nil {
return fmt.Errorf("failed to contact Anchore API: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
fmt.Println("fff")
return fmt.Errorf("failed to retrieve Anchore API version: %+v", resp)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read Anchore API version: %w", err)
}

switch version := string(body); version {
case "v1":
enterpriseEndpoint = reportAPIPathV1
case "v2":
ver := AnchoreVersion{}
err = json.Unmarshal(body, &ver)
if err != nil {
return fmt.Errorf("failed to parse API version: %w", err)
}

log.Debug("Anchore API version: ", ver)
if ver.API.Version == "2" {
enterpriseEndpoint = reportAPIPathV2
default:
return fmt.Errorf("unexpected Anchore API version: %s", version)
} else {
// If we can't parse the version, we'll assume it's v1 as 4.X does not include the version in the API version response
enterpriseEndpoint = reportAPIPathV1
}

log.Info("Using enterprise endpoint %s", enterpriseEndpoint)
log.Info("Using enterprise endpoint ", enterpriseEndpoint)
return nil
}

Expand Down
28 changes: 20 additions & 8 deletions pkg/reporter/reporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,19 @@ func TestPost(t *testing.T) {
Post(reportAPIPathV1).
Reply(200)
gock.New("https://ancho.re").
Get("/").
Get("/version").
Reply(200).
BodyString("v1")
JSON(map[string]interface{}{
"api": map[string]interface{}{},
"db": map[string]interface{}{"schema_version": "400"},
"service": map[string]interface{}{"version": "4.8.0"},
})
case "error when v1 and v2 are not found":
gock.New("https://ancho.re").
Post(enterpriseEndpoint).
Post(reportAPIPathV2).
Reply(404)
gock.New("https://ancho.re").
Get("/").
Get("/version").
Reply(404)
}

Expand Down Expand Up @@ -163,9 +167,13 @@ func TestPostSimulateV1ToV2HandoverFromEnterprise4Xto5X(t *testing.T) {
Post(reportAPIPathV2).
Reply(404)
gock.New("https://ancho.re").
Get("/").
Get("/version").
Reply(200).
BodyString("v1")
JSON(map[string]interface{}{
"api": map[string]interface{}{},
"db": map[string]interface{}{"schema_version": "400"},
"service": map[string]interface{}{"version": "4.8.0"},
})
gock.New("https://ancho.re").
Post(reportAPIPathV1).
Reply(200)
Expand All @@ -178,9 +186,13 @@ func TestPostSimulateV1ToV2HandoverFromEnterprise4Xto5X(t *testing.T) {
Post(reportAPIPathV1).
Reply(404)
gock.New("https://ancho.re").
Get("/").
Get("/version").
Reply(200).
BodyString("v2")
JSON(map[string]interface{}{
"api": map[string]interface{}{"version": "2"},
"db": map[string]interface{}{"schema_version": "400"},
"service": map[string]interface{}{"version": "4.8.0"},
})
gock.New("https://ancho.re").
Post(reportAPIPathV2).
Reply(200)
Expand Down

0 comments on commit 0f987d1

Please sign in to comment.