Skip to content

Commit 460e2b8

Browse files
thetechnickthcyron
authored andcommitted
Reference ssh key by fingerprint (#99)
1 parent ba96357 commit 460e2b8

167 files changed

Lines changed: 7761 additions & 2823 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Gopkg.lock

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
[[constraint]]
66
name = "github.com/hetznercloud/hcloud-go"
7-
version = "1.4.0"
7+
version = "1.5.0"
88

99
[[constraint]]
1010
name = "github.com/pelletier/go-toml"

server_create.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/hetznercloud/hcloud-go/hcloud"
99
"github.com/spf13/cobra"
1010
"github.com/spf13/pflag"
11+
"golang.org/x/crypto/ssh"
1112
)
1213

1314
func newServerCreateCommand(cli *CLI) *cobra.Command {
@@ -117,6 +118,14 @@ func optsFromFlags(cli *CLI, flags *pflag.FlagSet) (opts hcloud.ServerCreateOpts
117118
if err != nil {
118119
return
119120
}
121+
122+
if sshKey == nil {
123+
sshKey, err = getSSHKeyForFingerprint(cli, sshKeyIDOrName)
124+
if err != nil {
125+
return
126+
}
127+
}
128+
120129
if sshKey == nil {
121130
err = fmt.Errorf("SSH key not found: %s", sshKeyIDOrName)
122131
return
@@ -132,3 +141,33 @@ func optsFromFlags(cli *CLI, flags *pflag.FlagSet) (opts hcloud.ServerCreateOpts
132141

133142
return
134143
}
144+
145+
func getSSHKeyForFingerprint(cli *CLI, file string) (sshKey *hcloud.SSHKey, err error) {
146+
var (
147+
fileContent []byte
148+
publicKey ssh.PublicKey
149+
)
150+
151+
if fileContent, err = ioutil.ReadFile(file); err == os.ErrNotExist {
152+
err = nil
153+
return
154+
} else if err != nil {
155+
err = fmt.Errorf("lookup SSH key by fingerprint: %v", err)
156+
return
157+
}
158+
159+
if publicKey, _, _, _, err = ssh.ParseAuthorizedKey(fileContent); err != nil {
160+
err = fmt.Errorf("lookup SSH key by fingerprint: %v", err)
161+
return
162+
}
163+
sshKey, _, err = cli.Client().SSHKey.GetByFingerprint(cli.Context, ssh.FingerprintLegacyMD5(publicKey))
164+
if err != nil {
165+
err = fmt.Errorf("lookup SSH key by fingerprint: %v", err)
166+
return
167+
}
168+
if sshKey == nil {
169+
err = fmt.Errorf("SSH key not found by using fingerprint of public key: %s", file)
170+
return
171+
}
172+
return
173+
}

vendor/github.com/hetznercloud/hcloud-go/.travis.yml

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/hetznercloud/hcloud-go/CHANGES.md

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/hetznercloud/hcloud-go/hcloud/hcloud.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/hetznercloud/hcloud-go/hcloud/ssh_key.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/hetznercloud/hcloud-go/hcloud/ssh_key_test.go

Lines changed: 54 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/pelletier/go-toml/.gitignore

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/pelletier/go-toml/.travis.yml

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)