Skip to content
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

fix issue 20407 #20416

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/pkg/scan/rest/v1/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ type Registry struct {
// An optional value of the HTTP Authorization header sent with each request to the Docker Registry for getting or exchanging token.
// For example, `Basic: Base64(username:password)`.
Authorization string `json:"authorization"`
// Insecure is an indicator of https or http.
Insecure bool `json:"insecure"`
}

// ScanRequest represents a structure that is sent to a Scanner Adapter to initiate artifact scanning.
Expand Down
13 changes: 7 additions & 6 deletions src/pkg/scan/sbom/sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@
)

func init() {
scan.RegisterScanHanlder(v1.ScanTypeSbom, &scanHandler{GenAccessoryFunc: scan.GenAccessoryArt, RegistryServer: registryFQDN})
scan.RegisterScanHanlder(v1.ScanTypeSbom, &scanHandler{GenAccessoryFunc: scan.GenAccessoryArt, RegistryServer: registry})
}

// ScanHandler defines the Handler to generate sbom
type scanHandler struct {
GenAccessoryFunc func(scanRep v1.ScanRequest, sbomContent []byte, labels map[string]string, mediaType string, robot *model.Robot) (string, error)
RegistryServer func(ctx context.Context) string
RegistryServer func(ctx context.Context) (string, bool)
}

// RequestProducesMineTypes defines the mine types produced by the scan handler
Expand Down Expand Up @@ -96,7 +96,7 @@
Artifact: sr.Artifact,
}
// the registry server url is core by default, need to replace it with real registry server url
scanReq.Registry.URL = v.RegistryServer(ctx.SystemContext())
scanReq.Registry.URL, scanReq.Registry.Insecure = v.RegistryServer(ctx.SystemContext())
if len(scanReq.Registry.URL) == 0 {
return "", fmt.Errorf("empty registry server")
}
Expand Down Expand Up @@ -139,15 +139,16 @@
}

// extract server name from config, and remove the protocol prefix
func registryFQDN(ctx context.Context) string {
func registry(ctx context.Context) (string, bool) {

Check warning on line 142 in src/pkg/scan/sbom/sbom.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/scan/sbom/sbom.go#L142

Added line #L142 was not covered by tests
cfgMgr, ok := config.FromContext(ctx)
if ok {
extURL := cfgMgr.Get(context.Background(), common.ExtEndpoint).GetString()
insecure := strings.HasPrefix(extURL, "http://")

Check warning on line 146 in src/pkg/scan/sbom/sbom.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/scan/sbom/sbom.go#L146

Added line #L146 was not covered by tests
server := strings.TrimPrefix(extURL, "https://")
server = strings.TrimPrefix(server, "http://")
return server
return server, insecure

Check warning on line 149 in src/pkg/scan/sbom/sbom.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/scan/sbom/sbom.go#L149

Added line #L149 was not covered by tests
}
return ""
return "", false

Check warning on line 151 in src/pkg/scan/sbom/sbom.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/scan/sbom/sbom.go#L151

Added line #L151 was not covered by tests
}

// retrieveSBOMContent retrieves the "sbom" field from the raw report
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/scan/sbom/sbom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ func Test_scanHandler_RequestProducesMineTypes(t *testing.T) {
}
}

func mockGetRegistry(ctx context.Context) string {
return "myharbor.example.com"
func mockGetRegistry(ctx context.Context) (string, bool) {
return "myharbor.example.com", false
}

func mockGenAccessory(scanRep v1.ScanRequest, sbomContent []byte, labels map[string]string, mediaType string, robot *model.Robot) (string, error) {
Expand Down
3 changes: 3 additions & 0 deletions src/pkg/scan/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@
return "", err
}
accRef, err := name.ParseReference(fmt.Sprintf("%s/%s@%s", sq.Registry.URL, sq.Artifact.Repository, dgst.String()))
if sq.Registry.Insecure {
accRef, err = name.ParseReference(fmt.Sprintf("%s/%s@%s", sq.Registry.URL, sq.Artifact.Repository, dgst.String()), name.Insecure)
}

Check warning on line 91 in src/pkg/scan/util.go

View check run for this annotation

Codecov / codecov/patch

src/pkg/scan/util.go#L90-L91

Added lines #L90 - L91 were not covered by tests
if err != nil {
return "", err
}
Expand Down
Loading