-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Type S3Compliant to Kopia Repository Storage Args (#2159)
* Add S3Compliant to Storage Args Signed-off-by: Rajat Gupta <rajat.gupta@veeam.com> * Add Test for s3Compliant storage args Signed-off-by: Rajat Gupta <rajat.gupta@veeam.com> * Add Validation for s3Compliant Location Signed-off-by: Rajat Gupta <rajat.gupta@veeam.com> * Format Imports Signed-off-by: Rajat Gupta <rajat.gupta@veeam.com> * Add Bucket Validation Signed-off-by: Rajat Gupta <rajat.gupta@veeam.com> * Add Tests for S3 Compliant Secrets Validation Signed-off-by: Rajat Gupta <rajat.gupta@veeam.com> --------- Signed-off-by: Rajat Gupta <rajat.gupta@veeam.com>
- Loading branch information
Showing
6 changed files
with
202 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright 2023 The Kanister 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 repositoryserver | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
corev1 "k8s.io/api/core/v1" | ||
|
||
secerrors "github.com/kanisterio/kanister/pkg/secrets/errors" | ||
) | ||
|
||
type s3Compliant struct { | ||
storageLocation *corev1.Secret | ||
} | ||
|
||
func NewS3CompliantLocation(secret *corev1.Secret) *s3Compliant { | ||
return &s3Compliant{ | ||
storageLocation: secret, | ||
} | ||
} | ||
|
||
var _ Secret = &s3Compliant{} | ||
|
||
func (s s3Compliant) Validate() error { | ||
if s.storageLocation == nil { | ||
return errors.Wrapf(secerrors.ErrValidate, secerrors.NilSecretErrorMessage) | ||
} | ||
if len(s.storageLocation.Data) == 0 { | ||
return errors.Wrapf(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) | ||
} | ||
if _, ok := s.storageLocation.Data[EndpointKey]; !ok { | ||
return errors.Wrapf(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 nil | ||
} |
119 changes: 119 additions & 0 deletions
119
pkg/secrets/repositoryserver/s3compliant_secrets_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// Copyright 2023 The Kanister 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 repositoryserver | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
. "gopkg.in/check.v1" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
secerrors "github.com/kanisterio/kanister/pkg/secrets/errors" | ||
) | ||
|
||
type S3CompliantSecretTestSuite struct{} | ||
|
||
var _ = Suite(&S3CompliantSecretTestSuite{}) | ||
|
||
func (s *S3CompliantSecretTestSuite) TestValidateRepoServerS3CompliantCredentials(c *C) { | ||
for i, tc := range []struct { | ||
secret Secret | ||
errChecker Checker | ||
expectedError error | ||
}{ | ||
{ // Valid S3 Compatible Secret | ||
secret: NewS3CompliantLocation(&v1.Secret{ | ||
Type: v1.SecretType(LocTypes3Compliant), | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "sec", | ||
Namespace: "ns", | ||
}, | ||
Data: map[string][]byte{ | ||
BucketKey: []byte("bucket"), | ||
RegionKey: []byte("region"), | ||
EndpointKey: []byte("endpoint"), | ||
}, | ||
}), | ||
errChecker: IsNil, | ||
}, | ||
{ // Missing required field - Bucket Key | ||
secret: NewS3CompliantLocation(&v1.Secret{ | ||
Type: v1.SecretType(LocTypes3Compliant), | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "sec", | ||
Namespace: "ns", | ||
}, | ||
Data: map[string][]byte{ | ||
RegionKey: []byte("region"), | ||
EndpointKey: []byte("endpoint"), | ||
}, | ||
}), | ||
errChecker: NotNil, | ||
expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, BucketKey, "ns", "sec"), | ||
}, | ||
{ // Missing required field - Region Key | ||
secret: NewS3CompliantLocation(&v1.Secret{ | ||
Type: v1.SecretType(LocTypes3Compliant), | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "sec", | ||
Namespace: "ns", | ||
}, | ||
Data: map[string][]byte{ | ||
BucketKey: []byte("bucket"), | ||
EndpointKey: []byte("endpoint"), | ||
}, | ||
}), | ||
errChecker: NotNil, | ||
expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, RegionKey, "ns", "sec"), | ||
}, | ||
{ // Missing required field - Endpoint Key | ||
secret: NewS3CompliantLocation(&v1.Secret{ | ||
Type: v1.SecretType(LocTypes3Compliant), | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "sec", | ||
Namespace: "ns", | ||
}, | ||
Data: map[string][]byte{ | ||
RegionKey: []byte("region"), | ||
BucketKey: []byte("bucket"), | ||
}, | ||
}), | ||
errChecker: NotNil, | ||
expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.MissingRequiredFieldErrorMsg, EndpointKey, "ns", "sec"), | ||
}, | ||
{ // Empty Secret | ||
secret: NewS3CompliantLocation(&v1.Secret{ | ||
Type: v1.SecretType(LocTypes3Compliant), | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "sec", | ||
Namespace: "ns", | ||
}, | ||
}), | ||
errChecker: NotNil, | ||
expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.EmptySecretErrorMessage, "ns", "sec"), | ||
}, | ||
{ // Nil Secret | ||
secret: NewS3CompliantLocation(nil), | ||
errChecker: NotNil, | ||
expectedError: errors.Wrapf(secerrors.ErrValidate, secerrors.NilSecretErrorMessage), | ||
}, | ||
} { | ||
err := tc.secret.Validate() | ||
c.Check(err, tc.errChecker) | ||
if err != nil { | ||
c.Check(err.Error(), Equals, tc.expectedError.Error(), Commentf("test number: %d", i)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters