Skip to content

Commit ea67e56

Browse files
authored
Use ErrKeyUnableToVerify if fail to calc fingerprint in ssh-keygen (#10863)
* Use ErrKeyUnableToVerify if fail to calc fingerprint in ssh-keygen Fix #3985 Signed-off-by: Andrew Thornton <[email protected]> * Pass up the unable to verify
1 parent f9f2c16 commit ea67e56

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

models/ssh_key.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,9 @@ func calcFingerprintSSHKeygen(publicKeyContent string) (string, error) {
425425
defer os.Remove(tmpPath)
426426
stdout, stderr, err := process.GetManager().Exec("AddPublicKey", "ssh-keygen", "-lf", tmpPath)
427427
if err != nil {
428+
if strings.Contains(stderr, "is not a public key file") {
429+
return "", ErrKeyUnableVerify{stderr}
430+
}
428431
return "", fmt.Errorf("'ssh-keygen -lf %s' failed with error '%s': %s", tmpPath, err, stderr)
429432
} else if len(stdout) < 2 {
430433
return "", errors.New("not enough output for calculating fingerprint: " + stdout)
@@ -455,6 +458,10 @@ func calcFingerprint(publicKeyContent string) (string, error) {
455458
fp, err = calcFingerprintSSHKeygen(publicKeyContent)
456459
}
457460
if err != nil {
461+
if IsErrKeyUnableVerify(err) {
462+
log.Info("%s", publicKeyContent)
463+
return "", err
464+
}
458465
return "", fmt.Errorf("%s: %v", fnName, err)
459466
}
460467
return fp, nil

routers/user/setting/keys.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ func KeysPost(ctx *context.Context, form auth.AddKeyForm) {
9292

9393
ctx.Data["Err_Title"] = true
9494
ctx.RenderWithErr(ctx.Tr("settings.ssh_key_name_used"), tplSettingsKeys, &form)
95+
case models.IsErrKeyUnableVerify(err):
96+
ctx.Flash.Info(ctx.Tr("form.unable_verify_ssh_key"))
97+
ctx.Redirect(setting.AppSubURL + "/user/settings/keys")
9598
default:
9699
ctx.ServerError("AddPublicKey", err)
97100
}

0 commit comments

Comments
 (0)