-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add generate sbom object utility (#20097)
* add generate sbom object utility Leverage the go-containerregistry to generate the oci object for sbom and add it as an accessory of the subject artifact. Signed-off-by: wang yan <[email protected]> * remove vendor Signed-off-by: wang yan <[email protected]> * resolve comments Signed-off-by: wang yan <[email protected]> * fix ut Signed-off-by: wang yan <[email protected]> * resolve comments Signed-off-by: wang yan <[email protected]> * remove the todo comments Signed-off-by: wang yan <[email protected]> --------- Signed-off-by: wang yan <[email protected]>
- Loading branch information
1 parent
8bec57f
commit dbe9790
Showing
8 changed files
with
204 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright Project Harbor Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package scan | ||
|
||
import ( | ||
"crypto/tls" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/google/go-containerregistry/pkg/authn" | ||
"github.com/google/go-containerregistry/pkg/name" | ||
v1 "github.com/google/go-containerregistry/pkg/v1" | ||
"github.com/google/go-containerregistry/pkg/v1/empty" | ||
"github.com/google/go-containerregistry/pkg/v1/mutate" | ||
"github.com/google/go-containerregistry/pkg/v1/remote" | ||
"github.com/google/go-containerregistry/pkg/v1/static" | ||
"github.com/google/go-containerregistry/pkg/v1/types" | ||
"github.com/opencontainers/go-digest" | ||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
|
||
"github.com/goharbor/harbor/src/controller/robot" | ||
v1sq "github.com/goharbor/harbor/src/pkg/scan/rest/v1" | ||
) | ||
|
||
type Insecure bool | ||
|
||
func (i Insecure) RemoteOptions() []remote.Option { | ||
tr := http.DefaultTransport.(*http.Transport).Clone() | ||
tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: bool(i)} | ||
return []remote.Option{remote.WithTransport(tr)} | ||
} | ||
|
||
type referrer struct { | ||
Insecure | ||
} | ||
|
||
// GenAccessoryArt composes the accessory oci object and push it back to harbor core as an accessory of the scanned artifact. | ||
func GenAccessoryArt(sq v1sq.ScanRequest, accData []byte, accAnnotations map[string]string, mediaType string, robot robot.Robot) (string, error) { | ||
accArt, err := mutate.Append(empty.Image, mutate.Addendum{ | ||
Layer: static.NewLayer(accData, ocispec.MediaTypeImageLayer), | ||
History: v1.History{ | ||
Author: "harbor", | ||
CreatedBy: "harbor", | ||
Created: v1.Time{}, // static | ||
}, | ||
}) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
dg, err := digest.Parse(sq.Artifact.Digest) | ||
if err != nil { | ||
return "", err | ||
} | ||
accSubArt := &v1.Descriptor{ | ||
MediaType: types.MediaType(sq.Artifact.MimeType), | ||
Size: sq.Artifact.Size, | ||
Digest: v1.Hash{ | ||
Algorithm: dg.Algorithm().String(), | ||
Hex: dg.Hex(), | ||
}, | ||
} | ||
// TODO to leverage the artifactType of distribution spec v1.1 to specify the sbom type. | ||
// https://github.com/google/go-containerregistry/issues/1832 | ||
accArt = mutate.MediaType(accArt, ocispec.MediaTypeImageManifest) | ||
accArt = mutate.ConfigMediaType(accArt, types.MediaType(mediaType)) | ||
accArt = mutate.Subject(accArt, *accSubArt).(v1.Image) | ||
accArt = mutate.Annotations(accArt, accAnnotations).(v1.Image) | ||
|
||
digest, err := accArt.Digest() | ||
if err != nil { | ||
return "", err | ||
} | ||
accRef, err := name.ParseReference(fmt.Sprintf("%s/%s@%s", sq.Registry.URL, sq.Artifact.Repository, digest.String())) | ||
if err != nil { | ||
return "", err | ||
} | ||
opts := append(referrer{Insecure: true}.RemoteOptions(), remote.WithAuth(&authn.Basic{Username: robot.Name, Password: robot.Secret})) | ||
if err := remote.Write(accRef, accArt, opts...); err != nil { | ||
return "", err | ||
} | ||
return digest.String(), nil | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright Project Harbor Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package scan | ||
|
||
import ( | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/google/go-containerregistry/pkg/registry" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/goharbor/harbor/src/controller/robot" | ||
rm "github.com/goharbor/harbor/src/pkg/robot/model" | ||
v1sq "github.com/goharbor/harbor/src/pkg/scan/rest/v1" | ||
) | ||
|
||
func TestGenAccessoryArt(t *testing.T) { | ||
server := httptest.NewServer(registry.New(registry.WithReferrersSupport(true))) | ||
defer server.Close() | ||
u, err := url.Parse(server.URL) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
sq := v1sq.ScanRequest{ | ||
Registry: &v1sq.Registry{ | ||
URL: u.Host, | ||
}, | ||
Artifact: &v1sq.Artifact{ | ||
Repository: "library/hello-world", | ||
Tag: "latest", | ||
Size: 1234, | ||
MimeType: "application/vnd.docker.distribution.manifest.v2+json", | ||
Digest: "sha256:d37ada95d47ad12224c205a938129df7a3e52345828b4fa27b03a98825d1e2e7", | ||
}, | ||
} | ||
r := robot.Robot{ | ||
Robot: rm.Robot{ | ||
Name: "admin", | ||
Secret: "Harbor12345", | ||
}, | ||
} | ||
|
||
annotations := map[string]string{ | ||
"created-by": "trivy", | ||
"org.opencontainers.artifact.description": "SPDX JSON SBOM", | ||
} | ||
s, err := GenAccessoryArt(sq, []byte(`{"name": "harborAccTest", "version": "1.0"}`), annotations, "application/vnd.goharbor.harbor.main.v1", r) | ||
assert.Nil(t, err) | ||
assert.Equal(t, "sha256:8de6104b79deca0253ff8667692f03e34753494c77ec81f631b45aad69223c18", s) | ||
} |
Oops, something went wrong.