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