storj/cmd/certificates/claims.go

119 lines
2.3 KiB
Go
Raw Normal View History

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"
"storj.io/private/process"
"storj.io/storj/certificate/authorization"
2019-02-06 11:00:26 +00:00
)
var (
claimsCmd = &cobra.Command{
Use: "claims",
Short: "CSR authorization claim management",
2019-02-06 11:00: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
)
func cmdExportClaims(cmd *cobra.Command, args []string) (err error) {
ctx, _ := process.Ctx(cmd)
authDB, err := authorization.NewDBFromCfg(claimsExportCfg.Config.AuthorizationDB)
2019-02-06 11:00:26 +00:00
if err != nil {
return err
}
2019-02-06 11:00:26 +00:00
defer func() {
err = errs.Combine(err, authDB.Close())
}()
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 {
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 {
fmt.Printf("no claims in database: %s\n", claimsExportCfg.Config.AuthorizationDB.URL)
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
}
func cmdDeleteClaim(cmd *cobra.Command, args []string) (err error) {
ctx, _ := process.Ctx(cmd)
authDB, err := authorization.NewDBFromCfg(claimsDeleteCfg.AuthorizationDB)
if err != nil {
return err
}
defer func() {
err = errs.Combine(err, authDB.Close())
}()
if err := authDB.Unclaim(ctx, args[0]); err != nil {
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
}
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
}