github.com/nats-io/nsc/v2@v2.8.7-0.20240307184528-efd7023c6896/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/nats-io/nkeys"
    22  	"github.com/nats-io/nsc/v2/cmd/store"
    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  	params.curve.prefix = nkeys.PrefixByteCurve
    32  
    33  	cmd := &cobra.Command{
    34  		Use:   "nkey",
    35  		Short: "Generates an nkey",
    36  		RunE: func(cmd *cobra.Command, args []string) error {
    37  			return RunMaybeStorelessAction(cmd, args, &params)
    38  		},
    39  	}
    40  	cmd.Flags().BoolVarP(&params.operator.generate, "operator", "o", false, "operator")
    41  	cmd.Flags().BoolVarP(&params.account.generate, "account", "a", false, "account")
    42  	cmd.Flags().BoolVarP(&params.user.generate, "user", "u", false, "user")
    43  	cmd.Flags().BoolVarP(&params.curve.generate, "curve", "x", false, "curve")
    44  	cmd.Flags().BoolVarP(&params.store, "store", "S", false, "store in the keystore")
    45  
    46  	return cmd
    47  }
    48  
    49  func init() {
    50  	generateCmd.AddCommand(createGenerateNKeyCmd())
    51  }
    52  
    53  type GenerateNKeysParam struct {
    54  	operator KP
    55  	account  KP
    56  	user     KP
    57  	curve    KP
    58  	store    bool
    59  }
    60  
    61  type KP struct {
    62  	generate bool
    63  	prefix   nkeys.PrefixByte
    64  	kp       nkeys.KeyPair
    65  	fp       string
    66  }
    67  
    68  func (e *KP) kind() string {
    69  	switch e.prefix {
    70  	case nkeys.PrefixByteOperator:
    71  		return "operator"
    72  	case nkeys.PrefixByteAccount:
    73  		return "account"
    74  	case nkeys.PrefixByteUser:
    75  		return "user"
    76  	case nkeys.PrefixByteCurve:
    77  		return "curve"
    78  	}
    79  	return ""
    80  }
    81  
    82  func (e *KP) Generate() error {
    83  	var err error
    84  	e.kp, err = nkeys.CreatePair(e.prefix)
    85  	if err != nil {
    86  		panic(err)
    87  	}
    88  	return err
    89  }
    90  
    91  func (e *KP) String(pubOnly bool) string {
    92  	if e.kp != nil {
    93  		if pubOnly {
    94  			pk, err := e.kp.PublicKey()
    95  			if err != nil {
    96  				return ""
    97  			}
    98  			return fmt.Sprintf("%s\n%s key stored %s\n", pk, e.kind(), e.fp)
    99  		} else {
   100  			seed, err := e.kp.Seed()
   101  			if err != nil {
   102  				return ""
   103  			}
   104  			pk, err := e.kp.PublicKey()
   105  			if err != nil {
   106  				return ""
   107  			}
   108  			if e.fp == "" {
   109  				return fmt.Sprintf("%s\n%s\n", string(seed), pk)
   110  			}
   111  			return fmt.Sprintf("%s\n%s\n%s key stored %s\n", string(seed), pk, e.kind(), e.fp)
   112  		}
   113  	}
   114  	return ""
   115  }
   116  
   117  func (p *GenerateNKeysParam) SetDefaults(ctx ActionCtx) error {
   118  	if !ctx.AnySet("operator", "account", "user", "curve") {
   119  		return fmt.Errorf("set --operator, --account, --user, or --curve")
   120  	}
   121  	return nil
   122  }
   123  
   124  func (p *GenerateNKeysParam) PreInteractive(ctx ActionCtx) error {
   125  	return nil
   126  }
   127  
   128  func (p *GenerateNKeysParam) Load(ctx ActionCtx) error {
   129  	return nil
   130  }
   131  
   132  func (p *GenerateNKeysParam) PostInteractive(ctx ActionCtx) error {
   133  	return nil
   134  }
   135  
   136  func (p *GenerateNKeysParam) Validate(ctx ActionCtx) error {
   137  	return nil
   138  }
   139  
   140  func (p *GenerateNKeysParam) Run(ctx ActionCtx) (store.Status, error) {
   141  	var err error
   142  	var jobs []*KP
   143  	jobs = append(jobs, &p.operator, &p.account, &p.user, &p.curve)
   144  	for _, j := range jobs {
   145  		if j.generate {
   146  			if err := j.Generate(); err != nil {
   147  				return nil, err
   148  			}
   149  			if p.store {
   150  				j.fp, err = ctx.StoreCtx().KeyStore.Store(j.kp)
   151  				if err != nil {
   152  					return nil, err
   153  				}
   154  			}
   155  			ctx.CurrentCmd().Println(j.String(p.store))
   156  		}
   157  	}
   158  	return nil, nil
   159  }