2018-02-28 01:35:35 +00:00
|
|
|
/*
|
2021-02-18 18:30:52 +00:00
|
|
|
Copyright the Velero contributors.
|
2018-02-28 01:35:35 +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 (
|
2022-04-25 10:03:08 +00:00
|
|
|
"context"
|
2018-02-28 01:35:35 +00:00
|
|
|
"fmt"
|
2018-09-25 21:46:29 +00:00
|
|
|
"os"
|
2018-02-28 01:35:35 +00:00
|
|
|
"strings"
|
2018-06-06 21:32:28 +00:00
|
|
|
"time"
|
2018-02-28 01:35:35 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2020-06-04 00:15:59 +00:00
|
|
|
corev1api "k8s.io/api/core/v1"
|
2018-02-28 01:35:35 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/labels"
|
2022-04-25 10:03:08 +00:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
2018-02-28 01:35:35 +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
|
|
|
"github.com/vmware-tanzu/velero/internal/credentials"
|
2019-09-30 21:26:56 +00:00
|
|
|
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
|
|
|
"github.com/vmware-tanzu/velero/pkg/label"
|
|
|
|
"github.com/vmware-tanzu/velero/pkg/util/filesystem"
|
2018-02-28 01:35:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-08-06 20:40:35 +00:00
|
|
|
// DaemonSet is the name of the Velero restic daemonset.
|
|
|
|
DaemonSet = "restic"
|
|
|
|
|
|
|
|
// InitContainer is the name of the init container added
|
|
|
|
// to workload pods to help with restores.
|
|
|
|
InitContainer = "restic-wait"
|
|
|
|
|
|
|
|
// DefaultMaintenanceFrequency is the default time interval
|
2019-09-10 18:58:42 +00:00
|
|
|
// at which restic prune is run.
|
|
|
|
DefaultMaintenanceFrequency = 7 * 24 * time.Hour
|
2018-02-28 01:35:35 +00:00
|
|
|
|
2020-06-15 22:26:44 +00:00
|
|
|
// DefaultVolumesToRestic specifies whether restic should be used, by default, to
|
2020-06-05 22:34:02 +00:00
|
|
|
// take backup of all pod volumes.
|
2020-06-15 22:26:44 +00:00
|
|
|
DefaultVolumesToRestic = false
|
2020-06-05 22:34:02 +00:00
|
|
|
|
2019-08-06 20:40:35 +00:00
|
|
|
// PVCNameAnnotation is the key for the annotation added to
|
|
|
|
// pod volume backups when they're for a PVC.
|
|
|
|
PVCNameAnnotation = "velero.io/pvc-name"
|
|
|
|
|
2020-02-19 19:37:40 +00:00
|
|
|
// VolumesToBackupAnnotation is the annotation on a pod whose mounted volumes
|
|
|
|
// need to be backed up using restic.
|
|
|
|
VolumesToBackupAnnotation = "backup.velero.io/backup-volumes"
|
|
|
|
|
2020-06-04 00:15:59 +00:00
|
|
|
// VolumesToExcludeAnnotation is the annotation on a pod whose mounted volumes
|
|
|
|
// should be excluded from restic backup.
|
|
|
|
VolumesToExcludeAnnotation = "backup.velero.io/backup-volumes-excludes"
|
|
|
|
|
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
|
|
|
// credentialsFileKey is the key within a BSL config that is checked to see if
|
|
|
|
// the BSL is using its own credentials, rather than those in the environment
|
|
|
|
credentialsFileKey = "credentialsFile"
|
|
|
|
|
2019-07-24 19:51:20 +00:00
|
|
|
// Deprecated.
|
2019-08-06 20:40:35 +00:00
|
|
|
//
|
|
|
|
// TODO(2.0): remove
|
2019-07-24 19:51:20 +00:00
|
|
|
podAnnotationPrefix = "snapshot.velero.io/"
|
2018-02-28 01:35:35 +00:00
|
|
|
)
|
|
|
|
|
2019-08-06 20:17:36 +00:00
|
|
|
// getPodSnapshotAnnotations returns a map, of volume name -> snapshot id,
|
2018-02-28 01:35:35 +00:00
|
|
|
// of all restic snapshots for this pod.
|
2019-08-06 20:17:36 +00:00
|
|
|
// TODO(2.0) to remove
|
|
|
|
// Deprecated: we will stop using pod annotations to record restic snapshot IDs after they're taken,
|
|
|
|
// therefore we won't need to check if these annotations exist.
|
|
|
|
func getPodSnapshotAnnotations(obj metav1.Object) map[string]string {
|
2018-02-28 01:35:35 +00:00
|
|
|
var res map[string]string
|
|
|
|
|
2019-01-25 03:33:07 +00:00
|
|
|
insertSafe := func(k, v string) {
|
|
|
|
if res == nil {
|
|
|
|
res = make(map[string]string)
|
|
|
|
}
|
|
|
|
res[k] = v
|
|
|
|
}
|
|
|
|
|
2018-02-28 01:35:35 +00:00
|
|
|
for k, v := range obj.GetAnnotations() {
|
|
|
|
if strings.HasPrefix(k, podAnnotationPrefix) {
|
2019-01-25 03:33:07 +00:00
|
|
|
insertSafe(k[len(podAnnotationPrefix):], v)
|
|
|
|
}
|
2018-02-28 01:35:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-02-22 19:16:00 +00:00
|
|
|
func isPVBMatchPod(pvb *velerov1api.PodVolumeBackup, podName string, namespace string) bool {
|
|
|
|
return podName == pvb.Spec.Pod.Name && namespace == pvb.Spec.Pod.Namespace
|
2020-11-10 16:36:49 +00:00
|
|
|
}
|
|
|
|
|
2021-09-01 05:51:44 +00:00
|
|
|
// volumeHasNonRestorableSource checks if the given volume exists in the list of podVolumes
|
|
|
|
// and returns true if the volume's source is not restorable. This is true for volumes with
|
|
|
|
// a Projected or DownwardAPI source.
|
|
|
|
func volumeHasNonRestorableSource(volumeName string, podVolumes []corev1api.Volume) bool {
|
|
|
|
var volume corev1api.Volume
|
|
|
|
for _, v := range podVolumes {
|
|
|
|
if v.Name == volumeName {
|
|
|
|
volume = v
|
|
|
|
break
|
2021-06-17 18:00:37 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-01 05:51:44 +00:00
|
|
|
return volume.Projected != nil || volume.DownwardAPI != nil
|
2021-06-17 18:00:37 +00:00
|
|
|
}
|
|
|
|
|
2019-08-06 20:17:36 +00:00
|
|
|
// GetVolumeBackupsForPod returns a map, of volume name -> snapshot id,
|
|
|
|
// of the PodVolumeBackups that exist for the provided pod.
|
2021-06-17 18:00:37 +00:00
|
|
|
func GetVolumeBackupsForPod(podVolumeBackups []*velerov1api.PodVolumeBackup, pod *corev1api.Pod, sourcePodNs string) map[string]string {
|
2019-08-06 20:17:36 +00:00
|
|
|
volumes := make(map[string]string)
|
|
|
|
|
|
|
|
for _, pvb := range podVolumeBackups {
|
2021-02-22 19:16:00 +00:00
|
|
|
if !isPVBMatchPod(pvb, pod.GetName(), sourcePodNs) {
|
2019-11-04 23:18:08 +00:00
|
|
|
continue
|
2019-08-06 20:17:36 +00:00
|
|
|
}
|
2019-11-04 23:18:08 +00:00
|
|
|
|
|
|
|
// skip PVBs without a snapshot ID since there's nothing
|
|
|
|
// to restore (they could be failed, or for empty volumes).
|
|
|
|
if pvb.Status.SnapshotID == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-09-01 05:51:44 +00:00
|
|
|
// If the volume came from a projected or DownwardAPI source, skip its restore.
|
2021-06-17 18:00:37 +00:00
|
|
|
// This allows backups affected by https://github.com/vmware-tanzu/velero/issues/3863
|
2021-09-01 05:51:44 +00:00
|
|
|
// or https://github.com/vmware-tanzu/velero/issues/4053 to be restored successfully.
|
|
|
|
if volumeHasNonRestorableSource(pvb.Spec.Volume, pod.Spec.Volumes) {
|
2021-06-17 18:00:37 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-11-04 23:18:08 +00:00
|
|
|
volumes[pvb.Spec.Volume] = pvb.Status.SnapshotID
|
2019-08-06 20:17:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(volumes) > 0 {
|
|
|
|
return volumes
|
|
|
|
}
|
|
|
|
|
|
|
|
return getPodSnapshotAnnotations(pod)
|
|
|
|
}
|
|
|
|
|
2018-02-28 01:35:35 +00:00
|
|
|
// GetVolumesToBackup returns a list of volume names to backup for
|
|
|
|
// the provided pod.
|
2020-06-04 00:15:59 +00:00
|
|
|
// Deprecated: Use GetPodVolumesUsingRestic instead.
|
2018-02-28 01:35:35 +00:00
|
|
|
func GetVolumesToBackup(obj metav1.Object) []string {
|
|
|
|
annotations := obj.GetAnnotations()
|
|
|
|
if annotations == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-19 19:37:40 +00:00
|
|
|
backupsValue := annotations[VolumesToBackupAnnotation]
|
2018-02-28 01:35:35 +00:00
|
|
|
if backupsValue == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Split(backupsValue, ",")
|
|
|
|
}
|
|
|
|
|
2020-06-04 00:15:59 +00:00
|
|
|
func getVolumesToExclude(obj metav1.Object) []string {
|
|
|
|
annotations := obj.GetAnnotations()
|
|
|
|
if annotations == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Split(annotations[VolumesToExcludeAnnotation], ",")
|
|
|
|
}
|
|
|
|
|
|
|
|
func contains(list []string, k string) bool {
|
|
|
|
for _, i := range list {
|
|
|
|
if i == k {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPodVolumesUsingRestic returns a list of volume names to backup for the provided pod.
|
2020-06-15 22:26:44 +00:00
|
|
|
func GetPodVolumesUsingRestic(pod *corev1api.Pod, defaultVolumesToRestic bool) []string {
|
|
|
|
if !defaultVolumesToRestic {
|
2020-06-04 00:15:59 +00:00
|
|
|
return GetVolumesToBackup(pod)
|
|
|
|
}
|
|
|
|
|
|
|
|
volsToExclude := getVolumesToExclude(pod)
|
|
|
|
podVolumes := []string{}
|
|
|
|
for _, pv := range pod.Spec.Volumes {
|
|
|
|
// cannot backup hostpath volumes as they are not mounted into /var/lib/kubelet/pods
|
|
|
|
// and therefore not accessible to the restic daemon set.
|
|
|
|
if pv.HostPath != nil {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-28 03:27:49 +00:00
|
|
|
// don't backup volumes mounting secrets. Secrets will be backed up separately.
|
|
|
|
if pv.Secret != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// don't backup volumes mounting config maps. Config maps will be backed up separately.
|
|
|
|
if pv.ConfigMap != nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-06-10 07:39:39 +00:00
|
|
|
// don't backup volumes mounted as projected volumes, all data in those come from kube state.
|
|
|
|
if pv.Projected != nil {
|
|
|
|
continue
|
|
|
|
}
|
2021-09-01 05:51:44 +00:00
|
|
|
// don't backup DownwardAPI volumes, all data in those come from kube state.
|
|
|
|
if pv.DownwardAPI != nil {
|
|
|
|
continue
|
|
|
|
}
|
2020-06-04 00:15:59 +00:00
|
|
|
// don't backup volumes that are included in the exclude list.
|
|
|
|
if contains(volsToExclude, pv.Name) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// don't include volumes that mount the default service account token.
|
|
|
|
if strings.HasPrefix(pv.Name, "default-token") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
podVolumes = append(podVolumes, pv.Name)
|
|
|
|
}
|
|
|
|
return podVolumes
|
|
|
|
}
|
|
|
|
|
2018-02-28 01:35:35 +00:00
|
|
|
// SnapshotIdentifier uniquely identifies a restic snapshot
|
2019-01-25 03:33:07 +00:00
|
|
|
// taken by Velero.
|
2018-02-28 01:35:35 +00:00
|
|
|
type SnapshotIdentifier struct {
|
2018-09-25 20:20:58 +00:00
|
|
|
// VolumeNamespace is the namespace of the pod/volume that
|
|
|
|
// the restic snapshot is for.
|
|
|
|
VolumeNamespace string
|
2018-02-28 01:35:35 +00:00
|
|
|
|
2018-09-25 20:20:58 +00:00
|
|
|
// BackupStorageLocation is the backup's storage location
|
|
|
|
// name.
|
|
|
|
BackupStorageLocation string
|
|
|
|
|
|
|
|
// SnapshotID is the short ID of the restic snapshot.
|
2018-02-28 01:35:35 +00:00
|
|
|
SnapshotID string
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSnapshotsInBackup returns a list of all restic snapshot ids associated with
|
2019-01-25 03:33:07 +00:00
|
|
|
// a given Velero backup.
|
2022-04-25 10:03:08 +00:00
|
|
|
func GetSnapshotsInBackup(ctx context.Context, backup *velerov1api.Backup, kbClient client.Client) ([]SnapshotIdentifier, error) {
|
|
|
|
podVolumeBackups := &velerov1api.PodVolumeBackupList{}
|
|
|
|
options := &client.ListOptions{
|
|
|
|
LabelSelector: labels.Set(map[string]string{
|
|
|
|
velerov1api.BackupNameLabel: label.GetValidName(backup.Name),
|
|
|
|
}).AsSelector(),
|
|
|
|
}
|
2018-02-28 01:35:35 +00:00
|
|
|
|
2022-04-25 10:03:08 +00:00
|
|
|
err := kbClient.List(ctx, podVolumeBackups, options)
|
2018-02-28 01:35:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var res []SnapshotIdentifier
|
2022-04-25 10:03:08 +00:00
|
|
|
for _, item := range podVolumeBackups.Items {
|
2018-02-28 01:35:35 +00:00
|
|
|
if item.Status.SnapshotID == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
res = append(res, SnapshotIdentifier{
|
2018-09-25 20:20:58 +00:00
|
|
|
VolumeNamespace: item.Spec.Pod.Namespace,
|
|
|
|
BackupStorageLocation: backup.Spec.StorageLocation,
|
|
|
|
SnapshotID: item.Status.SnapshotID,
|
2018-02-28 01:35:35 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2020-03-24 21:50:48 +00:00
|
|
|
// TempCACertFile creates a temp file containing a CA bundle
|
|
|
|
// and returns its path. The caller should generally call os.Remove()
|
|
|
|
// to remove the file when done with it.
|
|
|
|
func TempCACertFile(caCert []byte, bsl string, fs filesystem.Interface) (string, error) {
|
|
|
|
file, err := fs.TempFile("", fmt.Sprintf("cacert-%s", bsl))
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := file.Write(caCert); err != nil {
|
|
|
|
// nothing we can do about an error closing the file here, and we're
|
|
|
|
// already returning an error about the write failing.
|
|
|
|
file.Close()
|
|
|
|
return "", errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
name := file.Name()
|
|
|
|
|
|
|
|
if err := file.Close(); err != nil {
|
|
|
|
return "", errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return name, nil
|
|
|
|
}
|
|
|
|
|
2018-06-13 23:40:18 +00:00
|
|
|
// NewPodVolumeRestoreListOptions creates a ListOptions with a label selector configured to
|
2019-01-25 03:33:07 +00:00
|
|
|
// find PodVolumeRestores for the restore identified by name.
|
|
|
|
func NewPodVolumeRestoreListOptions(name string) metav1.ListOptions {
|
2018-06-13 23:40:18 +00:00
|
|
|
return metav1.ListOptions{
|
2019-04-23 23:58:59 +00:00
|
|
|
LabelSelector: fmt.Sprintf("%s=%s", velerov1api.RestoreNameLabel, label.GetValidName(name)),
|
2018-06-13 23:40:18 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-25 21:46:29 +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
|
|
|
// CmdEnv returns a list of environment variables (in the format var=val) that
|
|
|
|
// should be used when running a restic command for a particular backend provider.
|
|
|
|
// This list is the current environment, plus any provider-specific variables restic needs.
|
|
|
|
func CmdEnv(backupLocation *velerov1api.BackupStorageLocation, credentialFileStore credentials.FileStore) ([]string, error) {
|
|
|
|
env := os.Environ()
|
|
|
|
customEnv := map[string]string{}
|
|
|
|
var err error
|
2018-09-25 21:46:29 +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
|
|
|
config := backupLocation.Spec.Config
|
|
|
|
if config == nil {
|
|
|
|
config = map[string]string{}
|
2018-09-25 21:46:29 +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
|
|
|
if backupLocation.Spec.Credential != nil {
|
|
|
|
credsFile, err := credentialFileStore.Path(backupLocation.Spec.Credential)
|
|
|
|
if err != nil {
|
|
|
|
return []string{}, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
config[credentialsFileKey] = credsFile
|
2018-09-25 21:46:29 +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(backupLocation.Spec.Provider)
|
2019-12-09 14:46:02 +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
|
|
|
switch backendType {
|
|
|
|
case AWSBackend:
|
|
|
|
customEnv, err = getS3ResticEnvVars(config)
|
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
|
|
|
case AzureBackend:
|
|
|
|
customEnv, err = getAzureResticEnvVars(config)
|
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
|
|
|
case GCPBackend:
|
|
|
|
customEnv, err = getGCPResticEnvVars(config)
|
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
2019-12-09 14:46:02 +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
|
|
|
for k, v := range customEnv {
|
2019-12-09 14:46:02 +00:00
|
|
|
env = append(env, fmt.Sprintf("%s=%s", k, v))
|
|
|
|
}
|
|
|
|
|
|
|
|
return env, nil
|
|
|
|
}
|