Skip to content

Commit

Permalink
✨ Support filter type for list tags
Browse files Browse the repository at this point in the history
  • Loading branch information
tosone committed Oct 19, 2023
1 parent c234432 commit 3ea428c
Show file tree
Hide file tree
Showing 31 changed files with 197 additions and 528 deletions.
2 changes: 1 addition & 1 deletion e2e/multiarch/buildkit.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[registry."10.3.201.221:3000"]
[registry."192.168.31.198:3000"]
http = true
11 changes: 11 additions & 0 deletions e2e/multiarch/cosign.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-----BEGIN ENCRYPTED SIGSTORE PRIVATE KEY-----
eyJrZGYiOnsibmFtZSI6InNjcnlwdCIsInBhcmFtcyI6eyJOIjo2NTUzNiwiciI6
OCwicCI6MX0sInNhbHQiOiJCUEJNY0E1MGdKK1FvWVJDZnVDT1RzU2hrSHQ3Snhw
ellGWEdKSWdyM093PSJ9LCJjaXBoZXIiOnsibmFtZSI6Im5hY2wvc2VjcmV0Ym94
Iiwibm9uY2UiOiJ4VE9qaC8rUEJwQ29jSkx4MXI5R2FIdlRRWUdIa3YvZSJ9LCJj
aXBoZXJ0ZXh0IjoickJFeGVmekRJUjdoQ1p6QWRTWjh0OXMzQVBCVVRHMDUweHo0
NUQxVDlaL0hqN3VrOEpVUGdna0d3aEhCWm82M0g3Q2grNEZNamdJdnI5d1NFUThm
SU1CbmQ1emhTWHBhNElRbzB3dEgvb3duMlBtTDRrSHFqdWVQSmdGbjQrTkRXZzBQ
bUVTMFhBQUQ5aWE0QWxWTVpzWExlblhDTDVET1VvVlFJUGVOMU5UNlNiMUFVaXRl
UThpT0ZuTVUxSm83QXZjSjd4YStjRCs3S0E9PSJ9
-----END ENCRYPTED SIGSTORE PRIVATE KEY-----
4 changes: 4 additions & 0 deletions e2e/multiarch/cosign.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEUV5SViVARNtgyAP+WnOguji79aRq
OXqW6WnoUk34nn2HpmEE3ChJvZS0MWsRs1JAUBXn20cYUySYe1W3DEMc2Q==
-----END PUBLIC KEY-----
6 changes: 5 additions & 1 deletion e2e/multiarch/push.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#!/bin/sh
# docker buildx create --use --config ./buildkit.toml

docker buildx build --platform linux/amd64,linux/arm64 --tag 10.3.201.221:3000/library/alpine:3.18.0 --file alpine.Dockerfile --push .
docker buildx build --sbom=true --platform linux/amd64,linux/arm64 --tag 192.168.31.198:3000/library/alpine:3.18.0 --file alpine.Dockerfile --push .

cosign generate-key-pair

env COSIGN_PASSWORD= cosign sign --tlog-upload=false --allow-http-registry --key cosign.key --recursive 192.168.31.198:3000/library/alpine@sha256:d780ea42ba737c40b6a7fcadd8b8e6f5dd4365fb2166053f132aafe3b6ac9bcd
5 changes: 4 additions & 1 deletion pkg/cmds/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,13 @@ func Serve(serverConfig ServerConfig) error {
reqPath := c.Request().URL.Path
if strings.HasPrefix(reqPath, "/assets/") {
if strings.HasSuffix(c.Request().URL.Path, ".js") ||
strings.HasSuffix(c.Request().URL.Path, ".map") ||
strings.HasSuffix(c.Request().URL.Path, ".css") ||
strings.HasSuffix(c.Request().URL.Path, ".svg") ||
strings.HasSuffix(c.Request().URL.Path, ".png") ||
strings.HasSuffix(c.Request().URL.Path, ".ttf") {
strings.HasSuffix(c.Request().URL.Path, ".ttf") ||
strings.HasSuffix(c.Request().URL.Path, ".json") ||
strings.HasSuffix(c.Request().URL.Path, ".yaml") {
c.Response().Header().Add("Cache-Control", "max-age=3600")
}
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/dal/dao/mocks/tag.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 25 additions & 11 deletions pkg/dal/dao/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package dao

import (
"context"
"database/sql/driver"
"errors"
"fmt"
"time"
Expand Down Expand Up @@ -51,7 +52,7 @@ type TagService interface {
// ListByDtPagination lists the tags by the specified repository and pagination.
ListByDtPagination(ctx context.Context, repository string, limit int, lastID ...int64) ([]*models.Tag, error)
// ListTag lists the tags by the specified request.
ListTag(ctx context.Context, repositoryID int64, name *string, pagination types.Pagination, sort types.Sortable) ([]*models.Tag, int64, error)
ListTag(ctx context.Context, repositoryID int64, name *string, types []enums.ArtifactType, pagination types.Pagination, sort types.Sortable) ([]*models.Tag, int64, error)
// CountArtifact counts the artifacts by the specified request.
CountTag(ctx context.Context, req types.ListTagRequest) (int64, error)
// CountByNamespace counts the tags by the specified namespace.
Expand Down Expand Up @@ -203,30 +204,43 @@ func (s *tagService) ListByDtPagination(ctx context.Context, repository string,
}

// ListTag lists the tags by the specified request.
func (s *tagService) ListTag(ctx context.Context, repositoryID int64, name *string, pagination types.Pagination, sort types.Sortable) ([]*models.Tag, int64, error) {
func (s *tagService) ListTag(ctx context.Context, repositoryID int64, name *string, types []enums.ArtifactType, pagination types.Pagination, sort types.Sortable) ([]*models.Tag, int64, error) {
var mTypes []driver.Valuer
if len(types) > 0 {
for _, t := range types {
mTypes = append(mTypes, t)
}

Check warning on line 212 in pkg/dal/dao/tag.go

View check run for this annotation

Codecov / codecov/patch

pkg/dal/dao/tag.go#L210-L212

Added lines #L210 - L212 were not covered by tests
}
pagination = utils.NormalizePagination(pagination)
query := s.tx.Tag.WithContext(ctx).Where(s.tx.Tag.RepositoryID.Eq(repositoryID))
if len(types) > 0 {
query = query.RightJoin(s.tx.Artifact, s.tx.Tag.ArtifactID.EqCol(s.tx.Artifact.ID), s.tx.Artifact.Type.In(mTypes...))

Check warning on line 217 in pkg/dal/dao/tag.go

View check run for this annotation

Codecov / codecov/patch

pkg/dal/dao/tag.go#L217

Added line #L217 was not covered by tests
} else {
query = query.RightJoin(s.tx.Artifact, s.tx.Tag.ArtifactID.EqCol(s.tx.Artifact.ID))
}
if name != nil {
query = query.Where(s.tx.Tag.Name.Like(fmt.Sprintf("%%%s%%", ptr.To(name))))
}
field, ok := s.tx.Tag.GetFieldByName(ptr.To(sort.Sort))
if ok {
switch ptr.To(sort.Method) {
case enums.SortMethodDesc:
query.Order(field.Desc())
query = query.Order(field.Desc())

Check warning on line 228 in pkg/dal/dao/tag.go

View check run for this annotation

Codecov / codecov/patch

pkg/dal/dao/tag.go#L228

Added line #L228 was not covered by tests
case enums.SortMethodAsc:
query.Order(field)
query = query.Order(field)

Check warning on line 230 in pkg/dal/dao/tag.go

View check run for this annotation

Codecov / codecov/patch

pkg/dal/dao/tag.go#L230

Added line #L230 was not covered by tests
default:
query.Order(s.tx.Tag.UpdatedAt.Desc())
query = query.Order(s.tx.Tag.UpdatedAt.Desc())

Check warning on line 232 in pkg/dal/dao/tag.go

View check run for this annotation

Codecov / codecov/patch

pkg/dal/dao/tag.go#L232

Added line #L232 was not covered by tests
}
} else {
query.Order(s.tx.Tag.UpdatedAt.Desc())
query = query.Order(s.tx.Tag.UpdatedAt.Desc())
}
query.UnderlyingDB().Preload("Artifact.ArtifactIndexes.Vulnerability")
query.UnderlyingDB().Preload("Artifact.ArtifactIndexes.Sbom")
query.Preload(s.tx.Tag.Artifact.ArtifactIndexes)
query.Preload(s.tx.Tag.Artifact.Vulnerability)
query.Preload(s.tx.Tag.Artifact.Sbom)
if len(types) > 0 {
query = query.Preload(s.tx.Tag.Artifact.ArtifactIndexes.On(s.tx.Artifact.Type.In(mTypes...)))

Check warning on line 238 in pkg/dal/dao/tag.go

View check run for this annotation

Codecov / codecov/patch

pkg/dal/dao/tag.go#L238

Added line #L238 was not covered by tests
} else {
query = query.Preload(s.tx.Tag.Artifact.ArtifactIndexes)
}
query = query.Preload(s.tx.Tag.Artifact.Vulnerability)
query = query.Preload(s.tx.Tag.Artifact.Sbom)
return query.FindByPage(ptr.To(pagination.Limit)*(ptr.To(pagination.Page)-1), ptr.To(pagination.Limit))
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/dal/dao/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func TestTagService(t *testing.T) {
Size: 123,
ContentType: "test",
Raw: []byte("test"),
Type: enums.ArtifactTypeImage,
},
}
err = tagService.Create(ctx, tagObj)
Expand All @@ -111,7 +112,7 @@ func TestTagService(t *testing.T) {
assert.Equal(t, tag3.PullTimes, int64(1))
assert.True(t, tag3.LastPull.Valid)

tags1, _, err := tagService.ListTag(ctx, repositoryObj.ID, nil, types.Pagination{
tags1, _, err := tagService.ListTag(ctx, repositoryObj.ID, nil, nil, types.Pagination{
Limit: ptr.Of(int(100)),
Page: ptr.Of(int(0)),
}, types.Sortable{})
Expand Down
20 changes: 20 additions & 0 deletions pkg/handlers/apidocs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions pkg/handlers/apidocs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,26 @@
"description": "search tag with name",
"name": "name",
"in": "query"
},
{
"type": "array",
"items": {
"enum": [
"image",
"imageIndex",
"chart",
"cnab",
"cosign",
"wasm",
"provenance",
"unknown"
],
"type": "string"
},
"collectionFormat": "multi",
"description": "search tag with type",
"name": "type",
"in": "query"
}
],
"responses": {
Expand Down
16 changes: 16 additions & 0 deletions pkg/handlers/apidocs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2202,6 +2202,22 @@ paths:
in: query
name: name
type: string
- collectionFormat: multi
description: search tag with type
in: query
items:
enum:
- image
- imageIndex
- chart
- cnab
- cosign
- wasm
- provenance
- unknown
type: string
name: type
type: array
produces:
- application/json
responses:
Expand Down
2 changes: 0 additions & 2 deletions pkg/handlers/distribution/manifest/manifest_head_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ func TestHeadManifestFallbackProxy(t *testing.T) {
Proxy: configs.ConfigurationProxy{
Endpoint: s.URL,
TlsVerify: true,
// Username: cUsername,
// Password: cPassword,
},
},
}
Expand Down
18 changes: 10 additions & 8 deletions pkg/handlers/tags/tags_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ import (
// @Accept json
// @Produce json
// @Router /namespaces/{namespace}/tags/ [get]
// @Param limit query int64 false "limit" minimum(10) maximum(100) default(10)
// @Param page query int64 false "page" minimum(1) default(1)
// @Param sort query string false "sort field"
// @Param method query string false "sort method" Enums(asc, desc)
// @Param namespace path string true "namespace"
// @Param repository query string false "repository"
// @Param name query string false "search tag with name"
// @Param limit query int64 false "limit" minimum(10) maximum(100) default(10)
// @Param page query int64 false "page" minimum(1) default(1)
// @Param sort query string false "sort field"
// @Param method query string false "sort method" Enums(asc, desc)
// @Param namespace path string true "namespace"
// @Param repository query string false "repository"
// @Param name query string false "search tag with name"
// @Param type query []string false "search tag with type" Enums(image, imageIndex, chart, cnab, cosign, wasm, provenance, unknown) collectionFormat(multi)
// @Success 200 {object} types.CommonList{items=[]types.TagItem}
// @Failure 404 {object} xerrors.ErrCode
// @Failure 500 {object} xerrors.ErrCode
Expand Down Expand Up @@ -85,7 +86,7 @@ func (h *handlers) ListTag(c echo.Context) error {
}

tagService := h.tagServiceFactory.New()
tags, total, err := tagService.ListTag(ctx, repositoryObj.ID, req.Name, req.Pagination, req.Sortable)
tags, total, err := tagService.ListTag(ctx, repositoryObj.ID, req.Name, req.Type, req.Pagination, req.Sortable)
if err != nil {
log.Error().Err(err).Msg("List tag from db failed")
return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeInternalError, err.Error())
Expand All @@ -95,6 +96,7 @@ func (h *handlers) ListTag(c echo.Context) error {
for _, tag := range tags {
if tag.Artifact == nil {
log.Error().Str("image", fmt.Sprintf("%s:%s", repositoryObj.Name, tag.Name)).Msg("Some tag's artifact reference invalid")
continue

Check warning on line 99 in pkg/handlers/tags/tags_list.go

View check run for this annotation

Codecov / codecov/patch

pkg/handlers/tags/tags_list.go#L99

Added line #L99 was not covered by tests
}
var artifacts []types.TagItemArtifact
for _, item := range tag.Artifact.ArtifactIndexes {
Expand Down
2 changes: 1 addition & 1 deletion pkg/handlers/tags/tags_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func TestListTag(t *testing.T) {
defer ctrl.Finish()

daoMockTagService := daomock.NewMockTagService(ctrl)
daoMockTagService.EXPECT().ListTag(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(_ context.Context, _ int64, _ *string, _ types.Pagination, _ types.Sortable) ([]*models.Tag, int64, error) {
daoMockTagService.EXPECT().ListTag(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(_ context.Context, _ int64, _ *string, _ []enums.ArtifactType, _ types.Pagination, _ types.Sortable) ([]*models.Tag, int64, error) {
return nil, 0, fmt.Errorf("test")
}).Times(1)
daoMockTagServiceFactory := daomock.NewMockTagServiceFactory(ctrl)
Expand Down
3 changes: 3 additions & 0 deletions pkg/middlewares/redirect_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,8 @@ func skipRedirect(c echo.Context) bool {
strings.HasSuffix(reqPath, ".js")) {
return true
}
if strings.HasPrefix(reqPath, "/swagger") {
return true
}

Check warning on line 77 in pkg/middlewares/redirect_repository.go

View check run for this annotation

Codecov / codecov/patch

pkg/middlewares/redirect_repository.go#L75-L77

Added lines #L75 - L77 were not covered by tests
return false
}
5 changes: 2 additions & 3 deletions pkg/signing/cosign/sign/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ import (
"github.com/go-sigma/sigma/pkg/utils/imagerefs"
)

// COSIGN_PASSWORD= ./cosign sign --tlog-upload=false --allow-http-registry --key cosign.key --registry-token eyJhbGciOiJSUzUxMiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzaWdtYSIsInN1YiI6IjIiLCJleHAiOjE2OTc2OTYxMDMsIm5iZiI6MTY5NzYwOTcwMywiaWF0IjoxNjk3NjA5NzAzLCJqdGkiOiIyYzIzNWNkOC0xYmJiLTRiNGYtODYyMi1iNjI2NDZiNTE3Y2EiLCJ1aWQiOiIyIn0.W_8F6Sh2Sj21lQm_ezwQHYvNTKSUdoYHru2hOrWqKtakx6s0arzW3NAyCo7ygSdjFpX18ZcSrsaOTDkccOYNmP-66ffYxLdV2PtfP8yGP-HkdvnO1cuTTUc5a25Xbn1nx2pJTDFpUKkehuBpSQ63pVnricRkQyxx-b7umg8i8MckMsSoX8eHM_IxKoFeKcMfmXqZb_RqDIzAXR2EBRTwPw16kZFD_ROtRTpeKihnXlUTcOz8t7ULoikBwNukwDG0nHXPwK4FQ64Rj-JyjGbRnSSJQTz0IhdnwFOW62_yHKic-4eK9MfWAlMvEXweV_uC1wbop-n5I84UtMPlQ0CT6Rz2iVHVkwPE4dZc9pLcBAv0UJJOnjPxQz5OUetBfIBgP9Kz7q7amA7qWf1fmq0Gf-GL7SkIqBp1ca45Fsqg2NOnNxOzxAPYUprQrQ9ObyooEJN85tTjQsIGmNMC853C9aPDN7pNBVWyba5I_xKFzaRNeO3BvdLjs8cq9K_SYgHA_yfZe-VBrfqc-jSx-t4HFxP429F9CBMKpz1JnH_ROg94Y5FAO9NgG9Jy0thZsMK6DjyiX_sG8sSeB4FULvmP7RV03ghMEu3nObDYeOxkKtUoNuGtL_BvVkOUF7zhvhQsjbeWOIOJVcJy-ljNk8AFnTZwpZQ0PAbWceRxFEEw-iw 10.3.198.80:3000/library/redis@sha256:b355b79a3117c4bfa2e8a05d5ed0496a19e542444d5ccf0328632091238a0136

type signing struct {
Multiarch bool
Http bool
Expand Down Expand Up @@ -81,8 +79,9 @@ func (s *signing) Sign(ctx context.Context, token, priKey, ref string) error {
cmd.Args = append(cmd.Args, "--key", temp.Name())
cmd.Args = append(cmd.Args, "--registry-token", token)
cmd.Args = append(cmd.Args, imageRef)
cmd.Env = append(cmd.Env, "COSIGN_PASSWORD=")

return nil
return cmd.Run()
}

// GetDigest ...
Expand Down
5 changes: 4 additions & 1 deletion pkg/types/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package types

import "github.com/go-sigma/sigma/pkg/types/enums"

// TagItemArtifact ...
type TagItemArtifact struct {
ID int64 `json:"id" example:"1"`
Expand Down Expand Up @@ -53,7 +55,8 @@ type ListTagRequest struct {
Namespace string `json:"namespace" param:"namespace" validate:"required,min=2,max=20,is_valid_namespace" example:"library"`
Repository string `json:"repository" query:"repository" validate:"required,is_valid_repository" example:"library/busybox"`

Name *string `json:"name" query:"name"`
Name *string `json:"name" query:"name"`
Type []enums.ArtifactType `json:"type" query:"type"`
}

// DeleteTagRequest represents the request to delete a tag.
Expand Down
23 changes: 0 additions & 23 deletions test/demo/.helmignore

This file was deleted.

24 changes: 0 additions & 24 deletions test/demo/Chart.yaml

This file was deleted.

Loading

0 comments on commit 3ea428c

Please sign in to comment.