Skip to content

Commit b2bda22

Browse files
authored
chore: support InsecureCipher (#59)
1 parent f77a582 commit b2bda22

File tree

2 files changed

+77
-38
lines changed

2 files changed

+77
-38
lines changed

easyssh.go

Lines changed: 54 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,35 @@ type (
3232
// Note: easyssh looking for private key in user's home directory (ex. /home/john + Key).
3333
// Then ensure your Key begins from '/' (ex. /.ssh/id_rsa)
3434
MakeConfig struct {
35-
User string
36-
Server string
37-
Key string
38-
KeyPath string
39-
Port string
40-
Passphrase string
41-
Password string
42-
Timeout time.Duration
43-
Proxy DefaultConfig
44-
Ciphers []string
45-
Fingerprint string
35+
User string
36+
Server string
37+
Key string
38+
KeyPath string
39+
Port string
40+
Passphrase string
41+
Password string
42+
Timeout time.Duration
43+
Proxy DefaultConfig
44+
Ciphers []string
45+
KeyExchanges []string
46+
Fingerprint string
47+
UseInsecureCipher bool
4648
}
4749

4850
// DefaultConfig for ssh proxy config
4951
DefaultConfig struct {
50-
User string
51-
Server string
52-
Key string
53-
KeyPath string
54-
Port string
55-
Passphrase string
56-
Password string
57-
Timeout time.Duration
58-
Ciphers []string
59-
Fingerprint string
52+
User string
53+
Server string
54+
Key string
55+
KeyPath string
56+
Port string
57+
Passphrase string
58+
Password string
59+
Timeout time.Duration
60+
Ciphers []string
61+
KeyExchanges []string
62+
Fingerprint string
63+
UseInsecureCipher bool
6064
}
6165
)
6266

@@ -125,8 +129,18 @@ func getSSHConfig(config DefaultConfig) (*ssh.ClientConfig, io.Closer) {
125129
}
126130

127131
c := ssh.Config{}
132+
if config.UseInsecureCipher {
133+
c.SetDefaults()
134+
c.Ciphers = append(c.Ciphers, "aes128-cbc")
135+
c.KeyExchanges = append(c.KeyExchanges, "diffie-hellman-group-exchange-sha1", "diffie-hellman-group-exchange-sha256")
136+
}
137+
128138
if len(config.Ciphers) > 0 {
129-
c.Ciphers = config.Ciphers
139+
c.Ciphers = append(c.Ciphers, config.Ciphers...)
140+
}
141+
142+
if len(config.KeyExchanges) > 0 {
143+
c.KeyExchanges = append(c.KeyExchanges, config.KeyExchanges...)
130144
}
131145

132146
hostKeyCallback := ssh.InsecureIgnoreHostKey()
@@ -154,14 +168,15 @@ func (ssh_conf *MakeConfig) Connect() (*ssh.Session, *ssh.Client, error) {
154168
var err error
155169

156170
targetConfig, closer := getSSHConfig(DefaultConfig{
157-
User: ssh_conf.User,
158-
Key: ssh_conf.Key,
159-
KeyPath: ssh_conf.KeyPath,
160-
Passphrase: ssh_conf.Passphrase,
161-
Password: ssh_conf.Password,
162-
Timeout: ssh_conf.Timeout,
163-
Ciphers: ssh_conf.Ciphers,
164-
Fingerprint: ssh_conf.Fingerprint,
171+
User: ssh_conf.User,
172+
Key: ssh_conf.Key,
173+
KeyPath: ssh_conf.KeyPath,
174+
Passphrase: ssh_conf.Passphrase,
175+
Password: ssh_conf.Password,
176+
Timeout: ssh_conf.Timeout,
177+
Ciphers: ssh_conf.Ciphers,
178+
KeyExchanges: ssh_conf.KeyExchanges,
179+
Fingerprint: ssh_conf.Fingerprint,
165180
})
166181
if closer != nil {
167182
defer closer.Close()
@@ -170,14 +185,15 @@ func (ssh_conf *MakeConfig) Connect() (*ssh.Session, *ssh.Client, error) {
170185
// Enable proxy command
171186
if ssh_conf.Proxy.Server != "" {
172187
proxyConfig, closer := getSSHConfig(DefaultConfig{
173-
User: ssh_conf.Proxy.User,
174-
Key: ssh_conf.Proxy.Key,
175-
KeyPath: ssh_conf.Proxy.KeyPath,
176-
Passphrase: ssh_conf.Proxy.Passphrase,
177-
Password: ssh_conf.Proxy.Password,
178-
Timeout: ssh_conf.Proxy.Timeout,
179-
Ciphers: ssh_conf.Proxy.Ciphers,
180-
Fingerprint: ssh_conf.Proxy.Fingerprint,
188+
User: ssh_conf.Proxy.User,
189+
Key: ssh_conf.Proxy.Key,
190+
KeyPath: ssh_conf.Proxy.KeyPath,
191+
Passphrase: ssh_conf.Proxy.Passphrase,
192+
Password: ssh_conf.Proxy.Password,
193+
Timeout: ssh_conf.Proxy.Timeout,
194+
Ciphers: ssh_conf.Proxy.Ciphers,
195+
KeyExchanges: ssh_conf.Proxy.KeyExchanges,
196+
Fingerprint: ssh_conf.Proxy.Fingerprint,
181197
})
182198
if closer != nil {
183199
defer closer.Close()

easyssh_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,3 +393,26 @@ func TestSSHWithPassphrase(t *testing.T) {
393393
assert.True(t, isTimeout)
394394
assert.Error(t, err)
395395
}
396+
397+
func TestSCPCommandUseInsecureCipher(t *testing.T) {
398+
ssh := &MakeConfig{
399+
Server: "localhost",
400+
User: "drone-scp",
401+
Port: "22",
402+
KeyPath: "./tests/.ssh/id_rsa",
403+
UseInsecureCipher: true,
404+
}
405+
406+
err := ssh.Scp("./tests/a.txt", "a.txt")
407+
assert.NoError(t, err)
408+
409+
u, err := user.Lookup("drone-scp")
410+
if err != nil {
411+
t.Fatalf("Lookup: %v", err)
412+
}
413+
414+
// check file exist
415+
if _, err := os.Stat(path.Join(u.HomeDir, "a.txt")); os.IsNotExist(err) {
416+
t.Fatalf("SCP-error: %v", err)
417+
}
418+
}

0 commit comments

Comments
 (0)