Skip to content

Commit

Permalink
chore: address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
binbin-li committed Sep 2, 2024
1 parent eb1148c commit 65617a1
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 37 deletions.
7 changes: 4 additions & 3 deletions pkg/controllers/clusterresource/verifier_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,13 @@ func (r *VerifierReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
return ctrl.Result{}, nil
}

// creates a verifier reference from CRD spec and add store to map
// creates a verifier reference from CR spec and add verifier to map
func verifierAddOrReplace(spec configv1beta1.VerifierSpec, objectName string) error {
verifierConfig, err := cutils.SpecToVerifierConfig(spec.Parameters.Raw, objectName, spec.Name, spec.ArtifactTypes, spec.Source)
if err != nil {
logrus.Error(err, "unable to convert crd specification to verifier config")
return re.ErrorCodeConfigInvalid.WithDetail("Unable to convert crd specification to verifier config").WithError(err)
errMsg := fmt.Sprintf("Unable to apply cluster-wide resource %s of Verifier kind", objectName)
logrus.Error(err, errMsg)
return re.ErrorCodeConfigInvalid.WithDetail(errMsg).WithError(err)
}

return cutils.UpsertVerifier(spec.Version, spec.Address, constants.EmptyNamespace, objectName, verifierConfig)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestVerifierAdd_WithParameters(t *testing.T) {
func TestVerifierAddOrReplace_PluginNotFound(t *testing.T) {
resetVerifierMap()
resource := "invalidplugin"
expectedMsg := "PLUGIN_NOT_FOUND: Plugin: pluginnotfound not found: failed to find plugin \"pluginnotfound\" in paths [test/path]"
expectedMsg := "PLUGIN_NOT_FOUND: Verifier plugin pluginnotfound not found: failed to find plugin \"pluginnotfound\" in paths [test/path]: Please check if the correct built-in verifier is specified or if required custom verifier plugin is available."
var testVerifierSpec = getInvalidVerifierSpec()
err := verifierAddOrReplace(testVerifierSpec, resource)

Expand Down
5 changes: 3 additions & 2 deletions pkg/controllers/namespaceresource/verifier_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ func (r *VerifierReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
func verifierAddOrReplace(spec configv1beta1.NamespacedVerifierSpec, objectName string, namespace string) error {
verifierConfig, err := cutils.SpecToVerifierConfig(spec.Parameters.Raw, objectName, spec.Name, spec.ArtifactTypes, spec.Source)
if err != nil {
logrus.Error(err, "unable to convert crd specification to verifier config")
return re.ErrorCodeConfigInvalid.WithDetail("Unable to convert crd specification to verifier config").WithError(err)
errMsg := fmt.Sprintf("Unable to apply the resource %s of NamespacedVerifier kind in the namespace %s", objectName, namespace)
logrus.Error(err, errMsg)
return re.ErrorCodeConfigInvalid.WithDetail(errMsg).WithError(err)
}

return cutils.UpsertVerifier(spec.Version, spec.Address, namespace, objectName, verifierConfig)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func TestVerifierAdd_WithParameters(t *testing.T) {
func TestVerifierAddOrReplace_PluginNotFound(t *testing.T) {
resetVerifierMap()
resource := "invalidplugin"
expectedMsg := "PLUGIN_NOT_FOUND: Plugin: pluginnotfound not found: failed to find plugin \"pluginnotfound\" in paths [test/path]"
expectedMsg := "PLUGIN_NOT_FOUND: Verifier plugin pluginnotfound not found: failed to find plugin \"pluginnotfound\" in paths [test/path]: Please check if the correct built-in verifier is specified or if required custom verifier plugin is available."
var testVerifierSpec = getInvalidVerifierSpec()
err := verifierAddOrReplace(testVerifierSpec, resource, testNamespace)

Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/utils/verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ func SpecToVerifierConfig(raw []byte, verifierName, verifierType, artifactTypes

if string(raw) != "" {
if err := json.Unmarshal(raw, &verifierConfig); err != nil {
errMsg := fmt.Sprintf("Unable to decode verifier parameters, Parameters.Raw: %s", string(raw))
errMsg := fmt.Sprintf("Unable to recognize the parameters of the Verifier resource %s", string(raw))
logrus.Error(err, errMsg)
return vc.VerifierConfig{}, re.ErrorCodeConfigInvalid.WithError(err).WithDetail(errMsg).WithRemediation("Please revise verifier parameters and try again. Refer to verifier configuration: https://ratify.dev/docs/reference/custom%20resources/verifiers")
return vc.VerifierConfig{}, re.ErrorCodeConfigInvalid.WithError(err).WithDetail(errMsg).WithRemediation("Please update the Verifier parameters and try again. Refer to the Verifier configuration guide: https://ratify.dev/docs/reference/custom%20resources/verifiers")
}
}
verifierConfig[types.Name] = verifierName
Expand Down
4 changes: 2 additions & 2 deletions pkg/executor/core/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type Executor struct {
// VerifySubject verifies the subject and returns results.
func (executor Executor) VerifySubject(ctx context.Context, verifyParameters e.VerifyParameters) (types.VerifyResult, error) {
if executor.PolicyEnforcer == nil {
return types.VerifyResult{}, errors.ErrorCodePolicyProviderNotFound.WithDetail("Policy Provider not found")
return types.VerifyResult{}, errors.ErrorCodePolicyProviderNotFound.WithDetail("Policy configuration not found")
}
result, err := executor.verifySubjectInternal(ctx, verifyParameters)
if err != nil {
Expand All @@ -83,7 +83,7 @@ func (executor Executor) verifySubjectInternal(ctx context.Context, verifyParame
}
if executor.PolicyEnforcer.GetPolicyType(ctx) == pt.ConfigPolicy {
if len(verifierReports) == 0 {
return types.VerifyResult{}, errors.ErrorCodeNoVerifierReport.WithDetail("No verifier report generated")
return types.VerifyResult{}, errors.ErrorCodeNoVerifierReport.WithDetail(fmt.Sprintf("No verification results for the artifact %s. Ensure verifiers are properly configured and that artifact metadata is attached", verifyParameters.Subject))
}
}
// If it requires embedded Rego Policy Engine make the decision, execute
Expand Down
8 changes: 4 additions & 4 deletions pkg/keymanagementprovider/keymanagementprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,15 @@ func setCertificatesInMap(resource string, certs map[KMPMapKey][]*x509.Certifica
// GetCertificatesFromMap gets the certificates from the map and returns an empty map of certificate arrays if not found or an error happened.
func GetCertificatesFromMap(ctx context.Context, resource string) (map[KMPMapKey][]*x509.Certificate, error) {
if !hasAccessToProvider(ctx, resource) {
return map[KMPMapKey][]*x509.Certificate{}, errors.ErrorCodeForbidden.WithDetail(fmt.Sprintf("namespace [%s] does not have access to key management provider [%s]", ctxUtils.GetNamespace(ctx), resource)).WithRemediation(fmt.Sprintf("Ensure the key management provider: %s is created under namespace: [%s] or as a cluster-wide resource.", resource, ctxUtils.GetNamespace(ctx)))
return map[KMPMapKey][]*x509.Certificate{}, errors.ErrorCodeForbidden.WithDetail(fmt.Sprintf("Resources in namespace [%s] do not have access to key management provider [%s]", ctxUtils.GetNamespace(ctx), resource)).WithRemediation(fmt.Sprintf("Make sure the key management provider: %s is created in the namespace: [%s] or as a cluster-wide resource.", resource, ctxUtils.GetNamespace(ctx)))
}
if err, ok := certificateErrMap.Load(resource); ok && err != nil {
return map[KMPMapKey][]*x509.Certificate{}, err.(error)
}
if certs, ok := certificatesMap.Load(resource); ok {
return certs.(map[KMPMapKey][]*x509.Certificate), nil
}
return map[KMPMapKey][]*x509.Certificate{}, errors.ErrorCodeNotFound.WithDetail(fmt.Sprintf("failed to access non-existent key management provider [%s]", resource)).WithRemediation(fmt.Sprintf("Ensure the key management provider: %s is created under namespace: [%s] or as a cluster-wide resource.", resource, ctxUtils.GetNamespace(ctx)))
return map[KMPMapKey][]*x509.Certificate{}, errors.ErrorCodeNotFound.WithDetail(fmt.Sprintf("The key management provider [%s] does not exist", resource)).WithRemediation(fmt.Sprintf("Make sure the key management provider: %s is created in the namespace: [%s] or as a cluster-wide resource.", resource, ctxUtils.GetNamespace(ctx)))
}

// DeleteResourceFromMap deletes the certificates, keys and errors from the map
Expand Down Expand Up @@ -186,15 +186,15 @@ func GetKeysFromMap(ctx context.Context, resource string) (map[KMPMapKey]PublicK
// A cluster-wide operation can access cluster-wide provider
// A namespaced operation can only fetch the provider in the same namespace or cluster-wide provider.
if !hasAccessToProvider(ctx, resource) {
return map[KMPMapKey]PublicKey{}, errors.ErrorCodeForbidden.WithDetail(fmt.Sprintf("namespace [%s] does not have access to key management provider [%s]", ctxUtils.GetNamespace(ctx), resource)).WithRemediation(fmt.Sprintf("Ensure the key management provider: %s is created under namespace: [%s] or as a cluster-wide resource.", resource, ctxUtils.GetNamespace(ctx)))
return map[KMPMapKey]PublicKey{}, errors.ErrorCodeForbidden.WithDetail(fmt.Sprintf("The resources in namespace [%s] do not have access to key management provider [%s]", ctxUtils.GetNamespace(ctx), resource)).WithRemediation(fmt.Sprintf("Make sure the key management provider [%s] is created in the namespace [%s] or as a cluster-wide resource.", resource, ctxUtils.GetNamespace(ctx)))
}
if err, ok := keyErrMap.Load(resource); ok && err != nil {
return map[KMPMapKey]PublicKey{}, err.(error)
}
if keys, ok := keyMap.Load(resource); ok {
return keys.(map[KMPMapKey]PublicKey), nil
}
return map[KMPMapKey]PublicKey{}, errors.ErrorCodeNotFound.WithDetail(fmt.Sprintf("failed to access non-existent key management provider [%s]", resource)).WithRemediation(fmt.Sprintf("Ensure the key management provider: %s is created under namespace: [%s] or as a cluster-wide resource.", resource, ctxUtils.GetNamespace(ctx)))
return map[KMPMapKey]PublicKey{}, errors.ErrorCodeNotFound.WithDetail(fmt.Sprintf("The key management provider [%s] does not exist", resource)).WithRemediation(fmt.Sprintf("Make sure the key management provider: %s is created in the namespace: [%s] or as a cluster-wide resource.", resource, ctxUtils.GetNamespace(ctx)))
}

// SetCertificateError sets the error while fetching certificates from key management provider.
Expand Down
14 changes: 7 additions & 7 deletions pkg/referrerstore/oras/oras.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func (store *orasStore) GetBlobContent(ctx context.Context, subjectReference com
func (store *orasStore) GetReferenceManifest(ctx context.Context, subjectReference common.Reference, referenceDesc ocispecs.ReferenceDescriptor) (ocispecs.ReferenceManifest, error) {
repository, err := store.createRepository(ctx, store, subjectReference)
if err != nil {
return ocispecs.ReferenceManifest{}, re.ErrorCodeCreateRepositoryFailure.WithDetail("Failed to create client to remote registry").WithError(err)
return ocispecs.ReferenceManifest{}, re.ErrorCodeRepositoryOperationFailure.WithDetail("Failed to connect to the remote registry").WithError(err)
}
var manifestBytes []byte
// check if manifest exists in local ORAS cache
Expand All @@ -336,12 +336,12 @@ func (store *orasStore) GetReferenceManifest(ctx context.Context, subjectReferen
manifestReader, err := repository.Fetch(ctx, referenceDesc.Descriptor)
if err != nil {
evictOnError(ctx, err, subjectReference.Original)
return ocispecs.ReferenceManifest{}, re.ErrorCodeRepositoryOperationFailure.WithDetail("Failed to fetch manifest content from repository").WithError(err)
return ocispecs.ReferenceManifest{}, re.ErrorCodeRepositoryOperationFailure.WithDetail("Failed to fetch the artifact metadata from the registry").WithError(err)
}

manifestBytes, err = io.ReadAll(manifestReader)
if err != nil {
return ocispecs.ReferenceManifest{}, re.ErrorCodeManifestInvalid.WithDetail("Failed to parse fetched manifest").WithError(err)
return ocispecs.ReferenceManifest{}, re.ErrorCodeManifestInvalid.WithDetail("Failed to parse the artifact metadata").WithError(err)
}

// push fetched manifest to local ORAS cache
Expand All @@ -360,15 +360,15 @@ func (store *orasStore) GetReferenceManifest(ctx context.Context, subjectReferen
if referenceDesc.Descriptor.MediaType == oci.MediaTypeImageManifest {
var imageManifest oci.Manifest
if err := json.Unmarshal(manifestBytes, &imageManifest); err != nil {
return ocispecs.ReferenceManifest{}, re.ErrorCodeDataDecodingFailure.WithDetail("Failed to parse manifest content in `application/vnd.oci.image.manifest.v1+json` mediatype").WithError(err).WithRemediation("Please check if the manifest was created correctly.")
return ocispecs.ReferenceManifest{}, re.ErrorCodeDataDecodingFailure.WithDetail("Failed to parse artifact metadata of mediatype `application/vnd.oci.image.manifest.v1+json`").WithError(err).WithRemediation("Please check if the artifact metadata was created correctly.")
}
referenceManifest = commonutils.OciManifestToReferenceManifest(imageManifest)
} else if referenceDesc.Descriptor.MediaType == ocispecs.MediaTypeArtifactManifest {
if err := json.Unmarshal(manifestBytes, &referenceManifest); err != nil {
return ocispecs.ReferenceManifest{}, re.ErrorCodeDataDecodingFailure.WithDetail("Failed to parse manifest content in `application/vnd.oci.artifact.manifest.v1+json` mediatype").WithError(err).WithRemediation("Please check if the manifest was created correctly.")
return ocispecs.ReferenceManifest{}, re.ErrorCodeDataDecodingFailure.WithDetail("Failed to parse artifact metadata of mediatype `application/vnd.oci.artifact.manifest.v1+json`").WithError(err).WithRemediation("Please check if the artifact metadata was created correctly.")
}
} else {
return ocispecs.ReferenceManifest{}, re.ErrorCodeGetReferenceManifestFailure.WithDetail(fmt.Sprintf("Unsupported manifest media type: %s", referenceDesc.Descriptor.MediaType)).WithRemediation("Please check if the manifest was created correctly.")
return ocispecs.ReferenceManifest{}, re.ErrorCodeGetReferenceManifestFailure.WithDetail(fmt.Sprintf("Unsupported artifact metadata of media type %s", referenceDesc.Descriptor.MediaType)).WithRemediation("Please check if the artifact metadata was created correctly.")
}

return referenceManifest, nil
Expand All @@ -377,7 +377,7 @@ func (store *orasStore) GetReferenceManifest(ctx context.Context, subjectReferen
func (store *orasStore) GetSubjectDescriptor(ctx context.Context, subjectReference common.Reference) (*ocispecs.SubjectDescriptor, error) {
repository, err := store.createRepository(ctx, store, subjectReference)
if err != nil {
return nil, re.ErrorCodeCreateRepositoryFailure.WithDetail("Failed to create client to remote registry").WithError(err)
return nil, re.ErrorCodeRepositoryOperationFailure.WithDetail("Failed to connect to remote registry").WithError(err)
}

desc, err := repository.Resolve(ctx, subjectReference.Original)
Expand Down
8 changes: 4 additions & 4 deletions pkg/verifier/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ func CreateVerifierFromConfig(verifierConfig config.VerifierConfig, configVersio
if value, ok := verifierConfig[types.Name]; ok {
verifierTypeStr = value.(string)
} else {
return nil, re.ErrorCodeConfigInvalid.WithDetail("Name field is required in verifier config")
return nil, re.ErrorCodeConfigInvalid.WithDetail(fmt.Sprintf("The name field is required in the Verifier configuration: %+v", verifierConfig))
}

if value, ok := verifierConfig[types.Type]; ok {
verifierTypeStr = value.(string)
}

if strings.ContainsRune(verifierTypeStr, os.PathSeparator) {
return nil, re.ErrorCodeConfigInvalid.WithDetail(fmt.Sprintf("Invalid verifier plugin name [%s], [%v] is disallowed", verifierTypeStr, os.PathSeparator))
return nil, re.ErrorCodeConfigInvalid.WithDetail(fmt.Sprintf("Invalid name [%s] in the Verifier configuration, [%v] is disallowed", verifierTypeStr, os.PathSeparator))
}

// if source is specified, download the plugin
Expand Down Expand Up @@ -94,7 +94,7 @@ func CreateVerifierFromConfig(verifierConfig config.VerifierConfig, configVersio
}

if _, err := pluginCommon.FindInPaths(verifierTypeStr, pluginBinDir); err != nil {
return nil, re.ErrorCodePluginNotFound.WithDetail(fmt.Sprintf("Plugin: %s not found", verifierTypeStr)).WithError(err)
return nil, re.ErrorCodePluginNotFound.WithDetail(fmt.Sprintf("Verifier plugin %s not found", verifierTypeStr)).WithError(err).WithRemediation("Please check if the correct built-in verifier is specified or if required custom verifier plugin is available.")
}

pluginVersion := configVersion
Expand All @@ -117,7 +117,7 @@ func CreateVerifiersFromConfig(verifiersConfig config.VerifiersConfig, defaultPl
}

if len(verifiersConfig.Verifiers) == 0 {
return nil, re.ErrorCodeConfigInvalid.WithDetail("Verifiers config should have at least one verifier")
return nil, re.ErrorCodeConfigInvalid.WithDetail("At least one verifier must be specified in the Verifiers configuration")
}

verifiers := make([]verifier.ReferenceVerifier, 0)
Expand Down
6 changes: 3 additions & 3 deletions pkg/verifier/notation/certstoresbytype.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,18 @@ func newCertStoreByType(confInNewFormat verificationCertStores) (certStores, err
s[certStoreType(certstoretype)] = make(map[string][]string)
parsedStoreData, ok := storeData.(verificationCertStores)
if !ok {
return nil, fmt.Errorf("configuration is unsupported: %+v", storeData)
return nil, fmt.Errorf("provided verificationCertStores configuration is invalid: %+v", confInNewFormat)
}
for storeName, certProviderList := range parsedStoreData {
var certProviderNames []string
parsedCertProviders, ok := certProviderList.([]interface{})
if !ok {
return nil, fmt.Errorf("configuration is unsupported: %+v", certProviderList)
return nil, fmt.Errorf("provided verificationCertStores configuration is invalid: %+v", confInNewFormat)
}
for _, certProvider := range parsedCertProviders {
certProviderName, ok := certProvider.(string)
if !ok {
return nil, fmt.Errorf("configuration is unsupported: %+v", certProvider)
return nil, fmt.Errorf("provided verificationCertStores configuration is invalid: %+v", confInNewFormat)
}
certProviderNames = append(certProviderNames, certProviderName)
}
Expand Down
Loading

0 comments on commit 65617a1

Please sign in to comment.