github.com/chipaca/snappy@v0.0.0-20210104084008-1f06296fe8ad/cmd/snap/cmd_create_key.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016 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  	"errors"
    24  	"fmt"
    25  
    26  	"github.com/jessevdk/go-flags"
    27  	"golang.org/x/crypto/ssh/terminal"
    28  
    29  	"github.com/snapcore/snapd/asserts"
    30  	"github.com/snapcore/snapd/i18n"
    31  )
    32  
    33  type cmdCreateKey struct {
    34  	Positional struct {
    35  		KeyName string
    36  	} `positional-args:"true"`
    37  }
    38  
    39  func init() {
    40  	cmd := addCommand("create-key",
    41  		i18n.G("Create cryptographic key pair"),
    42  		i18n.G(`
    43  The create-key command creates a cryptographic key pair that can be
    44  used for signing assertions.
    45  `),
    46  		func() flags.Commander {
    47  			return &cmdCreateKey{}
    48  		}, nil, []argDesc{{
    49  			// TRANSLATORS: This needs to begin with < and end with >
    50  			name: i18n.G("<key-name>"),
    51  			// TRANSLATORS: This should not start with a lowercase letter.
    52  			desc: i18n.G("Name of key to create; defaults to 'default'"),
    53  		}})
    54  	cmd.hidden = true
    55  	cmd.completeHidden = true
    56  }
    57  
    58  func (x *cmdCreateKey) Execute(args []string) error {
    59  	if len(args) > 0 {
    60  		return ErrExtraArgs
    61  	}
    62  
    63  	keyName := x.Positional.KeyName
    64  	if keyName == "" {
    65  		keyName = "default"
    66  	}
    67  	if !asserts.IsValidAccountKeyName(keyName) {
    68  		return fmt.Errorf(i18n.G("key name %q is not valid; only ASCII letters, digits, and hyphens are allowed"), keyName)
    69  	}
    70  
    71  	fmt.Fprint(Stdout, i18n.G("Passphrase: "))
    72  	passphrase, err := terminal.ReadPassword(0)
    73  	fmt.Fprint(Stdout, "\n")
    74  	if err != nil {
    75  		return err
    76  	}
    77  	fmt.Fprint(Stdout, i18n.G("Confirm passphrase: "))
    78  	confirmPassphrase, err := terminal.ReadPassword(0)
    79  	fmt.Fprint(Stdout, "\n")
    80  	if err != nil {
    81  		return err
    82  	}
    83  	if string(passphrase) != string(confirmPassphrase) {
    84  		return errors.New("passphrases do not match")
    85  	}
    86  	if err != nil {
    87  		return err
    88  	}
    89  
    90  	manager := asserts.NewGPGKeypairManager()
    91  	return manager.Generate(string(passphrase), keyName)
    92  }