github.com/nats-io/nsc@v0.0.0-20221206222106-35db9400b257/cmd/pubkeyparams.go (about)

     1  /*
     2   * Copyright 2018-2022 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  	cli "github.com/nats-io/cliprompts/v2"
    22  	"github.com/nats-io/jwt"
    23  	"github.com/nats-io/nkeys"
    24  	"github.com/nats-io/nsc/cmd/store"
    25  	"github.com/spf13/cobra"
    26  )
    27  
    28  type PubKeyChoice struct {
    29  	Label string
    30  	Key   string
    31  }
    32  
    33  type PubKeyParams struct {
    34  	flagName      string
    35  	kind          nkeys.PrefixByte
    36  	publicKey     string
    37  	AllowWildcard bool
    38  }
    39  
    40  func (e *PubKeyParams) SetDefaults(ctx ActionCtx) error {
    41  	if e.publicKey == "" || e.publicKey == "*" || store.IsPublicKey(e.kind, e.publicKey) {
    42  		return nil
    43  	}
    44  	switch e.kind {
    45  	case nkeys.PrefixByteAccount:
    46  		if c, err := ctx.StoreCtx().Store.ReadAccountClaim(e.publicKey); err != nil {
    47  			return err
    48  		} else {
    49  			e.publicKey = c.Subject
    50  		}
    51  	case nkeys.PrefixByteUser:
    52  		an := ctx.StoreCtx().Account.Name
    53  		if c, err := ctx.StoreCtx().Store.ReadUserClaim(an, e.publicKey); err != nil {
    54  			return err
    55  		} else {
    56  			e.publicKey = c.Subject
    57  		}
    58  	}
    59  	return nil
    60  }
    61  
    62  func (e *PubKeyParams) BindFlags(flagName string, shorthand string, kind nkeys.PrefixByte, cmd *cobra.Command) {
    63  	e.flagName = flagName
    64  	e.kind = kind
    65  	cmd.Flags().StringVarP(&e.publicKey, flagName, shorthand, "", flagName)
    66  }
    67  
    68  func (e *PubKeyParams) valid(s string) error {
    69  	if s == "" {
    70  		return fmt.Errorf("%s cannot be empty", e.flagName)
    71  	}
    72  	if e.AllowWildcard && s == jwt.All {
    73  		return nil
    74  	}
    75  
    76  	if !store.IsPublicKey(e.kind, s) {
    77  		return fmt.Errorf("%s is not a valid %q public key", e.publicKey, e.kind.String())
    78  	}
    79  
    80  	return nil
    81  }
    82  
    83  func (e *PubKeyParams) Valid() error {
    84  	return e.valid(e.publicKey)
    85  }
    86  
    87  func (e *PubKeyParams) Select(label string, choices ...PubKeyChoice) error {
    88  	var labels []string
    89  	for _, c := range choices {
    90  		labels = append(labels, c.Label)
    91  	}
    92  	sel, err := cli.Select(label, "", labels)
    93  	if err != nil {
    94  		return err
    95  	}
    96  	if sel == -1 {
    97  		return fmt.Errorf("nothing selected")
    98  	}
    99  	e.publicKey = choices[sel].Key
   100  	return nil
   101  }
   102  
   103  func (e *PubKeyParams) Edit() error {
   104  	m := fmt.Sprintf("%s public key", e.flagName)
   105  	if e.AllowWildcard {
   106  		m = fmt.Sprintf("%s or '*' to match any %s", m, e.flagName)
   107  	}
   108  	sv, err := cli.Prompt(m, e.publicKey, cli.Val(e.valid))
   109  	if err != nil {
   110  		return err
   111  	}
   112  	e.publicKey = sv
   113  	return nil
   114  }