Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Secrets: Error standardization - Secrets API #7691

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
104 changes: 104 additions & 0 deletions pkg/api/errors/secrets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
*
* Copyright 2024 The Dapr Authors
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* /
*/

package errors

import (
"fmt"
"net/http"

"github.com/dapr/kit/errors"

Check failure on line 22 in pkg/api/errors/secrets.go

View workflow job for this annotation

GitHub Actions / lint & proto validation (linux, amd64)

File is not `goimports`-ed with -local github.com/dapr/ (goimports)
"google.golang.org/grpc/codes"
)

type SecretsStoreError struct {
name string
skipResourceInfo bool
}

func SecretsStore(name string) *SecretsStoreError {
return &SecretsStoreError{
name: name,
}
}

func (s *SecretsStoreError) NotConfigured() error {
return s.build(
errors.NewBuilder(
codes.FailedPrecondition,
http.StatusInternalServerError,
fmt.Sprintf("secret store is not configured"),
"ERR_SECRET_STORES_NOT_CONFIGURED",
),
"NOT_CONFIGURED",
)
}

func (s *SecretsStoreError) PermissionDenied(key string) error {
return s.build(
errors.NewBuilder(
codes.PermissionDenied,
http.StatusForbidden,
fmt.Sprintf("access denied by policy to get %q from %q", key, s.name),
"ERR_SECRETS_PERMISSION_DENIED",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"ERR_SECRETS_PERMISSION_DENIED",
"ERR_PERMISSION_DENIED",

We have to keep this as the legacy tag for backwards compatibility

),
"PERMISSION_DENIED",
)
}

func (s *SecretsStoreError) NotFound() error {
return s.build(
errors.NewBuilder(
codes.InvalidArgument,
http.StatusUnauthorized,
fmt.Sprintf("failed finding secret store with key %s", s.name),
"ERR_SECRET_STORE_NOT_FOUND",
),
"NOT_FOUND",
)
}

func (s *SecretsStoreError) GetSecret(key string, error string) error {
return s.build(
errors.NewBuilder(
codes.Internal,
http.StatusInternalServerError,
fmt.Sprintf("failed getting secret with key %s from secret store %s: %s", key, s.name, error),
"ERR_SECRET_GET",
),
"SECRET_GET",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"SECRET_GET",
"GET_SECRET",

Since we prefix the reason with the errors.CodePrefixSecretStore in the build(), it might be worth changing it to SECRET_GET_SECRET - then its roughly api_operation_thing.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense
Should tags be updated accordingly? ERR_SECRET_GET => ERR_GET_SECRET (for other scenarios where applicable also)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"ERR_SECRET_GET" will have to remain as is since its the legacy tag. The new tag (or errCode) can be updated accordingly, as you have correctly already 🙂

)
}

func (s *SecretsStoreError) BulkSecretGet(error string) error {
return s.build(
errors.NewBuilder(
codes.Internal,
http.StatusInternalServerError,
fmt.Sprintf("failed getting secrets from secret store %s: %v", s.name, error),
"ERR_SECRET_GET",
),
"SECRET_GET",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"SECRET_GET",
"GET_BULK_SECRET",

)
}

func (s *SecretsStoreError) build(err *errors.ErrorBuilder, errCode string) error {
if !s.skipResourceInfo {
err = err.WithResourceInfo("secrets", s.name, "", "")
}
return err.
WithErrorInfo(errors.CodePrefixSecretStore+errCode, nil).
Build()
}
19 changes: 12 additions & 7 deletions pkg/api/universal/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"time"

"github.com/dapr/components-contrib/secretstores"
apierrors "github.com/dapr/dapr/pkg/api/errors"
diag "github.com/dapr/dapr/pkg/diagnostics"
"github.com/dapr/dapr/pkg/messages"
runtimev1pb "github.com/dapr/dapr/pkg/proto/runtime/v1"
"github.com/dapr/dapr/pkg/resiliency"
)
Expand All @@ -33,7 +33,9 @@
}

if !a.isSecretAllowed(in.GetStoreName(), in.GetKey()) {
err = messages.ErrSecretPermissionDenied.WithFormat(in.GetKey(), in.GetStoreName())
err := apierrors.

Check failure on line 36 in pkg/api/universal/secrets.go

View workflow job for this annotation

GitHub Actions / lint & proto validation (linux, amd64)

shadow: declaration of "err" shadows declaration at line 30 (govet)
SecretsStore(in.GetStoreName()).
PermissionDenied(in.GetKey())
a.logger.Debug(err)
return response, err
}
Expand All @@ -56,7 +58,7 @@
diag.DefaultComponentMonitoring.SecretInvoked(ctx, in.GetStoreName(), diag.Get, err == nil, elapsed)

if err != nil {
err = messages.ErrSecretGet.WithFormat(req.Name, in.GetStoreName(), err.Error())
err = apierrors.SecretsStore(in.GetStoreName()).GetSecret(req.Name, err.Error())
a.logger.Debug(err)
return response, err
}
Expand Down Expand Up @@ -94,7 +96,7 @@
diag.DefaultComponentMonitoring.SecretInvoked(ctx, in.GetStoreName(), diag.BulkGet, err == nil, elapsed)

if err != nil {
err = messages.ErrBulkSecretGet.WithFormat(in.GetStoreName(), err.Error())
err = apierrors.SecretsStore(in.GetStoreName()).BulkSecretGet(err.Error())
a.logger.Debug(err)
return response, err
}
Expand All @@ -107,7 +109,10 @@
if a.isSecretAllowed(in.GetStoreName(), key) {
filteredSecrets[key] = v
} else {
a.logger.Debugf(messages.ErrSecretPermissionDenied.WithFormat(key, in.GetStoreName()).String())
err = apierrors.
SecretsStore(in.GetStoreName()).
PermissionDenied(key)
a.logger.Debug(err)
}
}

Expand All @@ -125,14 +130,14 @@
// Internal method that checks if the request is for a valid secret store component.
func (a *Universal) secretsValidateRequest(componentName string) (secretstores.SecretStore, error) {
if a.compStore.SecretStoresLen() == 0 {
err := messages.ErrSecretStoreNotConfigured
err := apierrors.SecretsStore(componentName).NotConfigured()
a.logger.Debug(err)
return nil, err
}

component, ok := a.compStore.GetSecretStore(componentName)
if !ok {
err := messages.ErrSecretStoreNotFound.WithFormat(componentName)
err := apierrors.SecretsStore(componentName).NotFound()
a.logger.Debug(err)
return nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/api/universal/secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (

"github.com/dapr/components-contrib/secretstores"
"github.com/dapr/dapr/pkg/config"
"github.com/dapr/dapr/pkg/messages"
runtimev1pb "github.com/dapr/dapr/pkg/proto/runtime/v1"
"github.com/dapr/dapr/pkg/resiliency"
"github.com/dapr/dapr/pkg/runtime/compstore"
Expand All @@ -43,13 +42,13 @@ func TestSecretStoreNotConfigured(t *testing.T) {
t.Run("GetSecret", func(t *testing.T) {
_, err := fakeAPI.GetSecret(context.Background(), &runtimev1pb.GetSecretRequest{})
require.Error(t, err)
require.ErrorIs(t, err, messages.ErrSecretStoreNotConfigured)
require.Contains(t, err.Error(), "secret store is not configured")
})

t.Run("GetBulkSecret", func(t *testing.T) {
_, err := fakeAPI.GetBulkSecret(context.Background(), &runtimev1pb.GetBulkSecretRequest{})
require.Error(t, err)
require.ErrorIs(t, err, messages.ErrSecretStoreNotConfigured)
require.Contains(t, err.Error(), "secret store is not configured")
})
}

Expand Down
7 changes: 0 additions & 7 deletions pkg/messages/predefined.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,6 @@ var (
ErrOutboundHealthNotReady = APIError{"dapr outbound is not ready", "ERR_OUTBOUND_HEALTH_NOT_READY", http.StatusInternalServerError, grpcCodes.Internal}
ErrHealthAppIDNotMatch = APIError{"dapr app-id does not match", "ERR_HEALTH_APPID_NOT_MATCH", http.StatusInternalServerError, grpcCodes.Internal}

// Secrets.
ErrSecretStoreNotConfigured = APIError{"secret store is not configured", "ERR_SECRET_STORES_NOT_CONFIGURED", http.StatusInternalServerError, grpcCodes.FailedPrecondition}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cicoyle looks like typo here btw. ERR_SECRET_STORES_NOT_CONFIGURED => ERR_SECRET_STORE_NOT_CONFIGURED. Fix it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will have to keep the legacy tag as is: ERR_SECRET_STORES_NOT_CONFIGURED, the new tag that gets the api prefixed in the build() can be whatever makes the most sense.

ErrSecretStoreNotFound = APIError{"failed finding secret store with key %s", "ERR_SECRET_STORE_NOT_FOUND", http.StatusUnauthorized, grpcCodes.InvalidArgument}
ErrSecretPermissionDenied = APIError{"access denied by policy to get %q from %q", "ERR_PERMISSION_DENIED", http.StatusForbidden, grpcCodes.PermissionDenied}
ErrSecretGet = APIError{"failed getting secret with key %s from secret store %s: %s", "ERR_SECRET_GET", http.StatusInternalServerError, grpcCodes.Internal}
ErrBulkSecretGet = APIError{"failed getting secrets from secret store %s: %v", "ERR_SECRET_GET", http.StatusInternalServerError, grpcCodes.Internal}

// Crypto.
ErrCryptoProvidersNotConfigured = APIError{"crypto providers not configured", "ERR_CRYPTO_PROVIDERS_NOT_CONFIGURED", http.StatusInternalServerError, grpcCodes.Internal}
ErrCryptoProviderNotFound = APIError{"crypto provider %s not found", "ERR_CRYPTO_PROVIDER_NOT_FOUND", http.StatusBadRequest, grpcCodes.InvalidArgument}
Expand Down