Merge pull request from GHSA-55r9-5mx9-qq7r

when a client pushes an image zot's inline dedupe
will try to find the blob path corresponding with the blob digest
that it's currently pushed and if it's found in the cache
then zot will make a symbolic link to that cache entry and report
to the client that the blob already exists on the location.

Before this patch authorization was not applied on this process meaning
that a user could copy blobs without having permissions on the source repo.

Added a rule which says that the client should have read permissions on the source repo
before deduping, otherwise just Stat() the blob and return the corresponding status code.

Signed-off-by: Petu Eusebiu <peusebiu@cisco.com>
Co-authored-by: Petu Eusebiu <peusebiu@cisco.com>
This commit is contained in:
Ramkumar Chinchani
2024-07-08 11:35:44 -07:00
committed by GitHub
parent 002ff05f6e
commit aaee0220e4
12 changed files with 492 additions and 10 deletions
+51
View File
@@ -174,6 +174,57 @@ func (d *BoltDBDriver) PutBlob(digest godigest.Digest, path string) error {
return nil
}
func (d *BoltDBDriver) GetAllBlobs(digest godigest.Digest) ([]string, error) {
var blobPath strings.Builder
blobPaths := []string{}
if err := d.db.View(func(tx *bbolt.Tx) error {
root := tx.Bucket([]byte(constants.BlobsCache))
if root == nil {
// this is a serious failure
err := zerr.ErrCacheRootBucket
d.log.Error().Err(err).Msg("failed to access root bucket")
return err
}
bucket := root.Bucket([]byte(digest.String()))
if bucket != nil {
origin := bucket.Bucket([]byte(constants.OriginalBucket))
blobPath.Write(d.getOne(origin))
originBlob := blobPath.String()
blobPaths = append(blobPaths, originBlob)
deduped := bucket.Bucket([]byte(constants.DuplicatesBucket))
if deduped != nil {
cursor := deduped.Cursor()
for k, _ := cursor.First(); k != nil; k, _ = cursor.Next() {
var blobPath strings.Builder
blobPath.Write(k)
duplicateBlob := blobPath.String()
if duplicateBlob != originBlob {
blobPaths = append(blobPaths, duplicateBlob)
}
}
return nil
}
}
return zerr.ErrCacheMiss
}); err != nil {
return nil, err
}
return blobPaths, nil
}
func (d *BoltDBDriver) GetBlob(digest godigest.Digest) (string, error) {
var blobPath strings.Builder
+43
View File
@@ -122,4 +122,47 @@ func TestBoltDBCache(t *testing.T) {
So(err, ShouldNotBeNil)
So(val, ShouldBeEmpty)
})
Convey("Test cache.GetAllBlos()", t, func() {
dir := t.TempDir()
log := log.NewLogger("debug", "")
So(log, ShouldNotBeNil)
_, err := storage.Create("boltdb", "failTypeAssertion", log)
So(err, ShouldNotBeNil)
cacheDriver, _ := storage.Create("boltdb", cache.BoltDBDriverParameters{dir, "cache_test", false}, log)
So(cacheDriver, ShouldNotBeNil)
err = cacheDriver.PutBlob("digest", "first")
So(err, ShouldBeNil)
err = cacheDriver.PutBlob("digest", "second")
So(err, ShouldBeNil)
err = cacheDriver.PutBlob("digest", "third")
So(err, ShouldBeNil)
blobs, err := cacheDriver.GetAllBlobs("digest")
So(err, ShouldBeNil)
So(blobs, ShouldResemble, []string{"first", "second", "third"})
err = cacheDriver.DeleteBlob("digest", "first")
So(err, ShouldBeNil)
blobs, err = cacheDriver.GetAllBlobs("digest")
So(err, ShouldBeNil)
So(blobs, ShouldResemble, []string{"second", "third"})
err = cacheDriver.DeleteBlob("digest", "third")
So(err, ShouldBeNil)
blobs, err = cacheDriver.GetAllBlobs("digest")
So(err, ShouldBeNil)
So(blobs, ShouldResemble, []string{"second"})
})
}
+2
View File
@@ -11,6 +11,8 @@ type Cache interface {
// Retrieves the blob matching provided digest.
GetBlob(digest godigest.Digest) (string, error)
GetAllBlobs(digest godigest.Digest) ([]string, error)
// Uploads blob to cachedb.
PutBlob(digest godigest.Digest, path string) error
+36
View File
@@ -141,6 +141,42 @@ func (d *DynamoDBDriver) GetBlob(digest godigest.Digest) (string, error) {
return out.OriginalBlobPath, nil
}
func (d *DynamoDBDriver) GetAllBlobs(digest godigest.Digest) ([]string, error) {
blobPaths := []string{}
resp, err := d.client.GetItem(context.TODO(), &dynamodb.GetItemInput{
TableName: aws.String(d.tableName),
Key: map[string]types.AttributeValue{
"Digest": &types.AttributeValueMemberS{Value: digest.String()},
},
})
if err != nil {
d.log.Error().Err(err).Str("tableName", d.tableName).Msg("failed to get blob")
return nil, err
}
out := Blob{}
if resp.Item == nil {
d.log.Debug().Err(zerr.ErrCacheMiss).Str("digest", string(digest)).Msg("failed to find blob in cache")
return nil, zerr.ErrCacheMiss
}
_ = attributevalue.UnmarshalMap(resp.Item, &out)
blobPaths = append(blobPaths, out.OriginalBlobPath)
for _, item := range out.DuplicateBlobPath {
if item != out.OriginalBlobPath {
blobPaths = append(blobPaths, item)
}
}
return blobPaths, nil
}
func (d *DynamoDBDriver) PutBlob(digest godigest.Digest, path string) error {
if path == "" {
d.log.Error().Err(zerr.ErrEmptyValue).Str("digest", digest.String()).
+42
View File
@@ -144,6 +144,48 @@ func TestDynamoDB(t *testing.T) {
So(err, ShouldNotBeNil)
So(val, ShouldBeEmpty)
})
Convey("Test dynamoDB", t, func(c C) {
log := log.NewLogger("debug", "")
cacheDriver, err := storage.Create("dynamodb", cache.DynamoDBDriverParameters{
Endpoint: os.Getenv("DYNAMODBMOCK_ENDPOINT"),
TableName: "BlobTable",
Region: "us-east-2",
}, log)
So(cacheDriver, ShouldNotBeNil)
So(err, ShouldBeNil)
err = cacheDriver.PutBlob("digest", "first")
So(err, ShouldBeNil)
err = cacheDriver.PutBlob("digest", "second")
So(err, ShouldBeNil)
err = cacheDriver.PutBlob("digest", "third")
So(err, ShouldBeNil)
blobs, err := cacheDriver.GetAllBlobs("digest")
So(err, ShouldBeNil)
So(blobs, ShouldResemble, []string{"first", "second", "third"})
err = cacheDriver.DeleteBlob("digest", "first")
So(err, ShouldBeNil)
blobs, err = cacheDriver.GetAllBlobs("digest")
So(err, ShouldBeNil)
So(blobs, ShouldResemble, []string{"second", "third"})
err = cacheDriver.DeleteBlob("digest", "third")
So(err, ShouldBeNil)
blobs, err = cacheDriver.GetAllBlobs("digest")
So(err, ShouldBeNil)
So(blobs, ShouldResemble, []string{"second"})
})
}
func TestDynamoDBError(t *testing.T) {