2018-06-06 21:32:28 +00:00
|
|
|
/*
|
2019-03-29 20:32:43 +00:00
|
|
|
Copyright 2018, 2019 the Velero contributors.
|
2018-06-06 21:32:28 +00:00
|
|
|
|
|
|
|
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 restic
|
|
|
|
|
|
|
|
import (
|
2019-10-22 22:31:27 +00:00
|
|
|
"context"
|
2018-06-06 21:32:28 +00:00
|
|
|
"fmt"
|
2018-09-19 18:51:30 +00:00
|
|
|
"path"
|
2018-06-06 21:32:28 +00:00
|
|
|
"strings"
|
|
|
|
|
2019-10-22 22:31:27 +00:00
|
|
|
"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"
|
2019-08-08 21:06:49 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2019-09-30 21:26:56 +00:00
|
|
|
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
|
|
|
"github.com/vmware-tanzu/velero/pkg/persistence"
|
2018-06-06 21:32:28 +00:00
|
|
|
)
|
|
|
|
|
2018-06-15 03:24:01 +00:00
|
|
|
type BackendType string
|
|
|
|
|
|
|
|
const (
|
2019-04-16 16:21:50 +00:00
|
|
|
AWSBackend BackendType = "velero.io/aws"
|
|
|
|
AzureBackend BackendType = "velero.io/azure"
|
|
|
|
GCPBackend BackendType = "velero.io/gcp"
|
2018-06-15 03:24:01 +00:00
|
|
|
)
|
|
|
|
|
2018-06-18 17:54:07 +00:00
|
|
|
// this func is assigned to a package-level variable so it can be
|
|
|
|
// replaced when unit-testing
|
2019-10-22 22:31:27 +00:00
|
|
|
var getAWSBucketRegion = getBucketRegion
|
2018-06-18 17:54:07 +00:00
|
|
|
|
2018-06-06 21:32:28 +00:00
|
|
|
// getRepoPrefix returns the prefix of the value of the --repo flag for
|
|
|
|
// restic commands, i.e. everything except the "/<repo-name>".
|
2019-08-08 21:06:49 +00:00
|
|
|
func getRepoPrefix(location *velerov1api.BackupStorageLocation) (string, error) {
|
|
|
|
var bucket, prefix string
|
2018-09-19 18:51:30 +00:00
|
|
|
|
|
|
|
if location.Spec.ObjectStorage != nil {
|
|
|
|
layout := persistence.NewObjectStoreLayout(location.Spec.ObjectStorage.Prefix)
|
|
|
|
|
|
|
|
bucket = location.Spec.ObjectStorage.Bucket
|
|
|
|
prefix = layout.GetResticDir()
|
2018-06-06 21:32:28 +00:00
|
|
|
}
|
|
|
|
|
Use Credential from BSL for restic commands (#3489)
* Use Credential from BSL for restic commands
This change introduces support for restic to make use of per-BSL
credentials. It makes use of the `credentials.FileStore` introduced in
PR #3442 to write the BSL credentials to disk. To support per-BSL
credentials for restic, the environment for the restic commands needs to
be modified for each provider to ensure that the credentials are
provided via the correct provider specific environment variables.
This change introduces a new function `restic.CmdEnv` to check the BSL
provider and create the correct mapping of environment variables for
each provider.
Previously, AWS and GCP could rely on the environment variables in the
Velero deployments to obtain the credentials file, but now these
environment variables need to be set with the path to the serialized
credentials file if a credential is set on the BSL.
For Azure, the credentials file in the environment was loaded and parsed
to set the environment variables for restic. Now, we check if the BSL
has a credential, and if it does, load and parse that file instead.
This change also introduces a few other small improvements. Now that we
are fetching the BSL to check for the `Credential` field, we can use the
BSL directly to get the `CACert` which means that we can remove the
`GetCACert` function. Also, now that we have a way to serialize secrets
to disk, we can use the `credentials.FileStore` to get a temp file for
the restic repo password and remove the `restic.TempCredentialsFile`
function.
Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
* Add documentation for per-BSL credentials
Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
* Address review feedback
Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
* Address review comments
Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
2021-03-11 18:10:51 +00:00
|
|
|
backendType := getBackendType(location.Spec.Provider)
|
2019-04-16 16:21:50 +00:00
|
|
|
|
2019-08-08 21:07:21 +00:00
|
|
|
if repoPrefix := location.Spec.Config["resticRepoPrefix"]; repoPrefix != "" {
|
|
|
|
return repoPrefix, nil
|
|
|
|
}
|
|
|
|
|
Use Credential from BSL for restic commands (#3489)
* Use Credential from BSL for restic commands
This change introduces support for restic to make use of per-BSL
credentials. It makes use of the `credentials.FileStore` introduced in
PR #3442 to write the BSL credentials to disk. To support per-BSL
credentials for restic, the environment for the restic commands needs to
be modified for each provider to ensure that the credentials are
provided via the correct provider specific environment variables.
This change introduces a new function `restic.CmdEnv` to check the BSL
provider and create the correct mapping of environment variables for
each provider.
Previously, AWS and GCP could rely on the environment variables in the
Velero deployments to obtain the credentials file, but now these
environment variables need to be set with the path to the serialized
credentials file if a credential is set on the BSL.
For Azure, the credentials file in the environment was loaded and parsed
to set the environment variables for restic. Now, we check if the BSL
has a credential, and if it does, load and parse that file instead.
This change also introduces a few other small improvements. Now that we
are fetching the BSL to check for the `Credential` field, we can use the
BSL directly to get the `CACert` which means that we can remove the
`GetCACert` function. Also, now that we have a way to serialize secrets
to disk, we can use the `credentials.FileStore` to get a temp file for
the restic repo password and remove the `restic.TempCredentialsFile`
function.
Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
* Add documentation for per-BSL credentials
Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
* Address review feedback
Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
* Address review comments
Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
2021-03-11 18:10:51 +00:00
|
|
|
switch backendType {
|
2018-06-22 00:20:53 +00:00
|
|
|
case AWSBackend:
|
|
|
|
var url string
|
|
|
|
// non-AWS, S3-compatible object store
|
2021-06-09 16:08:19 +00:00
|
|
|
if s3Url := location.Spec.Config["s3Url"]; s3Url != "" {
|
|
|
|
url = strings.TrimSuffix(s3Url, "/")
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
region := location.Spec.Config["region"]
|
|
|
|
if region == "" {
|
|
|
|
region, err = getAWSBucketRegion(bucket)
|
|
|
|
}
|
2018-06-22 00:20:53 +00:00
|
|
|
if err != nil {
|
2021-11-15 12:20:27 +00:00
|
|
|
return "", errors.Wrapf(err, "failed to detect the region via bucket: %s", bucket)
|
2018-06-22 00:20:53 +00:00
|
|
|
}
|
2021-11-15 12:20:27 +00:00
|
|
|
url = fmt.Sprintf("s3-%s.amazonaws.com", region)
|
2018-06-22 00:20:53 +00:00
|
|
|
}
|
|
|
|
|
2021-06-09 16:08:19 +00:00
|
|
|
return fmt.Sprintf("s3:%s/%s", url, path.Join(bucket, prefix)), nil
|
2018-06-06 21:32:28 +00:00
|
|
|
case AzureBackend:
|
2019-08-08 21:06:49 +00:00
|
|
|
return fmt.Sprintf("azure:%s:/%s", bucket, prefix), nil
|
2018-06-06 21:32:28 +00:00
|
|
|
case GCPBackend:
|
2019-08-08 21:06:49 +00:00
|
|
|
return fmt.Sprintf("gs:%s:/%s", bucket, prefix), nil
|
2018-06-06 21:32:28 +00:00
|
|
|
}
|
|
|
|
|
2019-08-08 21:06:49 +00:00
|
|
|
return "", errors.New("restic repository prefix (resticRepoPrefix) not specified in backup storage location's config")
|
2018-06-06 21:32:28 +00:00
|
|
|
}
|
|
|
|
|
Use Credential from BSL for restic commands (#3489)
* Use Credential from BSL for restic commands
This change introduces support for restic to make use of per-BSL
credentials. It makes use of the `credentials.FileStore` introduced in
PR #3442 to write the BSL credentials to disk. To support per-BSL
credentials for restic, the environment for the restic commands needs to
be modified for each provider to ensure that the credentials are
provided via the correct provider specific environment variables.
This change introduces a new function `restic.CmdEnv` to check the BSL
provider and create the correct mapping of environment variables for
each provider.
Previously, AWS and GCP could rely on the environment variables in the
Velero deployments to obtain the credentials file, but now these
environment variables need to be set with the path to the serialized
credentials file if a credential is set on the BSL.
For Azure, the credentials file in the environment was loaded and parsed
to set the environment variables for restic. Now, we check if the BSL
has a credential, and if it does, load and parse that file instead.
This change also introduces a few other small improvements. Now that we
are fetching the BSL to check for the `Credential` field, we can use the
BSL directly to get the `CACert` which means that we can remove the
`GetCACert` function. Also, now that we have a way to serialize secrets
to disk, we can use the `credentials.FileStore` to get a temp file for
the restic repo password and remove the `restic.TempCredentialsFile`
function.
Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
* Add documentation for per-BSL credentials
Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
* Address review feedback
Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
* Address review comments
Signed-off-by: Bridget McErlean <bmcerlean@vmware.com>
2021-03-11 18:10:51 +00:00
|
|
|
func getBackendType(provider string) BackendType {
|
|
|
|
if !strings.Contains(provider, "/") {
|
|
|
|
provider = "velero.io/" + provider
|
|
|
|
}
|
|
|
|
|
|
|
|
return BackendType(provider)
|
|
|
|
}
|
|
|
|
|
2018-06-06 21:32:28 +00:00
|
|
|
// GetRepoIdentifier returns the string to be used as the value of the --repo flag in
|
|
|
|
// restic commands for the given repository.
|
2019-08-08 21:06:49 +00:00
|
|
|
func GetRepoIdentifier(location *velerov1api.BackupStorageLocation, name string) (string, error) {
|
|
|
|
prefix, err := getRepoPrefix(location)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2018-06-06 21:32:28 +00:00
|
|
|
|
2019-08-08 21:06:49 +00:00
|
|
|
return fmt.Sprintf("%s/%s", strings.TrimSuffix(prefix, "/"), name), nil
|
2018-06-06 21:32:28 +00:00
|
|
|
}
|
2019-10-22 22:31:27 +00:00
|
|
|
|
|
|
|
// getBucketRegion returns the AWS region that a bucket is in, or an error
|
|
|
|
// if the region cannot be determined.
|
|
|
|
func getBucketRegion(bucket string) (string, error) {
|
|
|
|
var region string
|
|
|
|
|
|
|
|
session, 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(), session, 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")
|
|
|
|
}
|