diff --git a/cmd/lk/agent_run.go b/cmd/lk/agent_run.go index 8c15dbf55..6d69f6280 100644 --- a/cmd/lk/agent_run.go +++ b/cmd/lk/agent_run.go @@ -224,6 +224,12 @@ func resolveCredentials(ctx context.Context, cmd *cli.Command, dir string, loadO explicit = agentCredentials{} } + // --dev with an API key or secret is an error, as in resolveProject. A + // complete set skips the project lookup below, so check it here. + if cmd.Bool("dev") && (explicit.apiKey != "" || explicit.apiSecret != "") { + return nil, devCredentialsError(cmd) + } + merged := explicit // Under --user-auth there is no stored API key to hand the agent; diff --git a/cmd/lk/agent_run_test.go b/cmd/lk/agent_run_test.go index 2f47dd7a2..e0face7e0 100644 --- a/cmd/lk/agent_run_test.go +++ b/cmd/lk/agent_run_test.go @@ -365,6 +365,76 @@ func TestResolveCredentials_FullyExplicitSkipsProjectLookup(t *testing.T) { }, env) } +// TestResolveCredentials_DevWithCredentialsIsAnError checks that --dev with an +// API key or secret fails, even when the URL, key and secret are all given and +// no project lookup runs. +func TestResolveCredentials_DevWithCredentialsIsAnError(t *testing.T) { + for _, tc := range []struct { + name string + env map[string]string + args []string + want string + }{ + {name: "complete env", + env: map[string]string{"LIVEKIT_URL": "wss://env.livekit.cloud", "LIVEKIT_API_KEY": "envkey", "LIVEKIT_API_SECRET": "envsecret"}, + want: "--dev can't be combined with API key credentials; unset LIVEKIT_API_KEY and LIVEKIT_API_SECRET or drop --dev"}, + {name: "complete flags", + args: []string{"--url", "wss://flag.livekit.cloud", "--api-key", "flagkey", "--api-secret", "flagsecret"}, + want: "--dev can't be combined with API key credentials; remove --api-key and --api-secret or drop --dev"}, + {name: "env key only", + env: map[string]string{"LIVEKIT_API_KEY": "envkey"}, + want: "--dev can't be combined with API key credentials; unset LIVEKIT_API_KEY or drop --dev"}, + } { + t.Run(tc.name, func(t *testing.T) { + clearLiveKitEnv(t) + for k, v := range tc.env { + t.Setenv(k, v) + } + var err error + app := &cli.Command{ + Name: "lk", + Flags: append(credentialFlags(), &cli.BoolFlag{Name: "dev"}, &cli.StringFlag{Name: "project"}, &cli.StringFlag{Name: "subdomain"}), + Action: func(_ context.Context, cmd *cli.Command) error { + _, err = resolveCredentials(context.Background(), cmd, t.TempDir()) + return nil + }, + } + require.NoError(t, app.Run(context.Background(), append([]string{"lk", "--dev"}, tc.args...))) + assert.EqualError(t, err, tc.want) + }) + } +} + +// TestResolveCredentials_DevAlone checks that --dev, alone or with --url, +// still hands the agent the dev credentials. +func TestResolveCredentials_DevAlone(t *testing.T) { + for _, tc := range []struct { + name string + args []string + want []string + }{ + {name: "dev", want: []string{"LIVEKIT_URL=" + localhostURL, "LIVEKIT_API_KEY=devkey", "LIVEKIT_API_SECRET=secret"}}, + {name: "dev and url", args: []string{"--url", "ws://dev.example"}, + want: []string{"LIVEKIT_URL=ws://dev.example", "LIVEKIT_API_KEY=devkey", "LIVEKIT_API_SECRET=secret"}}, + } { + t.Run(tc.name, func(t *testing.T) { + clearLiveKitEnv(t) + var env []string + app := &cli.Command{ + Name: "lk", + Flags: append(credentialFlags(), &cli.BoolFlag{Name: "dev"}, &cli.StringFlag{Name: "project"}, &cli.StringFlag{Name: "subdomain"}), + Action: func(_ context.Context, cmd *cli.Command) error { + var err error + env, err = resolveCredentials(context.Background(), cmd, t.TempDir()) + return err + }, + } + require.NoError(t, app.Run(context.Background(), append([]string{"lk", "--dev"}, tc.args...))) + assert.Equal(t, tc.want, env) + }) + } +} + // --- cloud console link -------------------------------------------------------- func TestCloudProject(t *testing.T) { diff --git a/cmd/lk/testdata/script/agent-dev-with-env-credentials.txtar b/cmd/lk/testdata/script/agent-dev-with-env-credentials.txtar new file mode 100644 index 000000000..95a69e17a --- /dev/null +++ b/cmd/lk/testdata/script/agent-dev-with-env-credentials.txtar @@ -0,0 +1,25 @@ +# lk agent dev and lk agent start with --dev and a full set of LIVEKIT_* +# credentials are an error naming what to remove, as for other commands. The +# agent never starts with those credentials in place of the dev ones. +env LIVEKIT_URL=ws://env.test +env LIVEKIT_API_KEY=APIenv +env LIVEKIT_API_SECRET=secretenv + +! exec lk agent dev --dev +stderr '^--dev can''t be combined with API key credentials; unset LIVEKIT_API_KEY and LIVEKIT_API_SECRET or drop --dev$' + +! exec lk agent start --dev +stderr '^--dev can''t be combined with API key credentials; unset LIVEKIT_API_KEY and LIVEKIT_API_SECRET or drop --dev$' + +env LIVEKIT_URL= +env LIVEKIT_API_KEY= +env LIVEKIT_API_SECRET= +! exec lk agent start --dev --url ws://flag.test --api-key APIflag --api-secret secretflag +stderr '^--dev can''t be combined with API key credentials; remove --api-key and --api-secret or drop --dev$' + +requests +! stdout . + +-- package.json -- +{"name":"agent","dependencies":{"@livekit/agents":"1.6.0"}} +-- src/main.js --