Skip to content

Commit

Permalink
Remove user config.
Browse files Browse the repository at this point in the history
Work towards #2090.

* remove UserConfig proto
* remove admin users endpoint
* change cli/user commands to use basic sql statements to store the data
  in the system.users table (#2235)
  • Loading branch information
marc committed Aug 24, 2015
1 parent 67a29f1 commit 42f4da1
Show file tree
Hide file tree
Showing 16 changed files with 19 additions and 411 deletions.
34 changes: 9 additions & 25 deletions cli/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,11 @@ package cli

import (
"fmt"
"strings"

"github.com/cockroachdb/cockroach/client"
"github.com/cockroachdb/cockroach/config"
"github.com/cockroachdb/cockroach/security"
"github.com/cockroachdb/cockroach/util/log"

"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v1"
)

// A getUserCmd command displays the config for the specified username.
Expand All @@ -46,13 +42,12 @@ func runGetUser(cmd *cobra.Command, args []string) {
cmd.Usage()
return
}
admin := client.NewAdminClient(&context.Context, context.Addr, client.User)
body, err := admin.GetYAML(args[0])
db := makeSQLClient()
err := processOneLine(db, fmt.Sprintf(`SELECT * FROM system.users WHERE username='%s'`, args[0]))
if err != nil {
log.Error(err)
return
}
fmt.Printf("User config for %q:\n%s\n", args[0], body)
}

// A lsUsersCmd command displays a list of user configs.
Expand All @@ -72,14 +67,12 @@ func runLsUsers(cmd *cobra.Command, args []string) {
cmd.Usage()
return
}
admin := client.NewAdminClient(&context.Context, context.Addr, client.User)
list, err := admin.List()
db := makeSQLClient()
err := processOneLine(db, `SELECT username FROM system.users`)
if err != nil {
log.Error(err)
return
}
fmt.Printf("Users:\n%s\n", strings.Join(list, "\n "))

}

// A rmUserCmd command removes the user config for the specified username.
Expand All @@ -98,13 +91,12 @@ func runRmUser(cmd *cobra.Command, args []string) {
cmd.Usage()
return
}
admin := client.NewAdminClient(&context.Context, context.Addr, client.User)
if err := admin.Delete(args[0]); err != nil {
db := makeSQLClient()
err := processOneLine(db, fmt.Sprintf(`DELETE FROM system.users WHERE username='%s'`, args[0]))
if err != nil {
log.Error(err)
return
}
fmt.Printf("Deleted user %q\n", args[0])

}

// A setUserCmd command creates a new or updates an existing user config.
Expand Down Expand Up @@ -132,20 +124,12 @@ func runSetUser(cmd *cobra.Command, args []string) {
log.Error(err)
return
}
// Build a UserConfig object. RunSetUser expects Yaml.
// TODO(marc): re-work admin client library to take other encodings.
pb := &config.UserConfig{HashedPassword: hashed}
contents, err := yaml.Marshal(pb)
db := makeSQLClient()
err = processOneLine(db, fmt.Sprintf(`INSERT INTO system.users VALUES ('%s','%s')`, args[0], hashed))
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])
}

var userCmds = []*cobra.Command{
Expand Down
2 changes: 0 additions & 2 deletions client/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ const (
Accounting = "acct"
// Quit only handles Get requests.
Quit = "quit"
// User expects config.UserConfig
User = "users"
// Zone expects config.ZoneConfig.
Zone = "zones"
)
Expand Down
101 changes: 0 additions & 101 deletions client/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,107 +120,6 @@ func Example_accounting() {
// Accounting prefixes: ["" "db1" "\xfe"]
}

// Example_user shows how to use the admin client to
// get/set/list/delete user configs.
func Example_user() {
s := server.StartTestServer(nil)
defer s.Stop()

context := testutils.NewRootTestBaseContext()
client := client.NewAdminClient(context, s.ServingAddr(), client.User)

const yamlConfig = `hashed_password:
- 10
- 20`
const jsonConfig = `{
"hashed_password": "ChQ="
}`
testData := []struct {
prefix proto.Key
cfg string
isJSON bool
}{
{proto.Key("db1"), yamlConfig, false},
{proto.Key("db 2"), jsonConfig, true},
{proto.Key("\xfe"), jsonConfig, true},
}

// Overwriting the default entry fails.
err := client.SetYAML("", yamlConfig)
if err == nil {
log.Fatal("expected error")
}

// Write configs.
for _, test := range testData {
prefix := string(test.prefix)
if test.isJSON {
fmt.Printf("Set JSON user config for %q\n", prefix)
if err := client.SetJSON(prefix, test.cfg); err != nil {
log.Fatal(err)
}
} else {
fmt.Printf("Set YAML user config for %q\n", prefix)
if err := client.SetYAML(prefix, test.cfg); err != nil {
log.Fatal(err)
}
}
}

// Get configs in various format.
body, err := client.GetJSON("db1")
if err != nil {
log.Fatal(err)
}
fmt.Printf("JSON config for \"db1\":\n%s\n", body)

body, err = client.GetYAML("db 2")
if err != nil {
log.Fatal(err)
}
fmt.Printf("YAML config for \"db 2\":\n%s\n", body)

// List keys.
keys, err := client.List()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Users: %q\n", keys)

// Remove keys: the default one cannot be removed.
err = client.Delete("")
if err == nil {
log.Fatal("expected error")
}
err = client.Delete("db 2")
if err != nil {
log.Fatal(err)
}

// List keys again.
keys, err = client.List()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Users: %q\n", keys)

// Output:
// Set YAML user config for "db1"
// Set JSON user config for "db 2"
// Set JSON user config for "\xfe"
// JSON config for "db1":
// {
// "hashed_password": "ChQ="
// }
// YAML config for "db 2":
// hashed_password:
// - 10
// - 20
//
// Users: ["" "db 2" "db1" "\xfe"]
// Users: ["" "db1" "\xfe"]
}

// Example_zone shows how to use the admin client to
// get/set/list/delete zone configs.
func Example_zone() {
Expand Down
Loading

0 comments on commit 42f4da1

Please sign in to comment.