From fb0a342a2061ed832c76a7dccb1c278eceed5fdc Mon Sep 17 00:00:00 2001 From: Brad Davidson Date: Tue, 14 Jun 2022 15:12:28 -0700 Subject: [PATCH] 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: Brad Davidson --- pkg/etcd/etcd.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/pkg/etcd/etcd.go b/pkg/etcd/etcd.go index 2b86a21b91..f4fa91087b 100644 --- a/pkg/etcd/etcd.go +++ b/pkg/etcd/etcd.go @@ -15,6 +15,7 @@ import ( "net/url" "os" "path/filepath" + "regexp" "runtime" "sort" "strconv" @@ -82,6 +83,8 @@ var ( ErrAddressNotSet = errors.New("apiserver addresses not yet set") ErrNotMember = errNotMember() + + invalidKeyChars = regexp.MustCompile(`[^-._a-zA-Z0-9]`) ) type NodeControllerGetter func() controllerv1.NodeController @@ -1682,6 +1685,7 @@ func (e *ETCD) addSnapshotData(sf snapshotFile) error { } snapshotConfigMap, getErr := e.config.Runtime.Core.Core().V1().ConfigMap().Get(metav1.NamespaceSystem, snapshotConfigMapName, metav1.GetOptions{}) + sfKey := generateSnapshotConfigMapKey(sf) marshalledSnapshotFile, err := json.Marshal(sf) if err != nil { return err @@ -1692,7 +1696,7 @@ func (e *ETCD) addSnapshotData(sf snapshotFile) error { Name: snapshotConfigMapName, 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) return err @@ -1702,7 +1706,6 @@ func (e *ETCD) addSnapshotData(sf snapshotFile) error { snapshotConfigMap.Data = make(map[string]string) } - sfKey := generateSnapshotConfigMapKey(sf) snapshotConfigMap.Data[sfKey] = string(marshalledSnapshotFile) _, err = e.config.Runtime.Core.Core().V1().ConfigMap().Update(snapshotConfigMap) @@ -1711,13 +1714,11 @@ func (e *ETCD) addSnapshotData(sf snapshotFile) error { } func generateSnapshotConfigMapKey(sf snapshotFile) string { - var sfKey string + name := invalidKeyChars.ReplaceAllString(sf.Name, "_") if sf.NodeName == "s3" { - sfKey = "s3-" + sf.Name - } else { - sfKey = "local-" + sf.Name + return "s3-" + name } - return sfKey + return "local-" + name } // ReconcileSnapshotData reconciles snapshot data in the snapshot ConfigMap.