forked from canonical/snapd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_sign_build.go
More file actions
114 lines (95 loc) · 3.18 KB
/
Copy pathcmd_sign_build.go
File metadata and controls
114 lines (95 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2014-2015 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package main
import (
"fmt"
"time"
_ "golang.org/x/crypto/sha3" // expected for digests
"github.com/jessevdk/go-flags"
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/i18n"
)
type cmdSignBuild struct {
Positional struct {
Filename string
} `positional-args:"yes" required:"yes"`
DeveloperID string `long:"developer-id" required:"yes"`
SnapID string `long:"snap-id" required:"yes"`
KeyName string `short:"k" default:"default" `
Grade string `long:"grade" choice:"devel" choice:"stable" default:"stable"`
}
var shortSignBuildHelp = i18n.G("Create snap build assertion")
var longSignBuildHelp = i18n.G("Create snap-build assertion for the provided snap file.")
func init() {
cmd := addCommand("sign-build",
shortSignBuildHelp,
longSignBuildHelp,
func() flags.Commander {
return &cmdSignBuild{}
}, map[string]string{
"developer-id": i18n.G("Identifier of the signer"),
"snap-id": i18n.G("Identifier of the snap package associated with the build"),
"k": i18n.G("Name of the GnuPG key to use (defaults to 'default' as key name)"),
"grade": i18n.G("Grade states the build quality of the snap (defaults to 'stable')"),
}, []argDesc{{
name: i18n.G("<filename>"),
desc: i18n.G("Filename of the snap you want to assert a build for"),
}})
cmd.hidden = true
}
func (x *cmdSignBuild) Execute(args []string) error {
if len(args) > 0 {
return ErrExtraArgs
}
snapDigest, snapSize, err := asserts.SnapFileSHA3_384(x.Positional.Filename)
if err != nil {
return err
}
gkm := asserts.NewGPGKeypairManager()
privKey, err := gkm.GetByName(x.KeyName)
if err != nil {
// TRANSLATORS: %q is the key name, %v the error message
return fmt.Errorf(i18n.G("cannot use %q key: %v"), x.KeyName, err)
}
pubKey := privKey.PublicKey()
timestamp := time.Now().Format(time.RFC3339)
headers := map[string]interface{}{
"developer-id": x.DeveloperID,
"authority-id": x.DeveloperID,
"snap-sha3-384": snapDigest,
"snap-id": x.SnapID,
"snap-size": fmt.Sprintf("%d", snapSize),
"grade": x.Grade,
"timestamp": timestamp,
}
adb, err := asserts.OpenDatabase(&asserts.DatabaseConfig{
KeypairManager: gkm,
})
if err != nil {
return fmt.Errorf(i18n.G("cannot open the assertions database: %v"), err)
}
a, err := adb.Sign(asserts.SnapBuildType, headers, nil, pubKey.ID())
if err != nil {
return fmt.Errorf(i18n.G("cannot sign assertion: %v"), err)
}
_, err = Stdout.Write(asserts.Encode(a))
if err != nil {
return err
}
return nil
}