github.com/kbehouse/nsc@v0.0.6/cmd/describeuser.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  
    23  	"github.com/nats-io/jwt/v2"
    24  	"github.com/spf13/cobra"
    25  )
    26  
    27  func CreateDescribeUserCmd() *cobra.Command {
    28  	var params DescribeUserParams
    29  	cmd := &cobra.Command{
    30  		Use:          "user",
    31  		Short:        "Describes an user",
    32  		Args:         cobra.MaximumNArgs(1),
    33  		SilenceUsage: true,
    34  		RunE: func(cmd *cobra.Command, args []string) error {
    35  			return RunAction(cmd, args, &params)
    36  		},
    37  	}
    38  	cmd.Flags().StringVarP(&params.outputFile, "output-file", "o", "--", "output file, '--' is stdout")
    39  	cmd.Flags().StringVarP(&params.user, "name", "n", "", "user name")
    40  	params.AccountContextParams.BindFlags(cmd)
    41  
    42  	return cmd
    43  }
    44  
    45  func init() {
    46  	describeCmd.AddCommand(CreateDescribeUserCmd())
    47  }
    48  
    49  type DescribeUserParams struct {
    50  	AccountContextParams
    51  	jwt.UserClaims
    52  	user       string
    53  	outputFile string
    54  	raw        []byte
    55  }
    56  
    57  func (p *DescribeUserParams) SetDefaults(ctx ActionCtx) error {
    58  	p.user = NameFlagOrArgument(p.user, ctx)
    59  	return p.AccountContextParams.SetDefaults(ctx)
    60  }
    61  
    62  func (p *DescribeUserParams) PreInteractive(ctx ActionCtx) error {
    63  	var err error
    64  
    65  	if err = p.AccountContextParams.Edit(ctx); err != nil {
    66  		return err
    67  	}
    68  	if p.user == "" {
    69  		p.user, err = ctx.StoreCtx().PickUser(p.AccountContextParams.Name)
    70  		if err != nil {
    71  			return err
    72  		}
    73  	}
    74  	return nil
    75  }
    76  
    77  func (p *DescribeUserParams) Load(ctx ActionCtx) error {
    78  	var err error
    79  
    80  	if err = p.AccountContextParams.Validate(ctx); err != nil {
    81  		return err
    82  	}
    83  
    84  	if p.user == "" {
    85  		n := ctx.StoreCtx().DefaultUser(p.AccountContextParams.Name)
    86  		if n != nil {
    87  			p.user = *n
    88  		}
    89  	}
    90  
    91  	if p.user == "" {
    92  		return fmt.Errorf("user is required")
    93  	}
    94  
    95  	if Json || Raw || JsonPath != "" {
    96  		p.raw, err = ctx.StoreCtx().Store.ReadRawUserClaim(p.AccountContextParams.Name, p.user)
    97  		if err != nil {
    98  			return err
    99  		}
   100  		if Json || JsonPath != "" {
   101  			p.raw, err = bodyAsJson(p.raw)
   102  			if err != nil {
   103  				return err
   104  			}
   105  			if JsonPath != "" {
   106  				p.raw, err = GetField(p.raw, JsonPath)
   107  				if err != nil {
   108  					return err
   109  				}
   110  			}
   111  		}
   112  	} else {
   113  		uc, err := ctx.StoreCtx().Store.ReadUserClaim(p.AccountContextParams.Name, p.user)
   114  		if err != nil {
   115  			return err
   116  		}
   117  		p.UserClaims = *uc
   118  	}
   119  	return nil
   120  }
   121  
   122  func (p *DescribeUserParams) Validate(_ ActionCtx) error {
   123  	return nil
   124  }
   125  
   126  func (p *DescribeUserParams) PostInteractive(_ ActionCtx) error {
   127  	return nil
   128  }
   129  
   130  func (p *DescribeUserParams) Run(ctx ActionCtx) (store.Status, error) {
   131  	if Raw || Json || JsonPath != "" {
   132  		if !IsStdOut(p.outputFile) {
   133  			var err error
   134  			p.raw, err = jwt.DecorateJWT(string(p.raw))
   135  			if err != nil {
   136  				return nil, err
   137  			}
   138  		}
   139  		p.raw = append(p.raw, '\n')
   140  
   141  		if err := Write(p.outputFile, p.raw); err != nil {
   142  			return nil, err
   143  		}
   144  	} else {
   145  		v := NewUserDescriber(p.UserClaims).Describe()
   146  		if aClaim, err := ctx.StoreCtx().Store.ReadAccountClaim(p.AccountContextParams.Name); err == nil {
   147  			if s, ok := aClaim.SigningKeys.GetScope(p.UserClaims.Issuer); ok && s != nil {
   148  				v = fmt.Sprintf("%s\n%s", v, NewScopedSkDescriber(s.(*jwt.UserScope)).Describe())
   149  			}
   150  		}
   151  		if err := Write(p.outputFile, []byte(v)); err != nil {
   152  			return nil, err
   153  		}
   154  	}
   155  	var s store.Status
   156  	if !IsStdOut(p.outputFile) {
   157  		k := "description"
   158  		if Raw {
   159  			k = "jwt"
   160  		}
   161  		s = store.OKStatus("wrote user %s to %#q", k, AbbrevHomePaths(p.outputFile))
   162  	}
   163  	return s, nil
   164  }