github.com/hashicorp/packer@v1.14.3/cmd/ssh-keygen/main.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package main
     5  
     6  import (
     7  	"flag"
     8  	"log"
     9  	"os"
    10  	"os/user"
    11  	"path/filepath"
    12  
    13  	"github.com/hashicorp/packer-plugin-sdk/communicator/sshkey"
    14  )
    15  
    16  type options struct {
    17  	Type     string
    18  	Bits     int
    19  	Filename string
    20  }
    21  
    22  func (o *options) AddFlagSets(fs *flag.FlagSet) {
    23  	fs.StringVar(&o.Type, "type", "rsa", `dsa | ecdsa | ed25519 | rsa
    24  Specifies the type of key to create. The possible values are 'dsa', 'ecdsa',
    25  'ed25519', or 'rsa'.
    26  `)
    27  	fs.IntVar(&o.Bits, "bits", 0, `Specifies the number of bits in the key to create. By default maximum
    28  number will be picked. For RSA keys, the minimum size is 1024 bits and the
    29  default is 3072 bits. Generally, 3072 bits is considered sufficient. DSA
    30  keys must be exactly 1024 bits as specified by FIPS 186-2. For ECDSA keys,
    31  the bits flag determines the key length by selecting from one of three
    32  elliptic curve sizes: 256, 384 or 521 bits. Attempting to use bit lengths
    33  other than these three values for ECDSA keys will fail. Ed25519 keys have a
    34  fixed length and the bits flag will be ignored.
    35  `)
    36  
    37  	defaultPath := ""
    38  	user, err := user.Current()
    39  	if err == nil {
    40  		defaultPath = filepath.Join(user.HomeDir, ".ssh", "tests")
    41  	}
    42  
    43  	fs.StringVar(&o.Filename, "filename", defaultPath, `Specifies the filename of the key file.
    44  `)
    45  }
    46  
    47  func main() {
    48  	log.SetFlags(0)
    49  	log.SetPrefix("ssh-keygen: ")
    50  	fs := flag.NewFlagSet("ssh-keygen", flag.ContinueOnError)
    51  	cla := options{}
    52  	cla.AddFlagSets(fs)
    53  	if err := fs.Parse(os.Args[1:]); err != nil {
    54  		log.Fatal(err)
    55  	}
    56  
    57  	algo, err := sshkey.AlgorithmString(cla.Type)
    58  	if err != nil {
    59  		log.Fatal(err)
    60  	}
    61  
    62  	log.Printf("Generating public/private %s key pair.", algo)
    63  
    64  	keypair, err := sshkey.GeneratePair(algo, nil, cla.Bits)
    65  	if err != nil {
    66  		log.Fatal(err)
    67  	}
    68  
    69  	if isDir(cla.Filename) {
    70  		cla.Filename = filepath.Join(cla.Filename, "id_"+algo.String())
    71  	}
    72  	if fileExists(cla.Filename) {
    73  		log.Fatalf("%s already exists.", cla.Filename)
    74  	}
    75  	log.Printf("Saving private key to %s", cla.Filename)
    76  	if err := os.WriteFile(cla.Filename, keypair.Private, 0600); err != nil {
    77  		log.Fatal(err)
    78  	}
    79  	publicFilename := cla.Filename + ".pub"
    80  	log.Printf("Saving public key to %s", publicFilename)
    81  	if err := os.WriteFile(publicFilename, keypair.Public, 0644); err != nil {
    82  		log.Fatal(err)
    83  	}
    84  }
    85  
    86  func isDir(filename string) bool {
    87  	info, err := os.Stat(filename)
    88  	if err != nil {
    89  		log.Fatal(err)
    90  	}
    91  	return info.IsDir()
    92  }
    93  
    94  func fileExists(filename string) bool {
    95  	_, err := os.Stat(filename)
    96  	return err == nil
    97  }