Skip to content

Commit 113f03b

Browse files
committed
Errors
1 parent 026a874 commit 113f03b

File tree

4 files changed

+108
-61
lines changed

4 files changed

+108
-61
lines changed

conn.go

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,36 @@ package sshutils
22

33
import (
44
"encoding/hex"
5-
"errors"
65
"fmt"
76
"net"
87
"strconv"
98

109
"golang.org/x/crypto/ssh"
1110
)
1211

13-
var (
14-
ErrEstablishSSH = errors.New("failed to establish SSH connection")
15-
ErrSendRequest = errors.New("failed to send request")
16-
ErrChannelOpen = errors.New("failed to open channel")
17-
)
12+
type EstablishError struct {
13+
err error
14+
}
15+
16+
func (e EstablishError) Error() string {
17+
return fmt.Sprintf("failed to establish SSH connection: %v", e.err)
18+
}
19+
20+
type SendRequestError struct {
21+
err error
22+
}
23+
24+
func (e SendRequestError) Error() string {
25+
return fmt.Sprintf("failed to send request: %v", e.err)
26+
}
27+
28+
type ChannelOpenError struct {
29+
err error
30+
}
31+
32+
func (e ChannelOpenError) Error() string {
33+
return fmt.Sprintf("failed to open channel: %v", e.err)
34+
}
1835

1936
type Listener struct {
2037
net.Listener
@@ -29,7 +46,7 @@ func (listener *Listener) Accept() (*Conn, error) {
2946
sshConn, sshNewChannels, sshRequests, err := ssh.NewServerConn(conn, &listener.config)
3047
if err != nil {
3148
conn.Close()
32-
return nil, fmt.Errorf("%w: %v", ErrEstablishSSH, err)
49+
return nil, EstablishError{err}
3350
}
3451
return handleConn(sshConn, sshNewChannels, sshRequests), nil
3552
}
@@ -52,7 +69,7 @@ type Conn struct {
5269
func (conn *Conn) RawChannel(name string, payload []byte) (*Channel, error) {
5370
sshChannel, sshRequests, err := conn.Conn.OpenChannel(name, payload)
5471
if err != nil {
55-
return nil, fmt.Errorf("%w: %v", ErrChannelOpen, err)
72+
return nil, ChannelOpenError{err}
5673
}
5774
return handleChannel(sshChannel, sshRequests, conn, name), nil
5875
}
@@ -68,7 +85,7 @@ func (conn *Conn) Channel(name string, payload Payload) (*Channel, error) {
6885
func (conn *Conn) RawRequest(name string, wantReply bool, payload []byte) (bool, []byte, error) {
6986
accepted, reply, err := conn.SendRequest(name, wantReply, payload)
7087
if err != nil {
71-
return false, nil, fmt.Errorf("%w: %v", ErrSendRequest, err)
88+
return false, nil, SendRequestError{err}
7289
}
7390
return accepted, reply, nil
7491
}
@@ -93,7 +110,7 @@ func Dial(address string, config *ssh.ClientConfig) (*Conn, error) {
93110
sshConn, sshNewChannels, sshRequests, err := ssh.NewClientConn(conn, address, config)
94111
if err != nil {
95112
conn.Close()
96-
return nil, fmt.Errorf("%w: %v", ErrEstablishSSH, err)
113+
return nil, EstablishError{err}
97114
}
98115
return handleConn(sshConn, sshNewChannels, sshRequests), nil
99116
}
@@ -138,7 +155,7 @@ type NewChannel struct {
138155
func (newChannel *NewChannel) AcceptChannel() (*Channel, error) {
139156
sshChannel, sshRequests, err := newChannel.Accept()
140157
if err != nil {
141-
return nil, fmt.Errorf("%w: %v", ErrChannelOpen, err)
158+
return nil, ChannelOpenError{err}
142159
}
143160
return handleChannel(sshChannel, sshRequests, newChannel.conn, newChannel.ChannelType()), nil
144161
}
@@ -178,7 +195,7 @@ func (channel *Channel) ConnMetadata() ssh.ConnMetadata {
178195
func (channel *Channel) RawRequest(name string, wantReply bool, payload []byte) (bool, error) {
179196
accepted, err := channel.SendRequest(name, wantReply, payload)
180197
if err != nil {
181-
return false, fmt.Errorf("%w: %v", ErrSendRequest, err)
198+
return false, SendRequestError{err}
182199
}
183200
return accepted, nil
184201
}

host_keys.go

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"crypto/rsa"
88
"crypto/x509"
99
"encoding/pem"
10-
"errors"
1110
"fmt"
1211
"io"
1312
"os"
@@ -20,11 +19,29 @@ const (
2019
hostKeyFilePerms = 0o600
2120
)
2221

23-
var (
24-
ErrInvalidKey = errors.New("invalid key")
25-
ErrInvalidKeyFile = errors.New("invalid key file")
26-
ErrUnsupportedKeyType = errors.New("unsupported key type")
27-
)
22+
type InvalidKeyError struct {
23+
err error
24+
}
25+
26+
func (e InvalidKeyError) Error() string {
27+
return fmt.Sprintf("invalid key: %v", e.err)
28+
}
29+
30+
type InvalidKeyFileError struct {
31+
err error
32+
}
33+
34+
func (e InvalidKeyFileError) Error() string {
35+
return fmt.Sprintf("invalid key file: %v", e.err)
36+
}
37+
38+
type UnsupportedKeyTypeError struct {
39+
t KeyType
40+
}
41+
42+
func (e UnsupportedKeyTypeError) Error() string {
43+
return fmt.Sprintf("unsupported key type: %v", e.t)
44+
}
2845

2946
type KeyType int
3047

@@ -59,7 +76,7 @@ func (key *HostKey) String() string {
5976
func hostKeyFromKey(key interface{}) (*HostKey, error) {
6077
signer, err := ssh.NewSignerFromKey(key)
6178
if err != nil {
62-
return nil, fmt.Errorf("%w: %v", ErrInvalidKey, err)
79+
return nil, InvalidKeyError{err}
6380
}
6481
return &HostKey{
6582
Signer: signer,
@@ -78,42 +95,42 @@ func GenerateHostKey(rand io.Reader, t KeyType) (*HostKey, error) {
7895
case Ed25519:
7996
_, key, err = ed25519.GenerateKey(rand)
8097
default:
81-
return nil, fmt.Errorf("%w: %v", ErrUnsupportedKeyType, t)
98+
return nil, UnsupportedKeyTypeError{t}
8299
}
83100
if err != nil {
84-
return nil, fmt.Errorf("%w: %v", ErrInvalidKey, err)
101+
return nil, InvalidKeyError{err}
85102
}
86103
return hostKeyFromKey(key)
87104
}
88105

89106
func LoadHostKey(fileName string) (*HostKey, error) {
90107
keyBytes, err := os.ReadFile(fileName)
91108
if err != nil {
92-
return nil, fmt.Errorf("%w: %v", ErrInvalidKeyFile, err)
109+
return nil, InvalidKeyFileError{err}
93110
}
94111
key, err := ssh.ParseRawPrivateKey(keyBytes)
95112
if err != nil {
96-
return nil, fmt.Errorf("%w: %v", ErrInvalidKeyFile, err)
113+
return nil, InvalidKeyFileError{err}
97114
}
98115
return hostKeyFromKey(key)
99116
}
100117

101118
func (key *HostKey) Save(fileName string) error {
102119
file, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE|os.O_EXCL, hostKeyFilePerms) //nolint:nosnakecase
103120
if err != nil {
104-
return fmt.Errorf("%w: %v", ErrInvalidKeyFile, err)
121+
return InvalidKeyFileError{err}
105122
}
106123
defer file.Close()
107124
keyBytes, err := x509.MarshalPKCS8PrivateKey(key.key)
108125
if err != nil {
109-
return fmt.Errorf("%w: %v", ErrInvalidKey, err)
126+
return InvalidKeyError{err}
110127
}
111128
if _, err = file.Write(pem.EncodeToMemory(&pem.Block{
112129
Type: "PRIVATE KEY",
113130
Headers: nil,
114131
Bytes: keyBytes,
115132
})); err != nil {
116-
return fmt.Errorf("%w: %v", ErrInvalidKeyFile, err)
133+
return InvalidKeyFileError{err}
117134
}
118135
return nil
119136
}

0 commit comments

Comments
 (0)