示例#1
0
// runSetUser invokes the REST API with POST action and username as
// path. Prompts for the password twice on stdin.
// 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 {
		cmd.Usage()
		return
	}
	hashed, err := security.PromptForPasswordAndHash()
	if err != nil {
		log.Error(err)
		return
	}
	// Build a UserConfig object. RunSetUser expects Yaml.
	// TODO(marc): re-work admin client library to take other encodings.
	pb := &proto.UserConfig{HashedPassword: hashed}
	contents, err := yaml.Marshal(pb)
	if err != nil {
		log.Error(err)
		return
	}
	admin := client.NewAdminClient(&Context.Context, Context.Addr, client.User)
	if err := admin.SetYAML(args[0], string(contents)); err != nil {
		log.Error(err)
		return
	}
	fmt.Printf("Wrote user config for %q\n", args[0])
}
示例#2
0
// 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)
	}
}
示例#3
0
// 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)
	}
}
示例#4
0
// runSetUser invokes the REST API with POST action and username as
// path. Prompts for the password twice on stdin.
// 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 {
		cmd.Usage()
		return
	}
	hashed, err := security.PromptForPasswordAndHash()
	if err != nil {
		log.Error(err)
		return
	}
	db := makeSQLClient()
	err = processOneLine(db, fmt.Sprintf(`INSERT INTO system.users VALUES ('%s','%s')`, args[0], hashed))
	if err != nil {
		log.Error(err)
		return
	}
}
示例#5
0
文件: user.go 项目: husttom/cockroach
// runSetUser invokes the REST API with POST action and username as
// path. Prompts for the password twice on stdin.
// 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 {
		cmd.Usage()
		return
	}
	hashed, err := security.PromptForPasswordAndHash()
	if err != nil {
		log.Error(err)
		return
	}
	db := makeSQLClient()
	err = runQuery(db, `INSERT INTO system.users VALUES ($1, $2)`, args[0], hashed)
	if err != nil {
		log.Error(err)
		return
	}
}
示例#6
0
// 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
	}
	hashed, err := security.PromptForPasswordAndHash()
	if err != nil {
		log.Error(err)
		return
	}
	db := makeSQLClient()
	// TODO(marc): switch to UPSERT.
	err = runPrettyQuery(db, `INSERT INTO system.users VALUES ($1, $2)`, args[0], hashed)
	if err != nil {
		log.Error(err)
		return
	}
}
示例#7
0
// runSetUser invokes the REST API with POST action and username as
// path. Prompts for the password twice on stdin.
// 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 {
		cmd.Usage()
		return
	}
	hashed, err := security.PromptForPasswordAndHash()
	if err != nil {
		log.Error(err)
		return
	}
	// Build a UserConfig object. RunSetUser expects Yaml.
	// TODO(marc): re-work admin client library to take other encodings.
	pb := &proto.UserConfig{HashedPassword: hashed}
	contents, err := yaml.Marshal(pb)
	if err != nil {
		log.Error(err)
		return
	}
	server.RunSetUser(Context, args[0], contents)
}