Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
Signed-off-by: Alberto Ricart <alberto@synadia.com>
  • Loading branch information
aricart committed Jan 25, 2025
1 parent f91ecc0 commit 2e61259
Showing 1 changed file with 85 additions and 31 deletions.
116 changes: 85 additions & 31 deletions cmd/exportoperator.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/nats-io/nsc/v2/cmd/store"
"github.com/spf13/cobra"
"os"
"path/filepath"
)

func createExportEnvironmentCmd() *cobra.Command {
Expand Down Expand Up @@ -62,12 +63,44 @@ func (e *EntityKey) GetKeyPair() nkeys.KeyPair {
if err != nil {
return nil
}
e.KeyPair = kp
return kp
}
return nil
}

func (e *EntityKey) Reissue() error {
var pk string
var err error
kp := e.GetKeyPair()
if kp != nil {
pk, err = kp.PublicKey()
if err != nil {
return err
}
e.Key = pk
}
var prefix nkeys.PrefixByte = 0
switch e.Key[0] {
case 'O':
prefix = nkeys.PrefixByteOperator
case 'A':
prefix = nkeys.PrefixByteAccount
case 'U':
prefix = nkeys.PrefixByteUser
case 'X':
prefix = nkeys.PrefixByteCurve
}
if prefix == 0 {
return fmt.Errorf("invalid key prefix: %s", e.Key[0])

Check failure on line 94 in cmd/exportoperator.go

View workflow job for this annotation

GitHub Actions / test (stable, ubuntu-latest, true)

fmt.Errorf format %s has arg e.Key[0] of wrong type byte

Check failure on line 94 in cmd/exportoperator.go

View workflow job for this annotation

GitHub Actions / test (stable, windows-latest, false)

fmt.Errorf format %s has arg e.Key[0] of wrong type byte

Check failure on line 94 in cmd/exportoperator.go

View workflow job for this annotation

GitHub Actions / test (stable, macos-latest, false)

fmt.Errorf format %s has arg e.Key[0] of wrong type byte

Check failure on line 94 in cmd/exportoperator.go

View workflow job for this annotation

GitHub Actions / test (stable, ubuntu-latest, true)

fmt.Errorf format %s has arg e.Key[0] of wrong type byte

Check failure on line 94 in cmd/exportoperator.go

View workflow job for this annotation

GitHub Actions / test (stable, windows-latest, false)

fmt.Errorf format %s has arg e.Key[0] of wrong type byte

Check failure on line 94 in cmd/exportoperator.go

View workflow job for this annotation

GitHub Actions / test (stable, macos-latest, false)

fmt.Errorf format %s has arg e.Key[0] of wrong type byte
}
kp, err = nkeys.CreatePair(prefix)
if err != nil {
return err
}
e.KeyPair = kp
return nil
}

func (e *EntityKey) MarshalJSON() ([]byte, error) {
type E struct {
Key string `json:"key,omitempty"`
Expand Down Expand Up @@ -103,12 +136,12 @@ func (p *ExportEnvironmentParams) SetDefaults(ctx ActionCtx) error {
}
if p.name != ctx.StoreCtx().Operator.Name {
current := GetConfig()
if err := current.SetOperator(p.name); err != nil {
return err
}
if err := current.Save(); err != nil {
fp := filepath.Join(current.StoreRoot, p.name)
sto, err := store.LoadStore(fp)
if err != nil {
return err
}
ctx.StoreCtx().Store = sto
}
return nil
}
Expand Down Expand Up @@ -139,7 +172,7 @@ func (p *ExportEnvironmentParams) Run(ctx ActionCtx) (store.Status, error) {
return nil, err
}

root := &Entity{Name: ctx.StoreCtx().Operator.Name, Jwt: string(token)}
root := &Entity{Name: s.Info.Name, Jwt: string(token)}
oKeys, err := ctx.StoreCtx().GetOperatorKeys()
if err != nil {
return nil, err
Expand All @@ -151,6 +184,11 @@ func (p *ExportEnvironmentParams) Run(ctx ActionCtx) (store.Status, error) {
root.Keys = append(root.Keys, &EntityKey{Key: k})
continue
}
if kp == nil {
r.AddWarning("unable operator key %s not found", k)
root.Keys = append(root.Keys, &EntityKey{Key: k})
continue
}
seed, err := kp.Seed()
if err != nil {
r.AddWarning("failed reading seed for %s: %v", k, err.Error())
Expand All @@ -160,7 +198,7 @@ func (p *ExportEnvironmentParams) Run(ctx ActionCtx) (store.Status, error) {
root.Keys = append(root.Keys, &EntityKey{Seed: string(seed), Key: k})
}

accounts, err := config.ListAccounts()
accounts, err := ctx.StoreCtx().Store.ListSubContainers(store.Accounts)
if err != nil {
r.AddError("error listing accounts: %v", err.Error())
return r, err
Expand Down Expand Up @@ -193,6 +231,11 @@ func (p *ExportEnvironmentParams) Run(ctx ActionCtx) (store.Status, error) {
account.Keys = append(account.Keys, &EntityKey{Key: k})
continue
}
if kp == nil {
r.AddWarning("unable account key %s not found", k)
account.Keys = append(account.Keys, &EntityKey{Key: k})
continue
}
seed, err := kp.Seed()
if err != nil {
r.AddWarning("failed reading seed for %s: %v", k, err.Error())
Expand Down Expand Up @@ -259,9 +302,11 @@ func (p *ExportEnvironmentParams) Run(ctx ActionCtx) (store.Status, error) {
}

type ImportEnvironmentParams struct {
in string
force bool
entity Entity
in string
force bool
entity Entity
rename string
reissue bool
}

func createImportEnvironment() *cobra.Command {
Expand All @@ -277,6 +322,8 @@ func createImportEnvironment() *cobra.Command {
}
cmd.Flags().StringVarP(&params.in, "in", "", "", "input file")
cmd.Flags().BoolVarP(&params.force, "force", "", false, "overwrite existing operator")
cmd.Flags().StringVarP(&params.rename, "rename", "", "", "rename operator")
cmd.Flags().BoolVarP(&params.reissue, "reissue", "", false, "regenerate all keys")
return cmd
}

Expand All @@ -292,6 +339,13 @@ func (p *ImportEnvironmentParams) PreInteractive(_ ActionCtx) error {
return nil
}

func (p *ImportEnvironmentParams) operatorName() string {
if p.rename != "" {
return p.rename
}
return p.entity.Name
}

func (p *ImportEnvironmentParams) Load(ctx ActionCtx) error {
if p.in == "" {
return fmt.Errorf("specify an input file")
Expand All @@ -306,10 +360,12 @@ func (p *ImportEnvironmentParams) Load(ctx ActionCtx) error {
if p.entity.Name == "" || p.entity.Jwt == "" {
return fmt.Errorf("invalid input file, operator name/jwt required")
}

opName := p.operatorName()
operators := config.ListOperators()
found := false
for _, o := range operators {
if o == p.entity.Name {
if o == opName {
found = true
break
}
Expand Down Expand Up @@ -337,26 +393,21 @@ func (p *ImportEnvironmentParams) Run(ctx ActionCtx) (store.Status, error) {
return op, err
}

var okp nkeys.KeyPair
theStore, err := GetConfig().LoadStore(p.operatorName())
if err == nil && theStore != nil && !p.force {
op.AddError("operator %s already exist, '--force' to overwrite after creating a backup", p.entity.Name)
}

var okp nkeys.KeyPair
for _, k := range p.entity.Keys {
kp := k.GetKeyPair()
if k.Key == oc.Subject {
okp = kp
}
}

if okp == nil {
op.AddError("unable to find operator key")
return op, nil
}

theStore, err := GetConfig().LoadStore(p.entity.Name)
if err == nil && theStore != nil && !p.force {
op.AddError("operator %s already exist, '--force' to overwrite after creating a backup", p.entity.Name)
}
if theStore == nil {
nk := store.NamedKey{Name: p.entity.Name, KP: okp}
nk := store.NamedKey{Name: p.operatorName(), KP: okp}
theStore, err = store.CreateStore(p.entity.Name, GetConfig().StoreRoot, &nk)
}
if theStore == nil {
Expand All @@ -370,15 +421,17 @@ func (p *ImportEnvironmentParams) Run(ctx ActionCtx) (store.Status, error) {

op.AddOK("imported operator %q", p.entity.Name)

for idx, k := range p.entity.Keys {
for _, k := range p.entity.Keys {
pk := k.Key
kp := k.GetKeyPair()
if kp != nil {
op.AddError("unable to store operator key %q: %v", p.entity.Keys[idx], err)
if kp == nil {
op.AddError("operator key %q is not available", pk)
continue
}
if _, err := ctx.StoreCtx().KeyStore.Store(kp); err != nil {
op.AddError("unable to store operator key %q: %v", p.entity.Keys[idx], err)
op.AddError("unable to store operator key %q: %v", pk, err)
}
op.AddOK(" imported key %s", k.Seed)
op.AddOK("imported key %s", k.Seed)
}

for _, a := range p.entity.Children {
Expand All @@ -393,11 +446,12 @@ func (p *ImportEnvironmentParams) Run(ctx ActionCtx) (store.Status, error) {

for _, k := range a.Keys {
kp := k.GetKeyPair()
pk := k.Key
if kp == nil {
ra.AddError("account key %q not available", k.Key)
}
if _, err := ctx.StoreCtx().KeyStore.Store(kp); err != nil {
ra.AddError("unable to store key %q: %v", k, err)
ra.AddError("account key %q not available", pk)
continue
} else if _, err := ctx.StoreCtx().KeyStore.Store(kp); err != nil {
ra.AddError("unable to store key %q: %v", pk, err)
continue
}
ra.AddOK("imported key %q", k.Seed)
Expand Down

0 comments on commit 2e61259

Please sign in to comment.