myxce/src/frontend/xcecli/cmd/permissions/list.go
Rongsong Shen 94f50ce6e1 fix(scheduler,cli): bug-1453/1454/1455/1456 remove dead CA-cert plumbing, fix scheduler HA config bugs
bug-1453: remove unused client-side CA-certificate plumbing. C-side:
drop ca_cert_path from xce_cluster_config_t, do_connect()'s ca_cert
param, --ca-cert flags on worker/scheduler, and controller's cert
population (TLS is server-side only, SSL_VERIFY_NONE everywhere on
the client side already, verified zero SSL_CTX_load_verify_locations
calls in the C tree). Go-side: delete xcecli's self-signed cert
TOFU-pinning flow (cmd/auth/cert/*), CACert field, SetRootCAs/
loadRootCAs, and every call site.

bug-1454: fix xcecli_ctx_t leak in scheduler.c sched_init() — the
unconditional initial ctx_init was never freed before the HA
candidate loop re-initialized it on the first iteration.

bug-1455: sched_init()'s memset(sched, 0, ...) was silently wiping
sched->cluster_cfg that main.c had populated from --peers just before
calling sched_init(), so --peers was always ignored and the scheduler
fell back to the hardcoded localhost:9990 single-host default.
Restructured sched_init() to take cluster_cfg as an explicit parameter
(assigned after the memset) instead of relying on caller-side
ordering with a separate sched_set_cluster_cfg() setter, which is now
removed.

bug-1456: revert an unsafe stack-local xce_client_config_t introduced
while fixing bug-1455 — xcecli_ctx_init() stores that pointer as
ctx->conf and client_failover.c dereferences it for the ctx's entire
life (runtime reconnect), so a per-loop-iteration stack copy would
dangle the moment sched_init() returned. Back to mutating the
caller-owned, process-lifetime config in place.

Verified: full ASAN build (all components), meson test suite 9/9 OK,
manual scheduler testsuite 15/15, Go build/vet/test all pass.
2026-07-20 12:01:10 +00:00

136 lines
3.2 KiB
Go

package permissions
import (
"fmt"
"strings"
"xcecli/common/request"
"xcecli/config"
"xcecli/models"
"github.com/spf13/cobra"
)
var (
resp models.PermsResponse
)
const (
minIDWidth = 7 // Length of "PERM_ID"
minNameWidth = 9 // Length of "PERM_NAME"
padding = 2
maxNameWidth = 60
)
type PermsQuerier struct {
configManager *config.ConfigManager
noHeader bool
fields []string
}
func NewPermsQuerier(configManager *config.ConfigManager) *PermsQuerier {
return &PermsQuerier{
configManager: configManager,
}
}
func NewListPermsCmd(configManager *config.ConfigManager) *cobra.Command {
permsQuerier := NewPermsQuerier(configManager)
listCmd := &cobra.Command{
Use: "list",
Short: "Query permissions info",
Long: `Query permissions detail info.
Example:
xce permissions list # list all permissions`,
RunE: func(cmd *cobra.Command, args []string) error {
if err := permsQuerier.ParseCmdArgs(cmd, args); err != nil {
fmt.Println(strings.ToLower(err.Error()))
return nil
}
if err := permsQuerier.RunPermsList(); err != nil {
fmt.Println(strings.ToLower(err.Error()))
return nil
}
return nil
},
}
flags := listCmd.Flags()
flags.BoolVarP(&permsQuerier.noHeader, "noheader", "n", false, "output no header")
return listCmd
}
func (pq *PermsQuerier) ParseCmdArgs(cmd *cobra.Command, args []string) error {
// No additional processing needed, all parameters are bound in flags
if len(args) > 0 {
return fmt.Errorf("too many arguments")
}
return nil
}
func (pq *PermsQuerier) RunPermsList() error {
// Get server information
serverInfo, err := pq.configManager.GetServerInfo()
if err != nil {
return fmt.Errorf("failed to get server info: %w", err)
}
// create API client
apiClient := request.NewAPIClient(serverInfo.URL)
// Build query parameters
params := make(map[string]string)
pq.fields = []string{}
// Make API request
err = apiClient.GetRequestFromAPIServer(serverInfo.Token, "", "/xce/v1/permissions", &resp, params)
if err != nil {
return err
}
data := resp.Data
if len(data) == 0 {
fmt.Println("no permissions found")
return nil
}
// Calculate column widths for proper alignment
permIDWidth := minIDWidth + padding
permNameWidth := minNameWidth + padding
// Get the maximum ID width (last item)
if len(data) > 0 {
lastIDStr := fmt.Sprintf("%d", len(data))
if len(lastIDStr) > permIDWidth-padding {
permIDWidth = len(lastIDStr) + padding
}
}
// Get maximum permission name width
for _, permStr := range data {
if len(permStr) > permNameWidth-padding {
permNameWidth = len(permStr) + padding
}
}
// Apply max width limit
if permNameWidth > maxNameWidth {
permNameWidth = maxNameWidth
}
// Add headers with proper alignment
if !pq.noHeader {
fmt.Printf("%-*s %-*s\n", permIDWidth, "PERM_ID", permNameWidth, "PERM_NAME")
}
// Print permissions with auto-incrementing IDs and aligned columns
for i, permStr := range data {
// Truncate long permission names
displayPermStr := permStr
if len(permStr) > permNameWidth-padding {
displayPermStr = permStr[:permNameWidth-padding] + ".."
}
fmt.Printf("%-*d %-*s\n", permIDWidth, i+1, permNameWidth, displayPermStr)
}
return nil
}