forked from vmware-tanzu/velero
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kopia Integration Change - Storage Configuration (vmware-tanzu#5142)
* unified repo storge config Signed-off-by: Lyndon-Li <[email protected]> * add UT Signed-off-by: Lyndon-Li <[email protected]>
- Loading branch information
1 parent
1ae8075
commit f25e56b
Showing
18 changed files
with
1,208 additions
and
102 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Kopia Integration: Add the Unified Repository Interface definition. | ||
Kopia Integration: Add the changes for Unified Repository storage config. | ||
|
||
Related Issues; #5076, #5080 |
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,99 @@ | ||
/* | ||
Copyright the Velero contributors. | ||
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 config | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"github.com/aws/aws-sdk-go/aws/credentials" | ||
"github.com/aws/aws-sdk-go/aws/endpoints" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/s3/s3manager" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
const ( | ||
// AWS specific environment variable | ||
awsProfileEnvVar = "AWS_PROFILE" | ||
awsProfileKey = "profile" | ||
awsCredentialsFileEnvVar = "AWS_SHARED_CREDENTIALS_FILE" | ||
) | ||
|
||
// GetS3ResticEnvVars gets the environment variables that restic | ||
// relies on (AWS_PROFILE) based on info in the provided object | ||
// storage location config map. | ||
func GetS3ResticEnvVars(config map[string]string) (map[string]string, error) { | ||
result := make(map[string]string) | ||
|
||
if credentialsFile, ok := config[CredentialsFileKey]; ok { | ||
result[awsCredentialsFileEnvVar] = credentialsFile | ||
} | ||
|
||
if profile, ok := config[awsProfileKey]; ok { | ||
result[awsProfileEnvVar] = profile | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
// GetS3Credentials gets the S3 credential values according to the information | ||
// of the provided config or the system's environment variables | ||
func GetS3Credentials(config map[string]string) (credentials.Value, error) { | ||
credentialsFile := config[CredentialsFileKey] | ||
if credentialsFile == "" { | ||
credentialsFile = os.Getenv("AWS_SHARED_CREDENTIALS_FILE") | ||
} | ||
|
||
if credentialsFile == "" { | ||
return credentials.Value{}, errors.New("missing credential file") | ||
} | ||
|
||
creds := credentials.NewSharedCredentials(credentialsFile, "") | ||
credValue, err := creds.Get() | ||
if err != nil { | ||
return credValue, err | ||
} | ||
|
||
return credValue, nil | ||
} | ||
|
||
// GetAWSBucketRegion returns the AWS region that a bucket is in, or an error | ||
// if the region cannot be determined. | ||
func GetAWSBucketRegion(bucket string) (string, error) { | ||
var region string | ||
|
||
sess, err := session.NewSession() | ||
if err != nil { | ||
return "", errors.WithStack(err) | ||
} | ||
|
||
for _, partition := range endpoints.DefaultPartitions() { | ||
for regionHint := range partition.Regions() { | ||
region, _ = s3manager.GetBucketRegion(context.Background(), sess, bucket, regionHint) | ||
|
||
// we only need to try a single region hint per partition, so break after the first | ||
break | ||
} | ||
|
||
if region != "" { | ||
return region, nil | ||
} | ||
} | ||
|
||
return "", errors.New("unable to determine bucket's region") | ||
} |
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
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
Oops, something went wrong.