github.com/kbehouse/nsc@v0.0.6/cmd/describe.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  	"bytes"
    20  	"encoding/base64"
    21  	"encoding/json"
    22  	"errors"
    23  	"fmt"
    24  
    25  	"github.com/spf13/cobra"
    26  )
    27  
    28  var Raw bool
    29  var WideFlag bool
    30  var Wide = noopNameFilter
    31  var Json bool
    32  var JsonPath string
    33  
    34  type WideFun = func(a string) string
    35  
    36  func noopNameFilter(a string) string {
    37  	return a
    38  }
    39  
    40  func friendlyNameFilter() (WideFun, error) {
    41  	m, err := friendlyNames(GetConfig().Operator)
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  	return func(a string) string {
    46  		v := m[a]
    47  		if v == "" {
    48  			v = a
    49  		}
    50  		return v
    51  	}, nil
    52  }
    53  
    54  var describeCmd = &cobra.Command{
    55  	Use:   "describe",
    56  	Short: "Describe assets such as operators, accounts, users, and jwt files",
    57  	PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
    58  		var err error
    59  		if WideFlag {
    60  			Wide = noopNameFilter
    61  		} else {
    62  			Wide, err = friendlyNameFilter()
    63  			if err != nil {
    64  				return err
    65  			}
    66  		}
    67  		return nil
    68  	},
    69  }
    70  
    71  func init() {
    72  	GetRootCmd().AddCommand(describeCmd)
    73  	describeCmd.PersistentFlags().BoolVarP(&Json, "json", "J", false, "display JWT body as JSON")
    74  	describeCmd.PersistentFlags().BoolVarP(&WideFlag, "long-ids", "W", false, "display account ids on imports")
    75  	describeCmd.PersistentFlags().BoolVarP(&Raw, "raw", "R", false, "output the raw JWT (exclusive of long-ids)")
    76  	describeCmd.PersistentFlags().StringVarP(&JsonPath, "field", "F", "", "extract value from specified field using json structure")
    77  }
    78  
    79  func bodyAsJson(data []byte) ([]byte, error) {
    80  	chunks := bytes.Split(data, []byte{'.'})
    81  	if len(chunks) != 3 {
    82  		return nil, errors.New("data is not a jwt")
    83  	}
    84  	body := chunks[1]
    85  	d, err := base64.RawURLEncoding.DecodeString(string(body))
    86  	if err != nil {
    87  		return nil, fmt.Errorf("error decoding base64: %v", err)
    88  	}
    89  	m := make(map[string]interface{})
    90  	if err := json.Unmarshal(d, &m); err != nil {
    91  		return nil, fmt.Errorf("error parsing json: %v", err)
    92  	}
    93  	f, err := json.MarshalIndent(m, "", " ")
    94  	if err != nil {
    95  		return nil, fmt.Errorf("error formatting json: %v", err)
    96  	}
    97  	return f, nil
    98  }