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

configuration for checksum algorithm for PutObject to S3 #194

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions backupstoragelocation.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,15 @@ spec:

# Tags that need to be placed on AWS S3 objects.
# For example "Key1=Value1&Key2=Value2"
#
# Optional (defaults to empty "")
tagging: ""

# The checksum algorithm to use for uploading objects to S3.
# The Supported values are "CRC32", "CRC32C", "SHA1", "SHA256".
# If the value is set as empty string "", no checksum will be calculated and attached to
# the request headers.
#
# Optional (defaults to "CRC32")
checksumAlgorithm: "CRC32"
```
20 changes: 20 additions & 0 deletions velero-plugin-for-aws/object_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const (
caCertKey = "caCert"
enableSharedConfigKey = "enableSharedConfig"
taggingKey = "tagging"
checksumAlgKey = "checksumAlgorithm"
)

type s3Interface interface {
Expand All @@ -73,6 +74,7 @@ type ObjectStore struct {
signatureVersion string
serverSideEncryption string
tagging string
checksumAlg string
}

func newObjectStore(logger logrus.FieldLogger) *ObjectStore {
Expand All @@ -94,6 +96,7 @@ func (o *ObjectStore) Init(config map[string]string) error {
insecureSkipTLSVerifyKey,
enableSharedConfigKey,
taggingKey,
checksumAlgKey,
); err != nil {
return err
}
Expand Down Expand Up @@ -187,9 +190,22 @@ func (o *ObjectStore) Init(config map[string]string) error {
return err
}
}
if alg, ok := config[checksumAlgKey]; ok {
if !validChecksumAlg(alg) {
return errors.Errorf("invalid checksum algorithm: %s", alg)
}
o.checksumAlg = alg
} else {
o.checksumAlg = string(types.ChecksumAlgorithmCrc32)
}
return nil
}

func validChecksumAlg(alg string) bool {
return alg == string(types.ChecksumAlgorithmCrc32) || alg == string(types.ChecksumAlgorithmCrc32c) ||
alg == string(types.ChecksumAlgorithmSha1) || alg == string(types.ChecksumAlgorithmSha256) || alg == ""
}

func readCustomerKey(customerKeyEncryptionFile string) (string, error) {
if _, err := os.Stat(customerKeyEncryptionFile); err != nil {
if os.IsNotExist(err) {
Expand Down Expand Up @@ -241,6 +257,10 @@ func (o *ObjectStore) PutObject(bucket, key string, body io.Reader) error {
input.ServerSideEncryption = types.ServerSideEncryption(o.serverSideEncryption)
}

if o.checksumAlg != "" {
input.ChecksumAlgorithm = types.ChecksumAlgorithm(o.checksumAlg)
}

_, err := o.s3Uploader.Upload(context.Background(), input)

return errors.Wrapf(err, "error putting object %s", key)
Expand Down
40 changes: 40 additions & 0 deletions velero-plugin-for-aws/object_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,43 @@ func TestObjectExists(t *testing.T) {
})
}
}

func TestValidChecksumAlg(t *testing.T) {
tests := []struct {
name string
input string
expected bool
}{
{
name: "md5 is invalid",
input: "MD5",
expected: false,
},
{
name: "sha256 is invalid",
input: "sha256",
expected: false,
},
{
name: "SHA256 is valid",
input: "SHA256",
expected: true,
},
{
name: "empty is valid",
input: "",
expected: true,
},
{
name: "blank string with space is invalid",
input: " ",
expected: false,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.expected, validChecksumAlg(tc.input))
})
}
}
Loading