Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion cmd/lk/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,11 @@ func analyticsSessionListFlags() []cli.Flag {
Usage: "List sessions carrying `TAG`; repeatable, matches any (requires --user-auth)",
Hidden: true,
}),
userAuthOnlyFlag(&cli.StringFlag{
Name: "agent-id",
Usage: "List sessions that the cloud agent `ID` (CA_...) served, as `lk agent list` shows it (requires --user-auth)",
Hidden: true,
}),
userAuthOnlyFlag(&cli.StringFlag{
Name: "sort-order",
Usage: "Order by start time: `ORDER` asc or desc, default desc (requires --user-auth)",
Expand Down Expand Up @@ -430,7 +435,7 @@ func analyticsEventFlags() []cli.Flag {
// --start and --end work in both modes. The filter flags are sent only to the Public API for now.
var analyticsListModeFlags = authModeFlags{
legacyOnly: []string{"page"},
userAuthOnly: []string{"cursor", "status", "room", "tag", "sort-order"},
userAuthOnly: []string{"cursor", "status", "room", "tag", "agent-id", "sort-order"},
}

func listAnalyticsSessions(ctx context.Context, cmd *cli.Command) error {
Expand Down Expand Up @@ -895,6 +900,7 @@ func sessionListOptions(cmd *cli.Command) (public.SessionListOptions, error) {
Statuses: cmd.StringSlice("status"),
RoomPrefix: cmd.String("room"),
Tags: cmd.StringSlice("tag"),
AgentID: cmd.String("agent-id"),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Empty agent flag returns unfiltered sessions

With --agent-id="", sessionListOptions accepts an empty ID. listProjectSessionsParams omits the agent filter, so results can include sessions from other agents.

Learn more

The public client uses an empty AgentID to represent an omitted filter, which is appropriate for its zero-value options. The CLI can distinguish an omitted flag from an explicitly empty flag via cmd.IsSet, as the auth mode validator already does. It currently passes the parsed string alone, so both cases reach listProjectSessionsParams as an absent filter.

Example: An automation script runs lk --user-auth analytics session list --agent-id "$AGENT_ID" --json with an empty AGENT_ID. The command succeeds and returns the project’s session page without restricting it to an agent.

Recommended fix: In sessionListOptions, reject an explicitly set but empty --agent-id before constructing the options. Keep an omitted flag valid and add a command options test for --agent-id=.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

SortOrder: cmd.String("sort-order"),
}
if err := opts.Validate(); err != nil {
Expand Down
9 changes: 8 additions & 1 deletion cmd/lk/analytics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,17 +268,23 @@ func TestSessionListOptions(t *testing.T) {
"--limit", "25", "--cursor", "abc",
"--status", "active", "--status", "closed",
"--room", "demo-", "--tag", "a", "--tag", "b",
"--sort-order", "asc",
"--agent-id", "CA_abc", "--sort-order", "asc",
},
want: public.SessionListOptions{
Limit: 25,
Cursor: "abc",
Statuses: []string{"active", "closed"},
RoomPrefix: "demo-",
Tags: []string{"a", "b"},
AgentID: "CA_abc",
SortOrder: "asc",
},
},
{
name: "agent name instead of an agent ID",
args: []string{"--agent-id", "front-desk"},
wantErr: `invalid agent ID "front-desk"`,
},
{
name: "since counts back from now, to the second",
args: []string{"--since", "14d"},
Expand Down Expand Up @@ -483,6 +489,7 @@ func TestAnalyticsListModeFlags(t *testing.T) {
{name: "status in API-key mode", args: []string{"--status", "active"}, wantErr: "--status is only supported with --user-auth"},
{name: "room in API-key mode", args: []string{"--room", "demo-"}, wantErr: "--room is only supported with --user-auth"},
{name: "tag in API-key mode", args: []string{"--tag", "a"}, wantErr: "--tag is only supported with --user-auth"},
{name: "agent ID in API-key mode", args: []string{"--agent-id", "CA_abc"}, wantErr: "--agent-id is only supported with --user-auth"},
{name: "sort order in API-key mode", args: []string{"--sort-order", "asc"}, wantErr: "--sort-order is only supported with --user-auth"},
}
for _, tt := range tests {
Expand Down
15 changes: 13 additions & 2 deletions pkg/public/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ type SessionListOptions struct {
RoomPrefix string
// Tags keeps sessions carrying any of these tags.
Tags []string
// AgentID keeps sessions that this cloud agent (CA_...) served. An agent
// that runs outside LiveKit Cloud has no agent ID.
AgentID string
// SortOrder orders by start time: "asc" or "desc" (the server's default).
SortOrder string
}
Expand Down Expand Up @@ -76,7 +79,8 @@ func parseSortOrder(s string) (oapi.LivekitPublicapiCommonV1SortOrder, error) {
}

// listProjectSessionsParams translates opts into the generated query params,
// rejecting unknown status or sort order names before any request is sent.
// rejecting unknown status or sort order names, and an agent ID that is not a
// cloud agent ID, before any request is sent.
func listProjectSessionsParams(opts SessionListOptions) (*oapi.AnalyticsServiceListProjectSessionsParams, error) {
params := &oapi.AnalyticsServiceListProjectSessionsParams{}
if opts.Limit > 0 {
Expand Down Expand Up @@ -108,6 +112,12 @@ func listProjectSessionsParams(opts SessionListOptions) (*oapi.AnalyticsServiceL
if len(opts.Tags) > 0 {
params.FilterTags = ptr(opts.Tags)
}
if opts.AgentID != "" {
if !strings.HasPrefix(opts.AgentID, "CA_") {
return nil, fmt.Errorf("invalid agent ID %q (expected a cloud agent ID, such as CA_..., from `lk agent list`)", opts.AgentID)
}
params.FilterAgentId = ptr(opts.AgentID)
}
if opts.SortOrder != "" {
order, err := parseSortOrder(opts.SortOrder)
if err != nil {
Expand All @@ -118,7 +128,8 @@ func listProjectSessionsParams(opts SessionListOptions) (*oapi.AnalyticsServiceL
return params, nil
}

// Validate reports an unknown status or sort order name. ListProjectSessions
// Validate reports an unknown status or sort order name, or an agent ID that
// is not a cloud agent ID. ListProjectSessions
// makes the same check before sending a request; callers can run it earlier.
func (o SessionListOptions) Validate() error {
_, err := listProjectSessionsParams(o)
Expand Down
5 changes: 5 additions & 0 deletions pkg/public/analytics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func TestListProjectSessionsQuery(t *testing.T) {
opts: SessionListOptions{RoomPrefix: "demo-", Tags: []string{"a", "b"}},
want: url.Values{"filter.roomName": {"demo-"}, "filter.tags": {"a", "b"}},
},
{
name: "agent ID",
opts: SessionListOptions{AgentID: "CA_abc"},
want: url.Values{"filter.agentId": {"CA_abc"}},
},
{
name: "sort order",
opts: SessionListOptions{SortOrder: "asc"},
Expand Down
Loading