github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/cmd/snap/cmd_keys.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package main
    21  
    22  import (
    23  	"encoding/json"
    24  	"fmt"
    25  
    26  	"github.com/snapcore/snapd/asserts"
    27  	"github.com/snapcore/snapd/i18n"
    28  
    29  	"github.com/jessevdk/go-flags"
    30  )
    31  
    32  type cmdKeys struct {
    33  	JSON bool `long:"json"`
    34  }
    35  
    36  func init() {
    37  	cmd := addCommand("keys",
    38  		i18n.G("List cryptographic keys"),
    39  		i18n.G(`
    40  The keys command lists cryptographic keys that can be used for signing
    41  assertions.
    42  `),
    43  		func() flags.Commander {
    44  			return &cmdKeys{}
    45  		}, map[string]string{
    46  			// TRANSLATORS: This should not start with a lowercase letter.
    47  			"json": i18n.G("Output results in JSON format"),
    48  		}, nil)
    49  	cmd.hidden = true
    50  	cmd.completeHidden = true
    51  }
    52  
    53  // Key represents a key that can be used for signing assertions.
    54  type Key struct {
    55  	Name     string `json:"name"`
    56  	Sha3_384 string `json:"sha3-384"`
    57  }
    58  
    59  func outputJSON(keys []Key) error {
    60  	obj, err := json.Marshal(keys)
    61  	if err != nil {
    62  		return err
    63  	}
    64  	fmt.Fprintf(Stdout, "%s\n", obj)
    65  	return nil
    66  }
    67  
    68  func outputText(keys []Key) error {
    69  	if len(keys) == 0 {
    70  		fmt.Fprintf(Stderr, "No keys registered, see `snapcraft create-key`\n")
    71  		return nil
    72  	}
    73  
    74  	w := tabWriter()
    75  	defer w.Flush()
    76  
    77  	fmt.Fprintln(w, i18n.G("Name\tSHA3-384"))
    78  	for _, key := range keys {
    79  		fmt.Fprintf(w, "%s\t%s\n", key.Name, key.Sha3_384)
    80  	}
    81  	return nil
    82  }
    83  
    84  func (x *cmdKeys) Execute(args []string) error {
    85  	if len(args) > 0 {
    86  		return ErrExtraArgs
    87  	}
    88  
    89  	keys := []Key{}
    90  
    91  	manager := asserts.NewGPGKeypairManager()
    92  	collect := func(privk asserts.PrivateKey, fpr string, uid string) error {
    93  		key := Key{
    94  			Name:     uid,
    95  			Sha3_384: privk.PublicKey().ID(),
    96  		}
    97  		keys = append(keys, key)
    98  		return nil
    99  	}
   100  	err := manager.Walk(collect)
   101  	if err != nil {
   102  		return err
   103  	}
   104  	if x.JSON {
   105  		return outputJSON(keys)
   106  	}
   107  
   108  	return outputText(keys)
   109  }