2019-02-06 11:00:26 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2019-09-05 16:11:21 +01:00
|
|
|
"storj.io/storj/pkg/certificate/authorization"
|
2019-06-04 12:36:27 +01:00
|
|
|
"storj.io/storj/pkg/process"
|
2019-02-06 11:00:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
claimsCmd = &cobra.Command{
|
|
|
|
Use: "claims",
|
2019-02-14 19:17:26 +00:00
|
|
|
Short: "CSR authorization claim management",
|
2019-02-06 11:00:26 +00:00
|
|
|
}
|
|
|
|
|
2019-02-14 19:17:26 +00:00
|
|
|
claimsExportCmd = &cobra.Command{
|
|
|
|
Use: "export",
|
|
|
|
Short: "Export all claim data as JSON",
|
|
|
|
RunE: cmdExportClaims,
|
|
|
|
}
|
|
|
|
|
|
|
|
claimDeleteCmd = &cobra.Command{
|
|
|
|
Use: "delete",
|
|
|
|
Short: "Delete a claim on an authorization",
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
RunE: cmdDeleteClaim,
|
|
|
|
}
|
2019-02-06 11:00:26 +00:00
|
|
|
)
|
|
|
|
|
2019-02-14 19:17:26 +00:00
|
|
|
func cmdExportClaims(cmd *cobra.Command, args []string) (err error) {
|
2019-06-04 12:36:27 +01:00
|
|
|
ctx := process.Ctx(cmd)
|
2019-09-05 16:11:21 +01:00
|
|
|
authDB, err := authorization.NewDBFromCfg(claimsExportCfg.Authorizations)
|
2019-02-06 11:00:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-09-05 16:11:21 +01:00
|
|
|
|
2019-02-06 11:00:26 +00:00
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, authDB.Close())
|
|
|
|
}()
|
|
|
|
|
2019-06-04 12:36:27 +01:00
|
|
|
auths, err := authDB.List(ctx)
|
2019-02-06 11:00:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var toPrint []interface{}
|
|
|
|
for _, auth := range auths {
|
2019-02-14 19:17:26 +00:00
|
|
|
if claimsExportCfg.Raw {
|
2019-02-06 11:00:26 +00:00
|
|
|
toPrint = append(toPrint, auth)
|
|
|
|
} else {
|
|
|
|
toPrint = append(toPrint, toPrintableAuth(auth))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(toPrint) == 0 {
|
2019-09-05 16:11:21 +01:00
|
|
|
fmt.Printf("no claims in database: %s\n", claimsExportCfg.Authorizations.DBURL)
|
2019-02-06 11:00:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytes, err := json.MarshalIndent(toPrint, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(string(jsonBytes))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-14 19:17:26 +00:00
|
|
|
func cmdDeleteClaim(cmd *cobra.Command, args []string) (err error) {
|
2019-06-04 12:36:27 +01:00
|
|
|
ctx := process.Ctx(cmd)
|
2019-09-05 16:11:21 +01:00
|
|
|
authDB, err := authorization.NewDBFromCfg(claimsDeleteCfg.Authorizations)
|
2019-02-14 19:17:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, authDB.Close())
|
|
|
|
}()
|
|
|
|
|
2019-06-04 12:36:27 +01:00
|
|
|
if err := authDB.Unclaim(ctx, args[0]); err != nil {
|
2019-02-14 19:17:26 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-06 11:00:26 +00:00
|
|
|
type printableAuth struct {
|
|
|
|
UserID string
|
|
|
|
Token string
|
|
|
|
Claim *printableClaim
|
|
|
|
}
|
|
|
|
type printableClaim struct {
|
|
|
|
Addr string
|
|
|
|
Time string
|
|
|
|
NodeID string
|
|
|
|
}
|
|
|
|
|
2019-09-05 16:11:21 +01:00
|
|
|
func toPrintableAuth(auth *authorization.Authorization) *printableAuth {
|
2019-02-06 11:00:26 +00:00
|
|
|
pAuth := new(printableAuth)
|
|
|
|
|
|
|
|
pAuth.UserID = auth.Token.UserID
|
|
|
|
pAuth.Token = auth.Token.String()
|
|
|
|
|
|
|
|
if auth.Claim != nil {
|
|
|
|
pAuth.Claim = &printableClaim{
|
|
|
|
Time: time.Unix(auth.Claim.Timestamp, 0).String(),
|
|
|
|
Addr: auth.Claim.Addr,
|
|
|
|
NodeID: auth.Claim.Identity.ID.String(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pAuth
|
|
|
|
}
|