diff --git a/api/v2.0/swagger.yaml b/api/v2.0/swagger.yaml index d0293ed76..4fe1aa99c 100644 --- a/api/v2.0/swagger.yaml +++ b/api/v2.0/swagger.yaml @@ -9382,10 +9382,9 @@ definitions: format: int64 description: The artifact id of the accessory x-omitempty: false - subject_artifact_id: - type: integer - format: int64 - description: The subject artifact id of the accessory + subject_artifact_digest: + type: string + description: The subject artifact digest of the accessory x-omitempty: false size: type: integer diff --git a/make/migrations/postgresql/0110_2.8.0_schema.up.sql b/make/migrations/postgresql/0110_2.8.0_schema.up.sql index 0c3921dbd..527285c2e 100644 --- a/make/migrations/postgresql/0110_2.8.0_schema.up.sql +++ b/make/migrations/postgresql/0110_2.8.0_schema.up.sql @@ -1,5 +1,26 @@ /* remove the redundant data from table artifact_blob */ delete from artifact_blob afb where not exists (select digest from blob b where b.digest = afb.digest_af); + +/* replace subject_artifact_id with subject_artifact_digest*/ +alter table artifact_accessory add column subject_artifact_digest varchar(1024); + +DO $$ +DECLARE + acc RECORD; + art RECORD; +BEGIN + FOR acc IN SELECT * FROM artifact_accessory + LOOP + SELECT * INTO art from artifact where id = acc.subject_artifact_id; + UPDATE artifact_accessory SET subject_artifact_digest=art.digest WHERE subject_artifact_id = art.id; + END LOOP; +END $$; + +alter table artifact_accessory drop CONSTRAINT artifact_accessory_subject_artifact_id_fkey; +alter table artifact_accessory drop CONSTRAINT unique_artifact_accessory; +alter table artifact_accessory add CONSTRAINT unique_artifact_accessory UNIQUE (artifact_id, subject_artifact_digest); +alter table artifact_accessory drop column subject_artifact_id; + /* Update the registry and replication policy associated with the chartmuseum */ UPDATE registry SET description = 'Chartmuseum has been deprecated in Harbor v2.8.0, please delete this registry.' diff --git a/src/controller/artifact/controller.go b/src/controller/artifact/controller.go index e86625e14..19907d66e 100644 --- a/src/controller/artifact/controller.go +++ b/src/controller/artifact/controller.go @@ -161,7 +161,7 @@ func (c *controller) Ensure(ctx context.Context, repository, digest string, opti } } for _, acc := range option.Accs { - if err = c.accessoryMgr.Ensure(ctx, artifact.ID, acc.ArtifactID, acc.Size, acc.Digest, acc.Type); err != nil { + if err = c.accessoryMgr.Ensure(ctx, artifact.Digest, acc.ArtifactID, acc.Size, acc.Digest, acc.Type); err != nil { return false, 0, err } } @@ -722,7 +722,7 @@ func (c *controller) populateAdditionLinks(ctx context.Context, artifact *Artifa } func (c *controller) populateAccessories(ctx context.Context, art *Artifact) { - accs, err := c.accessoryMgr.List(ctx, q.New(q.KeyWords{"SubjectArtifactID": art.ID})) + accs, err := c.accessoryMgr.List(ctx, q.New(q.KeyWords{"SubjectArtifactDigest": art.Digest})) if err != nil { log.Errorf("failed to list accessories of artifact %d: %v", art.ID, err) return diff --git a/src/controller/artifact/controller_test.go b/src/controller/artifact/controller_test.go index 3ad16dea6..40c6537d5 100644 --- a/src/controller/artifact/controller_test.go +++ b/src/controller/artifact/controller_test.go @@ -137,10 +137,10 @@ func (c *controllerTestSuite) TestAssembleArtifact() { }, nil) acc := &basemodel.Default{ Data: accessorymodel.AccessoryData{ - ID: 1, - ArtifactID: 2, - SubArtifactID: 1, - Type: accessorymodel.TypeCosignSignature, + ID: 1, + ArtifactID: 2, + SubArtifactDigest: "sha256:123", + Type: accessorymodel.TypeCosignSignature, }, } c.accMgr.On("List", mock.Anything, mock.Anything).Return([]accessorymodel.Accessory{ @@ -546,10 +546,10 @@ func (c *controllerTestSuite) TestCopy() { }, nil) acc := &basemodel.Default{ Data: accessorymodel.AccessoryData{ - ID: 1, - ArtifactID: 2, - SubArtifactID: 1, - Type: accessorymodel.TypeCosignSignature, + ID: 1, + ArtifactID: 2, + SubArtifactDigest: "sha256:418fb88ec412e340cdbef913b8ca1bbe8f9e8dc705f9617414c1f2c8db980180", + Type: accessorymodel.TypeCosignSignature, }, } c.accMgr.On("List", mock.Anything, mock.Anything).Return([]accessorymodel.Accessory{ diff --git a/src/controller/artifact/model_test.go b/src/controller/artifact/model_test.go index 93703efc5..a5ef1f87b 100644 --- a/src/controller/artifact/model_test.go +++ b/src/controller/artifact/model_test.go @@ -10,7 +10,7 @@ import ( ) func TestUnmarshalJSONWithACC(t *testing.T) { - data := []byte(`[{"accessories":[{"artifact_id":9,"creation_time":"2022-01-20T09:18:50.993Z","digest":"sha256:a7caa2636af890178a0b8c4cdbc47ced4dbdf29a1680e9e50823e85ce35b28d3","icon":"","id":4,"size":501,"subject_artifact_id":8,"type":"signature.cosign"}], + data := []byte(`[{"accessories":[{"artifact_id":9,"creation_time":"2022-01-20T09:18:50.993Z","digest":"sha256:a7caa2636af890178a0b8c4cdbc47ced4dbdf29a1680e9e50823e85ce35b28d3","icon":"","id":4,"size":501,"subject_artifact_digest":"sha256:e4b315ad03a1d1d9ff0c111e648a1a91066c09ead8352d3d6a48fa971a82922c","type":"signature.cosign"}], "addition_links":{"build_history":{"absolute":false,"href":"/api/v2.0/projects/source_project011642670285/repositories/redis/artifacts/sha256:e4b315ad03a1d1d9ff0c111e648a1a91066c09ead8352d3d6a48fa971a82922c/additions/build_history"}, "vulnerabilities":{"absolute":false,"href":"/api/v2.0/projects/source_project011642670285/repositories/redis/artifacts/sha256:e4b315ad03a1d1d9ff0c111e648a1a91066c09ead8352d3d6a48fa971a82922c/additions/vulnerabilities"}}, "digest":"sha256:e4b315ad03a1d1d9ff0c111e648a1a91066c09ead8352d3d6a48fa971a82922c", @@ -31,7 +31,7 @@ func TestUnmarshalJSONWithACC(t *testing.T) { } func TestUnmarshalJSONWithACCPartial(t *testing.T) { - data := []byte(`[{"accessories":[{"artifact_id":9,"creation_time":"2022-01-20T09:18:50.993Z","digest":"sha256:a7caa2636af890178a0b8c4cdbc47ced4dbdf29a1680e9e50823e85ce35b28d3","icon":"","id":4,"size":501,"subject_artifact_id":8,"type":"signature.cosign"}, {"artifact_id":2, "type":"signature.cosign"}], + data := []byte(`[{"accessories":[{"artifact_id":9,"creation_time":"2022-01-20T09:18:50.993Z","digest":"sha256:a7caa2636af890178a0b8c4cdbc47ced4dbdf29a1680e9e50823e85ce35b28d3","icon":"","id":4,"size":501,"subject_artifact_digest":"sha256:a7caa2636af890178a0b8c4cdbc47ced4dbdf29a1680e9e50823e85ce35b28d3","type":"signature.cosign"}, {"artifact_id":2, "type":"signature.cosign"}], "digest":"sha256:e4b315ad03a1d1d9ff0c111e648a1a91066c09ead8352d3d6a48fa971a82922c","tags":[{"artifact_id":8,"id":6,"immutable":false,"name":"latest","pull_time":"2022-01-20T09:18:50.783Z","push_time":"2022-01-20T09:18:50.303Z","repository_id":5,"signed":false}],"type":"IMAGE"}]`) var artifact []Artifact @@ -47,7 +47,7 @@ func TestUnmarshalJSONWithACCPartial(t *testing.T) { } func TestUnmarshalJSONWithACCUnknownType(t *testing.T) { - data := []byte(`[{"accessories":[{"artifact_id":9,"creation_time":"2022-01-20T09:18:50.993Z","digest":"sha256:a7caa2636af890178a0b8c4cdbc47ced4dbdf29a1680e9e50823e85ce35b28d3","icon":"","id":4,"size":501,"subject_artifact_id":8}], + data := []byte(`[{"accessories":[{"artifact_id":9,"creation_time":"2022-01-20T09:18:50.993Z","digest":"sha256:a7caa2636af890178a0b8c4cdbc47ced4dbdf29a1680e9e50823e85ce35b28d3","icon":"","id":4,"size":501,"subject_artifact_digest":"sha256:a7caa2636af890178a0b8c4cdbc47ced4dbdf29a1680e9e50823e85ce35b28d3"}], "digest":"sha256:e4b315ad03a1d1d9ff0c111e648a1a91066c09ead8352d3d6a48fa971a82922c","tags":[{"artifact_id":8,"id":6,"immutable":false,"name":"latest","pull_time":"2022-01-20T09:18:50.783Z","push_time":"2022-01-20T09:18:50.303Z","repository_id":5,"signed":false}],"type":"IMAGE"}]`) var artifact []Artifact diff --git a/src/pkg/accessory/dao/dao.go b/src/pkg/accessory/dao/dao.go index 791b08915..85fb1e709 100644 --- a/src/pkg/accessory/dao/dao.go +++ b/src/pkg/accessory/dao/dao.go @@ -95,11 +95,11 @@ func (d *dao) Create(ctx context.Context, acc *Accessory) (int64, error) { } id, err := ormer.Insert(acc) if err != nil { - if e := orm.AsConflictError(err, "accessory %s already exists under the artifact %d", - acc.Digest, acc.SubjectArtifactID); e != nil { + if e := orm.AsConflictError(err, "accessory %s already exists under the artifact %s", + acc.Digest, acc.SubjectArtifactDigest); e != nil { err = e - } else if e := orm.AsForeignKeyError(err, "the accessory %s tries to attach to a non existing artifact %d", - acc.Digest, acc.SubjectArtifactID); e != nil { + } else if e := orm.AsForeignKeyError(err, "the accessory %s tries to attach to a non existing artifact %s", + acc.Digest, acc.SubjectArtifactDigest); e != nil { err = e } } diff --git a/src/pkg/accessory/dao/dao_test.go b/src/pkg/accessory/dao/dao_test.go index 660224f60..2a77010ff 100644 --- a/src/pkg/accessory/dao/dao_test.go +++ b/src/pkg/accessory/dao/dao_test.go @@ -32,12 +32,13 @@ import ( type daoTestSuite struct { htesting.Suite - dao DAO - artDAO artdao.DAO - artifactID int64 - subArtifactID int64 - accID int64 - ctx context.Context + dao DAO + artDAO artdao.DAO + artifactID int64 + subArtifactID int64 + subArtifactDigest string + accID int64 + ctx context.Context } func (d *daoTestSuite) SetupSuite() { @@ -47,16 +48,18 @@ func (d *daoTestSuite) SetupSuite() { d.ClearTables = []string{"artifact", "artifact_accessory"} d.artDAO = artdao.New() - artifactID, err := d.artDAO.Create(d.ctx, &artdao.Artifact{ + art := &artdao.Artifact{ Type: "IMAGE", MediaType: "application/vnd.oci.image.config.v1+json", ManifestMediaType: "application/vnd.oci.image.manifest.v1+json", ProjectID: 1, RepositoryID: 1000, Digest: d.DigestString(), - }) - d.Require().Nil(err) + } + artifactID, err := d.artDAO.Create(d.ctx, art) d.subArtifactID = artifactID + d.Require().Nil(err) + d.subArtifactDigest = art.Digest d.artDAO = artdao.New() artifactID, err = d.artDAO.Create(d.ctx, &artdao.Artifact{ @@ -71,11 +74,11 @@ func (d *daoTestSuite) SetupSuite() { d.artifactID = artifactID accID, err := d.dao.Create(d.ctx, &Accessory{ - ArtifactID: d.artifactID, - SubjectArtifactID: d.subArtifactID, - Digest: d.DigestString(), - Size: 1234, - Type: "cosign.signature", + ArtifactID: d.artifactID, + SubjectArtifactDigest: d.subArtifactDigest, + Digest: d.DigestString(), + Size: 1234, + Type: "cosign.signature", }) d.Require().Nil(err) d.accID = accID @@ -103,7 +106,7 @@ func (d *daoTestSuite) TestCount() { d.True(total > 0) total, err = d.dao.Count(d.ctx, &q.Query{ Keywords: map[string]interface{}{ - "SubjectArtifactID": d.subArtifactID, + "SubjectArtifactDigest": d.subArtifactDigest, }, }) d.Require().Nil(err) @@ -125,7 +128,7 @@ func (d *daoTestSuite) TestList() { accs, err = d.dao.List(d.ctx, &q.Query{ Keywords: map[string]interface{}{ - "SubjectArtifactID": d.subArtifactID, + "SubjectArtifactDigest": d.subArtifactDigest, }, }) d.Require().Nil(err) @@ -149,27 +152,15 @@ func (d *daoTestSuite) TestCreate() { // conflict acc := &Accessory{ - ArtifactID: d.artifactID, - SubjectArtifactID: d.subArtifactID, - Digest: d.DigestString(), - Size: 1234, - Type: "cosign.signature", + ArtifactID: d.artifactID, + SubjectArtifactDigest: d.subArtifactDigest, + Digest: d.DigestString(), + Size: 1234, + Type: "cosign.signature", } _, err := d.dao.Create(d.ctx, acc) d.Require().NotNil(err) d.True(errors.IsErr(err, errors.ConflictCode)) - - // violating foreign key constraint: the artifact that the tag tries to attach doesn't exist - acc = &Accessory{ - ArtifactID: 999, - SubjectArtifactID: 998, - Digest: d.DigestString(), - Size: 1234, - Type: "cosign.signature", - } - _, err = d.dao.Create(d.ctx, acc) - d.Require().NotNil(err) - d.True(errors.IsErr(err, errors.ViolateForeignKeyConstraintCode)) } func (d *daoTestSuite) TestDelete() { @@ -184,14 +175,15 @@ func (d *daoTestSuite) TestDelete() { } func (d *daoTestSuite) TestDeleteOfArtifact() { - subArtID, err := d.artDAO.Create(d.ctx, &artdao.Artifact{ + art := &artdao.Artifact{ Type: "IMAGE", MediaType: "application/vnd.oci.image.config.v1+json", ManifestMediaType: "application/vnd.oci.image.manifest.v1+json", ProjectID: 1, RepositoryID: 1000, Digest: d.DigestString(), - }) + } + subArtID, err := d.artDAO.Create(d.ctx, art) d.Require().Nil(err) defer d.artDAO.Delete(d.ctx, subArtID) @@ -218,28 +210,28 @@ func (d *daoTestSuite) TestDeleteOfArtifact() { defer d.artDAO.Delete(d.ctx, artID2) acc1 := &Accessory{ - ArtifactID: artID1, - SubjectArtifactID: subArtID, - Digest: d.DigestString(), - Size: 1234, - Type: "cosign.signature", + ArtifactID: artID1, + SubjectArtifactDigest: art.Digest, + Digest: d.DigestString(), + Size: 1234, + Type: "cosign.signature", } _, err = d.dao.Create(d.ctx, acc1) d.Require().Nil(err) acc2 := &Accessory{ - ArtifactID: artID2, - SubjectArtifactID: subArtID, - Digest: d.DigestString(), - Size: 1234, - Type: "cosign.signature", + ArtifactID: artID2, + SubjectArtifactDigest: art.Digest, + Digest: d.DigestString(), + Size: 1234, + Type: "cosign.signature", } _, err = d.dao.Create(d.ctx, acc2) d.Require().Nil(err) accs, err := d.dao.List(d.ctx, &q.Query{ Keywords: map[string]interface{}{ - "SubjectArtifactID": subArtID, + "SubjectArtifactDigest": art.Digest, }, }) for _, acc := range accs { @@ -250,14 +242,14 @@ func (d *daoTestSuite) TestDeleteOfArtifact() { _, err = d.dao.DeleteAccessories(d.ctx, &q.Query{ Keywords: map[string]interface{}{ - "SubjectArtifactID": subArtID, + "SubjectArtifactDigest": art.Digest, }, }) d.Require().Nil(err) accs, err = d.dao.List(d.ctx, &q.Query{ Keywords: map[string]interface{}{ - "SubjectArtifactID": subArtID, + "SubjectArtifactDigest": art.Digest, }, }) d.Require().Nil(err) diff --git a/src/pkg/accessory/dao/model.go b/src/pkg/accessory/dao/model.go index 1c0f64e4a..0d084064e 100644 --- a/src/pkg/accessory/dao/model.go +++ b/src/pkg/accessory/dao/model.go @@ -26,13 +26,13 @@ func init() { // Accessory model in database type Accessory struct { - ID int64 `orm:"pk;auto;column(id)" json:"id"` - ArtifactID int64 `orm:"column(artifact_id)" json:"artifact_id"` - SubjectArtifactID int64 `orm:"column(subject_artifact_id)" json:"subject_artifact_id"` - Type string `orm:"column(type)" json:"type"` - Size int64 `orm:"column(size)" json:"size"` - Digest string `orm:"column(digest)" json:"digest"` - CreationTime time.Time `orm:"column(creation_time);auto_now_add" json:"creation_time"` + ID int64 `orm:"pk;auto;column(id)" json:"id"` + ArtifactID int64 `orm:"column(artifact_id)" json:"artifact_id"` + SubjectArtifactDigest string `orm:"column(subject_artifact_digest)" json:"subject_artifact_digest"` + Type string `orm:"column(type)" json:"type"` + Size int64 `orm:"column(size)" json:"size"` + Digest string `orm:"column(digest)" json:"digest"` + CreationTime time.Time `orm:"column(creation_time);auto_now_add" json:"creation_time"` } // TableName for artifact reference diff --git a/src/pkg/accessory/manager.go b/src/pkg/accessory/manager.go index c0f22a916..d14ec3ae2 100644 --- a/src/pkg/accessory/manager.go +++ b/src/pkg/accessory/manager.go @@ -38,7 +38,7 @@ var ( // Manager is the only interface of artifact module to provide the management functions for artifacts type Manager interface { // Ensure ... - Ensure(ctx context.Context, subArtID, artifactID, size int64, digest, accType string) error + Ensure(ctx context.Context, subArtDigest string, artifactID, size int64, digest, accType string) error // Get the artifact specified by the ID Get(ctx context.Context, id int64) (accessory model.Accessory, err error) // Count returns the total count of accessory according to the query. @@ -66,7 +66,7 @@ type manager struct { dao dao.DAO } -func (m *manager) Ensure(ctx context.Context, subArtID, artifactID, size int64, digest, accType string) error { +func (m *manager) Ensure(ctx context.Context, subArtDigest string, artifactID, size int64, digest, accType string) error { accs, err := m.dao.List(ctx, q.New(q.KeyWords{"ArtifactID": artifactID, "Digest": digest})) if err != nil { return err @@ -76,11 +76,11 @@ func (m *manager) Ensure(ctx context.Context, subArtID, artifactID, size int64, } acc := model.AccessoryData{ - ArtifactID: artifactID, - SubArtifactID: subArtID, - Digest: digest, - Size: size, - Type: accType, + ArtifactID: artifactID, + SubArtifactDigest: subArtDigest, + Digest: digest, + Size: size, + Type: accType, } _, err = m.Create(ctx, acc) return err @@ -92,13 +92,13 @@ func (m *manager) Get(ctx context.Context, id int64) (model.Accessory, error) { return nil, err } return model.New(acc.Type, model.AccessoryData{ - ID: acc.ID, - ArtifactID: acc.ArtifactID, - SubArtifactID: acc.SubjectArtifactID, - Size: acc.Size, - Digest: acc.Digest, - CreatTime: acc.CreationTime, - Icon: m.GetIcon(acc.Type), + ID: acc.ID, + ArtifactID: acc.ArtifactID, + SubArtifactDigest: acc.SubjectArtifactDigest, + Size: acc.Size, + Digest: acc.Digest, + CreatTime: acc.CreationTime, + Icon: m.GetIcon(acc.Type), }) } @@ -114,13 +114,13 @@ func (m *manager) List(ctx context.Context, query *q.Query) ([]model.Accessory, var accs []model.Accessory for _, accD := range accsDao { acc, err := model.New(accD.Type, model.AccessoryData{ - ID: accD.ID, - ArtifactID: accD.ArtifactID, - SubArtifactID: accD.SubjectArtifactID, - Size: accD.Size, - Digest: accD.Digest, - CreatTime: accD.CreationTime, - Icon: m.GetIcon(accD.Type), + ID: accD.ID, + ArtifactID: accD.ArtifactID, + SubArtifactDigest: accD.SubjectArtifactDigest, + 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) @@ -132,11 +132,11 @@ func (m *manager) List(ctx context.Context, query *q.Query) ([]model.Accessory, func (m *manager) Create(ctx context.Context, accessory model.AccessoryData) (int64, error) { acc := &dao.Accessory{ - ArtifactID: accessory.ArtifactID, - SubjectArtifactID: accessory.SubArtifactID, - Size: accessory.Size, - Digest: accessory.Digest, - Type: accessory.Type, + ArtifactID: accessory.ArtifactID, + SubjectArtifactDigest: accessory.SubArtifactDigest, + Size: accessory.Size, + Digest: accessory.Digest, + Type: accessory.Type, } return m.dao.Create(ctx, acc) } diff --git a/src/pkg/accessory/manager_test.go b/src/pkg/accessory/manager_test.go index fdc4d77d9..5136acdf1 100644 --- a/src/pkg/accessory/manager_test.go +++ b/src/pkg/accessory/manager_test.go @@ -45,7 +45,7 @@ func (m *managerTestSuite) SetupTest() { func (m *managerTestSuite) TestEnsure() { mock.OnAnything(m.dao, "List").Return([]*dao.Accessory{}, nil) mock.OnAnything(m.dao, "Create").Return(int64(1), nil) - err := m.mgr.Ensure(nil, int64(1), int64(1), int64(1), "sha256:1234", model.TypeCosignSignature) + err := m.mgr.Ensure(nil, string(""), int64(1), int64(1), "sha256:1234", model.TypeCosignSignature) m.Require().Nil(err) } diff --git a/src/pkg/accessory/model/accessory.go b/src/pkg/accessory/model/accessory.go index 83a0df9be..fed23973d 100644 --- a/src/pkg/accessory/model/accessory.go +++ b/src/pkg/accessory/model/accessory.go @@ -73,14 +73,14 @@ const ( // AccessoryData ... type AccessoryData struct { - ID int64 `json:"id"` - ArtifactID int64 `json:"artifact_id"` - SubArtifactID int64 `json:"subject_artifact_id"` - Type string `json:"type"` - Size int64 `json:"size"` - Digest string `json:"digest"` - CreatTime time.Time `json:"creation_time"` - Icon string `json:"icon"` + ID int64 `json:"id"` + ArtifactID int64 `json:"artifact_id"` + SubArtifactDigest string `json:"subject_artifact_id"` + Type string `json:"type"` + 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 diff --git a/src/pkg/accessory/model/base/base_test.go b/src/pkg/accessory/model/base/base_test.go index 9c3eb9a37..ec66a7872 100644 --- a/src/pkg/accessory/model/base/base_test.go +++ b/src/pkg/accessory/model/base/base_test.go @@ -21,10 +21,10 @@ func (suite *BaseTestSuite) SetupSuite() { suite.subDigest = suite.DigestString() suite.accessory, _ = model.New(model.TypeNone, model.AccessoryData{ - ArtifactID: 1, - SubArtifactID: 2, - Size: 1234, - Digest: suite.digest, + ArtifactID: 1, + SubArtifactDigest: suite.subDigest, + Size: 1234, + Digest: suite.digest, }) } @@ -37,7 +37,7 @@ func (suite *BaseTestSuite) TestGetArtID() { } func (suite *BaseTestSuite) TestSubGetArtID() { - suite.Equal(int64(2), suite.accessory.GetData().SubArtifactID) + suite.Equal(suite.subDigest, suite.accessory.GetData().SubArtifactDigest) } func (suite *BaseTestSuite) TestSubGetSize() { diff --git a/src/pkg/accessory/model/cosign/cosign_test.go b/src/pkg/accessory/model/cosign/cosign_test.go index 5d898ac2e..46d6b2b60 100644 --- a/src/pkg/accessory/model/cosign/cosign_test.go +++ b/src/pkg/accessory/model/cosign/cosign_test.go @@ -13,16 +13,18 @@ type CosignTestSuite struct { htesting.Suite accessory model.Accessory digest string + subDigest string } func (suite *CosignTestSuite) SetupSuite() { suite.digest = suite.DigestString() + suite.subDigest = suite.DigestString() suite.accessory, _ = model.New(model.TypeCosignSignature, model.AccessoryData{ - ArtifactID: 1, - SubArtifactID: 2, - Size: 4321, - Digest: suite.digest, + ArtifactID: 1, + SubArtifactDigest: suite.subDigest, + Size: 4321, + Digest: suite.digest, }) } @@ -35,7 +37,7 @@ func (suite *CosignTestSuite) TestGetArtID() { } func (suite *CosignTestSuite) TestSubGetArtID() { - suite.Equal(int64(2), suite.accessory.GetData().SubArtifactID) + suite.Equal(suite.subDigest, suite.accessory.GetData().SubArtifactDigest) } func (suite *CosignTestSuite) TestSubGetSize() { diff --git a/src/pkg/accessory/model/nydus/nydus_test.go b/src/pkg/accessory/model/nydus/nydus_test.go index b067d9398..90ae6c041 100644 --- a/src/pkg/accessory/model/nydus/nydus_test.go +++ b/src/pkg/accessory/model/nydus/nydus_test.go @@ -13,16 +13,18 @@ type NydusTestSuite struct { htesting.Suite accessory model.Accessory digest string + subDigest string } func (suite *NydusTestSuite) SetupSuite() { suite.digest = suite.DigestString() + suite.subDigest = suite.DigestString() suite.accessory, _ = model.New(model.TypeNydusAccelerator, model.AccessoryData{ - ArtifactID: 1, - SubArtifactID: 2, - Size: 4321, - Digest: suite.digest, + ArtifactID: 1, + SubArtifactDigest: suite.subDigest, + Size: 4321, + Digest: suite.digest, }) } @@ -35,7 +37,7 @@ func (suite *NydusTestSuite) TestGetArtID() { } func (suite *NydusTestSuite) TestSubGetArtID() { - suite.Equal(int64(2), suite.accessory.GetData().SubArtifactID) + suite.Equal(suite.subDigest, suite.accessory.GetData().SubArtifactDigest) } func (suite *NydusTestSuite) TestSubGetSize() { diff --git a/src/server/middleware/contenttrust/cosign_test.go b/src/server/middleware/contenttrust/cosign_test.go index 450b683ad..0935b9503 100644 --- a/src/server/middleware/contenttrust/cosign_test.go +++ b/src/server/middleware/contenttrust/cosign_test.go @@ -227,10 +227,10 @@ func (suite *CosignMiddlewareTestSuite) TestSignaturePulling() { mock.OnAnything(suite.projectController, "GetByName").Return(suite.project, nil) acc := &basemodel.Default{ Data: accessorymodel.AccessoryData{ - ID: 1, - ArtifactID: 2, - SubArtifactID: 1, - Type: accessorymodel.TypeCosignSignature, + ID: 1, + ArtifactID: 2, + SubArtifactDigest: suite.artifact.Digest, + Type: accessorymodel.TypeCosignSignature, }, } mock.OnAnything(suite.accessMgr, "List").Return([]accessorymodel.Accessory{ diff --git a/src/server/middleware/contenttrust/notary_test.go b/src/server/middleware/contenttrust/notary_test.go index 2b4f4906c..f0fda0b3d 100644 --- a/src/server/middleware/contenttrust/notary_test.go +++ b/src/server/middleware/contenttrust/notary_test.go @@ -210,10 +210,10 @@ func (suite *MiddlewareTestSuite) TestSignaturePulling() { mock.OnAnything(suite.projectController, "GetByName").Return(suite.project, nil) acc := &basemodel.Default{ Data: accessorymodel.AccessoryData{ - ID: 1, - ArtifactID: 2, - SubArtifactID: 1, - Type: accessorymodel.TypeCosignSignature, + ID: 1, + ArtifactID: 2, + SubArtifactDigest: suite.artifact.Digest, + Type: accessorymodel.TypeCosignSignature, }, } mock.OnAnything(suite.accessMgr, "List").Return([]accessorymodel.Accessory{ diff --git a/src/server/middleware/cosign/cosign.go b/src/server/middleware/cosign/cosign.go index 61c85de4f..e0b5d69cf 100644 --- a/src/server/middleware/cosign/cosign.go +++ b/src/server/middleware/cosign/cosign.go @@ -110,11 +110,11 @@ func SignatureMiddleware() func(http.Handler) http.Handler { if err := orm.WithTransaction(func(ctx context.Context) error { _, err := accessory.Mgr.Create(ctx, model.AccessoryData{ - ArtifactID: art.ID, - SubArtifactID: subjectArt.ID, - Size: desc.Size, - Digest: desc.Digest.String(), - Type: model.TypeCosignSignature, + ArtifactID: art.ID, + SubArtifactDigest: subjectArt.Digest, + Size: desc.Size, + Digest: desc.Digest.String(), + Type: model.TypeCosignSignature, }) return err })(orm.SetTransactionOpNameToContext(ctx, "tx-create-cosign-accessory")); err != nil { diff --git a/src/server/middleware/cosign/cosign_test.go b/src/server/middleware/cosign/cosign_test.go index 34fb80e3d..b6019a417 100644 --- a/src/server/middleware/cosign/cosign_test.go +++ b/src/server/middleware/cosign/cosign_test.go @@ -15,8 +15,8 @@ import ( "github.com/goharbor/harbor/src/lib/q" "github.com/goharbor/harbor/src/pkg" "github.com/goharbor/harbor/src/pkg/accessory" - accessorymodel "github.com/goharbor/harbor/src/pkg/accessory/model" "github.com/goharbor/harbor/src/pkg/accessory/model" + accessorymodel "github.com/goharbor/harbor/src/pkg/accessory/model" _ "github.com/goharbor/harbor/src/pkg/accessory/model/base" _ "github.com/goharbor/harbor/src/pkg/accessory/model/cosign" "github.com/goharbor/harbor/src/pkg/artifact" @@ -98,7 +98,7 @@ func (suite *MiddlewareTestSuite) addArtAcc(pid, repositoryID int64, repositoryN PushTime: time.Now(), PullTime: time.Now(), } - subafid, err := pkg.ArtifactMgr.Create(suite.Context(), subaf) + _, err := pkg.ArtifactMgr.Create(suite.Context(), subaf) suite.Nil(err, fmt.Sprintf("Add artifact failed for %d", repositoryID)) af := &artifact.Artifact{ @@ -115,11 +115,11 @@ func (suite *MiddlewareTestSuite) addArtAcc(pid, repositoryID int64, repositoryN suite.Nil(err, fmt.Sprintf("Add artifact failed for %d", repositoryID)) accid, err := accessory.Mgr.Create(suite.Context(), accessorymodel.AccessoryData{ - ID: 1, - ArtifactID: afid, - SubArtifactID: subafid, - Digest: accdgt, - Type: accessorymodel.TypeCosignSignature, + ID: 1, + ArtifactID: afid, + SubArtifactDigest: subaf.Digest, + Digest: accdgt, + Type: accessorymodel.TypeCosignSignature, }) suite.Nil(err, fmt.Sprintf("Add artifact accesspry failed for %d", repositoryID)) return accid @@ -134,7 +134,7 @@ func (suite *MiddlewareTestSuite) TestCosignSignature() { _, repoId, err := repository.Ctl.Ensure(suite.Context(), name) suite.Nil(err) - subjectArtID := suite.addArt(projectID, repoId, name, subArtDigest) + suite.addArt(projectID, repoId, name, subArtDigest) artID := suite.addArt(projectID, repoId, name, descriptor.Digest.String()) suite.Nil(err) @@ -145,11 +145,11 @@ func (suite *MiddlewareTestSuite) TestCosignSignature() { accs, err := accessory.Mgr.List(suite.Context(), &q.Query{ Keywords: map[string]interface{}{ - "SubjectArtifactID": subjectArtID, + "SubjectArtifactDigest": subArtDigest, }, }) suite.Equal(1, len(accs)) - suite.Equal(subjectArtID, accs[0].GetData().SubArtifactID) + suite.Equal(subArtDigest, accs[0].GetData().SubArtifactDigest) suite.Equal(artID, accs[0].GetData().ArtifactID) suite.True(accs[0].IsHard()) suite.Equal(model.TypeCosignSignature, accs[0].GetData().Type) diff --git a/src/server/middleware/nydus/nydus.go b/src/server/middleware/nydus/nydus.go index 0d95c1dbe..a2a62bf49 100644 --- a/src/server/middleware/nydus/nydus.go +++ b/src/server/middleware/nydus/nydus.go @@ -129,11 +129,11 @@ func AcceleratorMiddleware() func(http.Handler) http.Handler { if err := orm.WithTransaction(func(ctx context.Context) error { id, err := accessory.Mgr.Create(ctx, model.AccessoryData{ - ArtifactID: art.ID, - SubArtifactID: subjectArt.ID, - Size: desc.Size, - Digest: desc.Digest.String(), - Type: model.TypeNydusAccelerator, + ArtifactID: art.ID, + SubArtifactDigest: subjectArt.Digest, + Size: desc.Size, + Digest: desc.Digest.String(), + Type: model.TypeNydusAccelerator, }) log.Debug("accessory id:", id) return err diff --git a/src/server/middleware/nydus/nydus_test.go b/src/server/middleware/nydus/nydus_test.go index f67c83770..e3dde17b9 100644 --- a/src/server/middleware/nydus/nydus_test.go +++ b/src/server/middleware/nydus/nydus_test.go @@ -15,8 +15,8 @@ import ( "github.com/goharbor/harbor/src/lib/q" "github.com/goharbor/harbor/src/pkg" "github.com/goharbor/harbor/src/pkg/accessory" - accessorymodel "github.com/goharbor/harbor/src/pkg/accessory/model" "github.com/goharbor/harbor/src/pkg/accessory/model" + accessorymodel "github.com/goharbor/harbor/src/pkg/accessory/model" _ "github.com/goharbor/harbor/src/pkg/accessory/model/base" _ "github.com/goharbor/harbor/src/pkg/accessory/model/nydus" "github.com/goharbor/harbor/src/pkg/artifact" @@ -114,7 +114,7 @@ func (suite *MiddlewareTestSuite) addArtAcc(pid, repositoryID int64, repositoryN PushTime: time.Now(), PullTime: time.Now(), } - subafid, err := pkg.ArtifactMgr.Create(suite.Context(), subaf) + _, err := pkg.ArtifactMgr.Create(suite.Context(), subaf) suite.Nil(err, fmt.Sprintf("Add artifact failed for %d", repositoryID)) af := &artifact.Artifact{ @@ -131,11 +131,11 @@ func (suite *MiddlewareTestSuite) addArtAcc(pid, repositoryID int64, repositoryN suite.Nil(err, fmt.Sprintf("Add artifact failed for %d", repositoryID)) accid, err := accessory.Mgr.Create(suite.Context(), accessorymodel.AccessoryData{ - ID: 1, - ArtifactID: afid, - SubArtifactID: subafid, - Digest: accdgt, - Type: accessorymodel.TypeNydusAccelerator, + ID: 1, + ArtifactID: afid, + SubArtifactDigest: subaf.Digest, + Digest: accdgt, + Type: accessorymodel.TypeNydusAccelerator, }) suite.Nil(err, fmt.Sprintf("Add artifact accesspry failed for %d", repositoryID)) return accid @@ -152,7 +152,7 @@ func (suite *MiddlewareTestSuite) TestNydusAccelerator() { suite.Nil(err) // add subject artifact - subjectArtID := suite.addArt(projectID, repoId, name, subArtDigest) + suite.addArt(projectID, repoId, name, subArtDigest) // add nydus artifact artID := suite.addArt(projectID, repoId, name, descriptor.Digest.String()) @@ -165,11 +165,11 @@ func (suite *MiddlewareTestSuite) TestNydusAccelerator() { accs, _ := accessory.Mgr.List(suite.Context(), &q.Query{ Keywords: map[string]interface{}{ - "SubjectArtifactID": subjectArtID, + "SubjectArtifactDigest": subArtDigest, }, }) suite.Equal(1, len(accs)) - suite.Equal(subjectArtID, accs[0].GetData().SubArtifactID) + suite.Equal(subArtDigest, accs[0].GetData().SubArtifactDigest) suite.Equal(artID, accs[0].GetData().ArtifactID) suite.True(accs[0].IsHard()) suite.Equal(model.TypeNydusAccelerator, accs[0].GetData().Type) diff --git a/src/server/middleware/vulnerable/vulnerable_test.go b/src/server/middleware/vulnerable/vulnerable_test.go index b3a16a8b1..b216b9d3a 100644 --- a/src/server/middleware/vulnerable/vulnerable_test.go +++ b/src/server/middleware/vulnerable/vulnerable_test.go @@ -383,10 +383,10 @@ func (suite *MiddlewareTestSuite) TestSignaturePulling() { mock.OnAnything(suite.projectController, "Get").Return(suite.project, nil) acc := &basemodel.Default{ Data: accessorymodel.AccessoryData{ - ID: 1, - ArtifactID: 2, - SubArtifactID: 1, - Type: accessorymodel.TypeCosignSignature, + ID: 1, + ArtifactID: 2, + SubArtifactDigest: suite.artifact.Digest, + Type: accessorymodel.TypeCosignSignature, }, } mock.OnAnything(suite.accessMgr, "List").Return([]accessorymodel.Accessory{ diff --git a/src/server/v2.0/handler/artifact.go b/src/server/v2.0/handler/artifact.go index c65cda595..c9391064f 100644 --- a/src/server/v2.0/handler/artifact.go +++ b/src/server/v2.0/handler/artifact.go @@ -372,7 +372,7 @@ func (a *artifactAPI) ListAccessories(ctx context.Context, params operation.List if err != nil { return a.SendError(ctx, err) } - query.Keywords["SubjectArtifactID"] = artifact.ID + query.Keywords["SubjectArtifactDigest"] = artifact.Digest // list accessories according to the query total, err := a.accMgr.Count(ctx, query) diff --git a/src/server/v2.0/handler/model/accessory.go b/src/server/v2.0/handler/model/accessory.go index c83dd5cf9..3687ea9ef 100644 --- a/src/server/v2.0/handler/model/accessory.go +++ b/src/server/v2.0/handler/model/accessory.go @@ -15,14 +15,14 @@ type Accessory struct { // ToSwagger converts the label to the swagger model func (a *Accessory) ToSwagger() *models.Accessory { return &models.Accessory{ - ID: a.ID, - ArtifactID: a.ArtifactID, - SubjectArtifactID: a.SubArtifactID, - Size: a.Size, - Digest: a.Digest, - Type: a.Type, - Icon: a.Icon, - CreationTime: strfmt.DateTime(a.CreatTime), + ID: a.ID, + ArtifactID: a.ArtifactID, + SubjectArtifactDigest: a.SubArtifactDigest, + Size: a.Size, + Digest: a.Digest, + Type: a.Type, + Icon: a.Icon, + CreationTime: strfmt.DateTime(a.CreatTime), } } diff --git a/src/testing/pkg/accessory/manager.go b/src/testing/pkg/accessory/manager.go index a7a4b05a2..eb25fe22c 100644 --- a/src/testing/pkg/accessory/manager.go +++ b/src/testing/pkg/accessory/manager.go @@ -86,13 +86,13 @@ func (_m *Manager) DeleteAccessories(ctx context.Context, _a1 *q.Query) error { return r0 } -// Ensure provides a mock function with given fields: ctx, subArtID, artifactID, size, digest, accType -func (_m *Manager) Ensure(ctx context.Context, subArtID int64, artifactID int64, size int64, digest string, accType string) error { - ret := _m.Called(ctx, subArtID, artifactID, size, digest, accType) +// Ensure provides a mock function with given fields: ctx, subArtDigest, artifactID, size, digest, accType +func (_m *Manager) Ensure(ctx context.Context, subArtDigest string, artifactID int64, size int64, digest string, accType string) error { + ret := _m.Called(ctx, subArtDigest, artifactID, size, digest, accType) var r0 error - if rf, ok := ret.Get(0).(func(context.Context, int64, int64, int64, string, string) error); ok { - r0 = rf(ctx, subArtID, artifactID, size, digest, accType) + if rf, ok := ret.Get(0).(func(context.Context, string, int64, int64, string, string) error); ok { + r0 = rf(ctx, subArtDigest, artifactID, size, digest, accType) } else { r0 = ret.Error(0) }