github.com/nats-io/nsc/v2@v2.8.7-0.20240307184528-efd7023c6896/cmd/genericclaimparams.go (about)

     1  /*
     2   *
     3   *  * Copyright 2018-2019 The NATS Authors
     4   *  * Licensed under the Apache License, Version 2.0 (the "License");
     5   *  * you may not use this file except in compliance with the License.
     6   *  * You may obtain a copy of the License at
     7   *  *
     8   *  * http://www.apache.org/licenses/LICENSE-2.0
     9   *  *
    10   *  * Unless required by applicable law or agreed to in writing, software
    11   *  * distributed under the License is distributed on an "AS IS" BASIS,
    12   *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   *  * See the License for the specific language governing permissions and
    14   *  * limitations under the License.
    15   *
    16   */
    17  
    18  package cmd
    19  
    20  import (
    21  	"fmt"
    22  	"sort"
    23  	"strings"
    24  
    25  	cli "github.com/nats-io/cliprompts/v2"
    26  	"github.com/nats-io/jwt/v2"
    27  	"github.com/nats-io/nsc/v2/cmd/store"
    28  )
    29  
    30  // GenericClaimsParams - TimeParams and tags
    31  type GenericClaimsParams struct {
    32  	TimeParams
    33  	tags   []string
    34  	rmTags []string
    35  }
    36  
    37  func (sp *GenericClaimsParams) Edit(current []string) error {
    38  	var err error
    39  	if err := sp.TimeParams.Edit(); err != nil {
    40  		return err
    41  	}
    42  	sp.rmTags, err = sp.remove("tags", current)
    43  	if err != nil {
    44  		return err
    45  	}
    46  	sp.tags, err = sp.add("tags", current)
    47  	if err != nil {
    48  		return err
    49  	}
    50  	return nil
    51  }
    52  
    53  func (sp *GenericClaimsParams) add(label string, current []string) ([]string, error) {
    54  	first := true
    55  	var values []string
    56  	for {
    57  		m := fmt.Sprintf("add a %s", label)
    58  		if !first || len(current) > 0 {
    59  			m = fmt.Sprintf("add another %s", label)
    60  		}
    61  		first = false
    62  		ok, err := cli.Confirm(m, false)
    63  		if err != nil {
    64  			return nil, err
    65  		}
    66  		if !ok {
    67  			break
    68  		}
    69  		v, err := cli.Prompt(fmt.Sprintf("enter a %s", label), "")
    70  		if err != nil {
    71  			return nil, err
    72  		}
    73  		values = append(values, v)
    74  	}
    75  	return values, nil
    76  }
    77  
    78  func (sp *GenericClaimsParams) remove(label string, values []string) ([]string, error) {
    79  	var remove []string
    80  	if len(values) == 0 {
    81  		return nil, nil
    82  	}
    83  	ok, err := cli.Confirm("remove tags", false)
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  	if ok {
    88  		idx, err := cli.MultiSelect(fmt.Sprintf("select %s to remove", label), values)
    89  		if err != nil {
    90  			return nil, err
    91  		}
    92  		for _, v := range idx {
    93  			remove = append(remove, values[v])
    94  		}
    95  	}
    96  	return remove, nil
    97  }
    98  
    99  func (sp *GenericClaimsParams) Valid() error {
   100  	if err := sp.TimeParams.Validate(); err != nil {
   101  		return err
   102  	}
   103  	return nil
   104  }
   105  
   106  func (sp *GenericClaimsParams) Run(ctx ActionCtx, claim jwt.Claims, r *store.Report) error {
   107  	cd := claim.Claims()
   108  	if sp.TimeParams.IsStartChanged() {
   109  		ov := cd.NotBefore
   110  		cd.NotBefore, _ = sp.TimeParams.StartDate()
   111  		if r != nil && ov != cd.NotBefore {
   112  			if cd.NotBefore == 0 {
   113  				r.AddOK("changed jwt start to not have a start date")
   114  			} else {
   115  				r.AddOK("changed jwt valid start to %s - %s", UnixToDate(cd.NotBefore), strings.ToLower(HumanizedDate(cd.NotBefore)))
   116  			}
   117  		}
   118  	}
   119  
   120  	if sp.TimeParams.IsExpiryChanged() {
   121  		ov := cd.Expires
   122  		cd.Expires, _ = sp.TimeParams.ExpiryDate()
   123  		if r != nil && ov != cd.Expires {
   124  			if cd.Expires == 0 {
   125  				r.AddOK("changed jwt expiry to never expire")
   126  			} else {
   127  				r.AddOK("changed jwt expiry to %s - %s", UnixToDate(cd.Expires), strings.ToLower(HumanizedDate(cd.Expires)))
   128  			}
   129  		}
   130  	}
   131  
   132  	var tags *jwt.TagList
   133  
   134  	switch claim.ClaimType() {
   135  	case jwt.OperatorClaim:
   136  		tags = &claim.(*jwt.OperatorClaims).Tags
   137  	case jwt.ActivationClaim:
   138  		tags = &claim.(*jwt.ActivationClaims).Tags
   139  	case jwt.AccountClaim:
   140  		tags = &claim.(*jwt.AccountClaims).Tags
   141  	case jwt.UserClaim:
   142  		tags = &claim.(*jwt.UserClaims).Tags
   143  	default:
   144  		panic("unhandled claim type")
   145  	}
   146  
   147  	tags.Add(sp.tags...)
   148  	tags.Remove(sp.rmTags...)
   149  	sort.Strings(*tags)
   150  
   151  	if r != nil {
   152  		for _, t := range sp.tags {
   153  			r.AddOK("added tag %q", strings.ToLower(t))
   154  		}
   155  		for _, t := range sp.rmTags {
   156  			r.AddOK("removed tag %q", strings.ToLower(t))
   157  		}
   158  	}
   159  	return nil
   160  }