1
Fork 0
mirror of https://github.com/jech/galene.git synced 2024-12-22 23:35:46 +01:00
galene/galenectl/galenectl.go

1199 lines
26 KiB
Go
Raw Permalink Normal View History

2024-10-15 13:05:38 +02:00
package main
import (
2024-10-23 20:19:21 +02:00
"bytes"
2024-10-15 13:05:38 +02:00
"crypto/rand"
"crypto/sha256"
"crypto/tls"
2024-10-15 13:05:38 +02:00
"encoding/hex"
"encoding/json"
"errors"
"flag"
"fmt"
2024-10-30 14:58:58 +01:00
"io"
2024-10-15 13:05:38 +02:00
"log"
"net/http"
2024-10-23 20:39:53 +02:00
"net/url"
2024-10-15 13:05:38 +02:00
"os"
2024-10-30 17:48:49 +01:00
"path"
"path/filepath"
"sort"
2024-10-23 20:49:33 +02:00
"strings"
2024-10-25 17:51:42 +02:00
"time"
2024-10-15 13:05:38 +02:00
"golang.org/x/crypto/bcrypt"
"golang.org/x/crypto/pbkdf2"
2024-10-25 15:54:54 +02:00
"golang.org/x/term"
2024-10-15 13:05:38 +02:00
"github.com/jech/galene/group"
2024-10-30 15:44:53 +01:00
"github.com/jech/galene/token"
2024-10-15 13:05:38 +02:00
)
type configuration struct {
Server string `json:"server"`
AdminUsername string `json:"admin-username"`
AdminPassword string `json:"admin-password"`
AdminToken string `json:"admin-token"`
}
var insecure bool
var serverURL, adminUsername, adminPassword, adminToken string
var client http.Client
2024-10-15 13:05:38 +02:00
type command struct {
command func(string, []string)
description string
}
var commands = map[string]command{
"hash-password": {
command: hashPasswordCmd,
description: "generate a hashed password",
},
2024-10-23 20:39:53 +02:00
"set-password": {
command: setPasswordCmd,
description: "set a user's password",
},
"delete-password": {
command: deletePasswordCmd,
description: "delete a user's password",
},
2024-10-30 15:44:53 +01:00
"list-groups": {
command: listGroupsCmd,
description: "list groups",
},
2024-10-23 20:45:51 +02:00
"create-group": {
command: createGroupCmd,
description: "create a group",
},
"delete-group": {
command: deleteGroupCmd,
description: "delete a group",
},
2024-10-27 15:03:27 +01:00
"list-users": {
command: listUsersCmd,
description: "list users",
},
2024-10-23 20:49:33 +02:00
"create-user": {
command: createUserCmd,
description: "create a user",
},
"delete-user": {
command: deleteUserCmd,
description: "delete a user",
},
2024-10-23 20:20:40 +02:00
"update-user": {
command: updateUserCmd,
description: "change a user's permissions",
},
2024-10-30 15:44:53 +01:00
"list-tokens": {
command: listTokensCmd,
description: "list tokens",
2024-10-25 16:03:45 +02:00
},
2024-10-25 17:00:47 +02:00
"create-token": {
command: createTokenCmd,
2024-10-25 17:52:00 +02:00
description: "request a token",
},
"revoke-token": {
command: revokeTokenCmd,
description: "revoke a token",
2024-10-25 17:00:47 +02:00
},
"delete-token": {
command: deleteTokenCmd,
2024-10-25 17:52:00 +02:00
description: "delete a token",
2024-10-25 17:00:47 +02:00
},
2024-10-15 13:05:38 +02:00
}
func main() {
configdir, err := os.UserConfigDir()
if err != nil {
log.Fatalf("UserConfigDir: %v", err)
}
configFile := filepath.Join(
filepath.Join(configdir, "galene"),
"galenectl.json",
)
2024-10-15 13:05:38 +02:00
flag.Usage = func() {
fmt.Fprintf(
flag.CommandLine.Output(),
"%s [option...] command [option...] [args...]\n",
os.Args[0])
flag.PrintDefaults()
fmt.Fprintln(flag.CommandLine.Output())
names := make([]string, 0, len(commands))
for name := range commands {
names = append(names, name)
}
sort.Slice(names, func(i, j int) bool {
return names[i] < names[j]
})
2024-10-15 13:05:38 +02:00
for _, name := range names {
fmt.Fprintf(
flag.CommandLine.Output(),
" %-15s %s\n",
name, commands[name].description)
}
fmt.Fprintln(flag.CommandLine.Output())
fmt.Fprintf(flag.CommandLine.Output(),
"See \"%s command -help\" for information on individual commands.\n",
os.Args[0],
)
}
flag.StringVar(&serverURL, "server", "",
"server `url`")
flag.BoolVar(&insecure, "insecure", false,
"don't check server certificates")
flag.StringVar(&configFile, "config", configFile,
"configuration `file`")
flag.StringVar(&adminUsername, "admin-username", "",
"administrator `username`")
flag.StringVar(&adminPassword, "admin-password", "",
"administrator `password`")
flag.StringVar(&adminToken, "admin-token",
"", "administrator `token`")
2024-10-15 13:05:38 +02:00
flag.Parse()
if flag.NArg() < 1 {
flag.Usage()
os.Exit(1)
}
config, err := readConfig(configFile)
if err != nil {
log.Fatalf("Failed to read configuration file: %v", err)
}
if serverURL == "" {
serverURL = config.Server
}
if serverURL == "" {
serverURL = "https://localhost:8443"
}
if adminUsername == "" {
adminUsername = config.AdminUsername
}
if adminPassword == "" {
adminPassword = config.AdminPassword
}
if adminToken == "" {
adminToken = config.AdminToken
}
if insecure {
t := http.DefaultTransport.(*http.Transport).Clone()
t.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
client.Transport = t
}
2024-10-15 13:05:38 +02:00
cmdname := flag.Args()[0]
command, ok := commands[cmdname]
if !ok {
flag.Usage()
os.Exit(1)
}
command.command(cmdname, flag.Args()[1:])
}
func readConfig(filename string) (configuration, error) {
var config configuration
f, err := os.Open(filename)
if err != nil {
if os.IsNotExist(err) {
return config, nil
}
return config, err
}
defer f.Close()
decoder := json.NewDecoder(f)
decoder.DisallowUnknownFields()
err = decoder.Decode(&config)
if err != nil {
return config, err
}
return config, nil
}
2024-10-15 13:05:38 +02:00
func makePassword(pw string, algorithm string, iterations, length, saltlen, cost int) (group.Password, error) {
salt := make([]byte, saltlen)
_, err := rand.Read(salt)
if err != nil {
return group.Password{}, err
}
switch algorithm {
case "pbkdf2":
key := pbkdf2.Key(
[]byte(pw), salt, iterations, length, sha256.New,
)
encoded := hex.EncodeToString(key)
return group.Password{
Type: "pbkdf2",
Hash: "sha-256",
Key: &encoded,
Salt: hex.EncodeToString(salt),
Iterations: iterations,
}, nil
case "bcrypt":
key, err := bcrypt.GenerateFromPassword(
[]byte(pw), cost,
)
if err != nil {
return group.Password{}, err
}
k := string(key)
return group.Password{
Type: "bcrypt",
Key: &k,
}, nil
case "wildcard":
if pw != "" {
log.Fatalf(
"Wildcard password " +
"must be the empty string",
)
}
return group.Password{
Type: "wildcard",
}, nil
default:
return group.Password{}, errors.New("unknown password type")
}
}
2024-10-23 18:27:18 +02:00
func setUsage(cmd *flag.FlagSet, cmdname string, format string, args ...any) {
cmd.Usage = func() {
fmt.Fprintf(cmd.Output(), format, args...)
cmd.PrintDefaults()
}
}
2024-10-15 13:05:38 +02:00
func hashPasswordCmd(cmdname string, args []string) {
2024-10-25 15:54:54 +02:00
var password, algorithm string
2024-10-25 15:40:40 +02:00
var iterations, cost, length, saltlen int
2024-10-15 13:05:38 +02:00
cmd := flag.NewFlagSet(cmdname, flag.ExitOnError)
2024-10-23 18:27:18 +02:00
setUsage(cmd, cmdname,
2024-10-25 15:54:54 +02:00
"%v [option...] %v [option...]\n",
2024-10-23 18:27:18 +02:00
os.Args[0], cmdname,
)
2024-10-25 15:54:54 +02:00
cmd.StringVar(&password, "password", "", "new `password`")
2024-10-25 13:39:19 +02:00
cmd.StringVar(&algorithm, "type", "pbkdf2",
"password `type`")
2024-10-15 13:05:38 +02:00
cmd.IntVar(&iterations, "iterations", 4096,
"`number` of iterations (pbkdf2)")
cmd.IntVar(&cost, "cost", bcrypt.DefaultCost,
"`cost` (bcrypt)")
cmd.IntVar(&length, "key", 32, "key `length` (pbkdf2)")
cmd.IntVar(&saltlen, "salt", 8, "salt `length` (pbkdf2)")
cmd.Parse(args)
2024-10-25 15:54:54 +02:00
if cmd.NArg() != 0 {
2024-10-15 13:05:38 +02:00
cmd.Usage()
os.Exit(1)
}
2024-10-25 15:54:54 +02:00
if algorithm != "wildcard" && password == "" {
fmt.Fprint(os.Stdin, "New password: ")
pw, err := term.ReadPassword(int(os.Stdin.Fd()))
fmt.Println()
2024-10-15 13:05:38 +02:00
if err != nil {
2024-10-25 15:54:54 +02:00
log.Fatalf("ReadPassword: %v", err)
2024-10-15 13:05:38 +02:00
}
2024-10-25 15:54:54 +02:00
password = string(pw)
}
p, err := makePassword(
password, algorithm, iterations, length, saltlen, cost,
)
if err != nil {
log.Fatalf("Make password: %v", err)
}
e := json.NewEncoder(os.Stdout)
err = e.Encode(p)
if err != nil {
log.Fatalf("Encode: %v", err)
2024-10-15 13:05:38 +02:00
}
}
2024-10-23 20:19:21 +02:00
func setAuthorization(req *http.Request) {
if adminToken != "" {
req.Header.Set("Authorization", "Bearer "+adminToken)
} else if adminUsername != "" {
req.SetBasicAuth(adminUsername, adminPassword)
}
}
2024-12-04 13:33:40 +01:00
type httpError struct {
statusCode int
status string
}
func (e httpError) Error() string {
if e.status != "" {
return fmt.Sprintf("HTTP error: %v", e.status)
}
return fmt.Sprintf("HTTP error: %v", e.statusCode)
}
2024-12-04 13:13:45 +01:00
func getJSON(url string, value any) (string, error) {
2024-10-25 16:03:45 +02:00
req, err := http.NewRequest("GET", url, nil)
if err != nil {
2024-10-30 14:51:47 +01:00
return "", err
2024-10-25 16:03:45 +02:00
}
setAuthorization(req)
resp, err := client.Do(req)
if err != nil {
2024-10-30 14:51:47 +01:00
return "", err
2024-10-25 16:03:45 +02:00
}
defer resp.Body.Close()
2024-10-30 14:51:47 +01:00
etag := resp.Header.Get("ETag")
2024-10-25 16:03:45 +02:00
if resp.StatusCode >= 300 {
2024-12-04 13:33:40 +01:00
return etag, httpError{resp.StatusCode, resp.Status}
2024-10-25 16:03:45 +02:00
}
decoder := json.NewDecoder(resp.Body)
2024-10-30 14:51:47 +01:00
return etag, decoder.Decode(value)
2024-10-25 16:03:45 +02:00
}
2024-10-23 20:19:21 +02:00
func putJSON(url string, value any, overwrite bool) error {
j, err := json.Marshal(value)
if err != nil {
return err
}
req, err := http.NewRequest("PUT", url, bytes.NewReader(j))
if err != nil {
return err
}
setAuthorization(req)
req.Header.Set("Content-Type", "application/json")
if !overwrite {
req.Header.Set("If-None-Match", "*")
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 300 {
2024-12-04 13:33:40 +01:00
return httpError{resp.StatusCode, resp.Status}
2024-10-23 20:19:21 +02:00
}
2024-10-30 14:58:58 +01:00
io.Copy(io.Discard, resp.Body)
2024-10-23 20:19:21 +02:00
return nil
}
2024-10-25 17:00:47 +02:00
func postJSON(url string, value any) (string, error) {
j, err := json.Marshal(value)
if err != nil {
return "", err
}
req, err := http.NewRequest("POST", url, bytes.NewReader(j))
if err != nil {
return "", err
}
setAuthorization(req)
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode >= 300 {
2024-12-04 13:33:40 +01:00
return "", httpError{resp.StatusCode, resp.Status}
2024-10-25 17:00:47 +02:00
}
location := resp.Header.Get("location")
2024-10-30 14:58:58 +01:00
io.Copy(io.Discard, resp.Body)
2024-10-25 17:00:47 +02:00
return location, nil
}
2024-10-23 20:19:21 +02:00
func updateJSON[T any](url string, update func(T) T) error {
2024-10-30 14:51:47 +01:00
var old T
etag, err := getJSON(url, &old)
2024-10-23 20:19:21 +02:00
if err != nil {
return err
}
if etag == "" {
return errors.New("missing ETag")
}
value := update(old)
j, err := json.Marshal(value)
if err != nil {
return err
}
2024-10-30 14:51:47 +01:00
req, err := http.NewRequest("PUT", url, bytes.NewReader(j))
setAuthorization(req)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("If-Match", etag)
2024-10-23 20:19:21 +02:00
2024-10-30 14:51:47 +01:00
resp, err := client.Do(req)
2024-10-23 20:19:21 +02:00
if err != nil {
return err
}
2024-10-30 14:51:47 +01:00
defer resp.Body.Close()
if resp.StatusCode >= 300 {
2024-12-04 13:33:40 +01:00
return httpError{resp.StatusCode, resp.Status}
2024-10-23 20:19:21 +02:00
}
2024-10-30 14:58:58 +01:00
io.Copy(io.Discard, resp.Body)
2024-10-23 20:19:21 +02:00
return nil
}
func deleteValue(url string) error {
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return err
}
setAuthorization(req)
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode >= 300 {
2024-12-04 13:33:40 +01:00
return httpError{resp.StatusCode, resp.Status}
2024-10-23 20:19:21 +02:00
}
2024-10-30 14:58:58 +01:00
io.Copy(io.Discard, resp.Body)
2024-10-23 20:19:21 +02:00
return nil
}
2024-10-23 20:39:53 +02:00
func setPasswordCmd(cmdname string, args []string) {
2024-10-25 15:40:40 +02:00
var groupname, username string
var wildcard bool
2024-10-25 15:54:54 +02:00
var password, algorithm string
2024-10-25 15:40:40 +02:00
var iterations, cost, length, saltlen int
2024-10-23 20:39:53 +02:00
cmd := flag.NewFlagSet(cmdname, flag.ExitOnError)
setUsage(cmd, cmdname,
2024-10-25 15:54:54 +02:00
"%v [option...] %v [option...]\n",
2024-10-23 20:39:53 +02:00
os.Args[0], cmdname,
)
cmd.StringVar(&groupname, "group", "", "group `name`")
2024-10-25 15:40:40 +02:00
cmd.StringVar(&username, "user", "", "user `name`")
cmd.BoolVar(&wildcard, "wildcard", false, "set wildcard user's password")
2024-10-25 15:54:54 +02:00
cmd.StringVar(&password, "password", "", "new `password`")
2024-10-25 13:39:19 +02:00
cmd.StringVar(&algorithm, "type", "pbkdf2",
"password `type`")
2024-10-23 20:39:53 +02:00
cmd.IntVar(&iterations, "iterations", 4096,
"`number` of iterations (pbkdf2)")
cmd.IntVar(&cost, "cost", bcrypt.DefaultCost,
"`cost` (bcrypt)")
cmd.IntVar(&length, "key", 32, "key `length` (pbkdf2)")
cmd.IntVar(&saltlen, "salt", 8, "salt `length` (pbkdf2)")
cmd.Parse(args)
2024-10-25 15:54:54 +02:00
if cmd.NArg() != 0 {
2024-10-23 20:39:53 +02:00
cmd.Usage()
os.Exit(1)
}
if groupname == "" {
2024-10-25 15:40:40 +02:00
fmt.Fprintf(cmd.Output(),
"Option \"-group\" is required\n")
os.Exit(1)
}
if wildcard != (username == "") {
fmt.Fprintf(cmd.Output(),
"Exactly one of \"-user\" and \"-wildcard\" "+
"is required\n")
2024-10-23 20:39:53 +02:00
os.Exit(1)
}
2024-10-25 15:54:54 +02:00
if algorithm != "wildcard" && password == "" {
fmt.Fprint(os.Stdin, "New password: ")
pw, err := term.ReadPassword(int(os.Stdin.Fd()))
fmt.Println()
if err != nil {
log.Fatalf("ReadPassword: %v", err)
}
password = string(pw)
}
2024-10-23 20:39:53 +02:00
pw, err := makePassword(
2024-10-25 15:54:54 +02:00
password, algorithm, iterations, length, saltlen, cost,
2024-10-23 20:39:53 +02:00
)
if err != nil {
log.Fatalf("Make password: %v", err)
}
2024-10-25 15:40:40 +02:00
var u string
if wildcard {
u, err = url.JoinPath(
serverURL, "/galene-api/v0/.groups", groupname,
".wildcard-user/.password",
)
} else {
u, err = url.JoinPath(
serverURL, "/galene-api/v0/.groups", groupname,
".users", username, ".password",
)
}
2024-10-23 20:39:53 +02:00
if err != nil {
log.Fatalf("Build URL: %v", err)
}
2024-10-25 15:40:40 +02:00
err = putJSON(u, pw, true)
2024-10-23 20:39:53 +02:00
if err != nil {
log.Fatalf("Set password: %v", err)
}
}
func deletePasswordCmd(cmdname string, args []string) {
2024-10-25 15:40:40 +02:00
var groupname, username string
var wildcard bool
2024-10-23 20:39:53 +02:00
cmd := flag.NewFlagSet(cmdname, flag.ExitOnError)
setUsage(cmd, cmdname,
2024-10-25 15:40:40 +02:00
"%v [option...] %v [option...]\n",
2024-10-23 20:39:53 +02:00
os.Args[0], cmdname,
)
cmd.StringVar(&groupname, "group", "", "group `name`")
2024-10-25 15:40:40 +02:00
cmd.StringVar(&username, "user", "", "user `name`")
cmd.BoolVar(&wildcard, "wildcard", false, "set wildcard user's password")
2024-10-23 20:39:53 +02:00
cmd.Parse(args)
2024-10-25 15:40:40 +02:00
if cmd.NArg() != 0 {
2024-10-23 20:39:53 +02:00
cmd.Usage()
os.Exit(1)
}
if groupname == "" {
2024-10-25 15:40:40 +02:00
fmt.Fprintf(cmd.Output(),
"Option \"-group\" is required\n")
2024-10-23 20:39:53 +02:00
os.Exit(1)
}
2024-10-25 15:40:40 +02:00
if wildcard != (username == "") {
fmt.Fprintf(cmd.Output(),
"Exactly one of \"-user\" and \"-wildcard\" "+
"is required\n")
os.Exit(1)
}
var u string
var err error
if wildcard {
u, err = url.JoinPath(
serverURL, "/galene-api/v0/.groups", groupname,
".wildcard-user/.password",
)
} else {
u, err = url.JoinPath(
serverURL, "/galene-api/v0/.groups", groupname,
".users", username, ".password",
)
}
2024-10-23 20:39:53 +02:00
if err != nil {
log.Fatalf("Build URL: %v", err)
}
2024-10-25 15:40:40 +02:00
err = deleteValue(u)
2024-10-23 20:39:53 +02:00
if err != nil {
log.Fatalf("Delete password: %v", err)
}
}
2024-10-23 20:45:51 +02:00
func createGroupCmd(cmdname string, args []string) {
2024-10-25 15:40:40 +02:00
var groupname string
2024-10-23 20:45:51 +02:00
cmd := flag.NewFlagSet(cmdname, flag.ExitOnError)
setUsage(cmd, cmdname,
2024-10-25 15:40:40 +02:00
"%v [option...] %v [option...]\n",
2024-10-23 20:45:51 +02:00
os.Args[0], cmdname,
)
2024-10-25 15:40:40 +02:00
cmd.StringVar(&groupname, "group", "", "group `name`")
2024-10-23 20:45:51 +02:00
cmd.Parse(args)
2024-10-25 15:40:40 +02:00
if cmd.NArg() != 0 {
2024-10-23 20:45:51 +02:00
cmd.Usage()
os.Exit(1)
}
2024-10-25 15:40:40 +02:00
if groupname == "" {
fmt.Fprintf(cmd.Output(),
"Option \"-group\" is required\n")
os.Exit(1)
}
2024-10-23 20:45:51 +02:00
url, err := url.JoinPath(
2024-10-25 15:40:40 +02:00
serverURL, "/galene-api/v0/.groups", groupname,
2024-10-23 20:45:51 +02:00
)
if err != nil {
log.Fatalf("Build URL: %v", err)
}
err = putJSON(url, map[string]any{}, false)
if err != nil {
log.Fatalf("Create group: %v", err)
}
}
func deleteGroupCmd(cmdname string, args []string) {
2024-10-25 15:40:40 +02:00
var groupname string
2024-10-23 20:45:51 +02:00
cmd := flag.NewFlagSet(cmdname, flag.ExitOnError)
setUsage(cmd, cmdname,
2024-10-25 15:40:40 +02:00
"%v [option...] %v [option...]\n",
2024-10-23 20:45:51 +02:00
os.Args[0], cmdname,
)
2024-10-25 15:40:40 +02:00
cmd.StringVar(&groupname, "group", "", "group `name`")
2024-10-23 20:45:51 +02:00
cmd.Parse(args)
2024-10-25 15:40:40 +02:00
if cmd.NArg() != 0 {
2024-10-23 20:45:51 +02:00
cmd.Usage()
os.Exit(1)
}
2024-10-25 15:40:40 +02:00
if groupname == "" {
fmt.Fprintf(cmd.Output(),
"Option \"-group\" is required\n")
os.Exit(1)
}
2024-10-23 20:45:51 +02:00
url, err := url.JoinPath(
2024-10-25 15:40:40 +02:00
serverURL, "/galene-api/v0/.groups", groupname,
2024-10-23 20:45:51 +02:00
)
if err != nil {
log.Fatalf("Build URL: %v", err)
}
err = deleteValue(url)
if err != nil {
log.Fatalf("Delete group: %v", err)
}
}
2024-10-23 20:49:33 +02:00
2024-10-25 17:00:47 +02:00
func parsePermissions(p string, expand bool) (any, error) {
2024-10-23 20:49:33 +02:00
p = strings.TrimSpace(p)
if len(p) == 0 {
return nil, errors.New("empty permissions")
}
if p[0] == '[' {
var a []any
err := json.Unmarshal([]byte(p), &a)
if err != nil {
return nil, err
}
return a, nil
}
2024-10-25 17:00:47 +02:00
if !expand {
return p, nil
}
pp, err := group.NewPermissions(p)
if err != nil {
return nil, err
}
return pp.Permissions(nil), nil
2024-10-23 20:49:33 +02:00
}
func formatRawPermissions(permissions []string) string {
var perms []byte
for _, p := range permissions {
if len(p) > 0 {
perms = append(perms, p[0])
} else {
perms = append(perms, '?')
}
}
sort.Slice(perms, func(i, j int) bool {
return perms[i] < perms[j]
})
return fmt.Sprintf("[%s]", perms)
}
func formatPermissions(permissions group.Permissions) string {
s := permissions.String()
if len(s) > 0 && s[0] != '[' {
return s
}
return formatRawPermissions(permissions.Permissions(nil))
}
2024-10-30 17:48:49 +01:00
func match(patterns []string, value string) (bool, error) {
for _, p := range patterns {
found, err := path.Match(p, value)
if err != nil {
return false, err
}
if found {
return true, nil
}
}
return false, nil
}
2024-10-27 15:03:27 +01:00
func listUsersCmd(cmdname string, args []string) {
var groupname string
2024-10-30 14:10:56 +01:00
var long bool
2024-10-27 15:03:27 +01:00
cmd := flag.NewFlagSet(cmdname, flag.ExitOnError)
setUsage(cmd, cmdname,
2024-10-30 17:48:49 +01:00
"%v [option...] %v [option...] [pattern...]\n",
2024-10-27 15:03:27 +01:00
os.Args[0], cmdname,
)
2024-10-30 13:45:21 +01:00
cmd.StringVar(&groupname, "group", "", "group `name`")
2024-10-30 14:10:56 +01:00
cmd.BoolVar(&long, "l", false, "display permissions")
2024-10-27 15:03:27 +01:00
cmd.Parse(args)
2024-10-30 17:48:49 +01:00
patterns := cmd.Args()
2024-10-27 15:03:27 +01:00
if groupname == "" {
fmt.Fprintf(cmd.Output(),
"Option \"-group\" is required\n")
os.Exit(1)
}
u, err := url.JoinPath(serverURL, "/galene-api/v0/.groups/", groupname,
".users/")
if err != nil {
log.Fatalf("Build URL: %v", err)
}
var users []string
2024-10-30 14:51:47 +01:00
_, err = getJSON(u, &users)
2024-10-27 15:03:27 +01:00
if err != nil {
log.Fatalf("Get users: %v", err)
}
sort.Slice(users, func(i, j int) bool {
return users[i] < users[j]
})
2024-10-27 15:03:27 +01:00
for _, user := range users {
2024-10-30 17:48:49 +01:00
if len(patterns) > 0 {
found, err := match(patterns, user)
if err != nil {
log.Fatalf("Match: %v", err)
}
if !found {
continue
}
}
2024-10-30 14:10:56 +01:00
if !long {
fmt.Println(user)
} else {
uu, err := url.JoinPath(u, user)
if err != nil {
fmt.Printf("%-12s (ERROR=%v)\n", user, err)
continue
}
var d group.UserDescription
2024-10-30 14:51:47 +01:00
_, err = getJSON(uu, &d)
2024-10-30 14:10:56 +01:00
if err != nil {
fmt.Printf("%-12s (ERROR=%v)\n", user, err)
continue
}
fmt.Printf("%-12s %v\n",
user, formatPermissions(d.Permissions),
)
2024-10-30 14:10:56 +01:00
}
2024-10-27 15:03:27 +01:00
}
}
2024-10-23 20:49:33 +02:00
func createUserCmd(cmdname string, args []string) {
2024-10-25 15:40:40 +02:00
var groupname, username string
var wildcard bool
2024-10-23 20:49:33 +02:00
var permissions string
cmd := flag.NewFlagSet(cmdname, flag.ExitOnError)
setUsage(cmd, cmdname,
2024-10-27 14:58:39 +01:00
"%v [option...] %v [option...]\n",
2024-10-23 20:49:33 +02:00
os.Args[0], cmdname,
)
2024-10-30 13:45:21 +01:00
cmd.StringVar(&groupname, "group", "", "group `name`")
2024-10-25 15:40:40 +02:00
cmd.StringVar(&username, "user", "", "user `name`")
cmd.BoolVar(&wildcard, "wildcard", false, "create the wildcard user")
2024-10-23 20:49:33 +02:00
cmd.StringVar(&permissions, "permissions", "present", "permissions")
cmd.Parse(args)
2024-10-25 15:40:40 +02:00
if cmd.NArg() != 0 {
2024-10-23 20:49:33 +02:00
cmd.Usage()
os.Exit(1)
}
2024-10-25 15:40:40 +02:00
if groupname == "" {
fmt.Fprintf(cmd.Output(),
"Option \"-group\" is required\n")
os.Exit(1)
}
if wildcard != (username == "") {
fmt.Fprintf(cmd.Output(),
"Exactly one of \"-user\" and \"-wildcard\" "+
"is required\n")
os.Exit(1)
}
2024-10-25 17:00:47 +02:00
perms, err := parsePermissions(permissions, false)
2024-10-23 20:49:33 +02:00
if err != nil {
log.Fatalf("Parse permissions: %v", err)
}
2024-10-25 15:40:40 +02:00
var u string
if wildcard {
u, err = url.JoinPath(
serverURL, "/galene-api/v0/.groups", groupname,
".wildcard-user",
)
} else {
u, err = url.JoinPath(
serverURL, "/galene-api/v0/.groups", groupname,
".users", username,
)
}
2024-10-23 20:49:33 +02:00
if err != nil {
log.Fatalf("Build URL: %v", err)
}
dict := map[string]any{"permissions": perms}
2024-10-25 15:40:40 +02:00
err = putJSON(u, dict, false)
2024-10-23 20:49:33 +02:00
if err != nil {
log.Fatalf("Create user: %v", err)
}
}
2024-10-23 20:20:40 +02:00
func updateUserCmd(cmdname string, args []string) {
2024-10-25 15:40:40 +02:00
var groupname, username string
var wildcard bool
2024-10-23 20:20:40 +02:00
var permissions string
cmd := flag.NewFlagSet(cmdname, flag.ExitOnError)
setUsage(cmd, cmdname,
2024-10-25 15:40:40 +02:00
"%v [option...] %v [option...]\n",
2024-10-23 20:20:40 +02:00
os.Args[0], cmdname,
)
cmd.StringVar(&groupname, "group", "", "group `name`")
2024-10-25 15:40:40 +02:00
cmd.StringVar(&username, "user", "", "user `name`")
cmd.BoolVar(&wildcard, "wildcard", false, "update the wildcard user")
2024-10-23 20:20:40 +02:00
cmd.StringVar(&permissions, "permissions", "", "permissions")
cmd.Parse(args)
2024-10-25 15:40:40 +02:00
if cmd.NArg() != 0 {
2024-10-23 20:20:40 +02:00
cmd.Usage()
os.Exit(1)
}
2024-10-25 17:00:47 +02:00
perms, err := parsePermissions(permissions, false)
2024-10-23 20:20:40 +02:00
if err != nil {
log.Fatalf("Parse permissions: %v", err)
}
2024-10-25 15:40:40 +02:00
var u string
if wildcard {
u, err = url.JoinPath(
serverURL, "/galene-api/v0/.groups", groupname,
".wildcard-user",
)
} else {
u, err = url.JoinPath(
serverURL, "/galene-api/v0/.groups", groupname,
".users", username,
)
}
2024-10-23 20:20:40 +02:00
if err != nil {
log.Fatalf("Build URL: %v", err)
}
2024-10-25 15:40:40 +02:00
err = updateJSON(u, func(m map[string]any) map[string]any {
2024-10-23 20:20:40 +02:00
m["permissions"] = perms
return m
})
if err != nil {
log.Fatalf("Create user: %v", err)
}
}
2024-10-23 20:49:33 +02:00
func deleteUserCmd(cmdname string, args []string) {
2024-10-25 15:40:40 +02:00
var groupname, username string
var wildcard bool
2024-10-23 20:49:33 +02:00
cmd := flag.NewFlagSet(cmdname, flag.ExitOnError)
2024-10-25 15:40:40 +02:00
setUsage(cmd, cmdname, "%v [option...] %v [option...]\n",
2024-10-23 20:49:33 +02:00
os.Args[0], cmdname,
)
cmd.StringVar(&groupname, "group", "", "group `name`")
2024-10-25 15:40:40 +02:00
cmd.StringVar(&username, "user", "", "user `name`")
cmd.BoolVar(&wildcard, "wildcard", false, "delete the wildcard user")
2024-10-23 20:49:33 +02:00
cmd.Parse(args)
2024-10-25 15:40:40 +02:00
if cmd.NArg() != 0 {
2024-10-23 20:49:33 +02:00
cmd.Usage()
os.Exit(1)
}
if groupname == "" {
2024-10-25 15:40:40 +02:00
fmt.Fprintf(cmd.Output(),
"Option \"-group\" is required\n")
2024-10-23 20:49:33 +02:00
os.Exit(1)
}
2024-10-25 15:40:40 +02:00
if wildcard != (username == "") {
fmt.Fprintf(cmd.Output(),
"Exactly one of \"-user\" and \"-wildcard\" "+
"is required\n")
os.Exit(1)
}
var u string
var err error
if wildcard {
u, err = url.JoinPath(
serverURL, "/galene-api/v0/.groups", groupname,
".wildcard-user",
)
} else {
u, err = url.JoinPath(
serverURL, "/galene-api/v0/.groups", groupname,
".users", username,
)
}
2024-10-23 20:49:33 +02:00
if err != nil {
log.Fatalf("Build URL: %v", err)
}
2024-10-25 15:40:40 +02:00
err = deleteValue(u)
2024-10-23 20:49:33 +02:00
if err != nil {
log.Fatalf("Delete user: %v", err)
}
}
2024-10-25 16:03:45 +02:00
func listGroupsCmd(cmdname string, args []string) {
cmd := flag.NewFlagSet(cmdname, flag.ExitOnError)
setUsage(cmd, cmdname,
2024-10-30 17:48:49 +01:00
"%v [option...] %v [pattern...]\n",
2024-10-25 16:03:45 +02:00
os.Args[0], cmdname,
)
cmd.Parse(args)
2024-10-30 17:48:49 +01:00
patterns := cmd.Args()
2024-10-25 16:03:45 +02:00
u, err := url.JoinPath(serverURL, "/galene-api/v0/.groups/")
if err != nil {
log.Fatalf("Build URL: %v", err)
}
var groups []string
2024-10-30 14:51:47 +01:00
_, err = getJSON(u, &groups)
2024-10-25 16:03:45 +02:00
if err != nil {
log.Fatalf("Get groups: %v", err)
}
sort.Slice(groups, func(i, j int) bool {
return groups[i] < groups[j]
})
2024-10-25 16:03:45 +02:00
for _, g := range groups {
2024-10-30 17:48:49 +01:00
if len(patterns) > 0 {
found, err := match(patterns, g)
if err != nil {
log.Fatalf("Match: %v", err)
}
if !found {
continue
}
}
2024-10-25 16:03:45 +02:00
fmt.Println(g)
}
}
2024-10-25 17:00:47 +02:00
2024-10-30 15:44:53 +01:00
func listTokensCmd(cmdname string, args []string) {
var groupname string
var long bool
cmd := flag.NewFlagSet(cmdname, flag.ExitOnError)
setUsage(cmd, cmdname, "%v [option...] %v [option...]\n",
os.Args[0], cmdname,
)
cmd.StringVar(&groupname, "group", "", "group `name`")
cmd.BoolVar(&long, "l", false, "display token fields")
cmd.Parse(args)
if cmd.NArg() != 0 {
cmd.Usage()
os.Exit(1)
}
if groupname == "" {
fmt.Fprintf(cmd.Output(),
"Option \"-group\" is required\n")
os.Exit(1)
}
u, err := url.JoinPath(
serverURL, "/galene-api/v0/.groups/", groupname, ".tokens/",
)
if err != nil {
log.Fatalf("Build URL: %v", err)
}
var tokens []string
_, err = getJSON(u, &tokens)
if err != nil {
log.Fatalf("Get tokens: %v", err)
}
sort.Slice(tokens, func(i, j int) bool {
return tokens[i] < tokens[j]
})
now := time.Now()
for _, t := range tokens {
if !long {
fmt.Println(t)
} else {
uu, err := url.JoinPath(u, t)
if err != nil {
fmt.Printf("%-12s (ERROR=%v)\n", t, err)
continue
}
var tt token.Stateful
_, err = getJSON(uu, &tt)
if err != nil {
fmt.Printf("%-12s (ERROR=%v)\n", t, err)
continue
}
var username string
if tt.Username != nil {
username = *tt.Username
}
var exp string
if tt.Expires == nil {
exp = "(no expiration date)"
} else if tt.Expires.Before(now) {
exp = "(expired)"
} else {
exp = tt.Expires.Format(time.DateTime)
}
var perms []byte
for _, p := range tt.Permissions {
if len(p) > 0 {
perms = append(perms, p[0])
} else {
perms = append(perms, '?')
}
}
sort.Slice(perms, func(i, j int) bool {
return perms[i] < perms[j]
})
fmt.Printf("%-11s %-12s %-4s %-20s\n", t,
username, perms, exp,
)
}
}
}
2024-10-25 17:00:47 +02:00
func createTokenCmd(cmdname string, args []string) {
var groupname, username, permissions string
cmd := flag.NewFlagSet(cmdname, flag.ExitOnError)
setUsage(cmd, cmdname, "%v [option...] %v [option...]\n",
os.Args[0], cmdname,
)
cmd.StringVar(&groupname, "group", "", "group `name`")
cmd.StringVar(&username, "user", "", "encode user `name` in token")
cmd.StringVar(&permissions, "permissions", "present", "permissions")
cmd.Parse(args)
if cmd.NArg() != 0 {
cmd.Usage()
os.Exit(1)
}
if groupname == "" {
fmt.Fprintf(cmd.Output(),
"Option \"-group\" is required\n")
os.Exit(1)
}
perms, err := parsePermissions(permissions, true)
if err != nil {
log.Fatalf("Parse permissions: %v", err)
}
t := make(map[string]any)
t["permissions"] = perms
2024-10-25 17:51:42 +02:00
t["expires"] = time.Now().Add(24 * time.Hour)
2024-10-25 17:00:47 +02:00
if username != "" {
t["username"] = username
}
u, err := url.JoinPath(
serverURL, "/galene-api/v0/.groups/", groupname, ".tokens/",
)
if err != nil {
log.Fatalf("Build URL: %v", err)
}
location, err := postJSON(u, t)
if err != nil {
log.Fatalf("Create token: %v", err)
}
2024-10-25 18:22:55 +02:00
fmt.Println(location)
2024-10-25 17:00:47 +02:00
}
2024-10-25 17:52:00 +02:00
func revokeTokenCmd(cmdname string, args []string) {
var groupname, token string
cmd := flag.NewFlagSet(cmdname, flag.ExitOnError)
setUsage(cmd, cmdname, "%v [option...] %v [option...]\n",
os.Args[0], cmdname,
)
cmd.StringVar(&groupname, "group", "", "group `name`")
cmd.StringVar(&token, "token", "", "`token` to delete")
cmd.Parse(args)
if cmd.NArg() != 0 {
cmd.Usage()
os.Exit(1)
}
if groupname == "" || token == "" {
fmt.Fprintf(cmd.Output(),
"Options \"-group\" and \"-token\" are required\n")
os.Exit(1)
}
u, err := url.JoinPath(
serverURL, "/galene-api/v0/.groups/", groupname,
".tokens", token,
)
if err != nil {
log.Fatalf("Build URL: %v", err)
}
err = updateJSON(u, func(v map[string]any) map[string]any {
v["expires"] = time.Now().Add(-time.Minute)
return v
})
if err != nil {
log.Fatalf("Update token: %v", err)
}
}
2024-10-25 17:00:47 +02:00
func deleteTokenCmd(cmdname string, args []string) {
var groupname, token string
cmd := flag.NewFlagSet(cmdname, flag.ExitOnError)
setUsage(cmd, cmdname, "%v [option...] %v [option...]\n",
os.Args[0], cmdname,
)
cmd.StringVar(&groupname, "group", "", "group `name`")
cmd.StringVar(&token, "token", "", "`token` to delete")
cmd.Parse(args)
if cmd.NArg() != 0 {
cmd.Usage()
os.Exit(1)
}
if groupname == "" || token == "" {
fmt.Fprintf(cmd.Output(),
"Options \"-group\" and \"-token\" are required\n")
os.Exit(1)
}
u, err := url.JoinPath(
serverURL, "/galene-api/v0/.groups/", groupname,
".tokens", token,
)
if err != nil {
log.Fatalf("Build URL: %v", err)
}
err = deleteValue(u)
if err != nil {
log.Fatalf("Delete token: %v", err)
}
}