add cosign signature icon (#16533)

Return icon message for UI to render

Signed-off-by: Wang Yan <wangyan@vmware.com>
This commit is contained in:
Wang Yan 2022-03-17 11:08:39 +08:00 committed by GitHub
parent 4cec345a45
commit 7cb5c34f7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 65 additions and 11 deletions

BIN
icons/cosign.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

View File

@ -37,16 +37,37 @@ import (
) )
var ( var (
builtInIcons = map[string]string{ builtInIcons = map[string]buildInIcon{
icon.DigestOfIconImage: "./icons/image.png", icon.DigestOfIconImage: {
icon.DigestOfIconChart: "./icons/chart.png", path: "./icons/image.png",
icon.DigestOfIconCNAB: "./icons/cnab.png", resize: true,
icon.DigestOfIconDefault: "./icons/default.png", },
icon.DigestOfIconChart: {
path: "./icons/chart.png",
resize: true,
},
icon.DigestOfIconCNAB: {
path: "./icons/cnab.png",
resize: true,
},
icon.DigestOfIconAccCosign: {
path: "./icons/cosign.png",
resize: false,
},
icon.DigestOfIconDefault: {
path: "./icons/default.png",
resize: true,
},
} }
// Ctl is a global icon controller instance // Ctl is a global icon controller instance
Ctl = NewController() Ctl = NewController()
) )
type buildInIcon struct {
path string
resize bool
}
// Icon model for artifact icon // Icon model for artifact icon
type Icon struct { type Icon struct {
ContentType string ContentType string
@ -84,9 +105,9 @@ func (c *controller) Get(ctx context.Context, digest string) (*Icon, error) {
iconFile io.ReadCloser iconFile io.ReadCloser
err error err error
) )
// for the fixed icons: image, helm chart, CNAB and unknown
if path, exist := builtInIcons[digest]; exist { if i, exist := builtInIcons[digest]; exist {
iconFile, err = os.Open(path) iconFile, err = os.Open(i.path)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -118,7 +139,13 @@ func (c *controller) Get(ctx context.Context, digest string) (*Icon, error) {
} }
// resize the icon to 50x50 // resize the icon to 50x50
img = resize.Thumbnail(50, 50, img, resize.NearestNeighbor) if i, exist := builtInIcons[digest]; exist {
if i.resize {
img = resize.Thumbnail(50, 50, img, resize.NearestNeighbor)
}
} else {
img = resize.Thumbnail(50, 50, img, resize.NearestNeighbor)
}
// encode the resized icon to png // encode the resized icon to png
buf := &bytes.Buffer{} buf := &bytes.Buffer{}

View File

@ -9,5 +9,5 @@ const (
// ToDo add the accessories images // ToDo add the accessories images
DigestOfIconAccDefault = "" DigestOfIconAccDefault = ""
DigestOfIconAccCosign = "" DigestOfIconAccCosign = "sha256:20401d5b3a0f6dbc607c8d732eb08471af4ae6b19811a4efce8c6a724aed2882"
) )

View File

@ -17,6 +17,7 @@ package accessory
import ( import (
"context" "context"
"github.com/goharbor/harbor/src/lib/errors" "github.com/goharbor/harbor/src/lib/errors"
"github.com/goharbor/harbor/src/lib/icon"
"github.com/goharbor/harbor/src/lib/q" "github.com/goharbor/harbor/src/lib/q"
"github.com/goharbor/harbor/src/pkg/accessory/dao" "github.com/goharbor/harbor/src/pkg/accessory/dao"
"github.com/goharbor/harbor/src/pkg/accessory/model" "github.com/goharbor/harbor/src/pkg/accessory/model"
@ -28,6 +29,11 @@ import (
var ( var (
// Mgr is a global artifact manager instance // Mgr is a global artifact manager instance
Mgr = NewManager() Mgr = NewManager()
// icon digests for each known type
defaultIcons = map[string]string{
model.TypeCosignSignature: icon.DigestOfIconAccCosign,
}
) )
// Manager is the only interface of artifact module to provide the management functions for artifacts // Manager is the only interface of artifact module to provide the management functions for artifacts
@ -93,6 +99,7 @@ func (m *manager) Get(ctx context.Context, id int64) (model.Accessory, error) {
Size: acc.Size, Size: acc.Size,
Digest: acc.Digest, Digest: acc.Digest,
CreatTime: acc.CreationTime, CreatTime: acc.CreationTime,
Icon: m.GetIcon(acc.Type),
}) })
} }
@ -114,6 +121,7 @@ func (m *manager) List(ctx context.Context, query *q.Query) ([]model.Accessory,
Size: accD.Size, Size: accD.Size,
Digest: accD.Digest, Digest: accD.Digest,
CreatTime: accD.CreationTime, CreatTime: accD.CreationTime,
Icon: m.GetIcon(accD.Type),
}) })
if err != nil { if err != nil {
return nil, errors.New(err).WithCode(errors.BadRequestCode) return nil, errors.New(err).WithCode(errors.BadRequestCode)
@ -142,3 +150,11 @@ func (m *manager) DeleteAccessories(ctx context.Context, q *q.Query) error {
_, err := m.dao.DeleteAccessories(ctx, q) _, err := m.dao.DeleteAccessories(ctx, q)
return err return err
} }
func (m *manager) GetIcon(accType string) string {
accIcon := ""
if i, ok := defaultIcons[accType]; ok {
accIcon = i
}
return accIcon
}

View File

@ -104,6 +104,16 @@ func (m *managerTestSuite) TestDeleteOfArtifact() {
m.dao.AssertExpectations(m.T()) m.dao.AssertExpectations(m.T())
} }
func (m *managerTestSuite) TestGetIcon() {
var icon string
icon = m.mgr.GetIcon("")
m.Require().Empty(icon, "empty icon")
icon = m.mgr.GetIcon("signature.cosign")
m.Require().Equal("sha256:20401d5b3a0f6dbc607c8d732eb08471af4ae6b19811a4efce8c6a724aed2882", icon)
icon = m.mgr.GetIcon("unknown")
m.Require().Empty(icon, "empty icon")
}
func TestManager(t *testing.T) { func TestManager(t *testing.T) {
suite.Run(t, &managerTestSuite{}) suite.Run(t, &managerTestSuite{})
} }

View File

@ -74,6 +74,7 @@ type AccessoryData struct {
Size int64 `json:"size"` Size int64 `json:"size"`
Digest string `json:"digest"` Digest string `json:"digest"`
CreatTime time.Time `json:"creation_time"` CreatTime time.Time `json:"creation_time"`
Icon string `json:"icon"`
} }
// Accessory Independent, but linked to an existing subject artifact, which enabling the extensibility of an OCI artifact // Accessory Independent, but linked to an existing subject artifact, which enabling the extensibility of an OCI artifact

View File

@ -20,7 +20,7 @@ func (a *Accessory) ToSwagger() *models.Accessory {
Size: a.Size, Size: a.Size,
Digest: a.Digest, Digest: a.Digest,
Type: a.Type, Type: a.Type,
Icon: "", Icon: a.Icon,
CreationTime: strfmt.DateTime(a.CreatTime), CreationTime: strfmt.DateTime(a.CreatTime),
} }
} }