Repository: deislabs/ratify (go)

Issue:
Dynamic plugin should support pulling image with digest
### What happened in your environment?

This issue was reported by a Ratify contributor:

Plugins with source property as a tag works, the same image with digest specified should also work. 

Tag: wabbitnetworks.azurecr.io/test/sample-verifier-plugin:v1
digest: wabbitnetworks.azurecr.io/test/sample-verifier-plugin@sha256:294c517b9432a59f2d829f6d04912833a256560e4128aca86efa0c6b4ba6d44d

### What did you expect to happen?

Current pulls with a digest fails with an error. Digest should be supported as tag is mutable. 

### What version of Kubernetes are you running?

_No response_

### What version of Ratify are you running?

v1.1

### Anything else you would like to add?

_No response_

### Are you willing to submit PRs to contribute to this bug fix?

- [X] Yes, I am willing to implement it.

Pull request description:
fix: dynamic plugin should support pulling image with digest
# Description

## What this PR does / why we need it:

The download logic uses the source object to form the blob reference. However when a digest is used, the reference becomes malformed as the regex that looks for the blob reference would parse out the digest.

When a tag is passed:

{wabbitnetworks.azurecr.io/test/sample-store-plugin:v1 map[]}@sha256:26d320ceaa2d027ea2bceaae36a82e304782bbbbe92c8f0b1102482c596f32f5 (blob reference)

When a digest is passed:

{wabbitnetworks.azurecr.io/test/sample-store-plugin:294c517b9432a59f2d829f6d04912833a256560e4128aca86efa0c6b4ba6d44 map[]}@sha256:26d320ceaa2d027ea2bceaae36a82e304782bbbbe92c8f0b1102482c596f32f5 (blob reference)

@binbin-li  followed up with Oras folks to confirm only the reference is required. This PR updates the code path to ONLY pass the blob to the oras library.

## Which issue(s) this PR fixes *(optional, using `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when the PR gets merged)*:
Fixes #1278 

## Type of change

Please delete options that are not relevant.

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] Helm Chart Change (any edit/addition/update that is necessary for changes merged to the `main` branch)
- [ ] This change requires a documentation update

# How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Please also list any relevant details for your test configuration

Locally tested with dynamic plugin both using tag and digest

# Checklist:

- [ ] Does the affected code have corresponding tests?
- [ ] Are the changes documented, not just with inline documentation, but also with conceptual documentation such as an overview of a new feature, or task-based documentation like a tutorial? Consider if this change should be announced on your project blog.
- [ ] Does this introduce breaking changes that would require an announcement or bumping the major version?
- [ ] Do all new files have appropriate license header?

# Post Merge Requirements
- [ ] MAINTAINERS: manually trigger the "Publish Package" workflow after merging any PR that indicates `Helm Chart Change`


Patch:
diff --git a/pkg/common/plugin/download.go b/pkg/common/plugin/download.go
index ebcaf0c..075931c 100644
--- a/pkg/common/plugin/download.go
+++ b/pkg/common/plugin/download.go
@@ -126,10 +126,13 @@ func DownloadPlugin(source PluginSource, targetPath string) error {
 		return fmt.Errorf("unsupported manifest media type: %s", referenceManifestDescriptor.MediaType)
 	}
 
+	if len(referenceManifest.Blobs) == 0 {
+		return fmt.Errorf("no blobs found in the manifest")
+	}
+
 	// download the first blob to the target path
-	blobReference := fmt.Sprintf("%s@%s", source, referenceManifest.Blobs[0].Digest)
-	logrus.Debugf("Downloading blob %s", blobReference)
-	_, blobReader, err := repository.Blobs().FetchReference(ctx, blobReference)
+	logrus.Debugf("Downloading blob %s", referenceManifest.Blobs[0].Digest.String())
+	_, blobReader, err := repository.Blobs().FetchReference(ctx, referenceManifest.Blobs[0].Digest.String())
 	if err != nil {
 		return err
 	}


Test patch:
diff --git a/pkg/referrerstore/factory/factory_test.go b/pkg/referrerstore/factory/factory_test.go
index 3a447b4..9e46d32 100644
--- a/pkg/referrerstore/factory/factory_test.go
+++ b/pkg/referrerstore/factory/factory_test.go
@@ -95,37 +95,55 @@ func TestCreateStoresFromConfig_PluginStores_ReturnsExpected(t *testing.T) {
 func TestCreateStoresFromConfig_DynamicPluginStores_ReturnsExpected(t *testing.T) {
 	os.Setenv("RATIFY_EXPERIMENTAL_DYNAMIC_PLUGINS", "1")
 	featureflag.InitFeatureFlagsFromEnv()
-	storeConfig := map[string]interface{}{
-		"name": "plugin-store",
-		"source": map[string]interface{}{
-			"artifact": "wabbitnetworks.azurecr.io/test/sample-verifier-plugin:v1",
+	testCases := []struct {
+		name     string
+		artifact string
+	}{
+		{
+			name:     "image specified by tag",
+			artifact: "wabbitnetworks.azurecr.io/test/sample-store-plugin:v1",
+		},
+		{
+			name:     "image specified by digest",
+			artifact: "wabbitnetworks.azurecr.io/test/sample-store-plugin@sha256:96ba9f9636cde32df87d62dcad4e430d055e708b9f173475c5d7468b732d6566",
 		},
 	}
 
-	storesConfig := config.StoresConfig{
-		Stores: []config.StorePluginConfig{storeConfig},
-	}
+	for _, tc := range testCases {
+		t.Run(tc.name, func(t *testing.T) {
+			storeConfig := map[string]interface{}{
+				"name": "plugin-store",
+				"source": map[string]interface{}{
+					"artifact": tc.artifact,
+				},
+			}
 
-	stores, err := CreateStoresFromConfig(storesConfig, "")
+			storesConfig := config.StoresConfig{
+				Stores: []config.StorePluginConfig{storeConfig},
+			}
 
-	if err != nil {
-		t.Fatalf("create stores failed with err %v", err)
-	}
+			stores, err := CreateStoresFromConfig(storesConfig, "")
 
-	if len(stores) != 1 {
-		t.Fatalf("expected to have %d stores, actual count %d", 1, len(stores))
-	}
+			if err != nil {
+				t.Fatalf("create stores failed with err %v", err)
+			}
 
-	if stores[0].Name() != "plugin-store" {
-		t.Fatalf("expected to create plugin store")
-	}
+			if len(stores) != 1 {
+				t.Fatalf("expected to have %d stores, actual count %d", 1, len(stores))
+			}
 
-	if _, ok := stores[0].(*plugin.StorePlugin); !ok {
-		t.Fatalf("type assertion failed expected a plugin store")
-	}
+			if stores[0].Name() != "plugin-store" {
+				t.Fatalf("expected to create plugin store")
+			}
+
+			if _, ok := stores[0].(*plugin.StorePlugin); !ok {
+				t.Fatalf("type assertion failed expected a plugin store")
+			}
 
-	pluginPath := path.Join(stores[0].GetConfig().PluginBinDirs[0], stores[0].Name())
-	if _, err := os.Stat(pluginPath); errors.Is(err, os.ErrNotExist) {
-		t.Fatalf("downloaded plugin not found in path")
+			pluginPath := path.Join(stores[0].GetConfig().PluginBinDirs[0], stores[0].Name())
+			if _, err := os.Stat(pluginPath); errors.Is(err, os.ErrNotExist) {
+				t.Fatalf("downloaded plugin not found in path")
+			}
+		})
 	}
 }