Admin: add `src admin create` command by jdpleiness · Pull Request #957 · sourcegraph/src-cli · GitHub
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
38 changes: 38 additions & 0 deletions cmd/src/admin.go
100 changes: 100 additions & 0 deletions cmd/src/admin_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package main

import (
"flag"
"fmt"
"os"

"github.com/sourcegraph/src-cli/internal/users"

"github.com/sourcegraph/sourcegraph/lib/errors"
)

func init() {
usage := `
Examples:

Create an initial admin user on a new Sourcegraph deployment:

$ src admin create -url https://your-sourcegraph-url -username admin -email admin@yourcompany.com -with-token

Create an initial admin user on a new Sourcegraph deployment using '-password' flag.
WARNING: for security purposes we strongly recommend using the SRC_ADMIN_PASS environment variable when possible.

$ src admin create -url https://your-sourcegraph-url -username admin -email admin@yourcompany.com -password p@55w0rd -with-token

Environmental variables

SRC_ADMIN_PASS The new admin user's password
`

flagSet := flag.NewFlagSet("create", flag.ExitOnError)
usageFunc := func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of 'src users %s':\n", flagSet.Name())
flagSet.PrintDefaults()
fmt.Println(usage)
}

var (
urlFlag = flagSet.String("url", "", "The base URL for the Sourcegraph instance.")
usernameFlag = flagSet.String("username", "", "The new admin user's username.")
emailFlag = flagSet.String("email", "", "The new admin user's email address.")
passwordFlag = flagSet.String("password", "", "The new admin user's password.")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we read this from stdin instead? Otherwise it appears in the bash history.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added env var option to set password and a warning in usage examples to prefer using env var over the -password flag. The -password flag can still be useful in certain temporary situations however (CI pipelines, integration tests, etc...) so it will remain for now. See commit

tokenFlag = flagSet.Bool("with-token", false, "Optionally create and output an admin access token.")
)

handler := func(args []string) error {
if err := flagSet.Parse(args); err != nil {
return err
}

ok, _, err := users.NeedsSiteInit(*urlFlag)
if err != nil {
return err
}
if !ok {
return errors.New("failed to create admin, site already initialized")
}

envAdminPass := os.Getenv("SRC_ADMIN_PASS")

var client *users.Client

switch {
case envAdminPass != "" && *passwordFlag == "":
client, err = users.SiteAdminInit(*urlFlag, *emailFlag, *usernameFlag, envAdminPass)
if err != nil {
return err
}
case envAdminPass == "" && *passwordFlag != "":
client, err = users.SiteAdminInit(*urlFlag, *emailFlag, *usernameFlag, *passwordFlag)
if err != nil {
return err
}
case envAdminPass != "" && *passwordFlag != "":
return errors.New("failed to read admin password: environment variable and -password flag both set")
case envAdminPass == "" && *passwordFlag == "":
return errors.New("failed to read admin password from 'SRC_ADMIN_PASS' environment variable or -password flag")
}

if *tokenFlag {
token, err := client.CreateAccessToken("", []string{"user:all", "site-admin:sudo"}, "src-cli")
if err != nil {
return err
}

_, err = fmt.Fprintf(flag.CommandLine.Output(), "%s\n", token)
if err != nil {
return err
}
}

return nil
}

adminCommands = append(adminCommands, &command{
flagSet: flagSet,
handler: handler,
usageFunc: usageFunc,
})
}
128 changes: 128 additions & 0 deletions internal/lazyregexp/lazyregexp.go
Loading