Skip to content
Merged
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
6 changes: 6 additions & 0 deletions cmd/lk/agent_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
70 changes: 70 additions & 0 deletions cmd/lk/agent_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
25 changes: 25 additions & 0 deletions cmd/lk/testdata/script/agent-dev-with-env-credentials.txtar
Original file line number Diff line number Diff line change
@@ -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 --
Loading