diff --git a/pkg/helpers/machine.go b/pkg/helpers/machine.go index 233a695..b2836e2 100644 --- a/pkg/helpers/machine.go +++ b/pkg/helpers/machine.go @@ -91,33 +91,32 @@ func MachineCreateRequestFromCLI(c *config.Config) (*apiv2.MachineServiceCreateR allocationType apiv2.MachineAllocationType firewallSpec *apiv2.FirewallSpec - sshPublicKeyArgument = viper.GetString("ssh-public-key") + sshPublicKeyArgument = viper.GetStringSlice("ssh-public-key") dnsServersArgument = viper.GetStringSlice("dns-servers") ntpServersArgument = viper.GetStringSlice("ntp-servers") ) - - if strings.HasPrefix(sshPublicKeyArgument, "@") { - var err error - sshPublicKeyArgument, err = readFromFile(c.Fs, sshPublicKeyArgument[1:]) - if err != nil { - return nil, err + for _, k := range sshPublicKeyArgument { + if strings.HasPrefix(k, "@") { + var err error + k, err = readFromFile(c.Fs, k[1:]) + if err != nil { + return nil, err + } } + keys = append(keys, k) } - if len(sshPublicKeyArgument) == 0 { + if len(keys) == 0 { sshKey, err := SearchSSHKey() if err != nil { return nil, err } sshPublicKey := sshKey + ".pub" - sshPublicKeyArgument, err = readFromFile(c.Fs, sshPublicKey) + key, err := readFromFile(c.Fs, sshPublicKey) if err != nil { return nil, err } - } - - if sshPublicKeyArgument != "" { - keys = append(keys, sshPublicKeyArgument) + keys = append(keys, key) } userDataArgument := viper.GetString("userdata") @@ -548,9 +547,10 @@ func AddMachineCreateFlags(cmd *cobra.Command, name string, completion *completi cmd.Flags().StringP("project", "p", "", "Project where the "+name+" should belong to. [required]") cmd.Flags().String("size", "", "Size of the "+name+". [required, except for reserved machines]") cmd.Flags().String("allocation-type", "machine", "allocation type, can be either machine|firewall") - cmd.Flags().StringP("ssh-public-key", "i", "", + cmd.Flags().StringArrayP("ssh-public-key", "i", []string{}, `SSH public key for access via ssh and console. [optional] -Can be either the public key as string, or pointing to the public key file to use e.g.: "@~/.ssh/id_rsa.pub". +Multiple keys can be passed by doing -i [key] -i [key] ... +Can be either the public keys as strings, or pointing to the public keys file to use e.g.: "@~/.ssh/id_rsa.pub". If ~/.ssh/[id_ed25519.pub | id_rsa.pub | id_dsa.pub] is present it will be picked as default, matching the first one in this order.`) cmd.Flags().StringSlice("labels", []string{}, "labels to add to the "+name+", use it like: --labels \"a=b\" or --labels \"a=\".") cmd.Flags().String("userdata", "", `cloud-init.io compatible userdata. [optional] diff --git a/tests/e2e/admin/machine_test.go b/tests/e2e/admin/machine_test.go index 28b694f..b6ab8b9 100644 --- a/tests/e2e/admin/machine_test.go +++ b/tests/e2e/admin/machine_test.go @@ -288,6 +288,7 @@ func Test_MachineCmd_Create(t *testing.T) { "--partition", testresources.Machine2().Partition.Id, "--size", testresources.Machine2().Size.Id, "--ssh-public-key", "@.ssh/id_rsa.pub", + "--ssh-public-key", "ssh-ed25519 AAAA test", "--labels", "a=b", "--userdata", "@ignition.json", "--placement-labels", "cluster-id=cluster-uuid", @@ -311,7 +312,7 @@ func Test_MachineCmd_Create(t *testing.T) { Size: &testresources.Machine2().Size.Id, Image: testresources.Machine2().Allocation.Image.Id, FilesystemLayout: new("fsl1"), - SshPublicKeys: []string{"12345"}, + SshPublicKeys: []string{"12345", "ssh-ed25519 AAAA test"}, Userdata: new(base64.StdEncoding.EncodeToString([]byte("{}"))), Labels: &apiv2.Labels{ Labels: map[string]string{ diff --git a/tests/e2e/api/machine_test.go b/tests/e2e/api/machine_test.go index 97acf60..1f872b5 100644 --- a/tests/e2e/api/machine_test.go +++ b/tests/e2e/api/machine_test.go @@ -141,6 +141,7 @@ func Test_MachineCmd_Create(t *testing.T) { "--partition", testresources.Machine2().Partition.Id, "--size", testresources.Machine2().Size.Id, "--ssh-public-key", "@.ssh/id_rsa.pub", + "--ssh-public-key", "ssh-ed25519 AAAA test", "--labels", "a=b", "--userdata", "@ignition.json", "--placement-labels", "cluster-id=cluster-uuid", @@ -163,7 +164,7 @@ func Test_MachineCmd_Create(t *testing.T) { Size: &testresources.Machine2().Size.Id, Image: testresources.Machine2().Allocation.Image.Id, FilesystemLayout: new("fsl1"), - SshPublicKeys: []string{"12345"}, + SshPublicKeys: []string{"12345", "ssh-ed25519 AAAA test"}, Userdata: new(base64.StdEncoding.EncodeToString([]byte("{}"))), Labels: &apiv2.Labels{ Labels: map[string]string{