github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/cmd/snap/cmd_sign_build.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2014-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 // expected for digests 29 _ "golang.org/x/crypto/sha3" 30 31 "github.com/snapcore/snapd/asserts" 32 "github.com/snapcore/snapd/i18n" 33 ) 34 35 type cmdSignBuild struct { 36 Positional struct { 37 Filename string 38 } `positional-args:"yes" required:"yes"` 39 40 // XXX complete DeveloperID and SnapID 41 DeveloperID string `long:"developer-id" required:"yes"` 42 SnapID string `long:"snap-id" required:"yes"` 43 KeyName keyName `short:"k" default:"default" ` 44 Grade string `long:"grade" choice:"devel" choice:"stable" default:"stable"` 45 } 46 47 var shortSignBuildHelp = i18n.G("Create a snap-build assertion") 48 var longSignBuildHelp = i18n.G(` 49 The sign-build command creates a snap-build assertion for the provided 50 snap file. 51 `) 52 53 func init() { 54 cmd := addCommand("sign-build", 55 shortSignBuildHelp, 56 longSignBuildHelp, 57 func() flags.Commander { 58 return &cmdSignBuild{} 59 }, map[string]string{ 60 // TRANSLATORS: This should not start with a lowercase letter. 61 "developer-id": i18n.G("Identifier of the signer"), 62 // TRANSLATORS: This should not start with a lowercase letter. 63 "snap-id": i18n.G("Identifier of the snap package associated with the build"), 64 // TRANSLATORS: This should not start with a lowercase letter. 65 "k": i18n.G("Name of the GnuPG key to use (defaults to 'default' as key name)"), 66 // TRANSLATORS: This should not start with a lowercase letter. 67 "grade": i18n.G("Grade states the build quality of the snap (defaults to 'stable')"), 68 }, []argDesc{{ 69 // TRANSLATORS: This needs to begin with < and end with > 70 name: i18n.G("<filename>"), 71 // TRANSLATORS: This should not start with a lowercase letter. 72 desc: i18n.G("Filename of the snap you want to assert a build for"), 73 }}) 74 cmd.hidden = true 75 } 76 77 func (x *cmdSignBuild) Execute(args []string) error { 78 if len(args) > 0 { 79 return ErrExtraArgs 80 } 81 82 snapDigest, snapSize, err := asserts.SnapFileSHA3_384(x.Positional.Filename) 83 if err != nil { 84 return err 85 } 86 87 keypairMgr, err := getKeypairManager() 88 if err != nil { 89 return err 90 } 91 privKey, err := keypairMgr.GetByName(string(x.KeyName)) 92 if err != nil { 93 // TRANSLATORS: %q is the key name, %v the error message 94 return fmt.Errorf(i18n.G("cannot use %q key: %v"), x.KeyName, err) 95 } 96 97 pubKey := privKey.PublicKey() 98 timestamp := time.Now().Format(time.RFC3339) 99 100 headers := map[string]interface{}{ 101 "developer-id": x.DeveloperID, 102 "authority-id": x.DeveloperID, 103 "snap-sha3-384": snapDigest, 104 "snap-id": x.SnapID, 105 "snap-size": fmt.Sprintf("%d", snapSize), 106 "grade": x.Grade, 107 "timestamp": timestamp, 108 } 109 110 adb, err := asserts.OpenDatabase(&asserts.DatabaseConfig{ 111 KeypairManager: keypairMgr, 112 }) 113 if err != nil { 114 return fmt.Errorf(i18n.G("cannot open the assertions database: %v"), err) 115 } 116 117 a, err := adb.Sign(asserts.SnapBuildType, headers, nil, pubKey.ID()) 118 if err != nil { 119 return fmt.Errorf(i18n.G("cannot sign assertion: %v"), err) 120 } 121 122 _, err = Stdout.Write(asserts.Encode(a)) 123 if err != nil { 124 return err 125 } 126 127 return nil 128 }