From 69c2cff0d8a509f0589ff73aeaeac06950c7edbd Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Fri, 4 Dec 2026 11:24:34 -0800 Subject: [PATCH] admin: show the configured S3 endpoint in the service account CLI example (#13395) * admin: show the configured S3 endpoint in the service account CLI example The credentials modal always printed AWS_ENDPOINT_URL=http://localhost:8333. Use s3.public_endpoint from admin.toml (or weed mini's own S3 address) when it is set, and keep localhost:8333 only as the fallback. Co-Authored-By: Claude Opus 5.5 * admin: test the service account CLI example endpoint Co-Authored-By: Claude Opus 5.5 * admin: shell-quote the S3 endpoint in the service account CLI example A configured endpoint may contain characters such as ';' in its path, which would split the copied export command. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- weed/admin/dash/types.go | 1 + .../handlers/service_account_handlers.go | 2 ++ weed/admin/view/app/service_accounts.templ | 6 +++- weed/admin/view/app/service_accounts_templ.go | 30 +++++++++++++++- weed/admin/view/app/service_accounts_test.go | 34 +++++++++++++++++++ weed/admin/view/app/template_helpers.go | 11 ++++++ weed/admin/view/app/template_helpers_test.go | 15 ++++++++ weed/command/scaffold/admin.toml | 3 +- 8 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 weed/admin/view/app/service_accounts_test.go diff --git a/weed/admin/dash/types.go b/weed/admin/dash/types.go index 9453cab71a..cb1040d32f 100644 --- a/weed/admin/dash/types.go +++ b/weed/admin/dash/types.go @@ -657,6 +657,7 @@ type ServiceAccountsData struct { TotalAccounts int `json:"total_accounts"` ActiveAccounts int `json:"active_accounts"` AvailableUsers []string `json:"available_users"` // For parent user dropdown + S3Endpoint string `json:"s3_endpoint,omitempty"` LastUpdated time.Time `json:"last_updated"` } diff --git a/weed/admin/handlers/service_account_handlers.go b/weed/admin/handlers/service_account_handlers.go index 6e572a88b1..257c5a3295 100644 --- a/weed/admin/handlers/service_account_handlers.go +++ b/weed/admin/handlers/service_account_handlers.go @@ -180,6 +180,7 @@ func (h *ServiceAccountHandlers) getServiceAccountsData(r *http.Request) dash.Se Username: username, ServiceAccounts: []dash.ServiceAccount{}, TotalAccounts: 0, + S3Endpoint: h.adminServer.GetS3Endpoint(), LastUpdated: time.Now(), } } @@ -209,6 +210,7 @@ func (h *ServiceAccountHandlers) getServiceAccountsData(r *http.Request) dash.Se TotalAccounts: len(accounts), ActiveAccounts: activeCount, AvailableUsers: availableUsers, + S3Endpoint: h.adminServer.GetS3Endpoint(), LastUpdated: time.Now(), } } diff --git a/weed/admin/view/app/service_accounts.templ b/weed/admin/view/app/service_accounts.templ index 1face7f3e1..6f656df058 100644 --- a/weed/admin/view/app/service_accounts.templ +++ b/weed/admin/view/app/service_accounts.templ @@ -267,7 +267,11 @@ templ ServiceAccounts(data dash.ServiceAccountsData) {
export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=
-
export AWS_ENDPOINT_URL=http://localhost:8333
+ if data.S3Endpoint != "" { +
export AWS_ENDPOINT_URL={ shellQuote(data.S3Endpoint) }
+ } else { +
export AWS_ENDPOINT_URL=http://localhost:8333
+ }
# List buckets
aws s3 ls
# Upload a file
diff --git a/weed/admin/view/app/service_accounts_templ.go b/weed/admin/view/app/service_accounts_templ.go index 8a8f27fad0..70d345c265 100644 --- a/weed/admin/view/app/service_accounts_templ.go +++ b/weed/admin/view/app/service_accounts_templ.go @@ -256,7 +256,35 @@ func ServiceAccounts(data dash.ServiceAccountsData) templ.Component { return templ_7745c5c3_Err } } - templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 25, " The service account will inherit permissions from this user
Leave empty for no expiration
Service Account Details
Service Account Created Successfully
Important: This is the only time you will see the secret access key. Please save it securely.
AWS CLI Configuration

Use these credentials to configure AWS CLI or SDKs:

Example AWS CLI Usage:
export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=
export AWS_ENDPOINT_URL=http://localhost:8333
# List buckets
aws s3 ls
# Upload a file
aws s3 cp myfile.txt s3://mybucket/
") + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 25, " The service account will inherit permissions from this user
Leave empty for no expiration
Service Account Details
Service Account Created Successfully
Important: This is the only time you will see the secret access key. Please save it securely.
AWS CLI Configuration

Use these credentials to configure AWS CLI or SDKs:

Example AWS CLI Usage:
export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + if data.S3Endpoint != "" { + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 26, "
export AWS_ENDPOINT_URL=") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var15 string + templ_7745c5c3_Var15, templ_7745c5c3_Err = templ.JoinStringErrs(shellQuote(data.S3Endpoint)) + if templ_7745c5c3_Err != nil { + return templ.Error{Err: templ_7745c5c3_Err, FileName: `view/app/service_accounts.templ`, Line: 271, Col: 90} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var15)) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 27, "
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } else { + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 28, "
export AWS_ENDPOINT_URL=http://localhost:8333
") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 29, "
# List buckets
aws s3 ls
# Upload a file
aws s3 cp myfile.txt s3://mybucket/
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/weed/admin/view/app/service_accounts_test.go b/weed/admin/view/app/service_accounts_test.go new file mode 100644 index 0000000000..8958b531b6 --- /dev/null +++ b/weed/admin/view/app/service_accounts_test.go @@ -0,0 +1,34 @@ +package app + +import ( + "bytes" + "context" + "strings" + "testing" + + "github.com/seaweedfs/seaweedfs/weed/admin/dash" +) + +func TestServiceAccountsCLIExampleUsesS3Endpoint(t *testing.T) { + tests := []struct { + endpoint string + want string + }{ + {endpoint: "https://s3.example.com", want: "export AWS_ENDPOINT_URL=https://s3.example.com<"}, + {endpoint: "https://proxy.example.com/s3;v1", want: "export AWS_ENDPOINT_URL='https://proxy.example.com/s3;v1'<"}, + {endpoint: "", want: "export AWS_ENDPOINT_URL=http://localhost:8333<"}, + } + for _, tt := range tests { + var buf bytes.Buffer + if err := ServiceAccounts(dash.ServiceAccountsData{S3Endpoint: tt.endpoint}).Render(context.Background(), &buf); err != nil { + t.Fatalf("render with endpoint %q: %v", tt.endpoint, err) + } + got := buf.String() + if !strings.Contains(got, tt.want) { + t.Errorf("endpoint %q: page is missing %q", tt.endpoint, tt.want) + } + if tt.endpoint != "" && strings.Contains(got, "localhost:8333") { + t.Errorf("endpoint %q: page still shows localhost:8333", tt.endpoint) + } + } +} diff --git a/weed/admin/view/app/template_helpers.go b/weed/admin/view/app/template_helpers.go index c0f8e2a367..1ce87d4988 100644 --- a/weed/admin/view/app/template_helpers.go +++ b/weed/admin/view/app/template_helpers.go @@ -106,3 +106,14 @@ func formatLastHeartbeat(t time.Time) string { } return t.Format("2006-01-02 15:04:05") } + +// shellQuote returns s as one POSIX shell word, single-quoted unless every +// character is safe unquoted. +func shellQuote(s string) string { + if s != "" && strings.IndexFunc(s, func(r rune) bool { + return !(r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z' || r >= '0' && r <= '9' || strings.ContainsRune("@%+=:,./_-", r)) + }) < 0 { + return s + } + return "'" + strings.ReplaceAll(s, "'", `'\''`) + "'" +} diff --git a/weed/admin/view/app/template_helpers_test.go b/weed/admin/view/app/template_helpers_test.go index 44f9902dc1..b2c32722f0 100644 --- a/weed/admin/view/app/template_helpers_test.go +++ b/weed/admin/view/app/template_helpers_test.go @@ -14,3 +14,18 @@ func TestFormatLastHeartbeat(t *testing.T) { t.Fatalf("formatLastHeartbeat = %q", got) } } + +func TestShellQuote(t *testing.T) { + tests := map[string]string{ + "https://s3.example.com:8333/s3": "https://s3.example.com:8333/s3", + "https://proxy.example.com/s3;v1": "'https://proxy.example.com/s3;v1'", + "https://example.com/a b": "'https://example.com/a b'", + "https://example.com/it's": `'https://example.com/it'\''s'`, + "": "''", + } + for in, want := range tests { + if got := shellQuote(in); got != want { + t.Errorf("shellQuote(%q) = %q, want %q", in, got, want) + } + } +} diff --git a/weed/command/scaffold/admin.toml b/weed/command/scaffold/admin.toml index 29e074544e..9607df8474 100644 --- a/weed/command/scaffold/admin.toml +++ b/weed/command/scaffold/admin.toml @@ -14,7 +14,8 @@ [s3] # The address clients reach the S3 gateway at, including the scheme. # The file browser uses it to offer copyable object URLs; without it, -# the copy action is hidden. +# the copy action is hidden. The service account page shows it in its +# AWS CLI example instead of http://localhost:8333. # public_endpoint = "https://s3.example.com" [maintenance]