Skip to content

feat: Output username and password for code server --dev #1193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 13 additions & 8 deletions cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ import (
"github.com/coder/coder/provisionersdk/proto"
)

var defaultDevUser = codersdk.CreateFirstUserRequest{
Email: "admin@coder.com",
Username: "developer",
Password: "password",
OrganizationName: "acme-corp",
}

// nolint:gocyclo
func server() *cobra.Command {
var (
Expand Down Expand Up @@ -275,6 +282,9 @@ func server() *cobra.Command {
if err != nil {
return xerrors.Errorf("create first user: %w", err)
}
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "email: %s\n", defaultDevUser.Email)
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "password: %s\n", defaultDevUser.Password)
_, _ = fmt.Fprintln(cmd.ErrOrStderr())

_, _ = fmt.Fprintf(cmd.ErrOrStderr(), cliui.Styles.Wrap.Render(`Started in dev mode. All data is in-memory! `+cliui.Styles.Bold.Render("Do not use in production")+`. Press `+
cliui.Styles.Field.Render("ctrl+c")+` to clean up provisioned infrastructure.`)+"\n\n")
Expand Down Expand Up @@ -441,18 +451,13 @@ func server() *cobra.Command {
}

func createFirstUser(cmd *cobra.Command, client *codersdk.Client, cfg config.Root) error {
_, err := client.CreateFirstUser(cmd.Context(), codersdk.CreateFirstUserRequest{
Email: "admin@coder.com",
Username: "developer",
Password: "password",
OrganizationName: "acme-corp",
})
_, err := client.CreateFirstUser(cmd.Context(), defaultDevUser)
if err != nil {
return xerrors.Errorf("create first user: %w", err)
}
token, err := client.LoginWithPassword(cmd.Context(), codersdk.LoginWithPasswordRequest{
Email: "admin@coder.com",
Password: "password",
Email: defaultDevUser.Email,
Password: defaultDevUser.Password,
})
if err != nil {
return xerrors.Errorf("login with first user: %w", err)
Expand Down
7 changes: 7 additions & 0 deletions cli/server_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli_test

import (
"bytes"
"context"
"crypto/ecdsa"
"crypto/elliptic"
Expand All @@ -18,6 +19,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"

Expand Down Expand Up @@ -77,6 +79,8 @@ func TestServer(t *testing.T) {
err := root.ExecuteContext(ctx)
require.ErrorIs(t, err, context.Canceled)
}()
var stdoutBuf bytes.Buffer
root.SetOutput(&stdoutBuf)
var token string
require.Eventually(t, func() bool {
var err error
Expand All @@ -88,6 +92,9 @@ func TestServer(t *testing.T) {
require.NoError(t, err)
parsed, err := url.Parse(accessURL)
require.NoError(t, err)
// Verify that credentials were output to the terminal.
assert.Contains(t, stdoutBuf.String(), "email: admin@coder.com", "unexpected output")
Copy link
Member

Choose a reason for hiding this comment

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

minor: should we use the values from defaultDevUser instead of hard-coding? This would mean exporting it, but I don't see a huge issue there.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, we can do that. I think hard-coding would be acceptable here too, as changes to the defaults may require rethinking some things, but either works for me.

Copy link
Member

Choose a reason for hiding this comment

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

I'm a fan of hard coding here too. Customers that use the product will likely depend on the credentials, so having tests fail if they change seems reasonable.

assert.Contains(t, stdoutBuf.String(), "password: password", "unexpected output")
Copy link
Member

Choose a reason for hiding this comment

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

nit: change msgAndArgs to be different for both asserts

client := codersdk.New(parsed)
client.SessionToken = token
_, err = client.User(ctx, codersdk.Me)
Expand Down