Skip to content
This repository has been archived by the owner on Mar 29, 2018. It is now read-only.

Commit

Permalink
import ssh key from a file
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanl committed Jul 17, 2015
1 parent dff0c0b commit a756bf6
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
ArgKey = "key"
ArgKeyName = "key-name"
ArgKeyPublicKey = "public-key"
ArgKeyPublicKeyFile = "public-key-file"
ArgSSHUser = "ssh-user"

// Arguments for outputs
Expand Down
15 changes: 15 additions & 0 deletions cmd/doit/ssh_key_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func sshKeyCommands() cli.Command {
Subcommands: []cli.Command{
sshKeyList(),
sshKeyCreate(),
sshKeyImport(),
sshKeyGet(),
sshKeyUpdate(),
sshKeyDelete(),
Expand Down Expand Up @@ -45,6 +46,20 @@ func sshKeyCreate() cli.Command {
}
}

func sshKeyImport() cli.Command {
return cli.Command{
Name: "import",
Usage: "import ssh key",
Flags: []cli.Flag{
cli.StringFlag{
Name: doit.ArgKeyPublicKeyFile,
Usage: "ssh key file",
},
},
Action: doit.KeyImport,
}
}

func sshKeyGet() cli.Command {
return cli.Command{
Name: "get",
Expand Down
34 changes: 34 additions & 0 deletions sshkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package doit

import (
"fmt"
"io/ioutil"
"strconv"

"golang.org/x/crypto/ssh"

"github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"github.com/digitalocean/godo"
Expand Down Expand Up @@ -86,6 +89,37 @@ func KeyCreate(c *cli.Context) {
logrus.WithField("err", err).Fatal("could not create key")
}

err = displayOutput(c, r)
if err != nil {
logrus.WithField("err", err).Fatal("could not write output")
}
}

// KeyImport imports a key from a file
func KeyImport(c *cli.Context) {
client := NewClient(c, DefaultConfig)

keyPath := c.String(ArgKeyPublicKeyFile)
keyFile, err := ioutil.ReadFile(keyPath)
if err != nil {
Bail(err, "could not read the public key")
}

_, comment, _, _, err := ssh.ParseAuthorizedKey(keyFile)
if err != nil {
Bail(err, "could ot parse public key")
}

kcr := &godo.KeyCreateRequest{
Name: comment,
PublicKey: string(keyFile),
}

r, _, err := client.Keys.Create(kcr)
if err != nil {
logrus.WithField("err", err).Fatal("could not create key")
}

err = displayOutput(c, r)
if err != nil {
logrus.WithField("err", err).Fatal("could not write output")
Expand Down
34 changes: 34 additions & 0 deletions sshkeys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package doit

import (
"flag"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/codegangsta/cli"
Expand Down Expand Up @@ -213,3 +216,34 @@ func TestKeysUpdateByFingerprint(t *testing.T) {
KeyUpdate(c)
})
}

func TestSSHPublicKeyImport(t *testing.T) {
pubkey := "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCn6eZ8ve0ha04rPRZuoPXK1AQ/h21qslWCzoDcOciXn5OcyafkZw+31k/afaBTeW62D8fXd8e/1xWbFfp/2GqmslYpNCTPrtpNhsE8I0yKjJ8FxX9FfsCOu/Sv83dWgSpiT7pNWVKarZjW9KdKKRQljq1i+H5pX3r5Q9I1v+66mYTe7qsKGas9KWy0vkGoNSqmTCl+d+Y0286chtqBqBjSCUCI8oLKPnJB86Lj344tFGmzDIsJKXMVHTL0dF8n3u6iWN4qiRU+JvkoIkI3v0JvyZXxhR2uPIS1yUAY2GC+2O5mfxydJQzBdtag5Uw8Y7H5yYR1gar/h16bAy5XzRvp testkey"
path := filepath.Join(os.TempDir(), "key.pub")
err := ioutil.WriteFile(path, []byte(pubkey), 0600)
assert.NoError(t, err)
defer os.Remove(path)

client := &godo.Client{
Keys: &KeysServiceMock{
CreateFn: func(req *godo.KeyCreateRequest) (*godo.Key, *godo.Response, error) {
expected := &godo.KeyCreateRequest{
Name: "testkey",
PublicKey: pubkey,
}
assert.Equal(t, req, expected)
return &testKey, nil, nil
},
},
}

cs := NewTestConfig(client)
fs := flag.NewFlagSet("flag set", 0)
fs.String(ArgKeyName, "the key", ArgKeyName)
fs.String(ArgKeyPublicKey, "fingerprint", ArgKeyPublicKey)
fs.String(ArgKeyPublicKeyFile, path, ArgKeyPublicKeyFile)

withinTest(cs, fs, func(c *cli.Context) {
KeyImport(c)
})
}

0 comments on commit a756bf6

Please sign in to comment.