github.com/rigado/snapd@v2.42.5-go-mod+incompatible/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  }
    56  
    57  func (x *cmdCreateKey) Execute(args []string) error {
    58  	if len(args) > 0 {
    59  		return ErrExtraArgs
    60  	}
    61  
    62  	keyName := x.Positional.KeyName
    63  	if keyName == "" {
    64  		keyName = "default"
    65  	}
    66  	if !asserts.IsValidAccountKeyName(keyName) {
    67  		return fmt.Errorf(i18n.G("key name %q is not valid; only ASCII letters, digits, and hyphens are allowed"), keyName)
    68  	}
    69  
    70  	fmt.Fprint(Stdout, i18n.G("Passphrase: "))
    71  	passphrase, err := terminal.ReadPassword(0)
    72  	fmt.Fprint(Stdout, "\n")
    73  	if err != nil {
    74  		return err
    75  	}
    76  	fmt.Fprint(Stdout, i18n.G("Confirm passphrase: "))
    77  	confirmPassphrase, err := terminal.ReadPassword(0)
    78  	fmt.Fprint(Stdout, "\n")
    79  	if err != nil {
    80  		return err
    81  	}
    82  	if string(passphrase) != string(confirmPassphrase) {
    83  		return errors.New("passphrases do not match")
    84  	}
    85  	if err != nil {
    86  		return err
    87  	}
    88  
    89  	manager := asserts.NewGPGKeypairManager()
    90  	return manager.Generate(string(passphrase), keyName)
    91  }