(e Engine, key *PublicKey)
| 369 | } |
| 370 | |
| 371 | func addKey(e Engine, key *PublicKey) (err error) { |
| 372 | // Calculate fingerprint. |
| 373 | tmpPath := strings.ReplaceAll(path.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().Nanosecond()), "id_rsa.pub"), "\\", "/") |
| 374 | _ = os.MkdirAll(path.Dir(tmpPath), os.ModePerm) |
| 375 | if err = os.WriteFile(tmpPath, []byte(key.Content), 0o644); err != nil { |
| 376 | return err |
| 377 | } |
| 378 | |
| 379 | stdout, stderr, err := process.Exec("AddPublicKey", conf.SSH.KeygenPath, "-lf", tmpPath) |
| 380 | if err != nil { |
| 381 | return errors.Newf("fail to parse public key: %s - %s", err, stderr) |
| 382 | } else if len(stdout) < 2 { |
| 383 | return errors.New("not enough output for calculating fingerprint: " + stdout) |
| 384 | } |
| 385 | key.Fingerprint = strings.Split(stdout, " ")[1] |
| 386 | |
| 387 | // Save SSH key. |
| 388 | if _, err = e.Insert(key); err != nil { |
| 389 | return err |
| 390 | } |
| 391 | |
| 392 | // Don't need to rewrite this file if builtin SSH server is enabled. |
| 393 | if conf.SSH.StartBuiltinServer { |
| 394 | return nil |
| 395 | } |
| 396 | return appendAuthorizedKeysToFile(key) |
| 397 | } |
| 398 | |
| 399 | // AddPublicKey adds new public key to database and authorized_keys file. |
| 400 | func AddPublicKey(ownerID int64, name, content string) (*PublicKey, error) { |
no test coverage detected