Skip to content

Commit

Permalink
add cosign signature icon (goharbor#16533)
Browse files Browse the repository at this point in the history
Return icon message for UI to render

Signed-off-by: Wang Yan <wangyan@vmware.com>
  • Loading branch information
wy65701436 authored and sluetze committed Oct 29, 2022
1 parent 9572832 commit 6f87e6b
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 11 deletions.
Binary file added icons/cosign.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 36 additions & 9 deletions src/controller/icon/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,37 @@ import (
)

var (
builtInIcons = map[string]string{
icon.DigestOfIconImage: "./icons/image.png",
icon.DigestOfIconChart: "./icons/chart.png",
icon.DigestOfIconCNAB: "./icons/cnab.png",
icon.DigestOfIconDefault: "./icons/default.png",
builtInIcons = map[string]buildInIcon{
icon.DigestOfIconImage: {
path: "./icons/image.png",
resize: true,
},
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 = NewController()
)

type buildInIcon struct {
path string
resize bool
}

// Icon model for artifact icon
type Icon struct {
ContentType string
Expand Down Expand Up @@ -84,9 +105,9 @@ func (c *controller) Get(ctx context.Context, digest string) (*Icon, error) {
iconFile io.ReadCloser
err error
)
// for the fixed icons: image, helm chart, CNAB and unknown
if path, exist := builtInIcons[digest]; exist {
iconFile, err = os.Open(path)

if i, exist := builtInIcons[digest]; exist {
iconFile, err = os.Open(i.path)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -118,7 +139,13 @@ func (c *controller) Get(ctx context.Context, digest string) (*Icon, error) {
}

// 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
buf := &bytes.Buffer{}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/icon/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ const (

// ToDo add the accessories images
DigestOfIconAccDefault = ""
DigestOfIconAccCosign = ""
DigestOfIconAccCosign = "sha256:20401d5b3a0f6dbc607c8d732eb08471af4ae6b19811a4efce8c6a724aed2882"
)
16 changes: 16 additions & 0 deletions src/pkg/accessory/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package accessory
import (
"context"
"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/pkg/accessory/dao"
"github.com/goharbor/harbor/src/pkg/accessory/model"
Expand All @@ -28,6 +29,11 @@ import (
var (
// Mgr is a global artifact manager instance
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
Expand Down Expand Up @@ -93,6 +99,7 @@ func (m *manager) Get(ctx context.Context, id int64) (model.Accessory, error) {
Size: acc.Size,
Digest: acc.Digest,
CreatTime: acc.CreationTime,
Icon: m.GetIcon(acc.Type),
})
}

Expand All @@ -114,6 +121,7 @@ func (m *manager) List(ctx context.Context, query *q.Query) ([]model.Accessory,
Size: accD.Size,
Digest: accD.Digest,
CreatTime: accD.CreationTime,
Icon: m.GetIcon(accD.Type),
})
if err != nil {
return nil, errors.New(err).WithCode(errors.BadRequestCode)
Expand Down Expand Up @@ -142,3 +150,11 @@ func (m *manager) DeleteAccessories(ctx context.Context, q *q.Query) error {
_, err := m.dao.DeleteAccessories(ctx, q)
return err
}

func (m *manager) GetIcon(accType string) string {
accIcon := ""
if i, ok := defaultIcons[accType]; ok {
accIcon = i
}
return accIcon
}
10 changes: 10 additions & 0 deletions src/pkg/accessory/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ func (m *managerTestSuite) TestDeleteOfArtifact() {
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) {
suite.Run(t, &managerTestSuite{})
}
1 change: 1 addition & 0 deletions src/pkg/accessory/model/accessory.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ type AccessoryData struct {
Size int64 `json:"size"`
Digest string `json:"digest"`
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
Expand Down
2 changes: 1 addition & 1 deletion src/server/v2.0/handler/model/accessory.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (a *Accessory) ToSwagger() *models.Accessory {
Size: a.Size,
Digest: a.Digest,
Type: a.Type,
Icon: "",
Icon: a.Icon,
CreationTime: strfmt.DateTime(a.CreatTime),
}
}
Expand Down

0 comments on commit 6f87e6b

Please sign in to comment.