github.com/kbehouse/nsc@v0.0.6/cmd/generatenkey.go (about) 1 /* 2 * Copyright 2018-2020 The NATS Authors 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package cmd 17 18 import ( 19 "fmt" 20 21 "github.com/kbehouse/nsc/cmd/store" 22 "github.com/nats-io/nkeys" 23 "github.com/spf13/cobra" 24 ) 25 26 func createGenerateNKeyCmd() *cobra.Command { 27 var params GenerateNKeysParam 28 params.operator.prefix = nkeys.PrefixByteOperator 29 params.account.prefix = nkeys.PrefixByteAccount 30 params.user.prefix = nkeys.PrefixByteUser 31 32 cmd := &cobra.Command{ 33 Use: "nkey", 34 Short: "Generates an nkey", 35 RunE: func(cmd *cobra.Command, args []string) error { 36 return RunMaybeStorelessAction(cmd, args, ¶ms) 37 }, 38 } 39 cmd.Flags().BoolVarP(¶ms.operator.generate, "operator", "o", false, "operator") 40 cmd.Flags().BoolVarP(¶ms.account.generate, "account", "a", false, "account") 41 cmd.Flags().BoolVarP(¶ms.user.generate, "user", "u", false, "user") 42 cmd.Flags().BoolVarP(¶ms.store, "store", "S", false, "store in the keystore") 43 44 return cmd 45 } 46 47 func init() { 48 generateCmd.AddCommand(createGenerateNKeyCmd()) 49 } 50 51 type GenerateNKeysParam struct { 52 operator KP 53 account KP 54 user KP 55 store bool 56 } 57 58 type KP struct { 59 generate bool 60 prefix nkeys.PrefixByte 61 kp nkeys.KeyPair 62 fp string 63 } 64 65 func (e *KP) kind() string { 66 switch e.prefix { 67 case nkeys.PrefixByteOperator: 68 return "operator" 69 case nkeys.PrefixByteAccount: 70 return "account" 71 case nkeys.PrefixByteUser: 72 return "user" 73 } 74 return "" 75 } 76 77 func (e *KP) Generate() error { 78 var err error 79 e.kp, err = nkeys.CreatePair(e.prefix) 80 if err != nil { 81 panic(err) 82 } 83 return err 84 } 85 86 func (e *KP) String(pubOnly bool) string { 87 if e.kp != nil { 88 if pubOnly { 89 pk, err := e.kp.PublicKey() 90 if err != nil { 91 return "" 92 } 93 return fmt.Sprintf("%s\n%s key stored %s\n", pk, e.kind(), e.fp) 94 } else { 95 seed, err := e.kp.Seed() 96 if err != nil { 97 return "" 98 } 99 pk, err := e.kp.PublicKey() 100 if err != nil { 101 return "" 102 } 103 if e.fp == "" { 104 return fmt.Sprintf("%s\n%s\n", string(seed), pk) 105 } 106 return fmt.Sprintf("%s\n%s\n%s key stored %s\n", string(seed), pk, e.kind(), e.fp) 107 } 108 } 109 return "" 110 } 111 112 func (p *GenerateNKeysParam) SetDefaults(ctx ActionCtx) error { 113 if !ctx.AnySet("operator", "account", "user") { 114 return fmt.Errorf("set --operator, --account, or --user") 115 } 116 return nil 117 } 118 119 func (p *GenerateNKeysParam) PreInteractive(ctx ActionCtx) error { 120 return nil 121 } 122 123 func (p *GenerateNKeysParam) Load(ctx ActionCtx) error { 124 return nil 125 } 126 127 func (p *GenerateNKeysParam) PostInteractive(ctx ActionCtx) error { 128 return nil 129 } 130 131 func (p *GenerateNKeysParam) Validate(ctx ActionCtx) error { 132 return nil 133 } 134 135 func (p *GenerateNKeysParam) Run(ctx ActionCtx) (store.Status, error) { 136 var err error 137 var jobs []*KP 138 jobs = append(jobs, &p.operator, &p.account, &p.user) 139 for _, j := range jobs { 140 if j.generate { 141 if err := j.Generate(); err != nil { 142 return nil, err 143 } 144 if p.store { 145 j.fp, err = ctx.StoreCtx().KeyStore.Store(j.kp) 146 if err != nil { 147 return nil, err 148 } 149 } 150 ctx.CurrentCmd().Println(j.String(p.store)) 151 } 152 } 153 return nil, nil 154 }