(knownHostsFile string, newLine string, getUserVerification func() (*userinput.UserInputResponse, error))
| 475 | } |
| 476 | |
| 477 | func writeToKnownHosts(knownHostsFile string, newLine string, getUserVerification func() (*userinput.UserInputResponse, error)) error { |
| 478 | if getUserVerification == nil { |
| 479 | getUserVerification = func() (*userinput.UserInputResponse, error) { |
| 480 | return &userinput.UserInputResponse{ |
| 481 | Type: "confirm", |
| 482 | Confirm: true, |
| 483 | }, nil |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | path, _ := filepath.Split(knownHostsFile) |
| 488 | err := os.MkdirAll(path, 0700) |
| 489 | if err != nil { |
| 490 | return err |
| 491 | } |
| 492 | f, err := os.OpenFile(knownHostsFile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644) |
| 493 | if err != nil { |
| 494 | return err |
| 495 | } |
| 496 | // do not close writeable files with defer |
| 497 | |
| 498 | // this file works, so let's ask the user for permission |
| 499 | response, err := getUserVerification() |
| 500 | if err != nil { |
| 501 | f.Close() |
| 502 | return UserInputCancelError{Err: err} |
| 503 | } |
| 504 | if !response.Confirm { |
| 505 | f.Close() |
| 506 | return UserInputCancelError{Err: fmt.Errorf("canceled by the user")} |
| 507 | } |
| 508 | |
| 509 | _, err = f.WriteString(newLine + "\n") |
| 510 | if err != nil { |
| 511 | f.Close() |
| 512 | return err |
| 513 | } |
| 514 | return f.Close() |
| 515 | } |
| 516 | |
| 517 | func createUnknownKeyVerifier(ctx context.Context, knownHostsFile string, hostname string, remote string, key ssh.PublicKey) func() (*userinput.UserInputResponse, error) { |
| 518 | base64Key := base64.StdEncoding.EncodeToString(key.Marshal()) |
no test coverage detected