2019-01-11 14:59:35 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-01-18 15:01:55 +00:00
|
|
|
"bufio"
|
2019-01-11 14:59:35 +00:00
|
|
|
"encoding/csv"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"text/tabwriter"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/certificates"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
authCmd = &cobra.Command{
|
|
|
|
Use: "auth",
|
|
|
|
Short: "CSR authorization management",
|
|
|
|
}
|
|
|
|
|
|
|
|
authCreateCmd = &cobra.Command{
|
|
|
|
Use: "create <auth_increment_count> [<email>, ...]",
|
|
|
|
Short: "Create authorizations from a list of emails",
|
|
|
|
Args: cobra.MinimumNArgs(1),
|
|
|
|
RunE: cmdCreateAuth,
|
|
|
|
}
|
|
|
|
|
|
|
|
authInfoCmd = &cobra.Command{
|
|
|
|
Use: "info [<email>, ...]",
|
|
|
|
Short: "Get authorization(s) info from CSR authorization DB",
|
|
|
|
RunE: cmdInfoAuth,
|
|
|
|
}
|
|
|
|
|
|
|
|
authExportCmd = &cobra.Command{
|
|
|
|
Use: "export [<email>, ...]",
|
|
|
|
Short: "Export authorization(s) from CSR authorization DB to a CSV file (or stdout)",
|
|
|
|
RunE: cmdExportAuth,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-01-18 15:01:55 +00:00
|
|
|
func parseEmailsList(fileName, delimiter string) (emails []string, err error) {
|
|
|
|
file, err := os.Open(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Wrap(err)
|
|
|
|
}
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if strings.HasPrefix(line, "#") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
emails = append(emails, line)
|
|
|
|
}
|
|
|
|
return emails, file.Close()
|
|
|
|
}
|
|
|
|
|
2019-01-11 14:59:35 +00:00
|
|
|
func cmdCreateAuth(cmd *cobra.Command, args []string) error {
|
|
|
|
count, err := strconv.Atoi(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return errs.New("Count couldn't be parsed: %s", args[0])
|
|
|
|
}
|
2019-01-22 12:35:48 +00:00
|
|
|
authDB, err := config.Signer.NewAuthDB()
|
2019-01-11 14:59:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var emails []string
|
|
|
|
if len(args) > 1 {
|
2019-01-22 12:35:48 +00:00
|
|
|
if config.EmailsPath != "" {
|
2019-01-11 14:59:35 +00:00
|
|
|
return errs.New("Either use `--emails-path` or positional args, not both.")
|
|
|
|
}
|
|
|
|
emails = args[1:]
|
|
|
|
} else {
|
2019-01-22 12:35:48 +00:00
|
|
|
emails, err = parseEmailsList(config.EmailsPath, config.Delimiter)
|
2019-01-11 14:59:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-29 12:30:23 +00:00
|
|
|
var incErrs errs.Group
|
2019-01-11 14:59:35 +00:00
|
|
|
for _, email := range emails {
|
|
|
|
if _, err := authDB.Create(email, count); err != nil {
|
|
|
|
incErrs.Add(err)
|
|
|
|
}
|
|
|
|
}
|
2019-03-29 12:30:23 +00:00
|
|
|
return incErrs.Err()
|
2019-01-11 14:59:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func cmdInfoAuth(cmd *cobra.Command, args []string) error {
|
2019-01-22 12:35:48 +00:00
|
|
|
authDB, err := config.Signer.NewAuthDB()
|
2019-01-11 14:59:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var emails []string
|
|
|
|
if len(args) > 0 {
|
2019-01-22 12:35:48 +00:00
|
|
|
if config.EmailsPath != "" && !config.All {
|
2019-01-11 14:59:35 +00:00
|
|
|
return errs.New("Either use `--emails-path` or positional args, not both.")
|
|
|
|
}
|
|
|
|
emails = args
|
2019-01-22 12:35:48 +00:00
|
|
|
} else if len(args) == 0 || config.All {
|
2019-01-14 14:28:52 +00:00
|
|
|
emails, err = authDB.UserIDs()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-22 12:35:48 +00:00
|
|
|
} else if _, err := os.Stat(config.EmailsPath); err != nil {
|
2019-01-11 14:59:35 +00:00
|
|
|
return errs.New("Emails path error: %s", err)
|
|
|
|
} else {
|
2019-01-22 12:35:48 +00:00
|
|
|
emails, err = parseEmailsList(config.EmailsPath, config.Delimiter)
|
2019-01-11 14:59:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-29 12:30:23 +00:00
|
|
|
var emailErrs, printErrs errs.Group
|
2019-01-11 14:59:35 +00:00
|
|
|
w := tabwriter.NewWriter(os.Stdout, 0, 2, 2, ' ', 0)
|
|
|
|
if _, err := fmt.Fprintln(w, "Email\tClaimed\tAvail.\t"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, email := range emails {
|
|
|
|
if err := writeAuthInfo(authDB, email, w); err != nil {
|
|
|
|
emailErrs.Add(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := w.Flush(); err != nil {
|
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
2019-03-29 12:30:23 +00:00
|
|
|
return errs.Combine(emailErrs.Err(), printErrs.Err())
|
2019-01-11 14:59:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func writeAuthInfo(authDB *certificates.AuthorizationDB, email string, w io.Writer) error {
|
|
|
|
auths, err := authDB.Get(email)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(auths) < 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
claimed, open := auths.Group()
|
|
|
|
if _, err := fmt.Fprintf(w,
|
|
|
|
"%s\t%d\t%d\t\n",
|
|
|
|
email,
|
|
|
|
len(claimed),
|
|
|
|
len(open),
|
|
|
|
); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-22 12:35:48 +00:00
|
|
|
if config.ShowTokens {
|
2019-01-11 14:59:35 +00:00
|
|
|
if err := writeTokenInfo(claimed, open, w); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeTokenInfo(claimed, open certificates.Authorizations, w io.Writer) error {
|
|
|
|
groups := map[string]certificates.Authorizations{
|
|
|
|
"Claimed": claimed,
|
|
|
|
"Open": open,
|
|
|
|
}
|
|
|
|
for label, group := range groups {
|
|
|
|
if _, err := fmt.Fprintf(w, "\t%s:\n", label); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(group) > 0 {
|
|
|
|
for _, auth := range group {
|
|
|
|
if _, err := fmt.Fprintf(w, "\t\t%s\n", auth.Token.String()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, err := fmt.Fprintln(w, "\t\tnone"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdExportAuth(cmd *cobra.Command, args []string) error {
|
2019-01-22 12:35:48 +00:00
|
|
|
authDB, err := config.Signer.NewAuthDB()
|
2019-01-11 14:59:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var emails []string
|
2019-01-22 12:35:48 +00:00
|
|
|
if len(args) > 0 && !config.All {
|
|
|
|
if config.EmailsPath != "" {
|
2019-01-11 14:59:35 +00:00
|
|
|
return errs.New("Either use `--emails-path` or positional args, not both.")
|
|
|
|
}
|
|
|
|
emails = args
|
2019-01-22 12:35:48 +00:00
|
|
|
} else if len(args) == 0 || config.All {
|
2019-01-11 14:59:35 +00:00
|
|
|
emails, err = authDB.UserIDs()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2019-01-22 12:35:48 +00:00
|
|
|
emails, err = parseEmailsList(config.EmailsPath, config.Delimiter)
|
2019-01-11 14:59:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2019-03-29 12:30:23 +00:00
|
|
|
emailErrs, csvErrs errs.Group
|
2019-01-11 14:59:35 +00:00
|
|
|
output io.Writer
|
|
|
|
)
|
2019-01-22 12:35:48 +00:00
|
|
|
switch config.Out {
|
2019-01-11 14:59:35 +00:00
|
|
|
case "-":
|
|
|
|
output = os.Stdout
|
|
|
|
default:
|
2019-01-22 12:35:48 +00:00
|
|
|
if err := os.MkdirAll(filepath.Dir(config.Out), 0600); err != nil {
|
2019-01-11 14:59:35 +00:00
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
2019-01-22 12:35:48 +00:00
|
|
|
output, err = os.OpenFile(config.Out, os.O_CREATE, 0600)
|
2019-01-11 14:59:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
csvWriter := csv.NewWriter(output)
|
|
|
|
|
|
|
|
for _, email := range emails {
|
|
|
|
if err := writeAuthExport(authDB, email, csvWriter); err != nil {
|
|
|
|
emailErrs.Add(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
csvWriter.Flush()
|
2019-03-29 12:30:23 +00:00
|
|
|
return errs.Combine(emailErrs.Err(), csvErrs.Err())
|
2019-01-11 14:59:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func writeAuthExport(authDB *certificates.AuthorizationDB, email string, w *csv.Writer) error {
|
|
|
|
auths, err := authDB.Get(email)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(auths) < 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-29 12:30:23 +00:00
|
|
|
var authErrs errs.Group
|
2019-01-11 14:59:35 +00:00
|
|
|
for _, auth := range auths {
|
|
|
|
if err := w.Write([]string{email, auth.Token.String()}); err != nil {
|
|
|
|
authErrs.Add(err)
|
|
|
|
}
|
|
|
|
}
|
2019-03-29 12:30:23 +00:00
|
|
|
return authErrs.Err()
|
2019-01-11 14:59:35 +00:00
|
|
|
}
|