Commit ecddd267 authored by Brad Davidson's avatar Brad Davidson Committed by Brad Davidson

Sanitize filenames for use in configmap keys

If the user points S3 backups at a bucket containing other files, those file names may not be valid configmap keys. For example, RKE1 generates backup files with names like `s3-c-zrjnb-rs-6hxpk_2022-05-05T12:05:15Z.zip`; the semicolons in the timestamp portion of the name are not allowed for use in configmap keys. Signed-off-by: 's avatarBrad Davidson <brad.davidson@rancher.com>
parent 642402ea
...@@ -14,6 +14,7 @@ import ( ...@@ -14,6 +14,7 @@ import (
"net/url" "net/url"
"os" "os"
"path/filepath" "path/filepath"
"regexp"
"runtime" "runtime"
"sort" "sort"
"strconv" "strconv"
...@@ -79,6 +80,8 @@ var ( ...@@ -79,6 +80,8 @@ var (
ErrAddressNotSet = errors.New("apiserver addresses not yet set") ErrAddressNotSet = errors.New("apiserver addresses not yet set")
ErrNotMember = errNotMember() ErrNotMember = errNotMember()
invalidKeyChars = regexp.MustCompile(`[^-._a-zA-Z0-9]`)
) )
type NodeControllerGetter func() controllerv1.NodeController type NodeControllerGetter func() controllerv1.NodeController
...@@ -1599,6 +1602,7 @@ func (e *ETCD) addSnapshotData(sf snapshotFile) error { ...@@ -1599,6 +1602,7 @@ func (e *ETCD) addSnapshotData(sf snapshotFile) error {
} }
snapshotConfigMap, getErr := e.config.Runtime.Core.Core().V1().ConfigMap().Get(metav1.NamespaceSystem, snapshotConfigMapName, metav1.GetOptions{}) snapshotConfigMap, getErr := e.config.Runtime.Core.Core().V1().ConfigMap().Get(metav1.NamespaceSystem, snapshotConfigMapName, metav1.GetOptions{})
sfKey := generateSnapshotConfigMapKey(sf)
marshalledSnapshotFile, err := json.Marshal(sf) marshalledSnapshotFile, err := json.Marshal(sf)
if err != nil { if err != nil {
return err return err
...@@ -1609,7 +1613,7 @@ func (e *ETCD) addSnapshotData(sf snapshotFile) error { ...@@ -1609,7 +1613,7 @@ func (e *ETCD) addSnapshotData(sf snapshotFile) error {
Name: snapshotConfigMapName, Name: snapshotConfigMapName,
Namespace: metav1.NamespaceSystem, Namespace: metav1.NamespaceSystem,
}, },
Data: map[string]string{sf.Name: string(marshalledSnapshotFile)}, Data: map[string]string{sfKey: string(marshalledSnapshotFile)},
} }
_, err := e.config.Runtime.Core.Core().V1().ConfigMap().Create(&cm) _, err := e.config.Runtime.Core.Core().V1().ConfigMap().Create(&cm)
return err return err
...@@ -1619,7 +1623,6 @@ func (e *ETCD) addSnapshotData(sf snapshotFile) error { ...@@ -1619,7 +1623,6 @@ func (e *ETCD) addSnapshotData(sf snapshotFile) error {
snapshotConfigMap.Data = make(map[string]string) snapshotConfigMap.Data = make(map[string]string)
} }
sfKey := generateSnapshotConfigMapKey(sf)
snapshotConfigMap.Data[sfKey] = string(marshalledSnapshotFile) snapshotConfigMap.Data[sfKey] = string(marshalledSnapshotFile)
_, err = e.config.Runtime.Core.Core().V1().ConfigMap().Update(snapshotConfigMap) _, err = e.config.Runtime.Core.Core().V1().ConfigMap().Update(snapshotConfigMap)
...@@ -1628,13 +1631,11 @@ func (e *ETCD) addSnapshotData(sf snapshotFile) error { ...@@ -1628,13 +1631,11 @@ func (e *ETCD) addSnapshotData(sf snapshotFile) error {
} }
func generateSnapshotConfigMapKey(sf snapshotFile) string { func generateSnapshotConfigMapKey(sf snapshotFile) string {
var sfKey string name := invalidKeyChars.ReplaceAllString(sf.Name, "_")
if sf.NodeName == "s3" { if sf.NodeName == "s3" {
sfKey = "s3-" + sf.Name return "s3-" + name
} else {
sfKey = "local-" + sf.Name
} }
return sfKey return "local-" + name
} }
// ReconcileSnapshotData reconciles snapshot data in the snapshot ConfigMap. // ReconcileSnapshotData reconciles snapshot data in the snapshot ConfigMap.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment