github.com/ubuntu-core/snappy@v0.0.0-20210827154228-9e584df982bb/cmd/snap/cmd_export_key.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2021 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 "fmt" 24 "time" 25 26 "github.com/jessevdk/go-flags" 27 28 "github.com/snapcore/snapd/asserts" 29 "github.com/snapcore/snapd/i18n" 30 ) 31 32 type cmdExportKey struct { 33 Account string `long:"account"` 34 Positional struct { 35 KeyName keyName 36 } `positional-args:"true"` 37 } 38 39 func init() { 40 cmd := addCommand("export-key", 41 i18n.G("Export cryptographic public key"), 42 i18n.G(` 43 The export-key command exports a public key assertion body that may be 44 imported by other systems. 45 `), 46 func() flags.Commander { 47 return &cmdExportKey{} 48 }, map[string]string{ 49 "account": i18n.G("Format public key material as a request for an account-key for this account-id"), 50 }, []argDesc{{ 51 // TRANSLATORS: This needs to begin with < and end with > 52 name: i18n.G("<key-name>"), 53 // TRANSLATORS: This should not start with a lowercase letter. 54 desc: i18n.G("Name of key to export"), 55 }}) 56 cmd.hidden = true 57 } 58 59 func (x *cmdExportKey) Execute(args []string) error { 60 if len(args) > 0 { 61 return ErrExtraArgs 62 } 63 64 keyName := string(x.Positional.KeyName) 65 if keyName == "" { 66 keyName = "default" 67 } 68 69 keypairMgr, err := getKeypairManager() 70 if err != nil { 71 return err 72 } 73 if x.Account != "" { 74 privKey, err := keypairMgr.GetByName(keyName) 75 if err != nil { 76 return err 77 } 78 pubKey := privKey.PublicKey() 79 headers := map[string]interface{}{ 80 "account-id": x.Account, 81 "name": keyName, 82 "public-key-sha3-384": pubKey.ID(), 83 "since": time.Now().UTC().Format(time.RFC3339), 84 // XXX: To support revocation, we need to check for matching known assertions and set a suitable revision if we find one. 85 } 86 body, err := asserts.EncodePublicKey(pubKey) 87 if err != nil { 88 return err 89 } 90 assertion, err := asserts.SignWithoutAuthority(asserts.AccountKeyRequestType, headers, body, privKey) 91 if err != nil { 92 return err 93 } 94 fmt.Fprint(Stdout, string(asserts.Encode(assertion))) 95 } else { 96 encoded, err := keypairMgr.Export(keyName) 97 if err != nil { 98 return err 99 } 100 fmt.Fprintf(Stdout, "%s\n", encoded) 101 } 102 return nil 103 }