From 70ed252dd67e0a52e5ecf613cddeae971000edcb Mon Sep 17 00:00:00 2001 From: Javier de la Torre Date: Sat, 21 Nov 2026 19:41:05 +0100 Subject: [PATCH] shell: mask every remote secret in remote.configure's listing (#12856) * shell: mask every remote secret in remote.configure's listing remote.configure without arguments lists the configured remotes and replaces their secrets with stars, but the list was incomplete: the Azure account key, the Backblaze application key, the Baidu secret key (the access key was masked in its place) and the Contabo secret key were printed in clear, and so were inline gcs service account credentials (the field holds a file path or the JSON itself, private key included). Move the masking into maskRemoteConfSecrets, which covers every secret field of RemoteConf, and test it. An unset secret stays empty, so an Azure remote that authenticates with Entra ID still lists an empty key; a gcs credentials file path stays listed. * shell: compare each masked remote secret with its full-length mask Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Chris Lu Co-authored-by: Claude Opus 5.5 --- weed/shell/command_remote_configure.go | 36 ++++++++--- weed/shell/command_remote_configure_test.go | 69 +++++++++++++++++++++ 2 files changed, 97 insertions(+), 8 deletions(-) create mode 100644 weed/shell/command_remote_configure_test.go diff --git a/weed/shell/command_remote_configure.go b/weed/shell/command_remote_configure.go index af29b52556..a5408dea9c 100644 --- a/weed/shell/command_remote_configure.go +++ b/weed/shell/command_remote_configure.go @@ -277,14 +277,7 @@ func (c *commandRemoteConfigure) listExistingRemoteStorages(commandEnv *CommandE return fmt.Errorf("unmarshal %s/%s: %v", filer.DirectoryEtcRemote, entry.Name, err) } - // change secret key to stars - conf.S3SecretKey = strings.Repeat("*", len(conf.S3SecretKey)) - conf.AliyunSecretKey = strings.Repeat("*", len(conf.AliyunSecretKey)) - conf.BaiduAccessKey = strings.Repeat("*", len(conf.BaiduAccessKey)) - conf.FilebaseSecretKey = strings.Repeat("*", len(conf.FilebaseSecretKey)) - conf.StorjSecretKey = strings.Repeat("*", len(conf.StorjSecretKey)) - conf.TencentSecretKey = strings.Repeat("*", len(conf.TencentSecretKey)) - conf.WasabiSecretKey = strings.Repeat("*", len(conf.WasabiSecretKey)) + maskRemoteConfSecrets(conf) return filer.ProtoToText(writer, conf) @@ -325,3 +318,30 @@ func (c *commandRemoteConfigure) saveRemoteStorage(commandEnv *CommandEnv, write return nil } + +// maskRemoteConfSecrets replaces every secret of a remote configuration with +// stars of the same length before it is listed: the listing still shows +// whether a secret is set, never its value. +func maskRemoteConfSecrets(conf *remote_pb.RemoteConf) { + for _, secret := range []*string{ + &conf.S3SecretKey, + &conf.AzureAccountKey, + &conf.BackblazeApplicationKey, + &conf.AliyunSecretKey, + &conf.TencentSecretKey, + &conf.BaiduAccessKey, + &conf.BaiduSecretKey, + &conf.WasabiSecretKey, + &conf.FilebaseSecretKey, + &conf.StorjSecretKey, + &conf.ContaboSecretKey, + } { + *secret = strings.Repeat("*", len(*secret)) + } + // The gcs credentials are a file path (remote.configure's + // -gcs.appCredentialsFile) or the service account JSON itself, private key + // included: a path is listed, inline JSON is masked. + if strings.HasPrefix(strings.TrimSpace(conf.GcsGoogleApplicationCredentials), "{") { + conf.GcsGoogleApplicationCredentials = strings.Repeat("*", len(conf.GcsGoogleApplicationCredentials)) + } +} diff --git a/weed/shell/command_remote_configure_test.go b/weed/shell/command_remote_configure_test.go new file mode 100644 index 0000000000..5021a05452 --- /dev/null +++ b/weed/shell/command_remote_configure_test.go @@ -0,0 +1,69 @@ +package shell + +import ( + "strings" + "testing" + + "github.com/seaweedfs/seaweedfs/weed/pb/remote_pb" + "google.golang.org/protobuf/proto" +) + +func TestMaskRemoteConfSecrets(t *testing.T) { + conf := &remote_pb.RemoteConf{ + Type: "azure", + Name: "r1", + S3AccessKey: "AKID", + S3SecretKey: "s3-secret", + AzureAccountName: "account", + AzureAccountKey: "azure-account-key==", + BackblazeApplicationKey: "b2-secret", + AliyunSecretKey: "aliyun-secret", + TencentSecretKey: "tencent-secret", + BaiduAccessKey: "baidu-ak", + BaiduSecretKey: "baidu-secret", + WasabiSecretKey: "wasabi-secret", + FilebaseSecretKey: "filebase-secret", + StorjSecretKey: "storj-secret", + ContaboSecretKey: "contabo-secret", + } + original := proto.Clone(conf).(*remote_pb.RemoteConf) + maskRemoteConfSecrets(conf) + for name, pair := range map[string][2]string{ + "s3": {original.S3SecretKey, conf.S3SecretKey}, + "azure": {original.AzureAccountKey, conf.AzureAccountKey}, + "backblaze": {original.BackblazeApplicationKey, conf.BackblazeApplicationKey}, + "aliyun": {original.AliyunSecretKey, conf.AliyunSecretKey}, + "tencent": {original.TencentSecretKey, conf.TencentSecretKey}, + "baidu ak": {original.BaiduAccessKey, conf.BaiduAccessKey}, + "baidu sk": {original.BaiduSecretKey, conf.BaiduSecretKey}, + "wasabi": {original.WasabiSecretKey, conf.WasabiSecretKey}, + "filebase": {original.FilebaseSecretKey, conf.FilebaseSecretKey}, + "storj": {original.StorjSecretKey, conf.StorjSecretKey}, + "contabo": {original.ContaboSecretKey, conf.ContaboSecretKey}, + } { + if want := strings.Repeat("*", len(pair[0])); pair[1] != want { + t.Errorf("%s secret masked as %q, want %q", name, pair[1], want) + } + } + if conf.AzureAccountName != "account" || conf.S3AccessKey != "AKID" { + t.Errorf("non-secret fields changed: %q %q", conf.AzureAccountName, conf.S3AccessKey) + } + + empty := &remote_pb.RemoteConf{Type: "azure"} + maskRemoteConfSecrets(empty) + if empty.AzureAccountKey != "" { + t.Errorf("an unset key must stay empty (Entra ID), got %q", empty.AzureAccountKey) + } + + inline := ` {"type":"service_account","private_key":"-----BEGIN PRIVATE KEY-----"}` + gcs := &remote_pb.RemoteConf{Type: "gcs", GcsGoogleApplicationCredentials: inline} + maskRemoteConfSecrets(gcs) + if want := strings.Repeat("*", len(inline)); gcs.GcsGoogleApplicationCredentials != want { + t.Errorf("inline gcs credentials masked as %q, want %q", gcs.GcsGoogleApplicationCredentials, want) + } + path := &remote_pb.RemoteConf{Type: "gcs", GcsGoogleApplicationCredentials: "/etc/seaweedfs/gcs.json"} + maskRemoteConfSecrets(path) + if path.GcsGoogleApplicationCredentials != "/etc/seaweedfs/gcs.json" { + t.Errorf("a gcs credentials path must stay listed, got %q", path.GcsGoogleApplicationCredentials) + } +}