/
genericcli.go
613 lines (549 loc) · 15.3 KB
/
genericcli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/*
Package genericcli implements Device interface using regular expressions.
*/
package genericcli
import (
"bytes"
"context"
"errors"
"fmt"
"regexp"
"time"
"go.uber.org/zap"
"github.com/annetutil/gnetcli/pkg/cmd"
"github.com/annetutil/gnetcli/pkg/credentials"
"github.com/annetutil/gnetcli/pkg/device"
"github.com/annetutil/gnetcli/pkg/expr"
"github.com/annetutil/gnetcli/pkg/gerror"
"github.com/annetutil/gnetcli/pkg/streamer"
"github.com/annetutil/gnetcli/pkg/terminal"
)
var ErrorCLILogin = errors.New("CLI login is not supported")
const AnyNLPattern = `(\r\n|\n)`
const DefaultCLIConnectTimeout = 15 * time.Second
var defaultWriteNewLine = []byte("\n") // const
type terminalParams struct {
w int
h int
}
type GenericCLI struct {
prompt expr.Expr
login expr.Expr
password expr.Expr
error expr.Expr
question expr.Expr
passwordError expr.Expr
pager expr.Expr
autoCommands []cmd.Cmd
initWait time.Duration
echoExprFormat func(cmd.Cmd) expr.Expr
credsInterceptor func(credentials.Credentials) credentials.Credentials
writeNewline []byte
forceManualAuth bool
sftpEnabled bool
defaultAnswers []cmd.Answer
terminalParams *terminalParams
}
type GenericCLIOption func(*GenericCLI)
// WithLoginExprs implements login using Device (like telnet or console)
func WithLoginExprs(login, password, passwordError expr.Expr) GenericCLIOption {
return func(h *GenericCLI) {
h.login = login
h.password = password
h.passwordError = passwordError
}
}
func WithAnswers(answers []cmd.Answer) GenericCLIOption {
return func(h *GenericCLI) {
h.defaultAnswers = answers
}
}
// WithManualAuth forces manual auth
func WithManualAuth() GenericCLIOption {
return func(h *GenericCLI) {
h.forceManualAuth = true
}
}
// WithPager implements pager
func WithPager(pager expr.Expr) GenericCLIOption {
return func(h *GenericCLI) {
h.pager = pager
}
}
// WithAutoCommands add commands to run at the start
func WithAutoCommands(commands []cmd.Cmd) GenericCLIOption {
return func(h *GenericCLI) {
h.autoCommands = commands
}
}
// WithInitialWait sets sleep duration before first reading after login
func WithInitialWait(duration time.Duration) GenericCLIOption {
return func(h *GenericCLI) {
h.initWait = duration
}
}
// WithSFTPEnabled use sftp to download or upload
func WithSFTPEnabled() GenericCLIOption {
return func(h *GenericCLI) {
h.sftpEnabled = true
}
}
// WithEchoExprFn set echo expr fabric
func WithEchoExprFn(fn func(cmd.Cmd) expr.Expr) GenericCLIOption {
return func(h *GenericCLI) {
h.echoExprFormat = fn
}
}
// WithQuestion implements question
func WithQuestion(question expr.Expr) GenericCLIOption {
return func(h *GenericCLI) {
h.question = question
}
}
func WithCredentialInterceptor(inter func(credentials.Credentials) credentials.Credentials) GenericCLIOption {
return func(h *GenericCLI) {
h.credsInterceptor = inter
}
}
func WithTerminalParams(width, height int) GenericCLIOption {
return func(h *GenericCLI) {
if h.terminalParams == nil {
h.terminalParams = &terminalParams{w: width, h: height}
} else {
h.terminalParams.h = height
h.terminalParams.w = width
}
}
}
func WithWriteNewLine(newline []byte) GenericCLIOption {
return func(h *GenericCLI) {
h.writeNewline = newline
}
}
func MakeGenericCLI(prompt, error expr.Expr, opts ...GenericCLIOption) GenericCLI {
res := GenericCLI{
prompt: prompt,
login: nil,
password: nil,
error: error,
question: nil,
passwordError: nil,
pager: nil,
autoCommands: nil,
initWait: 0,
echoExprFormat: nil,
credsInterceptor: nil,
writeNewline: defaultWriteNewLine,
forceManualAuth: false,
sftpEnabled: false,
defaultAnswers: nil,
terminalParams: &terminalParams{w: 400, h: 0},
}
for _, opt := range opts {
opt(&res)
}
return res
}
type GenericDevice struct {
cli GenericCLI
connector streamer.Connector
logger *zap.Logger
cliConnected bool // whether connector.Init was called or not
}
var _ device.Device = (*GenericDevice)(nil)
type GenericDeviceOption func(*GenericDevice)
func WithDevLogger(logger *zap.Logger) GenericDeviceOption {
return func(h *GenericDevice) {
h.logger = logger
}
}
func (m *GenericDevice) GetAux() map[string]any {
return nil
}
type SetTerminalSize interface {
SetTerminalSize(w int, h int)
}
func (m *GenericDevice) Connect(ctx context.Context) (err error) {
m.connector.SetCredentialsInterceptor(m.cli.credsInterceptor)
if m.cli.sftpEnabled {
if sftpSupported, ok := m.connector.(device.SFTPSupport); ok {
sftpSupported.EnableSFTP()
}
}
if m.cli.terminalParams != nil {
if v, ok := m.connector.(SetTerminalSize); ok {
v.SetTerminalSize(m.cli.terminalParams.w, m.cli.terminalParams.h)
}
}
err = m.connector.Init(ctx)
m.cliConnected = false
// We postpone CLI initialization to first Execute call because we don't have to do this for Download/Upload.
return err
}
func (m *GenericDevice) connectCLI(ctx context.Context) (err error) {
m.cliConnected = true
if m.connector.HasFeature(streamer.AutoLogin) && !m.cli.forceManualAuth {
exprs := expr.NewSimpleExprListNamed(map[string][]expr.Expr{"prompt": {m.cli.prompt}, "question": {m.cli.question}})
match, err := m.connector.ReadTo(ctx, exprs)
if err != nil {
return err
}
matchName := exprs.GetName(match.GetPatternNo())
switch matchName {
case "prompt":
case "question":
answered := false
question := match.GetMatched()
for _, cmdAnswer := range m.cli.defaultAnswers {
ans, err := cmdAnswer.Match(question)
if err != nil {
return err
}
if len(ans) > 0 {
answered = true
err := m.connector.Write(ans)
if err != nil {
return fmt.Errorf("write error %w", err)
}
break
}
}
if !answered {
return device.ThrowQuestionException(question)
}
_, err = m.connector.ReadTo(ctx, m.cli.prompt)
if err != nil {
return err
}
}
} else { // login by Device
if m.cli.login == nil {
return ErrorCLILogin
}
err := genericLogin(ctx, m.connector, m.cli)
if err != nil {
return err
}
}
// TODO: fix case with question or manual login
if m.cli.initWait > 0 {
time.Sleep(m.cli.initWait)
}
_, err = m.ExecuteBulk(m.cli.autoCommands)
if err != nil {
return err
}
return err
}
func (m *GenericDevice) Execute(command cmd.Cmd) (cmd.CmdRes, error) {
ctx, cancel := context.WithTimeout(context.Background(), DefaultCLIConnectTimeout)
defer cancel()
m.logger.Debug("exec", zap.ByteString("command", command.Value()))
if !m.cliConnected {
err := m.connectCLI(ctx)
if err != nil {
return nil, err
}
}
return GenericExecute(command, m.connector, m.cli)
}
func (m *GenericDevice) Download(paths []string) (map[string]streamer.File, error) {
m.logger.Debug("download", zap.Any("paths", paths))
res, err := m.connector.Download(paths, true)
return res, err
}
func (m *GenericDevice) Upload(paths map[string]streamer.File) error {
m.logger.Debug("upload", zap.Any("paths", paths))
err := m.connector.Upload(paths)
return err
}
func (m *GenericDevice) ExecuteBulk(commands []cmd.Cmd) ([]cmd.CmdRes, error) {
var res []cmd.CmdRes
for _, command := range commands {
out, err := m.Execute(command)
if err != nil {
return nil, err
}
res = append(res, out)
}
return res, nil
}
func (m *GenericDevice) Close() {
m.connector.Close()
}
type GetAllRegex interface {
GetLogin() expr.Expr
GetPassword() expr.Expr
GetAuthError() expr.Expr
GetPrompt() expr.Expr
}
func (m *GenericDevice) GetLogin() expr.Expr {
return m.cli.login
}
func (m *GenericDevice) GetPassword() expr.Expr {
return m.cli.password
}
func (m *GenericDevice) GetAuthError() expr.Expr {
return m.cli.passwordError
}
func (m *GenericDevice) GetPrompt() expr.Expr {
return m.cli.prompt
}
func MakeGenericDevice(cli GenericCLI, connector streamer.Connector, opts ...GenericDeviceOption) GenericDevice {
res := GenericDevice{
cli: cli,
connector: connector,
logger: zap.NewNop(),
cliConnected: false,
}
for _, opt := range opts {
opt(&res)
}
return res
}
func genericLogin(ctx context.Context, connector streamer.Connector, cli GenericCLI) (err error) {
if cli.login == nil {
return errors.New("login Expr is not set but required for login procedure")
}
passwords := connector.GetCredentials().GetPasswords()
if len(passwords) == 0 {
return errors.New("empty password")
}
i := 0
checkExprs := []expr.NamedExpr{
{Name: "login", Exprs: []expr.Expr{cli.login}},
{Name: "password", Exprs: []expr.Expr{cli.password}},
{Name: "prompt", Exprs: []expr.Expr{cli.prompt}},
{Name: "passwordError", Exprs: []expr.Expr{cli.passwordError}},
}
for i < len(passwords) {
exprsLogin := expr.NewSimpleExprListNamedOrdered(checkExprs)
readResLogin, err := connector.ReadTo(ctx, exprsLogin)
if err != nil {
return err
}
matchedExprNameLogin := exprsLogin.GetName(readResLogin.GetPatternNo())
if matchedExprNameLogin == "login" {
username, err := connector.GetCredentials().GetUsername()
if err != nil {
return err
}
err = connector.Write([]byte(username))
if err != nil {
return err
}
newline := cli.writeNewline
if len(newline) > 0 {
err := connector.Write(newline)
if err != nil {
return fmt.Errorf("write error %w", err)
}
}
} else if matchedExprNameLogin == "password" {
err = connector.Write([]byte(passwords[i].Value()))
if err != nil {
return err
}
newline := cli.writeNewline
if len(newline) > 0 {
err := connector.Write(newline)
if err != nil {
return fmt.Errorf("write error %w", err)
}
}
i++
} else if matchedExprNameLogin == "passwordError" {
continue
} else if matchedExprNameLogin == "prompt" {
return nil
}
}
exprs := expr.NewSimpleExprListNamedOrdered(checkExprs)
readResLogin, err := connector.ReadTo(ctx, exprs)
if err != nil {
return err
}
matchedExprNameLogin := exprs.GetName(readResLogin.GetPatternNo())
if matchedExprNameLogin == "prompt" {
return nil
}
return gerror.NewAuthException("cli auth user")
}
func GenericExecute(command cmd.Cmd, connector streamer.Connector, cli GenericCLI) (cmd.CmdRes, error) {
ctx := context.Background()
if cmdTimeout := command.GetCmdTimeout(); cmdTimeout > 0 {
newCtx, cancel := context.WithTimeout(ctx, cmdTimeout)
ctx = newCtx
defer cancel()
}
if readTimeout := command.GetReadTimeout(); readTimeout > 0 {
prevTimeout := connector.SetReadTimeout(readTimeout)
defer connector.SetReadTimeout(prevTimeout)
}
err := connector.Write(command.Value())
if err != nil {
return nil, fmt.Errorf("write error %w", err)
}
newline := cli.writeNewline
if len(newline) > 0 {
err := connector.Write(newline)
if err != nil {
return nil, fmt.Errorf("write error %w", err)
}
}
// read echo
var expCmdEcho expr.Expr
if cli.echoExprFormat != nil {
expCmdEcho = cli.echoExprFormat(command)
} else {
expCmdEcho = expr.NewSimpleExpr(fmt.Sprintf("%s%s", regexp.QuoteMeta(string(command.Value())), AnyNLPattern))
}
var buffer bytes.Buffer
cmdQuestions := command.GetQuestionExprs()
questions := []expr.Expr{cli.question}
if len(cmdQuestions) > 0 {
questions = append(cmdQuestions, questions...)
}
checkExprs := []expr.NamedExpr{
{Name: "echo", Exprs: []expr.Expr{expCmdEcho}},
{Name: "prompt", Exprs: []expr.Expr{cli.prompt}},
{Name: "pager", Exprs: []expr.Expr{cli.pager}},
{Name: "question", Exprs: questions},
}
exprs := expr.NewSimpleExprListNamedOrdered(checkExprs)
exprsAdd, exprsAddMap := command.GetExprCallback()
for _, exprCB := range exprsAdd {
exprs.Add("cb", expr.NewSimpleExpr(exprCB))
}
cbLimit := 100
seenEcho := false
for { // pager loop
match, err := connector.ReadTo(ctx, exprs)
if err != nil {
var perr *streamer.ReadTimeoutException
if errors.As(err, &perr) {
// in some cases device messing up with output
outputErr := checkError(cli.error, perr.LastRead)
if outputErr != nil {
return nil, outputErr
}
}
return nil, err
}
matchId := match.GetPatternNo()
matchName := exprs.GetName(matchId)
if matchName == "echo" {
seenEcho = true
exprs.Delete("echo")
continue
}
mbefore := match.GetBefore()
if !seenEcho {
// case where we caught prompt before echo because of term codes in echo
if len(mbefore) < 2 || matchName != "prompt" { // don't bother to do complex logic
return nil, device.ThrowEchoReadException(mbefore)
}
termParsedEcho, err := terminal.Parse(mbefore)
if err != nil {
return nil, fmt.Errorf("echo terminal parse error %w", err)
}
mres, ok := exprs.Match(termParsedEcho)
if !ok {
// prompt expression may consume newline from echo, but it must be presented in echo
if mbefore[len(mbefore)-1] != '\n' {
mbefore = append(mbefore, '\n')
}
termParsedEcho, err = terminal.Parse(mbefore)
if err != nil {
return nil, fmt.Errorf("echo terminal parse error %w", err)
}
mres, ok = exprs.Match(termParsedEcho)
if !ok {
return nil, device.ThrowEchoReadException(mbefore)
}
}
// assuring that it is echo
if exprs.GetName(mres.PatternNo) != "echo" {
return nil, device.ThrowEchoReadException(mbefore)
}
if mres.End > len(termParsedEcho) {
return nil, errors.New("termParsedEcho len less than mres.End")
}
seenEcho = true
exprs.Delete("echo")
mbefore = termParsedEcho[mres.End:]
}
if matchName == "prompt" {
buffer.Write(mbefore)
break
} else if matchName == "pager" { // next page
buffer.Write(mbefore)
if store, ok := match.GetMatchedGroups()["store"]; ok {
buffer.Write(store)
}
err = connector.Write([]byte(` `))
if err != nil {
return nil, fmt.Errorf("write error %w", err)
}
} else if matchName == "question" { // question
question := match.GetMatched()
answer, err := command.QuestionHandler(question)
if err != nil {
return nil, fmt.Errorf("QuestionHandler error %w", err)
}
if len(answer) > 0 {
err := connector.Write(answer)
if err != nil {
return nil, fmt.Errorf("write error %w", err)
}
}
err = connector.Write([]byte("\n"))
if err != nil {
return nil, fmt.Errorf("write error %w", err)
}
} else if matchName == "cb" { // ExprCallback
if cbLimit == 0 { // reset cbLimit in other cases
return nil, fmt.Errorf("callback limit")
}
cbLimit--
wr := exprsAddMap[exprsAdd[matchId-3]]
err := connector.Write([]byte(wr))
if err != nil {
return nil, fmt.Errorf("write error %w", err)
}
} else {
panic("unknown option")
}
}
res := buffer.Bytes()
fondErr := checkError(cli.error, res)
if fondErr != nil {
fondErr = command.ErrorHandler(fondErr)
}
strippedRes, err := terminal.Parse(res)
if err != nil {
return nil, err
}
strippedRes = normalizeNewlines(strippedRes)
status := 0
var errorRes []byte
if fondErr != nil {
errorRes = strippedRes
strippedRes = []byte{}
status = 1
}
ret := cmd.NewCmdResFull(strippedRes, errorRes, status, nil)
return ret, nil
}
func checkError(errorExpression expr.Expr, data []byte) error {
mRes, ok := errorExpression.Match(data)
if ok {
return device.ThrowExecException(string(data[mRes.Start:mRes.End]))
}
return nil
}
func normalizeNewlines(data []byte) []byte {
data = bytes.ReplaceAll(data, []byte("\r\n"), []byte("\n"))
data = bytes.ReplaceAll(data, []byte(" \n"), []byte("\n"))
return data
}