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 <noreply@anthropic.com>

* admin: test the service account CLI example endpoint

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* 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 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Chris LuandClaude Opus 5.5 authored and GitHub committed 2026-12-04 11:24:34 -08:00
1 parent 25e0c6080a
commit 69c2cff0d8
8 files changed
+99 -3

No files matched your search

+1
View File
@@ -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"`
}
@@ -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(),
}
}
+5 -1
View File
@@ -267,7 +267,11 @@ templ ServiceAccounts(data dash.ServiceAccountsData) {
<div class="font-monospace small">
<div>export AWS_ACCESS_KEY_ID=<span id="exampleAccessKey"></span></div>
<div>export AWS_SECRET_ACCESS_KEY=<span id="exampleSecretKey"></span></div>
<div>export AWS_ENDPOINT_URL=http://localhost:8333</div>
if data.S3Endpoint != "" {
<div>export AWS_ENDPOINT_URL={ shellQuote(data.S3Endpoint) }</div>
} else {
<div>export AWS_ENDPOINT_URL=http://localhost:8333</div>
}
<div class="mt-2"># List buckets</div>
<div>aws s3 ls</div>
<div class="mt-2"># Upload a file</div>
+29 -1
View File
@@ -256,7 +256,35 @@ func ServiceAccounts(data dash.ServiceAccountsData) templ.Component {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 25, "</select> <small class=\"form-text text-muted\">The service account will inherit permissions from this user</small></div><div class=\"mb-3\"><label for=\"description\" class=\"form-label\">Description</label> <textarea class=\"form-control\" id=\"description\" name=\"description\" rows=\"2\" placeholder=\"What is this service account used for?\"></textarea></div><div class=\"mb-3\"><label for=\"expiration\" class=\"form-label\">Expiration (optional)</label> <input type=\"datetime-local\" class=\"form-control\" id=\"expiration\" name=\"expiration\"> <small class=\"form-text text-muted\">Leave empty for no expiration</small></div></form></div><div class=\"modal-footer\"><button type=\"button\" class=\"btn btn-secondary\" data-bs-dismiss=\"modal\">Cancel</button> <button type=\"button\" class=\"btn btn-primary\" onclick=\"handleCreateServiceAccount()\">Create</button></div></div></div></div><!-- Service Account Details Modal --><div class=\"modal fade\" id=\"saDetailsModal\" tabindex=\"-1\" role=\"dialog\"><div class=\"modal-dialog modal-lg\" role=\"document\"><div class=\"modal-content\"><div class=\"modal-header\"><h5 class=\"modal-title\"><i class=\"bi bi-robot me-2\"></i>Service Account Details</h5><button type=\"button\" class=\"btn-close\" data-bs-dismiss=\"modal\"></button></div><div class=\"modal-body\" id=\"saDetailsContent\"><!-- Content will be loaded dynamically --></div><div class=\"modal-footer\"><button type=\"button\" class=\"btn btn-secondary\" data-bs-dismiss=\"modal\">Close</button></div></div></div></div><!-- Credentials Display Modal --><div class=\"modal fade\" id=\"credentialsModal\" tabindex=\"-1\" role=\"dialog\" data-bs-backdrop=\"static\" data-bs-keyboard=\"false\"><div class=\"modal-dialog modal-lg\" role=\"document\"><div class=\"modal-content\"><div class=\"modal-header bg-success text-white\"><h5 class=\"modal-title\"><i class=\"bi bi-check-circle me-2\"></i>Service Account Created Successfully</h5></div><div class=\"modal-body\"><div class=\"alert alert-warning\"><i class=\"bi bi-exclamation-triangle me-2\"></i> <strong>Important:</strong> This is the only time you will see the secret access key. Please save it securely.</div><div class=\"mb-4\"><h6 class=\"text-muted mb-3\">AWS CLI Configuration</h6><p class=\"text-muted small\">Use these credentials to configure AWS CLI or SDKs:</p><div class=\"mb-3\"><label class=\"form-label fw-bold\">AWS_ACCESS_KEY_ID</label><div class=\"input-group\"><input type=\"text\" class=\"form-control font-monospace\" id=\"displayAccessKey\" readonly> <button class=\"btn btn-outline-secondary\" type=\"button\" onclick=\"copyCredentialToClipboard(this, 'displayAccessKey')\"><i class=\"bi bi-copy\"></i> Copy</button></div></div><div class=\"mb-3\"><label class=\"form-label fw-bold\">AWS_SECRET_ACCESS_KEY</label><div class=\"input-group\"><input type=\"text\" class=\"form-control font-monospace\" id=\"displaySecretKey\" readonly> <button class=\"btn btn-outline-secondary\" type=\"button\" onclick=\"copyCredentialToClipboard(this, 'displaySecretKey')\"><i class=\"bi bi-copy\"></i> Copy</button></div></div></div><div class=\"bg-light p-3 rounded\"><h6 class=\"text-muted mb-2\">Example AWS CLI Usage:</h6><div class=\"font-monospace small\"><div>export AWS_ACCESS_KEY_ID=<span id=\"exampleAccessKey\"></span></div><div>export AWS_SECRET_ACCESS_KEY=<span id=\"exampleSecretKey\"></span></div><div>export AWS_ENDPOINT_URL=http://localhost:8333</div><div class=\"mt-2\"># List buckets</div><div>aws s3 ls</div><div class=\"mt-2\"># Upload a file</div><div>aws s3 cp myfile.txt s3://mybucket/</div></div></div></div><div class=\"modal-footer\"><button type=\"button\" class=\"btn btn-primary\" onclick=\"closeCredentialsModal()\"><i class=\"bi bi-check me-1\"></i>I have saved the credentials</button></div></div></div></div><!-- JavaScript for service account management --><script>\n document.addEventListener('DOMContentLoaded', function() {\n document.addEventListener('click', function(e) {\n const button = e.target.closest('[data-action]');\n if (!button) return;\n \n const action = button.getAttribute('data-action');\n const saId = button.getAttribute('data-sa-id');\n \n switch (action) {\n case 'show-sa-details':\n showSADetails(saId);\n break;\n case 'toggle-sa-status':\n toggleSAStatus(saId, button.getAttribute('data-current-status'));\n break;\n case 'delete-sa':\n deleteSA(saId);\n break;\n }\n });\n });\n\n async function showSADetails(id) {\n try {\n const encodedId = encodeURIComponent(id);\n Line truncated
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 25, "</select> <small class=\"form-text text-muted\">The service account will inherit permissions from this user</small></div><div class=\"mb-3\"><label for=\"description\" class=\"form-label\">Description</label> <textarea class=\"form-control\" id=\"description\" name=\"description\" rows=\"2\" placeholder=\"What is this service account used for?\"></textarea></div><div class=\"mb-3\"><label for=\"expiration\" class=\"form-label\">Expiration (optional)</label> <input type=\"datetime-local\" class=\"form-control\" id=\"expiration\" name=\"expiration\"> <small class=\"form-text text-muted\">Leave empty for no expiration</small></div></form></div><div class=\"modal-footer\"><button type=\"button\" class=\"btn btn-secondary\" data-bs-dismiss=\"modal\">Cancel</button> <button type=\"button\" class=\"btn btn-primary\" onclick=\"handleCreateServiceAccount()\">Create</button></div></div></div></div><!-- Service Account Details Modal --><div class=\"modal fade\" id=\"saDetailsModal\" tabindex=\"-1\" role=\"dialog\"><div class=\"modal-dialog modal-lg\" role=\"document\"><div class=\"modal-content\"><div class=\"modal-header\"><h5 class=\"modal-title\"><i class=\"bi bi-robot me-2\"></i>Service Account Details</h5><button type=\"button\" class=\"btn-close\" data-bs-dismiss=\"modal\"></button></div><div class=\"modal-body\" id=\"saDetailsContent\"><!-- Content will be loaded dynamically --></div><div class=\"modal-footer\"><button type=\"button\" class=\"btn btn-secondary\" data-bs-dismiss=\"modal\">Close</button></div></div></div></div><!-- Credentials Display Modal --><div class=\"modal fade\" id=\"credentialsModal\" tabindex=\"-1\" role=\"dialog\" data-bs-backdrop=\"static\" data-bs-keyboard=\"false\"><div class=\"modal-dialog modal-lg\" role=\"document\"><div class=\"modal-content\"><div class=\"modal-header bg-success text-white\"><h5 class=\"modal-title\"><i class=\"bi bi-check-circle me-2\"></i>Service Account Created Successfully</h5></div><div class=\"modal-body\"><div class=\"alert alert-warning\"><i class=\"bi bi-exclamation-triangle me-2\"></i> <strong>Important:</strong> This is the only time you will see the secret access key. Please save it securely.</div><div class=\"mb-4\"><h6 class=\"text-muted mb-3\">AWS CLI Configuration</h6><p class=\"text-muted small\">Use these credentials to configure AWS CLI or SDKs:</p><div class=\"mb-3\"><label class=\"form-label fw-bold\">AWS_ACCESS_KEY_ID</label><div class=\"input-group\"><input type=\"text\" class=\"form-control font-monospace\" id=\"displayAccessKey\" readonly> <button class=\"btn btn-outline-secondary\" type=\"button\" onclick=\"copyCredentialToClipboard(this, 'displayAccessKey')\"><i class=\"bi bi-copy\"></i> Copy</button></div></div><div class=\"mb-3\"><label class=\"form-label fw-bold\">AWS_SECRET_ACCESS_KEY</label><div class=\"input-group\"><input type=\"text\" class=\"form-control font-monospace\" id=\"displaySecretKey\" readonly> <button class=\"btn btn-outline-secondary\" type=\"button\" onclick=\"copyCredentialToClipboard(this, 'displaySecretKey')\"><i class=\"bi bi-copy\"></i> Copy</button></div></div></div><div class=\"bg-light p-3 rounded\"><h6 class=\"text-muted mb-2\">Example AWS CLI Usage:</h6><div class=\"font-monospace small\"><div>export AWS_ACCESS_KEY_ID=<span id=\"exampleAccessKey\"></span></div><div>export AWS_SECRET_ACCESS_KEY=<span id=\"exampleSecretKey\"></span></div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if data.S3Endpoint != "" {
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 26, "<div>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, "</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
} else {
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 28, "<div>export AWS_ENDPOINT_URL=http://localhost:8333</div>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 29, "<div class=\"mt-2\"># List buckets</div><div>aws s3 ls</div><div class=\"mt-2\"># Upload a file</div><div>aws s3 cp myfile.txt s3://mybucket/</div></div></div></div><div class=\"modal-footer\"><button type=\"button\" class=\"btn btn-primary\" onclick=\"closeCredentialsModal()\"><i class=\"bi bi-check me-1\"></i>I have saved the credentials</button></div></div></div></div><!-- JavaScript for service account management --><script>\n document.addEventListener('DOMContentLoaded', function() {\n document.addEventListener('click', function(e) {\n const button = e.target.closest('[data-action]');\n if (!button) return;\n \n const action = button.getAttribute('data-action');\n const saId = button.getAttribute('data-sa-id');\n \n switch (action) {\n case 'show-sa-details':\n showSADetails(saId);\n break;\n case 'toggle-sa-status':\n toggleSAStatus(saId, button.getAttribute('data-current-status'));\n break;\n case 'delete-sa':\n deleteSA(saId);\n break;\n }\n });\n });\n\n async function showSADetails(id) {\n try {\n const encodedId = encodeURIComponent(id);\n const response = await fetch(basePath(`/api/service-accounts/${encodedId}`));\n if (response.ok) {\n const sa = await response.json();\n document.getElementById('saDetailsContent').innerHTML = createSADetailsContent(sa);\n const modal = new bootstrap.Modal(document.getElementById('saDetailsModal'));\n modal.show();\n } else {\n showErrorMessage('Failed to load service account details');\n }\n } catch (error) {\n console.error('Error loading service account details:', error);\n showErrorMessage('Failed to load service account details');\n }\n }\n\n async function toggleSAStatus(id, currentStatus) {\n const newStatus = currentStatus === 'Active' ? 'Inactive' : 'Active';\n try {\n const encodedId = encodeURIComponent(id);\n const response = await fetch(basePath(`/api/service-accounts/${encodedId}`), {\n method: 'PUT',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({ status: newStatus })\n });\n \n if (response.ok) {\n showSuccessMessage(`Service account ${newStatus === 'Active' ? 'activated' : 'deactivated'}`);\n setTimeout(() => window.location.reload(), 1000);\n } else {\n const error = await response.json();\n showErrorMessage('Failed to update status: ' + (error.error || 'Unknown error'));\n }\n } catch (error) {\n console.error('Error updating status:', error);\n showErrorMessage('Failed to update status: ' + error.message);\n }\n }\n\n async function deleteSA(id) {\n showDeleteConfirm(id, async function() {\n try {\n const encodedId = encodeURIComponent(id);\n const response = await fetch(basePath(`/api/service-accounts/${encodedId}`), {\n method: 'DELETE'\n });\n \n if (response.ok) {\n showSuccessMessage('Service account deleted successfully');\n setTimeout(() => window.location.reload(), 1000);\n } else {\n const error = await response.json();\n showErrorMessage('Failed to delete: ' + (error.error || 'Unknown error'));\n }\n } catch (error) {\n console.error('Error deleting service account:', error);\n showErrorMessage('Failed to delete: ' + error.message);\n }\n }, 'Are you sure you want to delete this service account? This action cannot be undone.');\n }\n\n async function handleCreateServiceAccount() {\n const form = document.getElementById('createSAForm');\n const formData = new FormData(form);\n \n const saData = {\n parent_user: formData.get('parent_user'),\n description: formData.get('description')\n };\n \n // Handle expiration if set\n const expiration = fLine truncated
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
@@ -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=&#39;https://proxy.example.com/s3;v1&#39;<"},
{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)
}
}
}
+11
View File
@@ -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, "'", `'\''`) + "'"
}
@@ -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)
}
}
}
+2 -1
View File
@@ -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]