...

Source file src/github.com/in-toto/in-toto-golang/cmd/key.go

Documentation: github.com/in-toto/in-toto-golang/cmd

     1  package cmd
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"strings"
     7  
     8  	intoto "github.com/in-toto/in-toto-golang/in_toto"
     9  	"github.com/spf13/cobra"
    10  )
    11  
    12  var keyCmd = &cobra.Command{
    13  	Use:   "key",
    14  	Short: "Key management commands",
    15  }
    16  
    17  var keyIDCmd = &cobra.Command{
    18  	Use:   "id <file>",
    19  	Short: "Output the key id for a given key",
    20  	Long:  "Output the key id for a given key",
    21  	Args:  cobra.ExactArgs(1),
    22  	RunE:  keyID,
    23  }
    24  
    25  var keyLayoutCmd = &cobra.Command{
    26  	Use:   "layout <file>",
    27  	Short: "Output the key layout for a given key in <KEYID>: <KEYOBJ> format",
    28  	Long:  "Output is a json formatted pubkey suitable for embedding in a layout file",
    29  	Args:  cobra.ExactArgs(1),
    30  	RunE:  keyLayout,
    31  }
    32  
    33  func init() {
    34  	rootCmd.AddCommand(keyCmd)
    35  
    36  	keyCmd.AddCommand(keyIDCmd)
    37  	keyCmd.AddCommand(keyLayoutCmd)
    38  }
    39  
    40  func keyID(cmd *cobra.Command, args []string) error {
    41  	var key intoto.Key
    42  
    43  	err := key.LoadKeyDefaults(args[0])
    44  	if err != nil {
    45  		return err
    46  	}
    47  
    48  	fmt.Printf("%s\n", key.KeyID)
    49  
    50  	return nil
    51  }
    52  
    53  func keyLayout(cmd *cobra.Command, args []string) error {
    54  	var key intoto.Key
    55  
    56  	err := key.LoadKeyDefaults(args[0])
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	// removed the private key from the struct such that it is not printed for use in the layout
    62  	key.KeyVal.Private = ""
    63  
    64  	b, err := json.Marshal(key)
    65  	if err != nil {
    66  		return err
    67  	}
    68  
    69  	s2 := strings.ReplaceAll(string(b), `"private":"",`, "")
    70  	fmt.Printf(`"%v": %s`, key.KeyID, s2)
    71  
    72  	return nil
    73  }
    74  

View as plain text