Skip to content

Commit 714f23d

Browse files
committed
move functions into ssh_parse.go
Signed-off-by: haoqixu <[email protected]>
1 parent e27ce34 commit 714f23d

File tree

3 files changed

+85
-64
lines changed

3 files changed

+85
-64
lines changed

age/keysource.go

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/sirupsen/logrus"
1717

1818
"github.com/getsops/sops/v3/logging"
19-
"golang.org/x/crypto/ssh"
2019
)
2120

2221
const (
@@ -238,69 +237,6 @@ func (key *MasterKey) TypeToIdentifier() string {
238237
return KeyTypeIdentifier
239238
}
240239

241-
// readPublicKeyFile attempts to read a public key based on the given private
242-
// key path. It assumes the public key is in the same directory, with the same
243-
// name, but with a ".pub" extension. If the public key cannot be read, an
244-
// error is returned.
245-
func readPublicKeyFile(privateKeyPath string) (ssh.PublicKey, error) {
246-
publicKeyPath := privateKeyPath + ".pub"
247-
f, err := os.Open(publicKeyPath)
248-
if err != nil {
249-
return nil, fmt.Errorf("failed to obtain public %q key for %q SSH key: %w", publicKeyPath, privateKeyPath, err)
250-
}
251-
defer f.Close()
252-
contents, err := io.ReadAll(f)
253-
if err != nil {
254-
return nil, fmt.Errorf("failed to read %q: %w", publicKeyPath, err)
255-
}
256-
pubKey, _, _, _, err := ssh.ParseAuthorizedKey(contents)
257-
if err != nil {
258-
return nil, fmt.Errorf("failed to parse %q: %w", publicKeyPath, err)
259-
}
260-
return pubKey, nil
261-
}
262-
263-
// parseSSHIdentityFromPrivateKeyFile returns an age.Identity from the given
264-
// private key file. If the private key file is encrypted, it will configure
265-
// the identity to prompt for a passphrase.
266-
func parseSSHIdentityFromPrivateKeyFile(keyPath string) (age.Identity, error) {
267-
keyFile, err := os.Open(keyPath)
268-
if err != nil {
269-
return nil, fmt.Errorf("failed to open file: %w", err)
270-
}
271-
defer keyFile.Close()
272-
contents, err := io.ReadAll(keyFile)
273-
if err != nil {
274-
return nil, fmt.Errorf("failed to read file: %w", err)
275-
}
276-
id, err := agessh.ParseIdentity(contents)
277-
if sshErr, ok := err.(*ssh.PassphraseMissingError); ok {
278-
pubKey := sshErr.PublicKey
279-
if pubKey == nil {
280-
pubKey, err = readPublicKeyFile(keyPath)
281-
if err != nil {
282-
return nil, err
283-
}
284-
}
285-
passphrasePrompt := func() ([]byte, error) {
286-
pass, err := readPassphrase(fmt.Sprintf("Enter passphrase for %q:", keyPath))
287-
if err != nil {
288-
return nil, fmt.Errorf("could not read passphrase for %q: %v", keyPath, err)
289-
}
290-
return pass, nil
291-
}
292-
i, err := agessh.NewEncryptedSSHIdentity(pubKey, contents, passphrasePrompt)
293-
if err != nil {
294-
return nil, fmt.Errorf("could not create encrypted SSH identity: %w", err)
295-
}
296-
return i, nil
297-
}
298-
if err != nil {
299-
return nil, fmt.Errorf("malformed SSH identity in %q: %w", keyPath, err)
300-
}
301-
return id, nil
302-
}
303-
304240
// loadAgeSSHIdentity attempts to load the age SSH identity based on an SSH
305241
// private key from the SopsAgeSshPrivateKeyFileEnv environment variable. If the
306242
// environment variable is not present, it will fall back to `~/.ssh/id_ed25519`

age/ssh_parse.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// These functions are similar to those in the age project
2+
// https://github.com/FiloSottile/age/blob/v1.0.0/cmd/age/parse.go
3+
//
4+
// Copyright 2021 The age Authors. All rights reserved.
5+
// Use of this source code is governed by a BSD-style
6+
// license that can be found in age's LICENSE file at
7+
// https://github.com/FiloSottile/age/blob/v1.0.0/LICENSE
8+
//
9+
// SPDX-License-Identifier: BSD-3-Clause
10+
11+
package age
12+
13+
import (
14+
"fmt"
15+
"io"
16+
"os"
17+
18+
"filippo.io/age"
19+
"filippo.io/age/agessh"
20+
"golang.org/x/crypto/ssh"
21+
)
22+
23+
// readPublicKeyFile attempts to read a public key based on the given private
24+
// key path. It assumes the public key is in the same directory, with the same
25+
// name, but with a ".pub" extension. If the public key cannot be read, an
26+
// error is returned.
27+
func readPublicKeyFile(privateKeyPath string) (ssh.PublicKey, error) {
28+
publicKeyPath := privateKeyPath + ".pub"
29+
f, err := os.Open(publicKeyPath)
30+
if err != nil {
31+
return nil, fmt.Errorf("failed to obtain public %q key for %q SSH key: %w", publicKeyPath, privateKeyPath, err)
32+
}
33+
defer f.Close()
34+
contents, err := io.ReadAll(f)
35+
if err != nil {
36+
return nil, fmt.Errorf("failed to read %q: %w", publicKeyPath, err)
37+
}
38+
pubKey, _, _, _, err := ssh.ParseAuthorizedKey(contents)
39+
if err != nil {
40+
return nil, fmt.Errorf("failed to parse %q: %w", publicKeyPath, err)
41+
}
42+
return pubKey, nil
43+
}
44+
45+
// parseSSHIdentityFromPrivateKeyFile returns an age.Identity from the given
46+
// private key file. If the private key file is encrypted, it will configure
47+
// the identity to prompt for a passphrase.
48+
func parseSSHIdentityFromPrivateKeyFile(keyPath string) (age.Identity, error) {
49+
keyFile, err := os.Open(keyPath)
50+
if err != nil {
51+
return nil, fmt.Errorf("failed to open file: %w", err)
52+
}
53+
defer keyFile.Close()
54+
contents, err := io.ReadAll(keyFile)
55+
if err != nil {
56+
return nil, fmt.Errorf("failed to read file: %w", err)
57+
}
58+
id, err := agessh.ParseIdentity(contents)
59+
if sshErr, ok := err.(*ssh.PassphraseMissingError); ok {
60+
pubKey := sshErr.PublicKey
61+
if pubKey == nil {
62+
pubKey, err = readPublicKeyFile(keyPath)
63+
if err != nil {
64+
return nil, err
65+
}
66+
}
67+
passphrasePrompt := func() ([]byte, error) {
68+
pass, err := readPassphrase(fmt.Sprintf("Enter passphrase for %q:", keyPath))
69+
if err != nil {
70+
return nil, fmt.Errorf("could not read passphrase for %q: %v", keyPath, err)
71+
}
72+
return pass, nil
73+
}
74+
i, err := agessh.NewEncryptedSSHIdentity(pubKey, contents, passphrasePrompt)
75+
if err != nil {
76+
return nil, fmt.Errorf("could not create encrypted SSH identity: %w", err)
77+
}
78+
return i, nil
79+
}
80+
if err != nil {
81+
return nil, fmt.Errorf("malformed SSH identity in %q: %w", keyPath, err)
82+
}
83+
return id, nil
84+
}

age/tui.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// These functions have been copied from the age project
22
// https://github.com/FiloSottile/age/blob/v1.0.0/cmd/age/encrypted_keys.go
3+
//
34
// Copyright 2021 The age Authors. All rights reserved.
45
// Use of this source code is governed by a BSD-style
56
// license that can be found in age's LICENSE file at

0 commit comments

Comments
 (0)