// runSetUser prompts for a password, then inserts the user and hash // into the system.users table. // TODO(marc): once we have more fields in the user config, we will need // to allow changing just some of them (eg: change email, but leave password). func runSetUser(cmd *cobra.Command, args []string) { if len(args) != 1 { mustUsage(cmd) return } var err error var hashed []byte switch password { case "": hashed, err = security.PromptForPasswordAndHash() if err != nil { panic(err) } case "-": scanner := bufio.NewScanner(os.Stdin) if scanner.Scan() { if b := scanner.Bytes(); len(b) > 0 { hashed, err = security.HashPassword(b) if err != nil { panic(err) } if scanner.Scan() { panic("multiline passwords are not permitted") } if err := scanner.Err(); err != nil { panic(err) } break // Success. } } else { if err := scanner.Err(); err != nil { panic(err) } } panic("empty passwords are not permitted") default: hashed, err = security.HashPassword([]byte(password)) if err != nil { panic(err) } } conn, err := makeSQLClient() if err != nil { panic(err) } defer conn.Close() // TODO(marc): switch to UPSERT. err = runPrettyQuery(conn, os.Stdout, makeQuery(`INSERT INTO system.users VALUES ($1, $2)`, args[0], hashed)) if err != nil { panic(err) } }
// runSetUser prompts for a password, then inserts the user and hash // into the system.users table. // TODO(marc): once we have more fields in the user config, we will need // to allow changing just some of them (eg: change email, but leave password). func runSetUser(cmd *cobra.Command, args []string) { if len(args) != 1 { mustUsage(cmd) return } var err error var hashed []byte switch password { case "": hashed, err = security.PromptForPasswordAndHash() if err != nil { panic(err) } case "-": scanner := bufio.NewScanner(os.Stdin) if scanner.Scan() { if b := scanner.Bytes(); len(b) > 0 { hashed, err = security.HashPassword(b) if err != nil { panic(err) } if scanner.Scan() { panic("multiline passwords are not permitted") } if err := scanner.Err(); err != nil { panic(err) } break // Success. } } else { if err := scanner.Err(); err != nil { panic(err) } } panic("empty passwords are not permitted") default: hashed, err = security.HashPassword([]byte(password)) if err != nil { panic(err) } } db, _ := makeSQLClient() defer func() { _ = db.Close() }() // TODO(marc): switch to UPSERT. // TODO(marc): switch to placeholders when they work again with pgwire. err = runPrettyQuery(db, os.Stdout, fmt.Sprintf(`INSERT INTO system.users VALUES ('%s', '%s'::bytes)`, args[0], string(hashed))) if err != nil { panic(err) } }