2023-07-18 12:27:26 -05:00
|
|
|
package dynamodb
|
2023-01-09 15:37:44 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-03-14 05:02:19 -05:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-01-09 15:37:44 -05:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go-v2/aws"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
|
|
|
|
godigest "github.com/opencontainers/go-digest"
|
|
|
|
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
2023-10-30 15:06:04 -05:00
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2024-01-31 23:34:07 -05:00
|
|
|
zerr "zotregistry.dev/zot/errors"
|
|
|
|
"zotregistry.dev/zot/pkg/api/constants"
|
|
|
|
zcommon "zotregistry.dev/zot/pkg/common"
|
|
|
|
"zotregistry.dev/zot/pkg/log"
|
|
|
|
"zotregistry.dev/zot/pkg/meta/common"
|
|
|
|
mConvert "zotregistry.dev/zot/pkg/meta/convert"
|
|
|
|
proto_go "zotregistry.dev/zot/pkg/meta/proto/gen"
|
|
|
|
mTypes "zotregistry.dev/zot/pkg/meta/types"
|
|
|
|
"zotregistry.dev/zot/pkg/meta/version"
|
|
|
|
reqCtx "zotregistry.dev/zot/pkg/requestcontext"
|
2023-01-09 15:37:44 -05:00
|
|
|
)
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
type DynamoDB struct {
|
2023-10-30 15:06:04 -05:00
|
|
|
Client *dynamodb.Client
|
|
|
|
APIKeyTablename string
|
|
|
|
RepoMetaTablename string
|
|
|
|
RepoBlobsTablename string
|
|
|
|
ImageMetaTablename string
|
|
|
|
UserDataTablename string
|
|
|
|
VersionTablename string
|
|
|
|
Patches []func(client *dynamodb.Client, tableNames map[string]string) error
|
|
|
|
imgTrustStore mTypes.ImageTrustStore
|
|
|
|
Log log.Logger
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-11-08 16:37:52 -05:00
|
|
|
func New(client *dynamodb.Client, params DBDriverParameters, log log.Logger,
|
2023-09-08 02:03:58 -05:00
|
|
|
) (*DynamoDB, error) {
|
2023-07-18 12:27:26 -05:00
|
|
|
dynamoWrapper := DynamoDB{
|
2023-10-30 15:06:04 -05:00
|
|
|
Client: client,
|
|
|
|
VersionTablename: params.VersionTablename,
|
|
|
|
UserDataTablename: params.UserDataTablename,
|
|
|
|
APIKeyTablename: params.APIKeyTablename,
|
|
|
|
RepoMetaTablename: params.RepoMetaTablename,
|
|
|
|
ImageMetaTablename: params.ImageMetaTablename,
|
|
|
|
RepoBlobsTablename: params.RepoBlobsInfoTablename,
|
|
|
|
Patches: version.GetDynamoDBPatches(),
|
|
|
|
imgTrustStore: nil,
|
|
|
|
Log: log,
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-03-28 12:20:09 -05:00
|
|
|
err := dynamoWrapper.createVersionTable()
|
2023-01-09 15:37:44 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
err = dynamoWrapper.createTable(dynamoWrapper.RepoMetaTablename)
|
2023-01-09 15:37:44 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
err = dynamoWrapper.createTable(dynamoWrapper.RepoBlobsTablename)
|
2023-01-09 15:37:44 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
err = dynamoWrapper.createTable(dynamoWrapper.ImageMetaTablename)
|
2023-02-27 14:23:18 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
err = dynamoWrapper.createTable(dynamoWrapper.UserDataTablename)
|
2023-04-24 13:13:15 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
err = dynamoWrapper.createTable(dynamoWrapper.APIKeyTablename)
|
2023-07-07 11:27:10 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
// Using the Config value, create the DynamoDB client
|
|
|
|
return &dynamoWrapper, nil
|
|
|
|
}
|
|
|
|
|
2023-11-16 13:39:27 -05:00
|
|
|
func (dwr *DynamoDB) GetAllRepoNames() ([]string, error) {
|
|
|
|
ctx := context.Background()
|
|
|
|
attributeIterator := NewBaseDynamoAttributesIterator(dwr.Client, dwr.RepoMetaTablename, "TableKey", 0, dwr.Log)
|
|
|
|
|
|
|
|
repoNames := []string{}
|
|
|
|
|
|
|
|
repoNameAttribute, err := attributeIterator.First(ctx)
|
|
|
|
|
|
|
|
for ; repoNameAttribute != nil; repoNameAttribute, err = attributeIterator.Next(ctx) {
|
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var repoName string
|
|
|
|
|
|
|
|
err := attributevalue.Unmarshal(repoNameAttribute, &repoName)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
repoNames = append(repoNames, repoName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return repoNames, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dwr *DynamoDB) GetRepoLastUpdated(repo string) time.Time {
|
|
|
|
resp, err := dwr.Client.GetItem(context.Background(), &dynamodb.GetItemInput{
|
|
|
|
TableName: aws.String(dwr.RepoBlobsTablename),
|
|
|
|
Key: map[string]types.AttributeValue{
|
|
|
|
"TableKey": &types.AttributeValueMemberS{Value: repo},
|
|
|
|
},
|
|
|
|
ProjectionExpression: aws.String("RepoLastUpdated"),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}
|
|
|
|
}
|
|
|
|
|
|
|
|
protoRepoLastUpdated := ×tamppb.Timestamp{}
|
|
|
|
repoLastUpdatedBlob := []byte{}
|
|
|
|
|
|
|
|
if resp.Item != nil {
|
|
|
|
err = attributevalue.Unmarshal(resp.Item["RepoLastUpdated"], &repoLastUpdatedBlob)
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(repoLastUpdatedBlob) > 0 {
|
|
|
|
err := proto.Unmarshal(repoLastUpdatedBlob, protoRepoLastUpdated)
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lastUpdated := *mConvert.GetTime(protoRepoLastUpdated)
|
|
|
|
|
|
|
|
return lastUpdated
|
|
|
|
}
|
|
|
|
|
2023-09-08 02:03:58 -05:00
|
|
|
func (dwr *DynamoDB) ImageTrustStore() mTypes.ImageTrustStore {
|
|
|
|
return dwr.imgTrustStore
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dwr *DynamoDB) SetImageTrustStore(imgTrustStore mTypes.ImageTrustStore) {
|
|
|
|
dwr.imgTrustStore = imgTrustStore
|
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func (dwr *DynamoDB) SetProtoImageMeta(digest godigest.Digest, protoImageMeta *proto_go.ImageMeta) error {
|
|
|
|
bytes, err := proto.Marshal(protoImageMeta)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mdAttributeValue, err := attributevalue.Marshal(bytes)
|
2023-01-09 15:37:44 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-08 16:37:52 -05:00
|
|
|
_, err = dwr.Client.UpdateItem(context.Background(), &dynamodb.UpdateItemInput{
|
2023-01-09 15:37:44 -05:00
|
|
|
ExpressionAttributeNames: map[string]string{
|
2023-11-08 16:37:52 -05:00
|
|
|
"#IM": "ImageMeta",
|
2023-01-09 15:37:44 -05:00
|
|
|
},
|
|
|
|
ExpressionAttributeValues: map[string]types.AttributeValue{
|
2023-10-30 15:06:04 -05:00
|
|
|
":ImageMeta": mdAttributeValue,
|
2023-01-09 15:37:44 -05:00
|
|
|
},
|
|
|
|
Key: map[string]types.AttributeValue{
|
2023-11-16 13:39:27 -05:00
|
|
|
"TableKey": &types.AttributeValueMemberS{
|
2023-11-08 16:37:52 -05:00
|
|
|
Value: digest.String(),
|
2023-01-09 15:37:44 -05:00
|
|
|
},
|
|
|
|
},
|
2023-10-30 15:06:04 -05:00
|
|
|
TableName: aws.String(dwr.ImageMetaTablename),
|
2023-11-08 16:37:52 -05:00
|
|
|
UpdateExpression: aws.String("SET #IM = :ImageMeta"),
|
2023-01-09 15:37:44 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func (dwr *DynamoDB) SetImageMeta(digest godigest.Digest, imageMeta mTypes.ImageMeta) error {
|
2023-11-08 16:37:52 -05:00
|
|
|
return dwr.SetProtoImageMeta(digest, mConvert.GetProtoImageMeta(imageMeta))
|
2023-10-30 15:06:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (dwr *DynamoDB) GetProtoImageMeta(ctx context.Context, digest godigest.Digest) (*proto_go.ImageMeta, error) {
|
|
|
|
resp, err := dwr.Client.GetItem(ctx, &dynamodb.GetItemInput{
|
|
|
|
TableName: aws.String(dwr.ImageMetaTablename),
|
2023-01-09 15:37:44 -05:00
|
|
|
Key: map[string]types.AttributeValue{
|
2023-11-16 13:39:27 -05:00
|
|
|
"TableKey": &types.AttributeValueMemberS{Value: digest.String()},
|
2023-01-09 15:37:44 -05:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-10-30 15:06:04 -05:00
|
|
|
return nil, err
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
blob := []byte{}
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
if resp.Item == nil {
|
|
|
|
return nil, zerr.ErrImageMetaNotFound
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
err = attributevalue.Unmarshal(resp.Item["ImageMeta"], &blob)
|
2023-01-09 15:37:44 -05:00
|
|
|
if err != nil {
|
2023-10-30 15:06:04 -05:00
|
|
|
return nil, err
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
imageMeta := &proto_go.ImageMeta{}
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
err = proto.Unmarshal(blob, imageMeta)
|
2023-01-09 15:37:44 -05:00
|
|
|
if err != nil {
|
2023-10-30 15:06:04 -05:00
|
|
|
return nil, err
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
return imageMeta, nil
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2024-10-21 11:57:43 -05:00
|
|
|
func (dwr *DynamoDB) getAllContainedMeta(ctx context.Context, imageIndexData *proto_go.ImageMeta,
|
|
|
|
) ([]*proto_go.ImageMeta, []*proto_go.ManifestMeta, error) {
|
|
|
|
manifestDataList := make([]*proto_go.ManifestMeta, 0, len(imageIndexData.Index.Index.Manifests))
|
|
|
|
imageMetaList := make([]*proto_go.ImageMeta, 0, len(imageIndexData.Index.Index.Manifests))
|
|
|
|
|
|
|
|
manifestDigests := make([]string, 0, len(imageIndexData.Index.Index.Manifests))
|
|
|
|
for i := range imageIndexData.Index.Index.Manifests {
|
|
|
|
manifestDigests = append(manifestDigests, imageIndexData.Index.Index.Manifests[i].Digest)
|
|
|
|
}
|
|
|
|
|
|
|
|
manifestsAttributes, err := dwr.fetchImageMetaAttributesByDigest(ctx, manifestDigests)
|
|
|
|
if err != nil {
|
|
|
|
return imageMetaList, manifestDataList, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, manifestAttribute := range manifestsAttributes {
|
|
|
|
imageManifestData, err := getProtoImageMetaFromAttribute(manifestAttribute["ImageMeta"])
|
|
|
|
if err != nil {
|
|
|
|
return imageMetaList, manifestDataList, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch imageManifestData.MediaType {
|
|
|
|
case ispec.MediaTypeImageManifest:
|
|
|
|
imageMetaList = append(imageMetaList, imageManifestData)
|
|
|
|
manifestDataList = append(manifestDataList, imageManifestData.Manifests[0])
|
|
|
|
case ispec.MediaTypeImageIndex:
|
|
|
|
partialImageDataList, partialManifestDataList, err := dwr.getAllContainedMeta(ctx, imageManifestData)
|
|
|
|
if err != nil {
|
|
|
|
return imageMetaList, manifestDataList, err
|
|
|
|
}
|
|
|
|
|
|
|
|
imageMetaList = append(imageMetaList, partialImageDataList...)
|
|
|
|
manifestDataList = append(manifestDataList, partialManifestDataList...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return imageMetaList, manifestDataList, nil
|
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func (dwr *DynamoDB) setProtoRepoMeta(repo string, repoMeta *proto_go.RepoMeta) error {
|
|
|
|
repoMeta.Name = repo
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
repoMetaBlob, err := proto.Marshal(repoMeta)
|
2023-01-09 15:37:44 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
repoAttributeValue, err := attributevalue.Marshal(repoMetaBlob)
|
2023-02-27 14:23:18 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = dwr.Client.UpdateItem(context.TODO(), &dynamodb.UpdateItemInput{
|
|
|
|
ExpressionAttributeNames: map[string]string{
|
2023-11-08 16:37:52 -05:00
|
|
|
"#RM": "RepoMeta",
|
2023-02-27 14:23:18 -05:00
|
|
|
},
|
|
|
|
ExpressionAttributeValues: map[string]types.AttributeValue{
|
2023-11-08 16:37:52 -05:00
|
|
|
":RepoMeta": repoAttributeValue,
|
2023-02-27 14:23:18 -05:00
|
|
|
},
|
|
|
|
Key: map[string]types.AttributeValue{
|
2023-11-16 13:39:27 -05:00
|
|
|
"TableKey": &types.AttributeValueMemberS{
|
2023-10-30 15:06:04 -05:00
|
|
|
Value: repo,
|
2023-02-27 14:23:18 -05:00
|
|
|
},
|
|
|
|
},
|
2023-10-30 15:06:04 -05:00
|
|
|
TableName: aws.String(dwr.RepoMetaTablename),
|
2023-11-08 16:37:52 -05:00
|
|
|
UpdateExpression: aws.String("SET #RM = :RepoMeta"),
|
2023-02-27 14:23:18 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func (dwr *DynamoDB) getProtoRepoMeta(ctx context.Context, repo string) (*proto_go.RepoMeta, error) {
|
|
|
|
resp, err := dwr.Client.GetItem(ctx, &dynamodb.GetItemInput{
|
|
|
|
TableName: aws.String(dwr.RepoMetaTablename),
|
2023-02-27 14:23:18 -05:00
|
|
|
Key: map[string]types.AttributeValue{
|
2023-11-16 13:39:27 -05:00
|
|
|
"TableKey": &types.AttributeValueMemberS{Value: repo},
|
2023-02-27 14:23:18 -05:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-10-30 15:06:04 -05:00
|
|
|
return nil, err
|
2023-02-27 14:23:18 -05:00
|
|
|
}
|
|
|
|
|
2023-11-02 16:35:49 -05:00
|
|
|
protoRepoMeta := &proto_go.RepoMeta{
|
|
|
|
Name: repo,
|
2023-02-27 14:23:18 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
blob := []byte{}
|
2023-02-27 14:23:18 -05:00
|
|
|
|
2023-11-02 16:35:49 -05:00
|
|
|
if resp.Item != nil {
|
2023-11-08 16:37:52 -05:00
|
|
|
err = attributevalue.Unmarshal(resp.Item["RepoMeta"], &blob)
|
2023-11-02 16:35:49 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = proto.Unmarshal(blob, protoRepoMeta)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-27 14:23:18 -05:00
|
|
|
}
|
|
|
|
|
2023-11-02 16:35:49 -05:00
|
|
|
if protoRepoMeta.Tags == nil {
|
|
|
|
protoRepoMeta.Tags = map[string]*proto_go.TagDescriptor{"": {}}
|
|
|
|
}
|
2023-02-27 14:23:18 -05:00
|
|
|
|
2023-11-02 16:35:49 -05:00
|
|
|
if protoRepoMeta.Statistics == nil {
|
|
|
|
protoRepoMeta.Statistics = map[string]*proto_go.DescriptorStatistics{"": {}}
|
|
|
|
}
|
|
|
|
|
|
|
|
if protoRepoMeta.Signatures == nil {
|
|
|
|
protoRepoMeta.Signatures = map[string]*proto_go.ManifestSignatures{"": {}}
|
|
|
|
}
|
|
|
|
|
|
|
|
if protoRepoMeta.Referrers == nil {
|
|
|
|
protoRepoMeta.Referrers = map[string]*proto_go.ReferrersInfo{"": {}}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(blob) == 0 || resp.Item == nil {
|
|
|
|
return protoRepoMeta, zerr.ErrRepoMetaNotFound
|
2023-03-10 13:37:29 -05:00
|
|
|
}
|
|
|
|
|
2023-11-02 16:35:49 -05:00
|
|
|
return protoRepoMeta, nil
|
2023-03-10 13:37:29 -05:00
|
|
|
}
|
|
|
|
|
2023-11-01 11:16:18 -05:00
|
|
|
func (dwr *DynamoDB) SetRepoReference(ctx context.Context, repo string, reference string,
|
|
|
|
imageMeta mTypes.ImageMeta,
|
|
|
|
) error {
|
2023-10-30 15:06:04 -05:00
|
|
|
if err := common.ValidateRepoReferenceInput(repo, reference, imageMeta.Digest); err != nil {
|
|
|
|
return err
|
2023-03-10 13:37:29 -05:00
|
|
|
}
|
|
|
|
|
2023-11-01 11:16:18 -05:00
|
|
|
var userid string
|
|
|
|
|
|
|
|
userAc, err := reqCtx.UserAcFromContext(ctx)
|
|
|
|
if err == nil {
|
|
|
|
userid = userAc.GetUsername()
|
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
// 1. Add image data to db if needed
|
|
|
|
protoImageMeta := mConvert.GetProtoImageMeta(imageMeta)
|
2023-03-10 13:37:29 -05:00
|
|
|
|
2023-11-01 11:16:18 -05:00
|
|
|
err = dwr.SetProtoImageMeta(imageMeta.Digest, protoImageMeta) //nolint: contextcheck
|
2023-03-10 13:37:29 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-01 11:16:18 -05:00
|
|
|
repoMeta, err := dwr.getProtoRepoMeta(ctx, repo)
|
2023-11-02 16:35:49 -05:00
|
|
|
if err != nil && !errors.Is(err, zerr.ErrRepoMetaNotFound) {
|
|
|
|
return err
|
2023-03-10 13:37:29 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
// 2. Referrers
|
|
|
|
if subject := mConvert.GetImageSubject(protoImageMeta); subject != nil {
|
|
|
|
refInfo := &proto_go.ReferrersInfo{}
|
|
|
|
if repoMeta.Referrers[subject.Digest.String()] != nil {
|
|
|
|
refInfo = repoMeta.Referrers[subject.Digest.String()]
|
|
|
|
}
|
2023-03-10 13:37:29 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
foundReferrer := false
|
2023-03-10 13:37:29 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
for i := range refInfo.List {
|
|
|
|
if refInfo.List[i].Digest == mConvert.GetImageDigestStr(protoImageMeta) {
|
|
|
|
foundReferrer = true
|
|
|
|
refInfo.List[i].Count += 1
|
2023-03-10 13:37:29 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2023-03-10 13:37:29 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
if !foundReferrer {
|
|
|
|
refInfo.List = append(refInfo.List, &proto_go.ReferrerInfo{
|
|
|
|
Count: 1,
|
|
|
|
MediaType: protoImageMeta.MediaType,
|
|
|
|
Digest: mConvert.GetImageDigestStr(protoImageMeta),
|
|
|
|
ArtifactType: mConvert.GetImageArtifactType(protoImageMeta),
|
|
|
|
Size: mConvert.GetImageManifestSize(protoImageMeta),
|
|
|
|
Annotations: mConvert.GetImageAnnotations(protoImageMeta),
|
|
|
|
})
|
2023-03-10 13:37:29 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
repoMeta.Referrers[subject.Digest.String()] = refInfo
|
2023-03-10 13:37:29 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
// 3. Update tag
|
|
|
|
if !common.ReferenceIsDigest(reference) {
|
|
|
|
repoMeta.Tags[reference] = &proto_go.TagDescriptor{
|
|
|
|
Digest: imageMeta.Digest.String(),
|
|
|
|
MediaType: imageMeta.MediaType,
|
|
|
|
}
|
2023-09-22 13:51:20 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
if _, ok := repoMeta.Statistics[imageMeta.Digest.String()]; !ok {
|
2023-11-01 11:16:18 -05:00
|
|
|
repoMeta.Statistics[imageMeta.Digest.String()] = &proto_go.DescriptorStatistics{
|
|
|
|
DownloadCount: 0,
|
|
|
|
LastPullTimestamp: ×tamppb.Timestamp{},
|
|
|
|
PushTimestamp: timestamppb.Now(),
|
|
|
|
PushedBy: userid,
|
|
|
|
}
|
2023-11-09 12:32:56 -05:00
|
|
|
} else if repoMeta.Statistics[imageMeta.Digest.String()].PushTimestamp.AsTime().IsZero() {
|
|
|
|
repoMeta.Statistics[imageMeta.Digest.String()].PushTimestamp = timestamppb.Now()
|
2023-09-22 13:51:20 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
if _, ok := repoMeta.Signatures[imageMeta.Digest.String()]; !ok {
|
|
|
|
repoMeta.Signatures[imageMeta.Digest.String()] = &proto_go.ManifestSignatures{
|
|
|
|
Map: map[string]*proto_go.SignaturesInfo{"": {}},
|
2023-09-22 13:51:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
if _, ok := repoMeta.Referrers[imageMeta.Digest.String()]; !ok {
|
|
|
|
repoMeta.Referrers[imageMeta.Digest.String()] = &proto_go.ReferrersInfo{
|
|
|
|
List: []*proto_go.ReferrerInfo{},
|
2023-09-22 13:51:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
// 4. Blobs
|
2023-11-02 16:35:49 -05:00
|
|
|
repoBlobs, err := dwr.getProtoRepoBlobs(ctx, repo)
|
2023-10-30 15:06:04 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-09-22 13:51:20 -05:00
|
|
|
}
|
|
|
|
|
2023-11-08 16:37:52 -05:00
|
|
|
repoMeta, repoBlobs = common.AddImageMetaToRepoMeta(repoMeta, repoBlobs, reference, imageMeta)
|
2023-09-22 13:51:20 -05:00
|
|
|
|
2023-11-01 11:16:18 -05:00
|
|
|
err = dwr.setRepoBlobsInfo(repo, repoBlobs) //nolint: contextcheck
|
2023-10-30 15:06:04 -05:00
|
|
|
if err != nil {
|
2023-01-09 15:37:44 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-01 11:16:18 -05:00
|
|
|
return dwr.setProtoRepoMeta(repo, repoMeta) //nolint: contextcheck
|
2023-10-30 15:06:04 -05:00
|
|
|
}
|
|
|
|
|
2023-11-16 13:39:27 -05:00
|
|
|
func (dwr *DynamoDB) updateRepoLastUpdated(ctx context.Context, repo string, time time.Time) error {
|
|
|
|
protoTime := timestamppb.New(time)
|
|
|
|
|
|
|
|
protoTimeBlob, err := proto.Marshal(protoTime)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mdAttributeValue, err := attributevalue.Marshal(protoTimeBlob)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = dwr.Client.UpdateItem(ctx, &dynamodb.UpdateItemInput{
|
|
|
|
ExpressionAttributeNames: map[string]string{
|
|
|
|
"#RLU": "RepoLastUpdated",
|
|
|
|
},
|
|
|
|
ExpressionAttributeValues: map[string]types.AttributeValue{
|
|
|
|
":RepoLastUpdated": mdAttributeValue,
|
|
|
|
},
|
|
|
|
Key: map[string]types.AttributeValue{
|
|
|
|
"TableKey": &types.AttributeValueMemberS{
|
|
|
|
Value: repo,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TableName: aws.String(dwr.RepoBlobsTablename),
|
|
|
|
UpdateExpression: aws.String("SET #RLU = :RepoLastUpdated"),
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-11-02 16:35:49 -05:00
|
|
|
func (dwr *DynamoDB) getProtoRepoBlobs(ctx context.Context, repo string) (*proto_go.RepoBlobs, error) {
|
|
|
|
resp, err := dwr.Client.GetItem(ctx, &dynamodb.GetItemInput{
|
2023-10-30 15:06:04 -05:00
|
|
|
TableName: aws.String(dwr.RepoBlobsTablename),
|
2023-01-09 15:37:44 -05:00
|
|
|
Key: map[string]types.AttributeValue{
|
2023-11-16 13:39:27 -05:00
|
|
|
"TableKey": &types.AttributeValueMemberS{Value: repo},
|
2023-01-09 15:37:44 -05:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-10-30 15:06:04 -05:00
|
|
|
return nil, err
|
2023-03-23 13:08:11 -05:00
|
|
|
}
|
2023-03-09 13:41:48 -05:00
|
|
|
|
2023-11-02 16:35:49 -05:00
|
|
|
repoBlobs := &proto_go.RepoBlobs{
|
|
|
|
Name: repo,
|
2023-03-23 13:08:11 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
repoBlobsBytes := []byte{}
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-11-02 16:35:49 -05:00
|
|
|
if resp.Item != nil {
|
|
|
|
err = attributevalue.Unmarshal(resp.Item["RepoBlobsInfo"], &repoBlobsBytes)
|
2023-10-30 15:06:04 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-02 16:35:49 -05:00
|
|
|
|
|
|
|
if len(repoBlobsBytes) > 0 {
|
|
|
|
err := proto.Unmarshal(repoBlobsBytes, repoBlobs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-11-02 16:35:49 -05:00
|
|
|
if repoBlobs.Blobs == nil {
|
|
|
|
repoBlobs.Blobs = map[string]*proto_go.BlobInfo{"": {}}
|
2023-11-01 11:16:18 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
return repoBlobs, nil
|
|
|
|
}
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func (dwr *DynamoDB) setRepoBlobsInfo(repo string, repoBlobs *proto_go.RepoBlobs) error {
|
2023-11-16 13:39:27 -05:00
|
|
|
protoTime := timestamppb.Now()
|
|
|
|
|
|
|
|
protoTimeBlob, err := proto.Marshal(protoTime)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
timeAttributeValue, err := attributevalue.Marshal(protoTimeBlob)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
bytes, err := proto.Marshal(repoBlobs)
|
2023-01-09 15:37:44 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
mdAttributeValue, err := attributevalue.Marshal(bytes)
|
2023-01-09 15:37:44 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = dwr.Client.UpdateItem(context.TODO(), &dynamodb.UpdateItemInput{
|
|
|
|
ExpressionAttributeNames: map[string]string{
|
2023-10-30 15:06:04 -05:00
|
|
|
"#RBI": "RepoBlobsInfo",
|
2023-11-16 13:39:27 -05:00
|
|
|
"#RLU": "RepoLastUpdated",
|
2023-01-09 15:37:44 -05:00
|
|
|
},
|
|
|
|
ExpressionAttributeValues: map[string]types.AttributeValue{
|
2023-11-16 13:39:27 -05:00
|
|
|
":RepoBlobsInfo": mdAttributeValue,
|
|
|
|
":RepoLastUpdated": timeAttributeValue,
|
2023-01-09 15:37:44 -05:00
|
|
|
},
|
|
|
|
Key: map[string]types.AttributeValue{
|
2023-11-16 13:39:27 -05:00
|
|
|
"TableKey": &types.AttributeValueMemberS{
|
2023-01-09 15:37:44 -05:00
|
|
|
Value: repo,
|
|
|
|
},
|
|
|
|
},
|
2023-10-30 15:06:04 -05:00
|
|
|
TableName: aws.String(dwr.RepoBlobsTablename),
|
2023-11-16 13:39:27 -05:00
|
|
|
UpdateExpression: aws.String("SET #RBI = :RepoBlobsInfo, #RLU = :RepoLastUpdated"),
|
2023-01-09 15:37:44 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func (dwr *DynamoDB) SearchRepos(ctx context.Context, searchText string) ([]mTypes.RepoMeta, error) {
|
|
|
|
repos := []mTypes.RepoMeta{}
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
userBookmarks := getUserBookmarks(ctx, dwr)
|
|
|
|
userStars := getUserStars(ctx, dwr)
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
repoMetaAttributeIterator := NewBaseDynamoAttributesIterator(
|
2023-11-08 16:37:52 -05:00
|
|
|
dwr.Client, dwr.RepoMetaTablename, "RepoMeta", 0, dwr.Log,
|
2023-10-30 15:06:04 -05:00
|
|
|
)
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
repoMetaAttribute, err := repoMetaAttributeIterator.First(ctx)
|
|
|
|
|
|
|
|
for ; repoMetaAttribute != nil; repoMetaAttribute, err = repoMetaAttributeIterator.Next(ctx) {
|
|
|
|
if err != nil {
|
|
|
|
return []mTypes.RepoMeta{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
repoMetaBlob := []byte{}
|
|
|
|
|
|
|
|
err := attributevalue.Unmarshal(repoMetaAttribute, &repoMetaBlob)
|
|
|
|
if err != nil {
|
|
|
|
return []mTypes.RepoMeta{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
protoRepoMeta := &proto_go.RepoMeta{}
|
|
|
|
|
|
|
|
err = proto.Unmarshal(repoMetaBlob, protoRepoMeta)
|
|
|
|
if err != nil {
|
|
|
|
return []mTypes.RepoMeta{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok, err := reqCtx.RepoIsUserAvailable(ctx, protoRepoMeta.Name); !ok || err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-11-08 16:35:51 -05:00
|
|
|
delete(protoRepoMeta.Tags, "")
|
|
|
|
|
|
|
|
if len(protoRepoMeta.Tags) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
rank := common.RankRepoName(searchText, protoRepoMeta.Name)
|
|
|
|
if rank == -1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-11-08 11:58:55 -05:00
|
|
|
protoRepoMeta.Rank = int32(rank) //nolint:gosec // ignore overflow
|
2023-10-30 15:06:04 -05:00
|
|
|
protoRepoMeta.IsStarred = zcommon.Contains(userStars, protoRepoMeta.Name)
|
|
|
|
protoRepoMeta.IsBookmarked = zcommon.Contains(userBookmarks, protoRepoMeta.Name)
|
|
|
|
|
|
|
|
repos = append(repos, mConvert.GetRepoMeta(protoRepoMeta))
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
return repos, nil
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func (dwr *DynamoDB) SearchTags(ctx context.Context, searchText string) ([]mTypes.FullImageMeta, error) {
|
|
|
|
images := []mTypes.FullImageMeta{}
|
|
|
|
userBookmarks := getUserBookmarks(ctx, dwr)
|
|
|
|
userStars := getUserStars(ctx, dwr)
|
|
|
|
|
|
|
|
searchedRepo, searchedTag, err := common.GetRepoTag(searchText)
|
2023-05-04 11:51:21 -05:00
|
|
|
if err != nil {
|
2023-10-30 15:06:04 -05:00
|
|
|
return []mTypes.FullImageMeta{},
|
2023-12-08 03:05:02 -05:00
|
|
|
fmt.Errorf("failed to parse search text, invalid format %w", err)
|
2023-05-04 11:51:21 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
if ok, err := reqCtx.RepoIsUserAvailable(ctx, searchedRepo); !ok || err != nil {
|
|
|
|
return []mTypes.FullImageMeta{}, err
|
2023-05-04 11:51:21 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
protoRepoMeta, err := dwr.getProtoRepoMeta(ctx, searchedRepo)
|
2023-05-04 11:51:21 -05:00
|
|
|
if err != nil {
|
2023-10-30 15:06:04 -05:00
|
|
|
if errors.Is(err, zerr.ErrRepoMetaNotFound) {
|
|
|
|
return []mTypes.FullImageMeta{}, nil
|
|
|
|
}
|
2023-05-04 11:51:21 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
return nil, err
|
2023-05-04 11:51:21 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
delete(protoRepoMeta.Tags, "")
|
2023-05-04 11:51:21 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
protoRepoMeta.IsBookmarked = zcommon.Contains(userBookmarks, searchedRepo)
|
|
|
|
protoRepoMeta.IsStarred = zcommon.Contains(userStars, searchedRepo)
|
2023-05-04 11:51:21 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
for tag, descriptor := range protoRepoMeta.Tags {
|
|
|
|
if !strings.HasPrefix(tag, searchedTag) {
|
|
|
|
continue
|
|
|
|
}
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
var protoImageMeta *proto_go.ImageMeta
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
switch descriptor.MediaType {
|
|
|
|
case ispec.MediaTypeImageManifest:
|
|
|
|
manifestDigest := descriptor.Digest
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
imageManifestData, err := dwr.GetProtoImageMeta(ctx, godigest.Digest(manifestDigest))
|
|
|
|
if err != nil {
|
|
|
|
return []mTypes.FullImageMeta{},
|
2023-12-08 03:05:02 -05:00
|
|
|
fmt.Errorf("error fetching manifest meta for manifest with digest %s %w", manifestDigest, err)
|
2023-10-30 15:06:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
protoImageMeta = imageManifestData
|
|
|
|
case ispec.MediaTypeImageIndex:
|
|
|
|
indexDigest := godigest.Digest(descriptor.Digest)
|
|
|
|
|
|
|
|
imageIndexData, err := dwr.GetProtoImageMeta(ctx, indexDigest)
|
|
|
|
if err != nil {
|
|
|
|
return []mTypes.FullImageMeta{},
|
2023-12-08 03:05:02 -05:00
|
|
|
fmt.Errorf("error fetching manifest meta for manifest with digest %s %w", indexDigest, err)
|
2023-10-30 15:06:04 -05:00
|
|
|
}
|
|
|
|
|
2024-10-21 11:57:43 -05:00
|
|
|
_, manifestDataList, err := dwr.getAllContainedMeta(ctx, imageIndexData)
|
|
|
|
if err != nil {
|
|
|
|
return []mTypes.FullImageMeta{}, err
|
2023-10-30 15:06:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
imageIndexData.Manifests = manifestDataList
|
|
|
|
|
|
|
|
protoImageMeta = imageIndexData
|
|
|
|
default:
|
2023-12-08 03:05:02 -05:00
|
|
|
dwr.Log.Error().Str("mediaType", descriptor.MediaType).Msg("unsupported media type")
|
2023-10-30 15:06:04 -05:00
|
|
|
|
|
|
|
continue
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
images = append(images, mConvert.GetFullImageMetaFromProto(tag, protoRepoMeta, protoImageMeta))
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
return images, err
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func (dwr *DynamoDB) FilterTags(ctx context.Context, filterRepoTag mTypes.FilterRepoTagFunc,
|
|
|
|
filterFunc mTypes.FilterFunc,
|
|
|
|
) ([]mTypes.FullImageMeta, error) {
|
|
|
|
images := []mTypes.FullImageMeta{}
|
|
|
|
userBookmarks := getUserBookmarks(ctx, dwr)
|
|
|
|
userStars := getUserStars(ctx, dwr)
|
2023-09-08 02:03:58 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
var viewError error
|
|
|
|
|
|
|
|
repoMetaAttributeIterator := NewBaseDynamoAttributesIterator(
|
2023-11-08 16:37:52 -05:00
|
|
|
dwr.Client, dwr.RepoMetaTablename, "RepoMeta", 0, dwr.Log,
|
2023-10-30 15:06:04 -05:00
|
|
|
)
|
2023-09-08 02:03:58 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
repoMetaAttribute, err := repoMetaAttributeIterator.First(ctx)
|
2023-07-06 06:57:59 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
for ; repoMetaAttribute != nil; repoMetaAttribute, err = repoMetaAttributeIterator.Next(ctx) {
|
|
|
|
if err != nil {
|
|
|
|
viewError = errors.Join(viewError, err)
|
2023-07-06 06:57:59 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
continue
|
2023-07-06 06:57:59 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
protoRepoMeta, err := getProtoRepoMetaFromAttribute(repoMetaAttribute)
|
|
|
|
if err != nil {
|
|
|
|
viewError = errors.Join(viewError, err)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
2023-07-06 06:57:59 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
if ok, err := reqCtx.RepoIsUserAvailable(ctx, protoRepoMeta.Name); !ok || err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2023-07-06 06:57:59 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
protoRepoMeta.IsBookmarked = zcommon.Contains(userBookmarks, protoRepoMeta.Name)
|
|
|
|
protoRepoMeta.IsStarred = zcommon.Contains(userStars, protoRepoMeta.Name)
|
|
|
|
repoMeta := mConvert.GetRepoMeta(protoRepoMeta)
|
2023-07-06 06:57:59 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
for tag, descriptor := range repoMeta.Tags {
|
|
|
|
if !filterRepoTag(repoMeta.Name, tag) {
|
|
|
|
continue
|
|
|
|
}
|
2023-07-06 06:57:59 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
switch descriptor.MediaType {
|
|
|
|
case ispec.MediaTypeImageManifest:
|
|
|
|
manifestDigest := descriptor.Digest
|
2023-07-06 06:57:59 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
imageManifestData, err := dwr.GetProtoImageMeta(ctx, godigest.Digest(manifestDigest))
|
|
|
|
if err != nil {
|
|
|
|
viewError = errors.Join(viewError, err)
|
|
|
|
|
|
|
|
continue
|
2023-07-06 06:57:59 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
imageMeta := mConvert.GetImageMeta(imageManifestData)
|
|
|
|
|
|
|
|
if filterFunc(repoMeta, imageMeta) {
|
|
|
|
images = append(images, mConvert.GetFullImageMetaFromProto(tag, protoRepoMeta, imageManifestData))
|
2023-07-06 06:57:59 -05:00
|
|
|
}
|
2023-10-30 15:06:04 -05:00
|
|
|
case ispec.MediaTypeImageIndex:
|
|
|
|
indexDigest := descriptor.Digest
|
2023-07-06 06:57:59 -05:00
|
|
|
|
2023-11-27 11:52:52 -05:00
|
|
|
protoImageIndexMeta, err := dwr.GetProtoImageMeta(ctx, godigest.Digest(indexDigest))
|
2023-10-30 15:06:04 -05:00
|
|
|
if err != nil {
|
|
|
|
viewError = errors.Join(viewError, err)
|
2023-07-06 06:57:59 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-11-27 11:52:52 -05:00
|
|
|
imageIndexMeta := mConvert.GetImageMeta(protoImageIndexMeta)
|
2023-10-30 15:06:04 -05:00
|
|
|
matchedManifests := []*proto_go.ManifestMeta{}
|
|
|
|
|
2024-10-21 11:57:43 -05:00
|
|
|
imageManifestDataList, _, err := dwr.getAllContainedMeta(context.Background(), protoImageIndexMeta)
|
|
|
|
if err != nil {
|
|
|
|
viewError = errors.Join(viewError, err)
|
2023-10-30 15:06:04 -05:00
|
|
|
|
2024-10-21 11:57:43 -05:00
|
|
|
continue
|
|
|
|
}
|
2023-10-30 15:06:04 -05:00
|
|
|
|
2024-10-21 11:57:43 -05:00
|
|
|
for _, imageManifestData := range imageManifestDataList {
|
2023-10-30 15:06:04 -05:00
|
|
|
imageMeta := mConvert.GetImageMeta(imageManifestData)
|
2023-11-27 11:52:52 -05:00
|
|
|
partialImageMeta := common.GetPartialImageMeta(imageIndexMeta, imageMeta)
|
2023-10-30 15:06:04 -05:00
|
|
|
|
2023-11-27 11:52:52 -05:00
|
|
|
if filterFunc(repoMeta, partialImageMeta) {
|
2023-10-30 15:06:04 -05:00
|
|
|
matchedManifests = append(matchedManifests, imageManifestData.Manifests[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matchedManifests) > 0 {
|
2023-11-27 11:52:52 -05:00
|
|
|
protoImageIndexMeta.Manifests = matchedManifests
|
2023-10-30 15:06:04 -05:00
|
|
|
|
2023-11-27 11:52:52 -05:00
|
|
|
images = append(images, mConvert.GetFullImageMetaFromProto(tag, protoRepoMeta, protoImageIndexMeta))
|
2023-10-30 15:06:04 -05:00
|
|
|
}
|
|
|
|
default:
|
2023-12-08 03:05:02 -05:00
|
|
|
dwr.Log.Error().Str("mediaType", descriptor.MediaType).Msg("unsupported media type")
|
2023-10-30 15:06:04 -05:00
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
2023-07-06 06:57:59 -05:00
|
|
|
}
|
2023-10-30 15:06:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
viewError = errors.Join(viewError, err)
|
|
|
|
|
|
|
|
return images, viewError
|
|
|
|
}
|
|
|
|
|
|
|
|
func getProtoRepoMetaFromAttribute(repoMetaAttribute types.AttributeValue) (*proto_go.RepoMeta, error) {
|
|
|
|
blob := []byte{}
|
2023-07-06 06:57:59 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
err := attributevalue.Unmarshal(repoMetaAttribute, &blob)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-07-06 06:57:59 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
protoRepoMeta := &proto_go.RepoMeta{}
|
2023-07-06 06:57:59 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
err = proto.Unmarshal(blob, protoRepoMeta)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return protoRepoMeta, nil
|
2023-05-24 11:46:16 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func getProtoImageMetaFromAttribute(imageMetaAttribute types.AttributeValue) (*proto_go.ImageMeta, error) {
|
|
|
|
blob := []byte{}
|
|
|
|
|
|
|
|
err := attributevalue.Unmarshal(imageMetaAttribute, &blob)
|
2023-01-09 15:37:44 -05:00
|
|
|
if err != nil {
|
2023-10-30 15:06:04 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-15 04:02:23 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
protoImageMeta := &proto_go.ImageMeta{}
|
2023-05-15 04:02:23 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
err = proto.Unmarshal(blob, protoImageMeta)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
return protoImageMeta, nil
|
|
|
|
}
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func (dwr *DynamoDB) ResetRepoReferences(repo string) error {
|
|
|
|
protoRepoMeta, err := dwr.getProtoRepoMeta(context.Background(), repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
return dwr.setProtoRepoMeta(repo, &proto_go.RepoMeta{
|
|
|
|
Name: repo,
|
|
|
|
Statistics: protoRepoMeta.Statistics,
|
|
|
|
Stars: protoRepoMeta.Stars,
|
|
|
|
Tags: map[string]*proto_go.TagDescriptor{"": {}},
|
|
|
|
Referrers: map[string]*proto_go.ReferrersInfo{"": {}},
|
|
|
|
Signatures: map[string]*proto_go.ManifestSignatures{"": {Map: map[string]*proto_go.SignaturesInfo{"": {}}}},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dwr *DynamoDB) GetRepoMeta(ctx context.Context, repo string) (mTypes.RepoMeta, error) {
|
|
|
|
protoRepoMeta, err := dwr.getProtoRepoMeta(ctx, repo)
|
|
|
|
if err != nil {
|
|
|
|
return mTypes.RepoMeta{}, err
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
delete(protoRepoMeta.Tags, "")
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
userBookmarks := getUserBookmarks(ctx, dwr)
|
|
|
|
userStars := getUserStars(ctx, dwr)
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
protoRepoMeta.IsBookmarked = zcommon.Contains(userBookmarks, repo)
|
|
|
|
protoRepoMeta.IsStarred = zcommon.Contains(userStars, repo)
|
|
|
|
|
|
|
|
return mConvert.GetRepoMeta(protoRepoMeta), nil
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func (dwr *DynamoDB) GetFullImageMeta(ctx context.Context, repo string, tag string) (mTypes.FullImageMeta, error) {
|
|
|
|
protoRepoMeta, err := dwr.getProtoRepoMeta(ctx, repo)
|
2023-01-09 15:37:44 -05:00
|
|
|
if err != nil {
|
2023-10-30 15:06:04 -05:00
|
|
|
return mTypes.FullImageMeta{}, err
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
delete(protoRepoMeta.Tags, "")
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
bookmarks, stars := dwr.getUserBookmarksAndStars(ctx)
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
protoRepoMeta.IsBookmarked = zcommon.Contains(bookmarks, repo)
|
|
|
|
protoRepoMeta.IsStarred = zcommon.Contains(stars, repo)
|
|
|
|
|
|
|
|
descriptor, ok := protoRepoMeta.Tags[tag]
|
|
|
|
if !ok {
|
|
|
|
return mTypes.FullImageMeta{}, zerr.ErrImageMetaNotFound
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
protoImageMeta, err := dwr.GetProtoImageMeta(ctx, godigest.Digest(descriptor.Digest))
|
|
|
|
if err != nil {
|
|
|
|
return mTypes.FullImageMeta{}, err
|
|
|
|
}
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
if protoImageMeta.MediaType == ispec.MediaTypeImageIndex {
|
2024-10-21 11:57:43 -05:00
|
|
|
_, manifestDataList, err := dwr.getAllContainedMeta(ctx, protoImageMeta)
|
|
|
|
if err != nil {
|
|
|
|
return mTypes.FullImageMeta{}, err
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
2023-10-30 15:06:04 -05:00
|
|
|
|
|
|
|
protoImageMeta.Manifests = manifestDataList
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
return mConvert.GetFullImageMetaFromProto(tag, protoRepoMeta, protoImageMeta), nil
|
|
|
|
}
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func (dwr *DynamoDB) getUserBookmarksAndStars(ctx context.Context) ([]string, []string) {
|
|
|
|
userData, err := dwr.GetUserData(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return []string{}, []string{}
|
|
|
|
}
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
return userData.BookmarkedRepos, userData.StarredRepos
|
|
|
|
}
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func (dwr *DynamoDB) GetImageMeta(digest godigest.Digest) (mTypes.ImageMeta, error) {
|
|
|
|
protoImageMeta, err := dwr.GetProtoImageMeta(context.Background(), digest)
|
|
|
|
if err != nil {
|
|
|
|
return mTypes.ImageMeta{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if protoImageMeta.MediaType == ispec.MediaTypeImageIndex {
|
2024-10-21 11:57:43 -05:00
|
|
|
_, manifestDataList, err := dwr.getAllContainedMeta(context.Background(), protoImageMeta)
|
|
|
|
if err != nil {
|
|
|
|
return mTypes.ImageMeta{}, err
|
2023-10-30 15:06:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
protoImageMeta.Manifests = manifestDataList
|
|
|
|
}
|
|
|
|
|
|
|
|
return mConvert.GetImageMeta(protoImageMeta), nil
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func (dwr *DynamoDB) GetMultipleRepoMeta(ctx context.Context, filter func(repoMeta mTypes.RepoMeta) bool,
|
|
|
|
) ([]mTypes.RepoMeta, error) {
|
2023-01-09 15:37:44 -05:00
|
|
|
var (
|
2023-10-30 15:06:04 -05:00
|
|
|
foundRepos = []mTypes.RepoMeta{}
|
2023-07-18 12:27:26 -05:00
|
|
|
repoMetaAttributeIterator AttributesIterator
|
2023-01-09 15:37:44 -05:00
|
|
|
)
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
repoMetaAttributeIterator = NewBaseDynamoAttributesIterator(
|
2023-11-08 16:37:52 -05:00
|
|
|
dwr.Client, dwr.RepoMetaTablename, "RepoMeta", 0, dwr.Log,
|
2023-01-09 15:37:44 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
repoMetaAttribute, err := repoMetaAttributeIterator.First(ctx)
|
|
|
|
|
|
|
|
for ; repoMetaAttribute != nil; repoMetaAttribute, err = repoMetaAttributeIterator.Next(ctx) {
|
|
|
|
if err != nil {
|
2023-10-30 15:06:04 -05:00
|
|
|
return []mTypes.RepoMeta{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
repoMetaBlob := []byte{}
|
|
|
|
|
|
|
|
err := attributevalue.Unmarshal(repoMetaAttribute, &repoMetaBlob)
|
|
|
|
if err != nil {
|
|
|
|
return []mTypes.RepoMeta{}, err
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
protoRepoMeta := &proto_go.RepoMeta{}
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
err = proto.Unmarshal(repoMetaBlob, protoRepoMeta)
|
2023-01-09 15:37:44 -05:00
|
|
|
if err != nil {
|
2023-10-30 15:06:04 -05:00
|
|
|
return []mTypes.RepoMeta{}, err
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
delete(protoRepoMeta.Tags, "")
|
|
|
|
|
|
|
|
if ok, err := reqCtx.RepoIsUserAvailable(ctx, protoRepoMeta.Name); !ok || err != nil {
|
2023-01-09 15:37:44 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
repoMeta := mConvert.GetRepoMeta(protoRepoMeta)
|
|
|
|
|
2023-01-09 15:37:44 -05:00
|
|
|
if filter(repoMeta) {
|
2023-07-31 14:16:09 -05:00
|
|
|
foundRepos = append(foundRepos, repoMeta)
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return foundRepos, err
|
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func (dwr *DynamoDB) FilterRepos(ctx context.Context, acceptName mTypes.FilterRepoNameFunc,
|
|
|
|
filterFunc mTypes.FilterFullRepoFunc,
|
|
|
|
) ([]mTypes.RepoMeta, error) {
|
|
|
|
repos := []mTypes.RepoMeta{}
|
|
|
|
userBookmarks := getUserBookmarks(ctx, dwr)
|
|
|
|
userStars := getUserStars(ctx, dwr)
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
repoMetaAttributeIterator := NewBaseDynamoAttributesIterator(
|
2023-11-08 16:37:52 -05:00
|
|
|
dwr.Client, dwr.RepoMetaTablename, "RepoMeta", 0, dwr.Log,
|
2023-01-09 15:37:44 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
repoMetaAttribute, err := repoMetaAttributeIterator.First(ctx)
|
|
|
|
|
|
|
|
for ; repoMetaAttribute != nil; repoMetaAttribute, err = repoMetaAttributeIterator.Next(ctx) {
|
|
|
|
if err != nil {
|
2023-10-30 15:06:04 -05:00
|
|
|
return []mTypes.RepoMeta{},
|
2023-07-31 14:16:09 -05:00
|
|
|
err
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
protoRepoMeta, err := getProtoRepoMetaFromAttribute(repoMetaAttribute)
|
2023-01-09 15:37:44 -05:00
|
|
|
if err != nil {
|
2023-10-30 15:06:04 -05:00
|
|
|
return nil, err
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
if ok, err := reqCtx.RepoIsUserAvailable(ctx, protoRepoMeta.Name); !ok || err != nil {
|
2023-01-09 15:37:44 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
if !acceptName(protoRepoMeta.Name) {
|
2023-04-24 13:13:15 -05:00
|
|
|
continue
|
|
|
|
}
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
protoRepoMeta.IsBookmarked = zcommon.Contains(userBookmarks, protoRepoMeta.Name)
|
|
|
|
protoRepoMeta.IsStarred = zcommon.Contains(userStars, protoRepoMeta.Name)
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
fullRepoMeta := mConvert.GetRepoMeta(protoRepoMeta)
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
if filterFunc(fullRepoMeta) {
|
|
|
|
repos = append(repos, fullRepoMeta)
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
return repos, err
|
2023-02-27 14:23:18 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func (dwr *DynamoDB) IncrementRepoStars(repo string) error {
|
|
|
|
repoMeta, err := dwr.getProtoRepoMeta(context.Background(), repo)
|
2023-04-24 13:13:15 -05:00
|
|
|
if err != nil {
|
2023-10-30 15:06:04 -05:00
|
|
|
return err
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
repoMeta.Stars++
|
|
|
|
|
|
|
|
return dwr.setProtoRepoMeta(repo, repoMeta)
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func (dwr *DynamoDB) DecrementRepoStars(repo string) error {
|
|
|
|
repoMeta, err := dwr.getProtoRepoMeta(context.Background(), repo)
|
2023-04-24 13:13:15 -05:00
|
|
|
if err != nil {
|
2023-10-30 15:06:04 -05:00
|
|
|
return err
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
if repoMeta.Stars > 0 {
|
|
|
|
repoMeta.Stars--
|
2023-02-27 14:23:18 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
return dwr.setProtoRepoMeta(repo, repoMeta)
|
2023-02-27 14:23:18 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func (dwr *DynamoDB) SetRepoMeta(repo string, repoMeta mTypes.RepoMeta) error {
|
|
|
|
protoRepoMeta := mConvert.GetProtoRepoMeta(repoMeta)
|
2023-02-27 14:23:18 -05:00
|
|
|
|
2023-11-16 13:39:27 -05:00
|
|
|
err := dwr.updateRepoLastUpdated(context.Background(), repo, time.Time{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
return dwr.setProtoRepoMeta(repo, protoRepoMeta)
|
2023-02-27 14:23:18 -05:00
|
|
|
}
|
|
|
|
|
2023-11-16 13:39:27 -05:00
|
|
|
func (dwr *DynamoDB) DeleteRepoMeta(repo string) error {
|
|
|
|
_, err := dwr.Client.TransactWriteItems(context.Background(), &dynamodb.TransactWriteItemsInput{
|
|
|
|
TransactItems: []types.TransactWriteItem{
|
|
|
|
{
|
|
|
|
Delete: &types.Delete{
|
|
|
|
Key: map[string]types.AttributeValue{
|
|
|
|
"TableKey": &types.AttributeValueMemberS{
|
|
|
|
Value: repo,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TableName: aws.String(dwr.RepoMetaTablename),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Delete: &types.Delete{
|
|
|
|
Key: map[string]types.AttributeValue{
|
|
|
|
"TableKey": &types.AttributeValueMemberS{
|
|
|
|
Value: repo,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TableName: aws.String(dwr.RepoBlobsTablename),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func (dwr *DynamoDB) GetReferrersInfo(repo string, referredDigest godigest.Digest, artifactTypes []string,
|
|
|
|
) ([]mTypes.ReferrerInfo, error) {
|
|
|
|
repoMeta, err := dwr.GetRepoMeta(context.Background(), repo)
|
2023-09-22 13:49:17 -05:00
|
|
|
if err != nil {
|
2023-10-30 15:06:04 -05:00
|
|
|
return []mTypes.ReferrerInfo{}, err
|
2023-09-22 13:49:17 -05:00
|
|
|
}
|
2023-01-17 17:31:54 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
referrersInfo := repoMeta.Referrers[referredDigest.String()]
|
2023-09-22 13:49:17 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
filteredResults := make([]mTypes.ReferrerInfo, 0, len(referrersInfo))
|
2023-01-17 17:31:54 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
for _, referrerInfo := range referrersInfo {
|
|
|
|
if !common.MatchesArtifactTypes(referrerInfo.ArtifactType, artifactTypes) {
|
2023-01-17 17:31:54 -05:00
|
|
|
continue
|
|
|
|
}
|
2023-04-24 13:13:15 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
filteredResults = append(filteredResults, referrerInfo)
|
|
|
|
}
|
2023-01-17 17:31:54 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
return filteredResults, nil
|
|
|
|
}
|
2023-01-17 17:31:54 -05:00
|
|
|
|
2023-11-01 11:16:18 -05:00
|
|
|
func (dwr *DynamoDB) UpdateStatsOnDownload(repo string, reference string) error {
|
2023-10-30 15:06:04 -05:00
|
|
|
repoMeta, err := dwr.getProtoRepoMeta(context.Background(), repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-22 13:49:17 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
descriptorDigest := reference
|
2023-01-17 17:31:54 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
if !common.ReferenceIsDigest(reference) {
|
|
|
|
// search digest for tag
|
|
|
|
descriptor, found := repoMeta.Tags[reference]
|
2023-02-27 14:23:18 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
if !found {
|
2023-12-08 03:05:02 -05:00
|
|
|
return zerr.ErrImageMetaNotFound
|
2023-10-30 15:06:04 -05:00
|
|
|
}
|
2023-09-22 13:49:17 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
descriptorDigest = descriptor.Digest
|
|
|
|
}
|
2023-01-17 17:31:54 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
manifestStatistics, ok := repoMeta.Statistics[descriptorDigest]
|
|
|
|
if !ok {
|
2023-12-08 03:05:02 -05:00
|
|
|
return zerr.ErrImageMetaNotFound
|
2023-10-30 15:06:04 -05:00
|
|
|
}
|
2023-01-17 17:31:54 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
manifestStatistics.DownloadCount++
|
2023-11-01 11:16:18 -05:00
|
|
|
manifestStatistics.LastPullTimestamp = timestamppb.Now()
|
2023-10-30 15:06:04 -05:00
|
|
|
repoMeta.Statistics[descriptorDigest] = manifestStatistics
|
2023-09-22 13:49:17 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
return dwr.setProtoRepoMeta(repo, repoMeta)
|
|
|
|
}
|
2023-02-27 14:23:18 -05:00
|
|
|
|
2023-11-24 03:40:10 -05:00
|
|
|
func (dwr *DynamoDB) UpdateSignaturesValidity(ctx context.Context, repo string, manifestDigest godigest.Digest) error {
|
2023-10-30 15:06:04 -05:00
|
|
|
imgTrustStore := dwr.ImageTrustStore()
|
2023-02-27 14:23:18 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
if imgTrustStore == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2023-02-27 14:23:18 -05:00
|
|
|
|
2023-11-24 03:40:10 -05:00
|
|
|
protoImageMeta, err := dwr.GetProtoImageMeta(ctx, manifestDigest)
|
2023-10-30 15:06:04 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-22 13:49:17 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
// update signatures with details about validity and author
|
2023-11-24 03:40:10 -05:00
|
|
|
protoRepoMeta, err := dwr.getProtoRepoMeta(ctx, repo)
|
2023-10-30 15:06:04 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-17 17:31:54 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
manifestSignatures := proto_go.ManifestSignatures{Map: map[string]*proto_go.SignaturesInfo{"": {}}}
|
2023-02-27 14:23:18 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
for sigType, sigs := range protoRepoMeta.Signatures[manifestDigest.String()].Map {
|
2023-11-24 03:40:10 -05:00
|
|
|
if zcommon.IsContextDone(ctx) {
|
|
|
|
return ctx.Err()
|
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
signaturesInfo := []*proto_go.SignatureInfo{}
|
2023-02-27 14:23:18 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
for _, sigInfo := range sigs.List {
|
|
|
|
layersInfo := []*proto_go.LayersInfo{}
|
2023-09-22 13:49:17 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
for _, layerInfo := range sigInfo.LayersInfo {
|
|
|
|
author, date, isTrusted, _ := imgTrustStore.VerifySignature(sigType, layerInfo.LayerContent,
|
|
|
|
layerInfo.SignatureKey, manifestDigest, mConvert.GetImageMeta(protoImageMeta), repo)
|
2023-02-27 14:23:18 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
if isTrusted {
|
|
|
|
layerInfo.Signer = author
|
|
|
|
}
|
2023-02-27 14:23:18 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
if !date.IsZero() {
|
|
|
|
layerInfo.Signer = author
|
|
|
|
layerInfo.Date = timestamppb.New(date)
|
2023-07-06 03:36:26 -05:00
|
|
|
}
|
2023-03-15 12:34:48 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
layersInfo = append(layersInfo, layerInfo)
|
2023-02-27 14:23:18 -05:00
|
|
|
}
|
2023-01-17 17:31:54 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
signaturesInfo = append(signaturesInfo, &proto_go.SignatureInfo{
|
|
|
|
SignatureManifestDigest: sigInfo.SignatureManifestDigest,
|
|
|
|
LayersInfo: layersInfo,
|
|
|
|
})
|
2023-01-17 17:31:54 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
manifestSignatures.Map[sigType] = &proto_go.SignaturesInfo{List: signaturesInfo}
|
2023-01-17 17:31:54 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
protoRepoMeta.Signatures[manifestDigest.String()] = &manifestSignatures
|
2023-04-24 13:13:15 -05:00
|
|
|
|
2023-11-24 03:40:10 -05:00
|
|
|
return dwr.setProtoRepoMeta(protoRepoMeta.Name, protoRepoMeta) //nolint: contextcheck
|
2023-10-30 15:06:04 -05:00
|
|
|
}
|
2023-04-24 13:13:15 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func (dwr *DynamoDB) AddManifestSignature(repo string, signedManifestDigest godigest.Digest,
|
2023-11-06 17:09:39 -05:00
|
|
|
sigMeta mTypes.SignatureMetadata,
|
2023-10-30 15:06:04 -05:00
|
|
|
) error {
|
|
|
|
protoRepoMeta, err := dwr.getProtoRepoMeta(context.Background(), repo)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, zerr.ErrRepoMetaNotFound) {
|
|
|
|
protoRepoMeta = &proto_go.RepoMeta{
|
|
|
|
Name: repo,
|
|
|
|
Tags: map[string]*proto_go.TagDescriptor{"": {}},
|
|
|
|
Statistics: map[string]*proto_go.DescriptorStatistics{"": {}},
|
|
|
|
Referrers: map[string]*proto_go.ReferrersInfo{"": {}},
|
|
|
|
Signatures: map[string]*proto_go.ManifestSignatures{
|
|
|
|
signedManifestDigest.String(): {
|
|
|
|
Map: map[string]*proto_go.SignaturesInfo{
|
2023-11-06 17:09:39 -05:00
|
|
|
sigMeta.SignatureType: {
|
2023-10-30 15:06:04 -05:00
|
|
|
List: []*proto_go.SignatureInfo{
|
|
|
|
{
|
2023-11-06 17:09:39 -05:00
|
|
|
SignatureManifestDigest: sigMeta.SignatureDigest,
|
|
|
|
LayersInfo: mConvert.GetProtoLayersInfo(sigMeta.LayersInfo),
|
2023-10-30 15:06:04 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2023-04-24 13:13:15 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
return dwr.setProtoRepoMeta(repo, protoRepoMeta)
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
return err
|
|
|
|
}
|
2023-04-24 13:13:15 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
var (
|
|
|
|
manifestSignatures *proto_go.ManifestSignatures
|
|
|
|
found bool
|
|
|
|
)
|
2023-04-24 13:13:15 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
if manifestSignatures, found = protoRepoMeta.Signatures[signedManifestDigest.String()]; !found {
|
|
|
|
manifestSignatures = &proto_go.ManifestSignatures{Map: map[string]*proto_go.SignaturesInfo{"": {}}}
|
|
|
|
}
|
2023-04-24 13:13:15 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
signatureSlice := &proto_go.SignaturesInfo{List: []*proto_go.SignatureInfo{}}
|
2023-11-06 17:09:39 -05:00
|
|
|
if sigSlice, found := manifestSignatures.Map[sigMeta.SignatureType]; found {
|
2023-10-30 15:06:04 -05:00
|
|
|
signatureSlice = sigSlice
|
|
|
|
}
|
2023-04-24 13:13:15 -05:00
|
|
|
|
2023-11-06 17:09:39 -05:00
|
|
|
if !common.ProtoSignatureAlreadyExists(signatureSlice.List, sigMeta) {
|
|
|
|
switch sigMeta.SignatureType {
|
2023-10-30 15:06:04 -05:00
|
|
|
case zcommon.NotationSignature:
|
|
|
|
signatureSlice.List = append(signatureSlice.List, &proto_go.SignatureInfo{
|
2023-11-06 17:09:39 -05:00
|
|
|
SignatureManifestDigest: sigMeta.SignatureDigest,
|
|
|
|
LayersInfo: mConvert.GetProtoLayersInfo(sigMeta.LayersInfo),
|
2023-10-30 15:06:04 -05:00
|
|
|
})
|
|
|
|
case zcommon.CosignSignature:
|
2023-11-06 17:09:39 -05:00
|
|
|
newCosignSig := &proto_go.SignatureInfo{
|
|
|
|
SignatureManifestDigest: sigMeta.SignatureDigest,
|
|
|
|
LayersInfo: mConvert.GetProtoLayersInfo(sigMeta.LayersInfo),
|
|
|
|
}
|
|
|
|
|
|
|
|
if zcommon.IsCosignTag(sigMeta.SignatureTag) {
|
|
|
|
// the entry for "sha256-{digest}.sig" signatures should be overwritten if
|
|
|
|
// it exists or added on the first position if it doesn't exist
|
|
|
|
if len(signatureSlice.GetList()) == 0 {
|
|
|
|
signatureSlice.List = []*proto_go.SignatureInfo{newCosignSig}
|
|
|
|
} else {
|
|
|
|
signatureSlice.List[0] = newCosignSig
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// the first position should be reserved for "sha256-{digest}.sig" signatures
|
|
|
|
if len(signatureSlice.GetList()) == 0 {
|
|
|
|
signatureSlice.List = []*proto_go.SignatureInfo{{
|
|
|
|
SignatureManifestDigest: "",
|
|
|
|
LayersInfo: []*proto_go.LayersInfo{},
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
signatureSlice.List = append(signatureSlice.List, newCosignSig)
|
|
|
|
}
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-06 17:09:39 -05:00
|
|
|
manifestSignatures.Map[sigMeta.SignatureType] = signatureSlice
|
2023-10-30 15:06:04 -05:00
|
|
|
protoRepoMeta.Signatures[signedManifestDigest.String()] = manifestSignatures
|
2023-04-24 13:13:15 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
return dwr.setProtoRepoMeta(protoRepoMeta.Name, protoRepoMeta)
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func (dwr *DynamoDB) DeleteSignature(repo string, signedManifestDigest godigest.Digest,
|
|
|
|
sigMeta mTypes.SignatureMetadata,
|
|
|
|
) error {
|
|
|
|
protoRepoMeta, err := dwr.getProtoRepoMeta(context.Background(), repo)
|
2023-01-09 15:37:44 -05:00
|
|
|
if err != nil {
|
2023-10-30 15:06:04 -05:00
|
|
|
return err
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
sigType := sigMeta.SignatureType
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
var (
|
|
|
|
manifestSignatures *proto_go.ManifestSignatures
|
|
|
|
found bool
|
|
|
|
)
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
if manifestSignatures, found = protoRepoMeta.Signatures[signedManifestDigest.String()]; !found {
|
2023-12-08 03:05:02 -05:00
|
|
|
return zerr.ErrImageMetaNotFound
|
2023-07-31 14:16:09 -05:00
|
|
|
}
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
signatureSlice := manifestSignatures.Map[sigType]
|
|
|
|
|
|
|
|
newSignatureSlice := make([]*proto_go.SignatureInfo, 0, len(signatureSlice.List)-1)
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
for _, sigDigest := range signatureSlice.List {
|
|
|
|
if sigDigest.SignatureManifestDigest != sigMeta.SignatureDigest {
|
|
|
|
newSignatureSlice = append(newSignatureSlice, sigDigest)
|
|
|
|
}
|
2023-07-31 14:16:09 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
manifestSignatures.Map[sigMeta.SignatureType] = &proto_go.SignaturesInfo{List: newSignatureSlice}
|
2023-07-31 14:16:09 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
protoRepoMeta.Signatures[signedManifestDigest.String()] = manifestSignatures
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
return dwr.setProtoRepoMeta(protoRepoMeta.Name, protoRepoMeta)
|
|
|
|
}
|
2023-04-24 13:13:15 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func (dwr *DynamoDB) FilterImageMeta(ctx context.Context, digests []string,
|
|
|
|
) (map[string]mTypes.ImageMeta, error) {
|
|
|
|
imageMetaAttributes, err := dwr.fetchImageMetaAttributesByDigest(ctx, digests)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-24 13:13:15 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
results := map[string]mTypes.ImageMeta{}
|
2023-04-24 13:13:15 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
for _, attributes := range imageMetaAttributes {
|
|
|
|
protoImageMeta, err := getProtoImageMetaFromAttribute(attributes["ImageMeta"])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-24 13:13:15 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
if protoImageMeta.MediaType == ispec.MediaTypeImageIndex {
|
2024-10-21 11:57:43 -05:00
|
|
|
_, manifestDataList, err := dwr.getAllContainedMeta(context.Background(), protoImageMeta)
|
2023-07-31 14:16:09 -05:00
|
|
|
if err != nil {
|
2023-10-30 15:06:04 -05:00
|
|
|
return nil, err
|
2023-07-31 14:16:09 -05:00
|
|
|
}
|
2023-02-27 14:23:18 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
protoImageMeta.Manifests = manifestDataList
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
2023-04-24 13:13:15 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
results[mConvert.GetImageDigestStr(protoImageMeta)] = mConvert.GetImageMeta(protoImageMeta)
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
return results, nil
|
2023-02-27 14:23:18 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func (dwr *DynamoDB) RemoveRepoReference(repo, reference string, manifestDigest godigest.Digest,
|
|
|
|
) error {
|
2023-11-16 13:39:27 -05:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
protoRepoMeta, err := dwr.getProtoRepoMeta(context.Background(), repo)
|
2023-01-09 15:37:44 -05:00
|
|
|
if err != nil {
|
2023-10-30 15:06:04 -05:00
|
|
|
if errors.Is(err, zerr.ErrRepoMetaNotFound) {
|
|
|
|
return nil
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
protoImageMeta, err := dwr.GetProtoImageMeta(context.TODO(), manifestDigest)
|
2023-10-21 04:07:32 -05:00
|
|
|
if err != nil {
|
2023-10-30 15:06:04 -05:00
|
|
|
if errors.Is(err, zerr.ErrImageMetaNotFound) {
|
2023-10-21 04:07:32 -05:00
|
|
|
return nil
|
|
|
|
}
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-01-16 09:01:35 -05:00
|
|
|
return err
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
// Remove Referrers
|
|
|
|
if subject := mConvert.GetImageSubject(protoImageMeta); subject != nil {
|
|
|
|
referredDigest := subject.Digest.String()
|
|
|
|
refInfo := &proto_go.ReferrersInfo{}
|
2023-01-16 09:01:35 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
if protoRepoMeta.Referrers[referredDigest] != nil {
|
|
|
|
refInfo = protoRepoMeta.Referrers[referredDigest]
|
|
|
|
}
|
2023-01-16 09:01:35 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
referrers := refInfo.List
|
2023-01-16 09:01:35 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
for i := range referrers {
|
|
|
|
if referrers[i].Digest == manifestDigest.String() {
|
|
|
|
referrers[i].Count -= 1
|
2023-01-16 09:01:35 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
if referrers[i].Count == 0 || common.ReferenceIsDigest(reference) {
|
|
|
|
referrers = append(referrers[:i], referrers[i+1:]...)
|
|
|
|
}
|
2023-01-16 09:01:35 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
break
|
|
|
|
}
|
2023-10-21 04:07:32 -05:00
|
|
|
}
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
refInfo.List = referrers
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
protoRepoMeta.Referrers[referredDigest] = refInfo
|
|
|
|
}
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
if !common.ReferenceIsDigest(reference) {
|
|
|
|
delete(protoRepoMeta.Tags, reference)
|
|
|
|
} else {
|
|
|
|
// find all tags pointing to this digest
|
|
|
|
tags := []string{}
|
2024-07-29 12:32:51 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
for tag, desc := range protoRepoMeta.Tags {
|
|
|
|
if desc.Digest == reference {
|
|
|
|
tags = append(tags, tag)
|
|
|
|
}
|
2023-10-21 04:07:32 -05:00
|
|
|
}
|
2023-02-27 14:23:18 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
// remove all tags
|
|
|
|
for _, tag := range tags {
|
|
|
|
delete(protoRepoMeta.Tags, tag)
|
|
|
|
}
|
2023-02-27 14:23:18 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
/* try to find at least one tag pointing to manifestDigest
|
|
|
|
if not found then we can also remove everything related to this digest */
|
|
|
|
var foundTag bool
|
2023-02-27 14:23:18 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
for _, desc := range protoRepoMeta.Tags {
|
|
|
|
if desc.Digest == manifestDigest.String() {
|
|
|
|
foundTag = true
|
2023-01-16 09:01:35 -05:00
|
|
|
}
|
2023-10-30 15:06:04 -05:00
|
|
|
}
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
if !foundTag {
|
|
|
|
delete(protoRepoMeta.Statistics, manifestDigest.String())
|
|
|
|
delete(protoRepoMeta.Signatures, manifestDigest.String())
|
|
|
|
delete(protoRepoMeta.Referrers, manifestDigest.String())
|
2023-01-16 09:01:35 -05:00
|
|
|
}
|
|
|
|
|
2023-11-16 13:39:27 -05:00
|
|
|
repoBlobsInfo, err := dwr.getProtoRepoBlobs(ctx, repo)
|
2023-01-16 09:01:35 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-11-08 16:37:52 -05:00
|
|
|
protoRepoMeta, repoBlobsInfo = common.RemoveImageFromRepoMeta(protoRepoMeta, repoBlobsInfo, reference)
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-11-01 11:16:18 -05:00
|
|
|
err = dwr.setRepoBlobsInfo(repo, repoBlobsInfo) //nolint: contextcheck
|
2023-01-09 15:37:44 -05:00
|
|
|
if err != nil {
|
2023-10-30 15:06:04 -05:00
|
|
|
return err
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
2024-07-29 12:32:51 -05:00
|
|
|
|
2023-11-01 11:16:18 -05:00
|
|
|
err = dwr.setProtoRepoMeta(repo, protoRepoMeta) //nolint: contextcheck
|
2023-01-09 15:37:44 -05:00
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
return err
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func getUserStars(ctx context.Context, dwr *DynamoDB) []string {
|
|
|
|
starredRepos, err := dwr.GetStarredRepos(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return []string{}
|
2023-01-16 09:01:35 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
return starredRepos
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func getUserBookmarks(ctx context.Context, dwr *DynamoDB) []string {
|
|
|
|
bookmarkedRepos, err := dwr.GetBookmarkedRepos(ctx)
|
2023-01-09 15:37:44 -05:00
|
|
|
if err != nil {
|
2023-10-30 15:06:04 -05:00
|
|
|
return []string{}
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
return bookmarkedRepos
|
2023-01-09 15:37:44 -05:00
|
|
|
}
|
2023-04-24 13:13:15 -05:00
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
func (dwr *DynamoDB) ToggleBookmarkRepo(ctx context.Context, repo string) (
|
|
|
|
mTypes.ToggleState, error,
|
2023-04-24 13:13:15 -05:00
|
|
|
) {
|
2023-07-18 12:27:26 -05:00
|
|
|
res := mTypes.NotChanged
|
2023-04-24 13:13:15 -05:00
|
|
|
|
2023-09-01 13:13:53 -05:00
|
|
|
userAc, err := reqCtx.UserAcFromContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return mTypes.NotChanged, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if userAc.IsAnonymous() || !userAc.Can(constants.ReadPermission, repo) {
|
|
|
|
return mTypes.NotChanged, zerr.ErrUserDataNotAllowed
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
|
2023-07-07 11:27:10 -05:00
|
|
|
userData, err := dwr.GetUserData(ctx)
|
2023-10-30 15:06:04 -05:00
|
|
|
if err != nil && !errors.Is(err, zerr.ErrUserDataNotFound) {
|
2023-04-24 13:13:15 -05:00
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
2023-07-07 11:27:10 -05:00
|
|
|
if !zcommon.Contains(userData.BookmarkedRepos, repo) {
|
|
|
|
userData.BookmarkedRepos = append(userData.BookmarkedRepos, repo)
|
2023-07-18 12:27:26 -05:00
|
|
|
res = mTypes.Added
|
2023-04-24 13:13:15 -05:00
|
|
|
} else {
|
2023-07-07 11:27:10 -05:00
|
|
|
userData.BookmarkedRepos = zcommon.RemoveFrom(userData.BookmarkedRepos, repo)
|
2023-07-18 12:27:26 -05:00
|
|
|
res = mTypes.Removed
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
if res != mTypes.NotChanged {
|
2023-07-07 11:27:10 -05:00
|
|
|
err = dwr.SetUserData(ctx, userData)
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2023-07-18 12:27:26 -05:00
|
|
|
res = mTypes.NotChanged
|
2023-04-24 13:13:15 -05:00
|
|
|
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
func (dwr *DynamoDB) GetBookmarkedRepos(ctx context.Context) ([]string, error) {
|
2023-07-07 11:27:10 -05:00
|
|
|
userMeta, err := dwr.GetUserData(ctx)
|
|
|
|
if errors.Is(err, zerr.ErrUserDataNotFound) || errors.Is(err, zerr.ErrUserDataNotAllowed) {
|
2023-04-24 13:13:15 -05:00
|
|
|
return []string{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return userMeta.BookmarkedRepos, err
|
|
|
|
}
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
func (dwr *DynamoDB) ToggleStarRepo(ctx context.Context, repo string) (
|
|
|
|
mTypes.ToggleState, error,
|
2023-04-24 13:13:15 -05:00
|
|
|
) {
|
2023-07-18 12:27:26 -05:00
|
|
|
res := mTypes.NotChanged
|
2023-04-24 13:13:15 -05:00
|
|
|
|
2023-09-01 13:13:53 -05:00
|
|
|
userAc, err := reqCtx.UserAcFromContext(ctx)
|
2023-04-24 13:13:15 -05:00
|
|
|
if err != nil {
|
2023-09-01 13:13:53 -05:00
|
|
|
return mTypes.NotChanged, err
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
|
2023-09-01 13:13:53 -05:00
|
|
|
if userAc.IsAnonymous() || !userAc.Can(constants.ReadPermission, repo) {
|
|
|
|
return mTypes.NotChanged, zerr.ErrUserDataNotAllowed
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
|
2023-09-01 13:13:53 -05:00
|
|
|
userid := userAc.GetUsername()
|
2023-04-24 13:13:15 -05:00
|
|
|
|
2023-07-07 11:27:10 -05:00
|
|
|
userData, err := dwr.GetUserData(ctx)
|
2023-04-24 13:13:15 -05:00
|
|
|
if err != nil && !errors.Is(err, zerr.ErrUserDataNotFound) {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !zcommon.Contains(userData.StarredRepos, repo) {
|
|
|
|
userData.StarredRepos = append(userData.StarredRepos, repo)
|
2023-07-18 12:27:26 -05:00
|
|
|
res = mTypes.Added
|
2023-04-24 13:13:15 -05:00
|
|
|
} else {
|
|
|
|
userData.StarredRepos = zcommon.RemoveFrom(userData.StarredRepos, repo)
|
2023-07-18 12:27:26 -05:00
|
|
|
res = mTypes.Removed
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
if res != mTypes.NotChanged {
|
2023-10-30 15:06:04 -05:00
|
|
|
repoMeta, err := dwr.getProtoRepoMeta(ctx, repo) //nolint:contextcheck
|
2023-04-24 13:13:15 -05:00
|
|
|
if err != nil {
|
2023-07-18 12:27:26 -05:00
|
|
|
return mTypes.NotChanged, err
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
switch res {
|
2023-07-18 12:27:26 -05:00
|
|
|
case mTypes.Added:
|
2023-04-24 13:13:15 -05:00
|
|
|
repoMeta.Stars++
|
2023-07-18 12:27:26 -05:00
|
|
|
case mTypes.Removed:
|
2023-04-24 13:13:15 -05:00
|
|
|
repoMeta.Stars--
|
|
|
|
}
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
repoMetaBlob, err := proto.Marshal(repoMeta)
|
|
|
|
if err != nil {
|
|
|
|
return mTypes.NotChanged, err
|
|
|
|
}
|
|
|
|
|
|
|
|
repoAttributeValue, err := attributevalue.Marshal(repoMetaBlob)
|
2023-04-24 13:13:15 -05:00
|
|
|
if err != nil {
|
2023-07-18 12:27:26 -05:00
|
|
|
return mTypes.NotChanged, err
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
userAttributeValue, err := attributevalue.Marshal(userData)
|
|
|
|
if err != nil {
|
2023-07-18 12:27:26 -05:00
|
|
|
return mTypes.NotChanged, err
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = dwr.Client.TransactWriteItems(ctx, &dynamodb.TransactWriteItemsInput{
|
|
|
|
TransactItems: []types.TransactWriteItem{
|
|
|
|
{
|
2023-07-07 11:27:10 -05:00
|
|
|
// Update User Profile
|
2023-04-24 13:13:15 -05:00
|
|
|
Update: &types.Update{
|
|
|
|
ExpressionAttributeNames: map[string]string{
|
2023-07-07 11:27:10 -05:00
|
|
|
"#UP": "UserData",
|
2023-04-24 13:13:15 -05:00
|
|
|
},
|
|
|
|
ExpressionAttributeValues: map[string]types.AttributeValue{
|
|
|
|
":UserData": userAttributeValue,
|
|
|
|
},
|
|
|
|
Key: map[string]types.AttributeValue{
|
2023-11-16 13:39:27 -05:00
|
|
|
"TableKey": &types.AttributeValueMemberS{
|
2023-04-24 13:13:15 -05:00
|
|
|
Value: userid,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TableName: aws.String(dwr.UserDataTablename),
|
2023-07-07 11:27:10 -05:00
|
|
|
UpdateExpression: aws.String("SET #UP = :UserData"),
|
2023-04-24 13:13:15 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Update Repo Meta with updated repo stars
|
|
|
|
Update: &types.Update{
|
|
|
|
ExpressionAttributeNames: map[string]string{
|
2023-11-08 16:37:52 -05:00
|
|
|
"#RM": "RepoMeta",
|
2023-04-24 13:13:15 -05:00
|
|
|
},
|
|
|
|
ExpressionAttributeValues: map[string]types.AttributeValue{
|
2023-11-08 16:37:52 -05:00
|
|
|
":RepoMeta": repoAttributeValue,
|
2023-04-24 13:13:15 -05:00
|
|
|
},
|
|
|
|
Key: map[string]types.AttributeValue{
|
2023-11-16 13:39:27 -05:00
|
|
|
"TableKey": &types.AttributeValueMemberS{
|
2023-04-24 13:13:15 -05:00
|
|
|
Value: repo,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TableName: aws.String(dwr.RepoMetaTablename),
|
2023-11-08 16:37:52 -05:00
|
|
|
UpdateExpression: aws.String("SET #RM = :RepoMeta"),
|
2023-04-24 13:13:15 -05:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-07-18 12:27:26 -05:00
|
|
|
return mTypes.NotChanged, err
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
func (dwr *DynamoDB) GetStarredRepos(ctx context.Context) ([]string, error) {
|
2023-07-07 11:27:10 -05:00
|
|
|
userMeta, err := dwr.GetUserData(ctx)
|
|
|
|
if errors.Is(err, zerr.ErrUserDataNotFound) || errors.Is(err, zerr.ErrUserDataNotAllowed) {
|
2023-04-24 13:13:15 -05:00
|
|
|
return []string{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return userMeta.StarredRepos, err
|
|
|
|
}
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
func (dwr DynamoDB) SetUserGroups(ctx context.Context, groups []string) error {
|
2023-07-07 11:27:10 -05:00
|
|
|
userData, err := dwr.GetUserData(ctx)
|
|
|
|
if err != nil && !errors.Is(err, zerr.ErrUserDataNotFound) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
userData.Groups = append(userData.Groups, groups...)
|
|
|
|
|
|
|
|
return dwr.SetUserData(ctx, userData)
|
|
|
|
}
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
func (dwr DynamoDB) GetUserGroups(ctx context.Context) ([]string, error) {
|
2023-07-07 11:27:10 -05:00
|
|
|
userData, err := dwr.GetUserData(ctx)
|
|
|
|
|
|
|
|
return userData.Groups, err
|
|
|
|
}
|
|
|
|
|
2023-08-29 11:38:38 -05:00
|
|
|
func (dwr *DynamoDB) IsAPIKeyExpired(ctx context.Context, hashedKey string) (bool, error) {
|
|
|
|
userData, err := dwr.GetUserData(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var isExpired bool
|
|
|
|
|
|
|
|
apiKeyDetails := userData.APIKeys[hashedKey]
|
|
|
|
if apiKeyDetails.IsExpired {
|
|
|
|
isExpired = true
|
|
|
|
|
|
|
|
return isExpired, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// if expiresAt is not nil value
|
|
|
|
if !apiKeyDetails.ExpirationDate.Equal(time.Time{}) && time.Now().After(apiKeyDetails.ExpirationDate) {
|
|
|
|
isExpired = true
|
|
|
|
apiKeyDetails.IsExpired = true
|
|
|
|
}
|
|
|
|
|
|
|
|
userData.APIKeys[hashedKey] = apiKeyDetails
|
|
|
|
|
|
|
|
err = dwr.SetUserData(ctx, userData)
|
|
|
|
|
|
|
|
return isExpired, err
|
|
|
|
}
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
func (dwr DynamoDB) UpdateUserAPIKeyLastUsed(ctx context.Context, hashedKey string) error {
|
2023-09-01 13:13:53 -05:00
|
|
|
userAc, err := reqCtx.UserAcFromContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if userAc.IsAnonymous() {
|
|
|
|
return zerr.ErrUserDataNotAllowed
|
|
|
|
}
|
|
|
|
|
2023-07-07 11:27:10 -05:00
|
|
|
userData, err := dwr.GetUserData(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
apiKeyDetails := userData.APIKeys[hashedKey]
|
|
|
|
apiKeyDetails.LastUsed = time.Now()
|
|
|
|
|
|
|
|
userData.APIKeys[hashedKey] = apiKeyDetails
|
|
|
|
|
|
|
|
err = dwr.SetUserData(ctx, userData)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-29 11:38:38 -05:00
|
|
|
func (dwr DynamoDB) GetUserAPIKeys(ctx context.Context) ([]mTypes.APIKeyDetails, error) {
|
|
|
|
apiKeys := make([]mTypes.APIKeyDetails, 0)
|
|
|
|
|
2023-09-01 13:13:53 -05:00
|
|
|
userAc, err := reqCtx.UserAcFromContext(ctx)
|
2023-08-29 11:38:38 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-09-01 13:13:53 -05:00
|
|
|
if userAc.IsAnonymous() {
|
2023-08-29 11:38:38 -05:00
|
|
|
return nil, zerr.ErrUserDataNotAllowed
|
|
|
|
}
|
|
|
|
|
2023-09-01 13:13:53 -05:00
|
|
|
userid := userAc.GetUsername()
|
|
|
|
|
2023-08-29 11:38:38 -05:00
|
|
|
userData, err := dwr.GetUserData(ctx)
|
|
|
|
if err != nil && !errors.Is(err, zerr.ErrUserDataNotFound) {
|
2023-12-08 03:05:02 -05:00
|
|
|
return nil, fmt.Errorf("failed to get userData for identity %s %w", userid, err)
|
2023-08-29 11:38:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for hashedKey, apiKeyDetails := range userData.APIKeys {
|
|
|
|
// if expiresAt is not nil value
|
|
|
|
if !apiKeyDetails.ExpirationDate.Equal(time.Time{}) && time.Now().After(apiKeyDetails.ExpirationDate) {
|
|
|
|
apiKeyDetails.IsExpired = true
|
|
|
|
}
|
|
|
|
|
|
|
|
userData.APIKeys[hashedKey] = apiKeyDetails
|
|
|
|
|
|
|
|
err = dwr.SetUserData(ctx, userData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
apiKeys = append(apiKeys, apiKeyDetails)
|
|
|
|
}
|
|
|
|
|
|
|
|
return apiKeys, nil
|
|
|
|
}
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
func (dwr DynamoDB) AddUserAPIKey(ctx context.Context, hashedKey string, apiKeyDetails *mTypes.APIKeyDetails) error {
|
2023-09-01 13:13:53 -05:00
|
|
|
userAc, err := reqCtx.UserAcFromContext(ctx)
|
2023-04-24 13:13:15 -05:00
|
|
|
if err != nil {
|
2023-07-07 11:27:10 -05:00
|
|
|
return err
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
|
2023-09-01 13:13:53 -05:00
|
|
|
if userAc.IsAnonymous() {
|
2023-07-07 11:27:10 -05:00
|
|
|
return zerr.ErrUserDataNotAllowed
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
|
2023-09-01 13:13:53 -05:00
|
|
|
userid := userAc.GetUsername()
|
|
|
|
|
2023-07-07 11:27:10 -05:00
|
|
|
userData, err := dwr.GetUserData(ctx)
|
|
|
|
if err != nil && !errors.Is(err, zerr.ErrUserDataNotFound) {
|
2023-12-08 03:05:02 -05:00
|
|
|
return fmt.Errorf("failed to get userData for identity %s %w", userid, err)
|
2023-07-07 11:27:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if userData.APIKeys == nil {
|
2023-07-18 12:27:26 -05:00
|
|
|
userData.APIKeys = make(map[string]mTypes.APIKeyDetails)
|
2023-07-07 11:27:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
userData.APIKeys[hashedKey] = *apiKeyDetails
|
|
|
|
|
|
|
|
userAttributeValue, err := attributevalue.Marshal(userData)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = dwr.Client.TransactWriteItems(ctx, &dynamodb.TransactWriteItemsInput{
|
|
|
|
TransactItems: []types.TransactWriteItem{
|
|
|
|
{
|
|
|
|
// Update UserData
|
|
|
|
Update: &types.Update{
|
|
|
|
ExpressionAttributeNames: map[string]string{
|
|
|
|
"#UP": "UserData",
|
|
|
|
},
|
|
|
|
ExpressionAttributeValues: map[string]types.AttributeValue{
|
|
|
|
":UserData": userAttributeValue,
|
|
|
|
},
|
|
|
|
Key: map[string]types.AttributeValue{
|
2023-11-16 13:39:27 -05:00
|
|
|
"TableKey": &types.AttributeValueMemberS{
|
2023-07-07 11:27:10 -05:00
|
|
|
Value: userid,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TableName: aws.String(dwr.UserDataTablename),
|
|
|
|
UpdateExpression: aws.String("SET #UP = :UserData"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Update APIKeyInfo
|
|
|
|
Update: &types.Update{
|
|
|
|
ExpressionAttributeNames: map[string]string{
|
|
|
|
"#EM": "Identity",
|
|
|
|
},
|
|
|
|
ExpressionAttributeValues: map[string]types.AttributeValue{
|
|
|
|
":Identity": &types.AttributeValueMemberS{Value: userid},
|
|
|
|
},
|
|
|
|
Key: map[string]types.AttributeValue{
|
2023-11-16 13:39:27 -05:00
|
|
|
"TableKey": &types.AttributeValueMemberS{
|
2023-07-07 11:27:10 -05:00
|
|
|
Value: hashedKey,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TableName: aws.String(dwr.APIKeyTablename),
|
|
|
|
UpdateExpression: aws.String("SET #EM = :Identity"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
func (dwr DynamoDB) DeleteUserAPIKey(ctx context.Context, keyID string) error {
|
2023-07-07 11:27:10 -05:00
|
|
|
userData, err := dwr.GetUserData(ctx)
|
|
|
|
if err != nil {
|
2023-12-08 03:05:02 -05:00
|
|
|
return fmt.Errorf("failed to get userData %w", err)
|
2023-07-07 11:27:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for hash, apiKeyDetails := range userData.APIKeys {
|
|
|
|
if apiKeyDetails.UUID == keyID {
|
|
|
|
delete(userData.APIKeys, hash)
|
|
|
|
|
|
|
|
_, err = dwr.Client.DeleteItem(ctx, &dynamodb.DeleteItemInput{
|
|
|
|
TableName: aws.String(dwr.APIKeyTablename),
|
|
|
|
Key: map[string]types.AttributeValue{
|
2023-11-16 13:39:27 -05:00
|
|
|
"TableKey": &types.AttributeValueMemberS{Value: hash},
|
2023-07-07 11:27:10 -05:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-12-08 03:05:02 -05:00
|
|
|
return fmt.Errorf("failed to delete userAPIKey entry for hash %s %w", hash, err)
|
2023-07-07 11:27:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
err := dwr.SetUserData(ctx, userData)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
func (dwr DynamoDB) GetUserAPIKeyInfo(hashedKey string) (string, error) {
|
2023-07-07 11:27:10 -05:00
|
|
|
var userid string
|
|
|
|
|
|
|
|
resp, err := dwr.Client.GetItem(context.Background(), &dynamodb.GetItemInput{
|
|
|
|
TableName: aws.String(dwr.APIKeyTablename),
|
2023-04-24 13:13:15 -05:00
|
|
|
Key: map[string]types.AttributeValue{
|
2023-11-16 13:39:27 -05:00
|
|
|
"TableKey": &types.AttributeValueMemberS{Value: hashedKey},
|
2023-04-24 13:13:15 -05:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-07-07 11:27:10 -05:00
|
|
|
return "", err
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Item == nil {
|
2023-07-07 11:27:10 -05:00
|
|
|
return "", zerr.ErrUserAPIKeyNotFound
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
|
2023-07-07 11:27:10 -05:00
|
|
|
err = attributevalue.Unmarshal(resp.Item["Identity"], &userid)
|
2023-04-24 13:13:15 -05:00
|
|
|
if err != nil {
|
2023-07-07 11:27:10 -05:00
|
|
|
return "", err
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
|
2023-07-07 11:27:10 -05:00
|
|
|
return userid, nil
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
func (dwr DynamoDB) GetUserData(ctx context.Context) (mTypes.UserData, error) {
|
|
|
|
var userData mTypes.UserData
|
2023-07-07 11:27:10 -05:00
|
|
|
|
2023-09-01 13:13:53 -05:00
|
|
|
userAc, err := reqCtx.UserAcFromContext(ctx)
|
2023-07-07 11:27:10 -05:00
|
|
|
if err != nil {
|
|
|
|
return userData, err
|
|
|
|
}
|
|
|
|
|
2023-09-01 13:13:53 -05:00
|
|
|
if userAc.IsAnonymous() {
|
2023-07-07 11:27:10 -05:00
|
|
|
return userData, zerr.ErrUserDataNotAllowed
|
|
|
|
}
|
|
|
|
|
2023-09-01 13:13:53 -05:00
|
|
|
userid := userAc.GetUsername()
|
|
|
|
|
2023-07-07 11:27:10 -05:00
|
|
|
resp, err := dwr.Client.GetItem(ctx, &dynamodb.GetItemInput{
|
2023-04-24 13:13:15 -05:00
|
|
|
TableName: aws.String(dwr.UserDataTablename),
|
2023-07-07 11:27:10 -05:00
|
|
|
Key: map[string]types.AttributeValue{
|
2023-11-16 13:39:27 -05:00
|
|
|
"TableKey": &types.AttributeValueMemberS{Value: userid},
|
2023-04-24 13:13:15 -05:00
|
|
|
},
|
|
|
|
})
|
2023-07-07 11:27:10 -05:00
|
|
|
if err != nil {
|
2023-07-18 12:27:26 -05:00
|
|
|
return mTypes.UserData{}, err
|
2023-07-07 11:27:10 -05:00
|
|
|
}
|
2023-04-24 13:13:15 -05:00
|
|
|
|
2023-07-07 11:27:10 -05:00
|
|
|
if resp.Item == nil {
|
2023-07-18 12:27:26 -05:00
|
|
|
return mTypes.UserData{}, zerr.ErrUserDataNotFound
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
|
2023-07-07 11:27:10 -05:00
|
|
|
err = attributevalue.Unmarshal(resp.Item["UserData"], &userData)
|
|
|
|
if err != nil {
|
2023-07-18 12:27:26 -05:00
|
|
|
return mTypes.UserData{}, err
|
2023-07-07 11:27:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return userData, nil
|
2023-04-24 13:13:15 -05:00
|
|
|
}
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
func (dwr DynamoDB) SetUserData(ctx context.Context, userData mTypes.UserData) error {
|
2023-09-01 13:13:53 -05:00
|
|
|
userAc, err := reqCtx.UserAcFromContext(ctx)
|
2023-04-24 13:13:15 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-01 13:13:53 -05:00
|
|
|
if userAc.IsAnonymous() {
|
2023-04-24 13:13:15 -05:00
|
|
|
return zerr.ErrUserDataNotAllowed
|
|
|
|
}
|
|
|
|
|
2023-09-01 13:13:53 -05:00
|
|
|
userid := userAc.GetUsername()
|
|
|
|
|
2023-07-07 11:27:10 -05:00
|
|
|
userAttributeValue, err := attributevalue.Marshal(userData)
|
2023-04-24 13:13:15 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = dwr.Client.UpdateItem(ctx, &dynamodb.UpdateItemInput{
|
|
|
|
ExpressionAttributeNames: map[string]string{
|
2023-07-07 11:27:10 -05:00
|
|
|
"#UP": "UserData",
|
2023-04-24 13:13:15 -05:00
|
|
|
},
|
|
|
|
ExpressionAttributeValues: map[string]types.AttributeValue{
|
|
|
|
":UserData": userAttributeValue,
|
|
|
|
},
|
|
|
|
Key: map[string]types.AttributeValue{
|
2023-11-16 13:39:27 -05:00
|
|
|
"TableKey": &types.AttributeValueMemberS{
|
2023-04-24 13:13:15 -05:00
|
|
|
Value: userid,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TableName: aws.String(dwr.UserDataTablename),
|
2023-07-07 11:27:10 -05:00
|
|
|
UpdateExpression: aws.String("SET #UP = :UserData"),
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-18 12:27:26 -05:00
|
|
|
func (dwr DynamoDB) DeleteUserData(ctx context.Context) error {
|
2023-09-01 13:13:53 -05:00
|
|
|
userAc, err := reqCtx.UserAcFromContext(ctx)
|
2023-07-07 11:27:10 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-01 13:13:53 -05:00
|
|
|
if userAc.IsAnonymous() {
|
2023-07-07 11:27:10 -05:00
|
|
|
return zerr.ErrUserDataNotAllowed
|
|
|
|
}
|
|
|
|
|
2023-09-01 13:13:53 -05:00
|
|
|
userid := userAc.GetUsername()
|
|
|
|
|
2023-07-07 11:27:10 -05:00
|
|
|
_, err = dwr.Client.DeleteItem(ctx, &dynamodb.DeleteItemInput{
|
|
|
|
TableName: aws.String(dwr.UserDataTablename),
|
|
|
|
Key: map[string]types.AttributeValue{
|
2023-11-16 13:39:27 -05:00
|
|
|
"TableKey": &types.AttributeValueMemberS{Value: userid},
|
2023-07-07 11:27:10 -05:00
|
|
|
},
|
2023-04-24 13:13:15 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
2023-10-30 15:06:04 -05:00
|
|
|
|
2024-09-12 10:26:43 -05:00
|
|
|
const AwsS3BatchLimit = 100
|
|
|
|
|
2023-10-30 15:06:04 -05:00
|
|
|
func (dwr *DynamoDB) fetchImageMetaAttributesByDigest(ctx context.Context, digests []string,
|
|
|
|
) ([]map[string]types.AttributeValue, error) {
|
2024-09-12 10:26:43 -05:00
|
|
|
// AWS S3 as a limit (=100) on number of keys that can retrieved in one
|
|
|
|
// request, so break it up
|
|
|
|
batchedResp := []map[string]types.AttributeValue{}
|
|
|
|
|
|
|
|
for start := 0; start < len(digests); {
|
|
|
|
size := min(len(digests)-start, AwsS3BatchLimit)
|
|
|
|
end := start + size
|
|
|
|
|
|
|
|
resp, err := dwr.Client.BatchGetItem(ctx, &dynamodb.BatchGetItemInput{
|
|
|
|
RequestItems: map[string]types.KeysAndAttributes{
|
|
|
|
dwr.ImageMetaTablename: {
|
|
|
|
Keys: getBatchImageKeys(digests[start:end]),
|
|
|
|
},
|
2023-10-30 15:06:04 -05:00
|
|
|
},
|
2024-09-12 10:26:43 -05:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-30 15:06:04 -05:00
|
|
|
|
2024-09-12 10:26:43 -05:00
|
|
|
if len(resp.Responses[dwr.ImageMetaTablename]) != size {
|
|
|
|
return nil, zerr.ErrImageMetaNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
batchedResp = append(batchedResp, resp.Responses[dwr.ImageMetaTablename]...)
|
|
|
|
start = end
|
2023-10-30 15:06:04 -05:00
|
|
|
}
|
|
|
|
|
2024-10-21 11:57:43 -05:00
|
|
|
// Order the responses based on initial digest order
|
|
|
|
// as BatchGetItem does not guarantee the key order is respected
|
|
|
|
orderedResp := []map[string]types.AttributeValue{}
|
|
|
|
respMap := map[string]map[string]types.AttributeValue{}
|
|
|
|
|
|
|
|
for _, item := range batchedResp {
|
|
|
|
var digest string
|
|
|
|
|
|
|
|
err := attributevalue.Unmarshal(item["TableKey"], &digest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
respMap[digest] = item
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, digest := range digests {
|
|
|
|
imageMeta, ok := respMap[digest]
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("%w for digest %s", zerr.ErrImageMetaNotFound, digest)
|
|
|
|
}
|
|
|
|
|
|
|
|
orderedResp = append(orderedResp, imageMeta)
|
|
|
|
}
|
|
|
|
|
|
|
|
return orderedResp, nil
|
2023-10-30 15:06:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func getBatchImageKeys(digests []string) []map[string]types.AttributeValue {
|
|
|
|
result := []map[string]types.AttributeValue{}
|
|
|
|
|
|
|
|
for _, digest := range digests {
|
|
|
|
result = append(result, map[string]types.AttributeValue{
|
2023-11-16 13:39:27 -05:00
|
|
|
"TableKey": &types.AttributeValueMemberS{
|
2023-10-30 15:06:04 -05:00
|
|
|
Value: digest,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dwr *DynamoDB) PatchDB() error {
|
|
|
|
DBVersion, err := dwr.getDBVersion()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("patching dynamo failed, error retrieving database version %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if version.GetVersionIndex(DBVersion) == -1 {
|
|
|
|
return fmt.Errorf("DB has broken format, no version found %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for patchIndex, patch := range dwr.Patches {
|
|
|
|
if patchIndex < version.GetVersionIndex(DBVersion) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
tableNames := map[string]string{
|
|
|
|
"RepoMetaTablename": dwr.RepoMetaTablename,
|
|
|
|
"VersionTablename": dwr.VersionTablename,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := patch(dwr.Client, tableNames)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dwr *DynamoDB) ResetDB() error {
|
|
|
|
err := dwr.ResetTable(dwr.APIKeyTablename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = dwr.ResetTable(dwr.ImageMetaTablename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = dwr.ResetTable(dwr.RepoBlobsTablename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = dwr.ResetTable(dwr.RepoMetaTablename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = dwr.ResetTable(dwr.UserDataTablename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dwr *DynamoDB) ResetTable(tableName string) error {
|
|
|
|
err := dwr.deleteTable(tableName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dwr.createTable(tableName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dwr *DynamoDB) createTable(tableName string) error {
|
|
|
|
_, err := dwr.Client.CreateTable(context.Background(), &dynamodb.CreateTableInput{
|
|
|
|
TableName: aws.String(tableName),
|
|
|
|
AttributeDefinitions: []types.AttributeDefinition{
|
|
|
|
{
|
2023-11-16 13:39:27 -05:00
|
|
|
AttributeName: aws.String("TableKey"),
|
2023-10-30 15:06:04 -05:00
|
|
|
AttributeType: types.ScalarAttributeTypeS,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
KeySchema: []types.KeySchemaElement{
|
|
|
|
{
|
2023-11-16 13:39:27 -05:00
|
|
|
AttributeName: aws.String("TableKey"),
|
2023-10-30 15:06:04 -05:00
|
|
|
KeyType: types.KeyTypeHash,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
BillingMode: types.BillingModePayPerRequest,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil && !strings.Contains(err.Error(), "Table already exists") {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return dwr.waitTableToBeCreated(tableName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dwr *DynamoDB) deleteTable(tableName string) error {
|
|
|
|
_, err := dwr.Client.DeleteTable(context.Background(), &dynamodb.DeleteTableInput{
|
|
|
|
TableName: aws.String(tableName),
|
|
|
|
})
|
|
|
|
|
|
|
|
if temp := new(types.ResourceNotFoundException); errors.As(err, &temp) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return dwr.waitTableToBeDeleted(tableName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dwr *DynamoDB) waitTableToBeCreated(tableName string) error {
|
2024-10-20 04:14:50 -05:00
|
|
|
const maxWaitTime = 120 * time.Second
|
2023-10-30 15:06:04 -05:00
|
|
|
|
|
|
|
waiter := dynamodb.NewTableExistsWaiter(dwr.Client)
|
|
|
|
|
|
|
|
return waiter.Wait(context.Background(), &dynamodb.DescribeTableInput{
|
|
|
|
TableName: &tableName,
|
|
|
|
}, maxWaitTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dwr *DynamoDB) waitTableToBeDeleted(tableName string) error {
|
|
|
|
const maxWaitTime = 20 * time.Second
|
|
|
|
|
|
|
|
waiter := dynamodb.NewTableNotExistsWaiter(dwr.Client)
|
|
|
|
|
|
|
|
return waiter.Wait(context.Background(), &dynamodb.DescribeTableInput{
|
|
|
|
TableName: &tableName,
|
|
|
|
}, maxWaitTime)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dwr *DynamoDB) createVersionTable() error {
|
|
|
|
_, err := dwr.Client.CreateTable(context.Background(), &dynamodb.CreateTableInput{
|
|
|
|
TableName: aws.String(dwr.VersionTablename),
|
|
|
|
AttributeDefinitions: []types.AttributeDefinition{
|
|
|
|
{
|
2023-11-16 13:39:27 -05:00
|
|
|
AttributeName: aws.String("TableKey"),
|
2023-10-30 15:06:04 -05:00
|
|
|
AttributeType: types.ScalarAttributeTypeS,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
KeySchema: []types.KeySchemaElement{
|
|
|
|
{
|
2023-11-16 13:39:27 -05:00
|
|
|
AttributeName: aws.String("TableKey"),
|
2023-10-30 15:06:04 -05:00
|
|
|
KeyType: types.KeyTypeHash,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
BillingMode: types.BillingModePayPerRequest,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), "Table already exists") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = dwr.waitTableToBeCreated(dwr.VersionTablename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
mdAttributeValue, err := attributevalue.Marshal(version.CurrentVersion)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = dwr.Client.UpdateItem(context.TODO(), &dynamodb.UpdateItemInput{
|
|
|
|
ExpressionAttributeNames: map[string]string{
|
|
|
|
"#V": "Version",
|
|
|
|
},
|
|
|
|
ExpressionAttributeValues: map[string]types.AttributeValue{
|
|
|
|
":Version": mdAttributeValue,
|
|
|
|
},
|
|
|
|
Key: map[string]types.AttributeValue{
|
2023-11-16 13:39:27 -05:00
|
|
|
"TableKey": &types.AttributeValueMemberS{
|
2023-10-30 15:06:04 -05:00
|
|
|
Value: version.DBVersionKey,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TableName: aws.String(dwr.VersionTablename),
|
|
|
|
UpdateExpression: aws.String("SET #V = :Version"),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dwr *DynamoDB) getDBVersion() (string, error) {
|
|
|
|
resp, err := dwr.Client.GetItem(context.TODO(), &dynamodb.GetItemInput{
|
|
|
|
TableName: aws.String(dwr.VersionTablename),
|
|
|
|
Key: map[string]types.AttributeValue{
|
2023-11-16 13:39:27 -05:00
|
|
|
"TableKey": &types.AttributeValueMemberS{Value: version.DBVersionKey},
|
2023-10-30 15:06:04 -05:00
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.Item == nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var version string
|
|
|
|
|
|
|
|
err = attributevalue.Unmarshal(resp.Item["Version"], &version)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return version, nil
|
|
|
|
}
|