github.com/rigado/snapd@v2.42.5-go-mod+incompatible/cmd/snap/cmd_sign_build.go (about)

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