From 75653f55ab7e797e0964392641daee87d902b064 Mon Sep 17 00:00:00 2001 From: Eugen Sumin Date: Fri, 4 Oct 2024 17:30:58 +0200 Subject: [PATCH] Migrate pkg/secrets to errkit --- pkg/secrets/aws.go | 8 ++++---- pkg/secrets/azure.go | 8 ++++---- pkg/secrets/gcp.go | 12 ++++++------ pkg/secrets/gcp_test.go | 12 ++++++------ pkg/secrets/repositoryserver/aws_secrets.go | 10 +++++----- pkg/secrets/repositoryserver/aws_secrets_test.go | 10 +++++----- pkg/secrets/repositoryserver/azure_secrets.go | 8 ++++---- pkg/secrets/repositoryserver/azure_secrets_test.go | 8 ++++---- pkg/secrets/repositoryserver/gcp_secrets.go | 8 ++++---- pkg/secrets/repositoryserver/gcp_secrets_test.go | 8 ++++---- .../repositoryserver/repository_password.go | 10 +++++----- .../repositoryserver/repository_password_test.go | 10 +++++----- .../repository_server_admin_credentials.go | 12 ++++++------ .../repository_server_admin_credentials_test.go | 12 ++++++------ .../repository_server_useraccess_credentials.go | 6 +++--- .../repositoryserver/s3compliant_secrets.go | 12 ++++++------ .../repositoryserver/s3compliant_secrets_test.go | 12 ++++++------ pkg/secrets/secrets.go | 14 ++++++++------ pkg/secrets/secrets_test.go | 6 +++--- 19 files changed, 94 insertions(+), 92 deletions(-) diff --git a/pkg/secrets/aws.go b/pkg/secrets/aws.go index 9692c0c25b..768dcb2511 100644 --- a/pkg/secrets/aws.go +++ b/pkg/secrets/aws.go @@ -19,7 +19,7 @@ import ( "time" "github.com/aws/aws-sdk-go/aws/credentials" - "github.com/pkg/errors" + "github.com/kanisterio/errkit" corev1 "k8s.io/api/core/v1" "github.com/kanisterio/kanister/pkg/aws" @@ -55,7 +55,7 @@ const ( // - session_token func ValidateAWSCredentials(secret *corev1.Secret) error { if string(secret.Type) != AWSSecretType { - return errors.Wrapf(secerrors.ErrValidate, secerrors.IncompatibleSecretTypeErrorMsg, AWSSecretType, secret.Namespace, secret.Name) + return errkit.Wrap(secerrors.ErrValidate, secerrors.IncompatibleSecretTypeErrorMsg, AWSSecretType, secret.Namespace, secret.Name) } count := 0 if _, ok := secret.Data[AWSAccessKeyID]; ok { @@ -68,7 +68,7 @@ func ValidateAWSCredentials(secret *corev1.Secret) error { count++ } if len(secret.Data) > count { - return errors.New("Secret has an unknown field") + return errkit.New("Secret has an unknown field") } return nil } @@ -107,7 +107,7 @@ func ExtractAWSCredentials(ctx context.Context, secret *corev1.Secret, assumeRol } val, err := creds.Get() if err != nil { - return nil, errors.Wrap(err, "Failed to get AWS credentials") + return nil, errkit.Wrap(err, "Failed to get AWS credentials") } exp, err := creds.ExpiresAt() if err == nil { diff --git a/pkg/secrets/azure.go b/pkg/secrets/azure.go index f03a0dddce..29e743a996 100644 --- a/pkg/secrets/azure.go +++ b/pkg/secrets/azure.go @@ -15,7 +15,7 @@ package secrets import ( - "github.com/pkg/errors" + "github.com/kanisterio/errkit" corev1 "k8s.io/api/core/v1" "github.com/kanisterio/kanister/pkg/objectstore" @@ -46,7 +46,7 @@ const ( // - azure_storage_environment func ValidateAzureCredentials(secret *corev1.Secret) error { if string(secret.Type) != AzureSecretType { - return errors.Wrapf(secerrors.ErrValidate, secerrors.IncompatibleSecretTypeErrorMsg, AzureSecretType, secret.Namespace, secret.Name) + return errkit.Wrap(secerrors.ErrValidate, secerrors.IncompatibleSecretTypeErrorMsg, AzureSecretType, secret.Namespace, secret.Name) } count := 0 if _, ok := secret.Data[AzureStorageAccountID]; ok { @@ -59,7 +59,7 @@ func ValidateAzureCredentials(secret *corev1.Secret) error { count++ } if len(secret.Data) > count { - return errors.New("Secret has an unknown field") + return errkit.New("Secret has an unknown field") } return nil } @@ -88,7 +88,7 @@ func ExtractAzureCredentials(secret *corev1.Secret) (*objectstore.SecretAzure, e azSecret.EnvironmentName = string(envName) } if azSecret.StorageAccount == "" || azSecret.StorageKey == "" { - return nil, errors.New("Azure secret is missing storage account ID or storage key") + return nil, errkit.New("Azure secret is missing storage account ID or storage key") } return azSecret, nil } diff --git a/pkg/secrets/gcp.go b/pkg/secrets/gcp.go index a074c9f90e..5d53fde61c 100644 --- a/pkg/secrets/gcp.go +++ b/pkg/secrets/gcp.go @@ -15,7 +15,7 @@ package secrets import ( - "github.com/pkg/errors" + "github.com/kanisterio/errkit" corev1 "k8s.io/api/core/v1" secerrors "github.com/kanisterio/kanister/pkg/secrets/errors" @@ -40,19 +40,19 @@ func ValidateGCPCredentials(secret *corev1.Secret) error { // - GCPProjectID // - GCPServiceAccountJSONKey if secret == nil { - return errors.Wrapf(secerrors.ErrValidate, secerrors.NilSecretErrorMessage) + return errkit.Wrap(secerrors.ErrValidate, secerrors.NilSecretErrorMessage) } if string(secret.Type) != GCPSecretType { - return errors.Wrapf(secerrors.ErrValidate, secerrors.IncompatibleSecretTypeErrorMsg, GCPSecretType, secret.Namespace, secret.Name) + return errkit.Wrap(secerrors.ErrValidate, secerrors.IncompatibleSecretTypeErrorMsg, GCPSecretType, secret.Namespace, secret.Name) } if len(secret.Data) == 0 { - return errors.Wrapf(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, secret.Namespace, secret.Name) + return errkit.Wrap(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, secret.Namespace, secret.Name) } if _, ok := secret.Data[GCPProjectID]; !ok { - return errors.Wrapf(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, GCPProjectID, secret.Namespace, secret.Name) + return errkit.Wrap(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, GCPProjectID, secret.Namespace, secret.Name) } if _, ok := secret.Data[GCPServiceAccountJSONKey]; !ok { - return errors.Wrapf(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, GCPServiceAccountJSONKey, secret.Namespace, secret.Name) + return errkit.Wrap(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, GCPServiceAccountJSONKey, secret.Namespace, secret.Name) } return nil } diff --git a/pkg/secrets/gcp_test.go b/pkg/secrets/gcp_test.go index 1fe66bc9d4..48df6bf9c0 100644 --- a/pkg/secrets/gcp_test.go +++ b/pkg/secrets/gcp_test.go @@ -17,7 +17,7 @@ package secrets import ( "encoding/base64" - "github.com/pkg/errors" + "github.com/kanisterio/errkit" "gopkg.in/check.v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -65,7 +65,7 @@ func (s *GCPSecretSuite) TestValidateGCPCredentials(c *check.C) { }, }, errChecker: check.NotNil, - expectedErr: errors.Wrapf(secerrors.ErrValidate, secerrors.IncompatibleSecretTypeErrorMsg, GCPSecretType, "ns", "sec"), + expectedErr: errkit.Wrap(secerrors.ErrValidate, secerrors.IncompatibleSecretTypeErrorMsg, GCPSecretType, "ns", "sec"), }, { // missing field - GCPServiceKey secret: &corev1.Secret{ @@ -78,7 +78,7 @@ func (s *GCPSecretSuite) TestValidateGCPCredentials(c *check.C) { GCPProjectID: []byte("key_id"), }, }, - expectedErr: errors.Wrapf(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, GCPServiceAccountJSONKey, "ns", "sec"), + expectedErr: errkit.Wrap(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, GCPServiceAccountJSONKey, "ns", "sec"), errChecker: check.NotNil, }, { // missing field - GCPProjectID @@ -92,7 +92,7 @@ func (s *GCPSecretSuite) TestValidateGCPCredentials(c *check.C) { GCPServiceAccountJSONKey: []byte("service_account_json"), }, }, - expectedErr: errors.Wrapf(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, GCPProjectID, "ns", "sec"), + expectedErr: errkit.Wrap(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, GCPProjectID, "ns", "sec"), errChecker: check.NotNil, }, { // secret is Empty @@ -103,12 +103,12 @@ func (s *GCPSecretSuite) TestValidateGCPCredentials(c *check.C) { Namespace: "ns", }, }, - expectedErr: errors.Wrapf(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, "ns", "sec"), + expectedErr: errkit.Wrap(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, "ns", "sec"), errChecker: check.NotNil, }, { // secret is nil secret: nil, - expectedErr: errors.Wrapf(secerrors.ErrValidate, secerrors.NilSecretErrorMessage), + expectedErr: errkit.Wrap(secerrors.ErrValidate, secerrors.NilSecretErrorMessage), errChecker: check.NotNil, }, } { diff --git a/pkg/secrets/repositoryserver/aws_secrets.go b/pkg/secrets/repositoryserver/aws_secrets.go index a835224689..3beb7f5da1 100644 --- a/pkg/secrets/repositoryserver/aws_secrets.go +++ b/pkg/secrets/repositoryserver/aws_secrets.go @@ -16,7 +16,7 @@ package repositoryserver import ( - "github.com/pkg/errors" + "github.com/kanisterio/errkit" corev1 "k8s.io/api/core/v1" secerrors "github.com/kanisterio/kanister/pkg/secrets/errors" @@ -36,16 +36,16 @@ func NewAWSLocation(secret *corev1.Secret) *aws { func (l *aws) Validate() (err error) { if l.storageLocation == nil { - return errors.Wrapf(secerrors.ErrValidate, secerrors.NilSecretErrorMessage) + return errkit.Wrap(secerrors.ErrValidate, secerrors.NilSecretErrorMessage) } if len(l.storageLocation.Data) == 0 { - return errors.Wrapf(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, l.storageLocation.Namespace, l.storageLocation.Name) + return errkit.Wrap(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, l.storageLocation.Namespace, l.storageLocation.Name) } if _, ok := l.storageLocation.Data[BucketKey]; !ok { - return errors.Wrapf(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, BucketKey, l.storageLocation.Namespace, l.storageLocation.Name) + return errkit.Wrap(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, BucketKey, l.storageLocation.Namespace, l.storageLocation.Name) } if _, ok := l.storageLocation.Data[RegionKey]; !ok { - return errors.Wrapf(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, RegionKey, l.storageLocation.Namespace, l.storageLocation.Name) + return errkit.Wrap(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, RegionKey, l.storageLocation.Namespace, l.storageLocation.Name) } return nil } diff --git a/pkg/secrets/repositoryserver/aws_secrets_test.go b/pkg/secrets/repositoryserver/aws_secrets_test.go index 1b5914fb82..a4d10be37e 100644 --- a/pkg/secrets/repositoryserver/aws_secrets_test.go +++ b/pkg/secrets/repositoryserver/aws_secrets_test.go @@ -15,7 +15,7 @@ package repositoryserver import ( - "github.com/pkg/errors" + "github.com/kanisterio/errkit" "gopkg.in/check.v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -59,7 +59,7 @@ func (s *AWSSecretCredsSuite) TestValidateRepoServerAWSCredentials(c *check.C) { }, }), errChecker: check.NotNil, - expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, RegionKey, "ns", "sec"), + expectedError: errkit.Wrap(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, RegionKey, "ns", "sec"), }, { // Missing required field - Bucket Key secret: NewAWSLocation(&corev1.Secret{ @@ -73,7 +73,7 @@ func (s *AWSSecretCredsSuite) TestValidateRepoServerAWSCredentials(c *check.C) { }, }), errChecker: check.NotNil, - expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, BucketKey, "ns", "sec"), + expectedError: errkit.Wrap(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, BucketKey, "ns", "sec"), }, { // Empty Secret secret: NewAWSLocation(&corev1.Secret{ @@ -84,12 +84,12 @@ func (s *AWSSecretCredsSuite) TestValidateRepoServerAWSCredentials(c *check.C) { }, }), errChecker: check.NotNil, - expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, "ns", "sec"), + expectedError: errkit.Wrap(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, "ns", "sec"), }, { // Nil Secret secret: NewAWSLocation(nil), errChecker: check.NotNil, - expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.NilSecretErrorMessage), + expectedError: errkit.Wrap(secerrors.ErrValidate, secerrors.NilSecretErrorMessage), }, } { err := tc.secret.Validate() diff --git a/pkg/secrets/repositoryserver/azure_secrets.go b/pkg/secrets/repositoryserver/azure_secrets.go index c0fc3de8da..5e39493151 100644 --- a/pkg/secrets/repositoryserver/azure_secrets.go +++ b/pkg/secrets/repositoryserver/azure_secrets.go @@ -15,7 +15,7 @@ package repositoryserver import ( - "github.com/pkg/errors" + "github.com/kanisterio/errkit" corev1 "k8s.io/api/core/v1" secerrors "github.com/kanisterio/kanister/pkg/secrets/errors" @@ -35,13 +35,13 @@ func NewAzureLocation(secret *corev1.Secret) *azure { func (l *azure) Validate() error { if l.storageLocation == nil { - return errors.Wrapf(secerrors.ErrValidate, secerrors.NilSecretErrorMessage) + return errkit.Wrap(secerrors.ErrValidate, secerrors.NilSecretErrorMessage) } if len(l.storageLocation.Data) == 0 { - return errors.Wrapf(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, l.storageLocation.Namespace, l.storageLocation.Name) + return errkit.Wrap(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, l.storageLocation.Namespace, l.storageLocation.Name) } if _, ok := l.storageLocation.Data[BucketKey]; !ok { - return errors.Wrapf(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, BucketKey, l.storageLocation.Namespace, l.storageLocation.Name) + return errkit.Wrap(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, BucketKey, l.storageLocation.Namespace, l.storageLocation.Name) } return nil } diff --git a/pkg/secrets/repositoryserver/azure_secrets_test.go b/pkg/secrets/repositoryserver/azure_secrets_test.go index 14925dc027..c9f57bfaa0 100644 --- a/pkg/secrets/repositoryserver/azure_secrets_test.go +++ b/pkg/secrets/repositoryserver/azure_secrets_test.go @@ -15,7 +15,7 @@ package repositoryserver import ( - "github.com/pkg/errors" + "github.com/kanisterio/errkit" "gopkg.in/check.v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -59,7 +59,7 @@ func (s *AzureSecretCredsSuite) TestValidateRepoServerAzureCredentials(c *check. }, }), errChecker: check.NotNil, - expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, BucketKey, "ns", "sec"), + expectedError: errkit.Wrap(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, BucketKey, "ns", "sec"), }, { // Empty Secret secret: NewAzureLocation(&corev1.Secret{ @@ -70,12 +70,12 @@ func (s *AzureSecretCredsSuite) TestValidateRepoServerAzureCredentials(c *check. }, }), errChecker: check.NotNil, - expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, "ns", "sec"), + expectedError: errkit.Wrap(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, "ns", "sec"), }, { // Nil Secret secret: NewAzureLocation(nil), errChecker: check.NotNil, - expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.NilSecretErrorMessage), + expectedError: errkit.Wrap(secerrors.ErrValidate, secerrors.NilSecretErrorMessage), }, } { err := tc.secret.Validate() diff --git a/pkg/secrets/repositoryserver/gcp_secrets.go b/pkg/secrets/repositoryserver/gcp_secrets.go index b901ed904b..bb472276b8 100644 --- a/pkg/secrets/repositoryserver/gcp_secrets.go +++ b/pkg/secrets/repositoryserver/gcp_secrets.go @@ -15,7 +15,7 @@ package repositoryserver import ( - "github.com/pkg/errors" + "github.com/kanisterio/errkit" corev1 "k8s.io/api/core/v1" secerrors "github.com/kanisterio/kanister/pkg/secrets/errors" @@ -35,13 +35,13 @@ func NewGCPLocation(secret *corev1.Secret) *gcp { func (l *gcp) Validate() error { if l.storageLocation == nil { - return errors.Wrapf(secerrors.ErrValidate, secerrors.NilSecretErrorMessage) + return errkit.Wrap(secerrors.ErrValidate, secerrors.NilSecretErrorMessage) } if len(l.storageLocation.Data) == 0 { - return errors.Wrapf(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, l.storageLocation.Namespace, l.storageLocation.Name) + return errkit.Wrap(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, l.storageLocation.Namespace, l.storageLocation.Name) } if _, ok := l.storageLocation.Data[BucketKey]; !ok { - return errors.Wrapf(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, BucketKey, l.storageLocation.Namespace, l.storageLocation.Name) + return errkit.Wrap(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, BucketKey, l.storageLocation.Namespace, l.storageLocation.Name) } return nil } diff --git a/pkg/secrets/repositoryserver/gcp_secrets_test.go b/pkg/secrets/repositoryserver/gcp_secrets_test.go index 078fcfef21..365c9762fc 100644 --- a/pkg/secrets/repositoryserver/gcp_secrets_test.go +++ b/pkg/secrets/repositoryserver/gcp_secrets_test.go @@ -15,7 +15,7 @@ package repositoryserver import ( - "github.com/pkg/errors" + "github.com/kanisterio/errkit" "gopkg.in/check.v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -59,7 +59,7 @@ func (s *GCPSecretCredsSuite) TestValidateRepoServerGCPCredentials(c *check.C) { }, }), errChecker: check.NotNil, - expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, BucketKey, "ns", "sec"), + expectedError: errkit.Wrap(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, BucketKey, "ns", "sec"), }, { // Empty Secret secret: NewGCPLocation(&corev1.Secret{ @@ -70,12 +70,12 @@ func (s *GCPSecretCredsSuite) TestValidateRepoServerGCPCredentials(c *check.C) { }, }), errChecker: check.NotNil, - expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, "ns", "sec"), + expectedError: errkit.Wrap(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, "ns", "sec"), }, { // Nil Secret secret: NewGCPLocation(nil), errChecker: check.NotNil, - expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.NilSecretErrorMessage), + expectedError: errkit.Wrap(secerrors.ErrValidate, secerrors.NilSecretErrorMessage), }, } { err := tc.secret.Validate() diff --git a/pkg/secrets/repositoryserver/repository_password.go b/pkg/secrets/repositoryserver/repository_password.go index 3cc2ceb7ae..b012d0c0a3 100644 --- a/pkg/secrets/repositoryserver/repository_password.go +++ b/pkg/secrets/repositoryserver/repository_password.go @@ -15,7 +15,7 @@ package repositoryserver import ( - "github.com/pkg/errors" + "github.com/kanisterio/errkit" corev1 "k8s.io/api/core/v1" secerrors "github.com/kanisterio/kanister/pkg/secrets/errors" @@ -36,17 +36,17 @@ func NewRepoPassword(secret *corev1.Secret) *repositoryPassword { // Validate the kopia repository password for required fields as well as unknown fields func (r *repositoryPassword) Validate() error { if r.password == nil { - return errors.Wrapf(secerrors.ErrValidate, secerrors.NilSecretErrorMessage) + return errkit.Wrap(secerrors.ErrValidate, secerrors.NilSecretErrorMessage) } if len(r.password.Data) == 0 { - return errors.Wrapf(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, r.password.Namespace, r.password.Name) + return errkit.Wrap(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, r.password.Namespace, r.password.Name) } // kopia repository must have exactly 1 field if len(r.password.Data) != 1 { - return errors.Wrapf(secerrors.ErrValidate, secerrors.UnknownFieldErrorMsg, r.password.Namespace, r.password.Name) + return errkit.Wrap(secerrors.ErrValidate, secerrors.UnknownFieldErrorMsg, r.password.Namespace, r.password.Name) } if _, ok := r.password.Data[RepoPasswordKey]; !ok { - return errors.Wrapf(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, RepoPasswordKey, r.password.Namespace, r.password.Name) + return errkit.Wrap(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, RepoPasswordKey, r.password.Namespace, r.password.Name) } return nil } diff --git a/pkg/secrets/repositoryserver/repository_password_test.go b/pkg/secrets/repositoryserver/repository_password_test.go index e2a9c68d7e..13686b2d05 100644 --- a/pkg/secrets/repositoryserver/repository_password_test.go +++ b/pkg/secrets/repositoryserver/repository_password_test.go @@ -15,7 +15,7 @@ package repositoryserver import ( - "github.com/pkg/errors" + "github.com/kanisterio/errkit" "gopkg.in/check.v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -58,7 +58,7 @@ func (s *GCPSecretCredsSuite) TestValidateRepositoryPassword(c *check.C) { }, }), errChecker: check.NotNil, - expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, RepoPasswordKey, "ns", "sec"), + expectedError: errkit.Wrap(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, RepoPasswordKey, "ns", "sec"), }, { // Secret should contain only 1 key value pair secret: NewRepoPassword(&corev1.Secret{ @@ -73,7 +73,7 @@ func (s *GCPSecretCredsSuite) TestValidateRepositoryPassword(c *check.C) { }, }), errChecker: check.NotNil, - expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.UnknownFieldErrorMsg, "ns", "sec"), + expectedError: errkit.Wrap(secerrors.ErrValidate, secerrors.UnknownFieldErrorMsg, "ns", "sec"), }, { // Empty Secret secret: NewRepoPassword(&corev1.Secret{ @@ -84,12 +84,12 @@ func (s *GCPSecretCredsSuite) TestValidateRepositoryPassword(c *check.C) { }, }), errChecker: check.NotNil, - expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, "ns", "sec"), + expectedError: errkit.Wrap(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, "ns", "sec"), }, { // Nil Secret secret: NewRepoPassword(nil), errChecker: check.NotNil, - expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.NilSecretErrorMessage), + expectedError: errkit.Wrap(secerrors.ErrValidate, secerrors.NilSecretErrorMessage), }, } { err := tc.secret.Validate() diff --git a/pkg/secrets/repositoryserver/repository_server_admin_credentials.go b/pkg/secrets/repositoryserver/repository_server_admin_credentials.go index 5db8076b7a..bd847f045f 100644 --- a/pkg/secrets/repositoryserver/repository_server_admin_credentials.go +++ b/pkg/secrets/repositoryserver/repository_server_admin_credentials.go @@ -15,7 +15,7 @@ package repositoryserver import ( - "github.com/pkg/errors" + "github.com/kanisterio/errkit" corev1 "k8s.io/api/core/v1" secerrors "github.com/kanisterio/kanister/pkg/secrets/errors" @@ -35,21 +35,21 @@ func NewRepositoryServerAdminCredentials(secret *corev1.Secret) *repositoryServe func (r *repositoryServerAdminCredentials) Validate() error { if r.credentials == nil { - return errors.Wrapf(secerrors.ErrValidate, secerrors.NilSecretErrorMessage) + return errkit.Wrap(secerrors.ErrValidate, secerrors.NilSecretErrorMessage) } if len(r.credentials.Data) == 0 { - return errors.Wrapf(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, r.credentials.Namespace, r.credentials.Name) + return errkit.Wrap(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, r.credentials.Namespace, r.credentials.Name) } // kopia repository server admin credentials secret must have exactly 2 fields if len(r.credentials.Data) != 2 { - return errors.Wrapf(secerrors.ErrValidate, secerrors.UnknownFieldErrorMsg, r.credentials.Namespace, r.credentials.Name) + return errkit.Wrap(secerrors.ErrValidate, secerrors.UnknownFieldErrorMsg, r.credentials.Namespace, r.credentials.Name) } if _, ok := r.credentials.Data[AdminUsernameKey]; !ok { - return errors.Wrapf(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, AdminUsernameKey, r.credentials.Namespace, r.credentials.Name) + return errkit.Wrap(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, AdminUsernameKey, r.credentials.Namespace, r.credentials.Name) } if _, ok := r.credentials.Data[AdminPasswordKey]; !ok { - return errors.Wrapf(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, AdminPasswordKey, r.credentials.Namespace, r.credentials.Name) + return errkit.Wrap(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, AdminPasswordKey, r.credentials.Namespace, r.credentials.Name) } return nil } diff --git a/pkg/secrets/repositoryserver/repository_server_admin_credentials_test.go b/pkg/secrets/repositoryserver/repository_server_admin_credentials_test.go index 568ca4c8c4..146b8d3a64 100644 --- a/pkg/secrets/repositoryserver/repository_server_admin_credentials_test.go +++ b/pkg/secrets/repositoryserver/repository_server_admin_credentials_test.go @@ -15,7 +15,7 @@ package repositoryserver import ( - "github.com/pkg/errors" + "github.com/kanisterio/errkit" "gopkg.in/check.v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -60,7 +60,7 @@ func (s *GCPSecretCredsSuite) TestValidateRepositoryServerAdminCredentials(c *ch }, }), errChecker: check.NotNil, - expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, AdminUsernameKey, "ns", "sec"), + expectedError: errkit.Wrap(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, AdminUsernameKey, "ns", "sec"), }, { // Missing required field - AdminPasswordKey secret: NewRepositoryServerAdminCredentials(&corev1.Secret{ @@ -75,7 +75,7 @@ func (s *GCPSecretCredsSuite) TestValidateRepositoryServerAdminCredentials(c *ch }, }), errChecker: check.NotNil, - expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, AdminPasswordKey, "ns", "sec"), + expectedError: errkit.Wrap(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, AdminPasswordKey, "ns", "sec"), }, { // Secret should contain only 2 key value pairs secret: NewRepositoryServerAdminCredentials(&corev1.Secret{ @@ -91,7 +91,7 @@ func (s *GCPSecretCredsSuite) TestValidateRepositoryServerAdminCredentials(c *ch }, }), errChecker: check.NotNil, - expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.UnknownFieldErrorMsg, "ns", "sec"), + expectedError: errkit.Wrap(secerrors.ErrValidate, secerrors.UnknownFieldErrorMsg, "ns", "sec"), }, { // Empty Secret secret: NewRepositoryServerAdminCredentials(&corev1.Secret{ @@ -102,12 +102,12 @@ func (s *GCPSecretCredsSuite) TestValidateRepositoryServerAdminCredentials(c *ch }, }), errChecker: check.NotNil, - expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, "ns", "sec"), + expectedError: errkit.Wrap(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, "ns", "sec"), }, { // Nil Secret secret: NewRepositoryServerAdminCredentials(nil), errChecker: check.NotNil, - expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.NilSecretErrorMessage), + expectedError: errkit.Wrap(secerrors.ErrValidate, secerrors.NilSecretErrorMessage), }, } { err := tc.secret.Validate() diff --git a/pkg/secrets/repositoryserver/repository_server_useraccess_credentials.go b/pkg/secrets/repositoryserver/repository_server_useraccess_credentials.go index 1ac767ddf6..d677e76e45 100644 --- a/pkg/secrets/repositoryserver/repository_server_useraccess_credentials.go +++ b/pkg/secrets/repositoryserver/repository_server_useraccess_credentials.go @@ -15,7 +15,7 @@ package repositoryserver import ( - "github.com/pkg/errors" + "github.com/kanisterio/errkit" corev1 "k8s.io/api/core/v1" secerrors "github.com/kanisterio/kanister/pkg/secrets/errors" @@ -35,10 +35,10 @@ func NewRepositoryServerUserAccessCredentials(secret *corev1.Secret) *repository func (r *repositoryServerUserAccessCredentials) Validate() error { if r.credentials == nil { - return errors.Wrapf(secerrors.ErrValidate, secerrors.NilSecretErrorMessage) + return errkit.Wrap(secerrors.ErrValidate, secerrors.NilSecretErrorMessage) } if len(r.credentials.Data) == 0 { - return errors.Wrapf(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, r.credentials.Namespace, r.credentials.Name) + return errkit.Wrap(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, r.credentials.Namespace, r.credentials.Name) } return nil } diff --git a/pkg/secrets/repositoryserver/s3compliant_secrets.go b/pkg/secrets/repositoryserver/s3compliant_secrets.go index 0a0a071dd1..f660e62594 100644 --- a/pkg/secrets/repositoryserver/s3compliant_secrets.go +++ b/pkg/secrets/repositoryserver/s3compliant_secrets.go @@ -15,7 +15,7 @@ package repositoryserver import ( - "github.com/pkg/errors" + "github.com/kanisterio/errkit" corev1 "k8s.io/api/core/v1" secerrors "github.com/kanisterio/kanister/pkg/secrets/errors" @@ -35,19 +35,19 @@ var _ Secret = &s3Compliant{} func (s s3Compliant) Validate() error { if s.storageLocation == nil { - return errors.Wrapf(secerrors.ErrValidate, secerrors.NilSecretErrorMessage) + return errkit.Wrap(secerrors.ErrValidate, secerrors.NilSecretErrorMessage) } if len(s.storageLocation.Data) == 0 { - return errors.Wrapf(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, s.storageLocation.Namespace, s.storageLocation.Name) + return errkit.Wrap(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, s.storageLocation.Namespace, s.storageLocation.Name) } if _, ok := s.storageLocation.Data[BucketKey]; !ok { - return errors.Wrapf(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, BucketKey, s.storageLocation.Namespace, s.storageLocation.Name) + return errkit.Wrap(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, BucketKey, s.storageLocation.Namespace, s.storageLocation.Name) } if _, ok := s.storageLocation.Data[EndpointKey]; !ok { - return errors.Wrapf(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, EndpointKey, s.storageLocation.Namespace, s.storageLocation.Name) + return errkit.Wrap(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, EndpointKey, s.storageLocation.Namespace, s.storageLocation.Name) } if _, ok := s.storageLocation.Data[RegionKey]; !ok { - return errors.Wrapf(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, RegionKey, s.storageLocation.Namespace, s.storageLocation.Name) + return errkit.Wrap(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, RegionKey, s.storageLocation.Namespace, s.storageLocation.Name) } return nil } diff --git a/pkg/secrets/repositoryserver/s3compliant_secrets_test.go b/pkg/secrets/repositoryserver/s3compliant_secrets_test.go index 745dceedba..9709234d42 100644 --- a/pkg/secrets/repositoryserver/s3compliant_secrets_test.go +++ b/pkg/secrets/repositoryserver/s3compliant_secrets_test.go @@ -15,7 +15,7 @@ package repositoryserver import ( - "github.com/pkg/errors" + "github.com/kanisterio/errkit" "gopkg.in/check.v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -61,7 +61,7 @@ func (s *S3CompliantSecretTestSuite) TestValidateRepoServerS3CompliantCredential }, }), errChecker: check.NotNil, - expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, BucketKey, "ns", "sec"), + expectedError: errkit.Wrap(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, BucketKey, "ns", "sec"), }, { // Missing required field - Region Key secret: NewS3CompliantLocation(&corev1.Secret{ @@ -76,7 +76,7 @@ func (s *S3CompliantSecretTestSuite) TestValidateRepoServerS3CompliantCredential }, }), errChecker: check.NotNil, - expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, RegionKey, "ns", "sec"), + expectedError: errkit.Wrap(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, RegionKey, "ns", "sec"), }, { // Missing required field - Endpoint Key secret: NewS3CompliantLocation(&corev1.Secret{ @@ -91,7 +91,7 @@ func (s *S3CompliantSecretTestSuite) TestValidateRepoServerS3CompliantCredential }, }), errChecker: check.NotNil, - expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, EndpointKey, "ns", "sec"), + expectedError: errkit.Wrap(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, EndpointKey, "ns", "sec"), }, { // Empty Secret secret: NewS3CompliantLocation(&corev1.Secret{ @@ -102,12 +102,12 @@ func (s *S3CompliantSecretTestSuite) TestValidateRepoServerS3CompliantCredential }, }), errChecker: check.NotNil, - expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, "ns", "sec"), + expectedError: errkit.Wrap(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, "ns", "sec"), }, { // Nil Secret secret: NewS3CompliantLocation(nil), errChecker: check.NotNil, - expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.NilSecretErrorMessage), + expectedError: errkit.Wrap(secerrors.ErrValidate, secerrors.NilSecretErrorMessage), }, } { err := tc.secret.Validate() diff --git a/pkg/secrets/secrets.go b/pkg/secrets/secrets.go index 7a2b3419de..a8d597c4b9 100644 --- a/pkg/secrets/secrets.go +++ b/pkg/secrets/secrets.go @@ -15,7 +15,9 @@ package secrets import ( - "github.com/pkg/errors" + "fmt" + + "github.com/kanisterio/errkit" corev1 "k8s.io/api/core/v1" secerrors "github.com/kanisterio/kanister/pkg/secrets/errors" @@ -27,7 +29,7 @@ import ( // - AWS typed secret with required AWS secret fields. func ValidateCredentials(secret *corev1.Secret) error { if secret == nil { - return errors.New("Nil secret") + return errkit.New("Nil secret") } switch string(secret.Type) { case AWSSecretType: @@ -41,7 +43,7 @@ func ValidateCredentials(secret *corev1.Secret) error { // dont need credentials for file store return nil default: - return errors.Errorf("Unsupported type '%s' for secret '%s:%s'", string(secret.Type), secret.Namespace, secret.Name) + return errkit.New(fmt.Sprintf("Unsupported type '%s' for secret '%s:%s'", string(secret.Type), secret.Namespace, secret.Name)) } } @@ -49,11 +51,11 @@ func getLocationSecret(secret *corev1.Secret) (reposerver.Secret, error) { var locationType []byte var ok bool if secret == nil { - return nil, errors.New("Secret for kopia repository location is Nil") + return nil, errkit.New("Secret for kopia repository location is Nil") } if locationType, ok = (secret.Data[reposerver.TypeKey]); !ok { - return nil, errors.Wrapf(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, reposerver.TypeKey, secret.Namespace, secret.Name) + return nil, errkit.Wrap(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, reposerver.TypeKey, secret.Namespace, secret.Name) } switch reposerver.LocType(string(locationType)) { @@ -68,7 +70,7 @@ func getLocationSecret(secret *corev1.Secret) (reposerver.Secret, error) { case reposerver.LocTypeFilestore: return reposerver.NewFileStoreLocation(secret), nil default: - return nil, errors.Wrapf(secerrors.ErrValidate, secerrors.UnsupportedLocationTypeErrorMsg, locationType, secret.Namespace, secret.Name) + return nil, errkit.Wrap(secerrors.ErrValidate, secerrors.UnsupportedLocationTypeErrorMsg, locationType, secret.Namespace, secret.Name) } } diff --git a/pkg/secrets/secrets_test.go b/pkg/secrets/secrets_test.go index 27b27bb54e..2151a7f921 100644 --- a/pkg/secrets/secrets_test.go +++ b/pkg/secrets/secrets_test.go @@ -17,7 +17,7 @@ package secrets import ( "testing" - "github.com/pkg/errors" + "github.com/kanisterio/errkit" "gopkg.in/check.v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -93,7 +93,7 @@ func (s *SecretUtilsSuite) TestGetLocationSecret(c *check.C) { }, errChecker: check.NotNil, locationSecretChecker: check.IsNil, - expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, repositoryserver.TypeKey, "ns", "sec"), + expectedError: errkit.Wrap(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, repositoryserver.TypeKey, "ns", "sec"), }, { // Unsupported location type secret: &corev1.Secret{ @@ -108,7 +108,7 @@ func (s *SecretUtilsSuite) TestGetLocationSecret(c *check.C) { }, errChecker: check.NotNil, locationSecretChecker: check.IsNil, - expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.UnsupportedLocationTypeErrorMsg, "invalid", "ns", "sec"), + expectedError: errkit.Wrap(secerrors.ErrValidate, secerrors.UnsupportedLocationTypeErrorMsg, "invalid", "ns", "sec"), }, } { rsecret, err := getLocationSecret(tc.secret)