Skip to content

Commit

Permalink
chore: Errkit migration 6 (pkg/secrets) (#3173)
Browse files Browse the repository at this point in the history
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
e-sumin and mergify[bot] authored Oct 7, 2024
1 parent 0039d6f commit 7b2bb9c
Show file tree
Hide file tree
Showing 19 changed files with 94 additions and 92 deletions.
8 changes: 4 additions & 4 deletions pkg/secrets/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions pkg/secrets/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
12 changes: 6 additions & 6 deletions pkg/secrets/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
12 changes: 6 additions & 6 deletions pkg/secrets/gcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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,
},
} {
Expand Down
10 changes: 5 additions & 5 deletions pkg/secrets/repositoryserver/aws_secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
10 changes: 5 additions & 5 deletions pkg/secrets/repositoryserver/aws_secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand All @@ -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{
Expand All @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions pkg/secrets/repositoryserver/azure_secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
8 changes: 4 additions & 4 deletions pkg/secrets/repositoryserver/azure_secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand All @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions pkg/secrets/repositoryserver/gcp_secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
8 changes: 4 additions & 4 deletions pkg/secrets/repositoryserver/gcp_secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand All @@ -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()
Expand Down
10 changes: 5 additions & 5 deletions pkg/secrets/repositoryserver/repository_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Loading

0 comments on commit 7b2bb9c

Please sign in to comment.