CheckPublicKeyString checks if the given public key string is recognized by SSH. It returns the actual public key line on success.
(content string)
| 271 | // CheckPublicKeyString checks if the given public key string is recognized by SSH. |
| 272 | // It returns the actual public key line on success. |
| 273 | func CheckPublicKeyString(content string) (_ string, err error) { |
| 274 | if conf.SSH.Disabled { |
| 275 | return "", errors.New("SSH is disabled") |
| 276 | } |
| 277 | |
| 278 | content, err = parseKeyString(content) |
| 279 | if err != nil { |
| 280 | return "", err |
| 281 | } |
| 282 | |
| 283 | content = strings.TrimRight(content, "\n\r") |
| 284 | if strings.ContainsAny(content, "\n\r") { |
| 285 | return "", errors.New("only a single line with a single key please") |
| 286 | } |
| 287 | |
| 288 | // Remove any unnecessary whitespace |
| 289 | content = strings.TrimSpace(content) |
| 290 | |
| 291 | if !conf.SSH.MinimumKeySizeCheck { |
| 292 | return content, nil |
| 293 | } |
| 294 | |
| 295 | var ( |
| 296 | fnName string |
| 297 | keyType string |
| 298 | length int |
| 299 | ) |
| 300 | if conf.SSH.StartBuiltinServer { |
| 301 | fnName = "SSHNativeParsePublicKey" |
| 302 | keyType, length, err = SSHNativeParsePublicKey(content) |
| 303 | } else { |
| 304 | fnName = "SSHKeygenParsePublicKey" |
| 305 | keyType, length, err = SSHKeygenParsePublicKey(content, conf.SSH.KeyTestPath, conf.SSH.KeygenPath) |
| 306 | } |
| 307 | if err != nil { |
| 308 | return "", errors.Newf("%s: %v", fnName, err) |
| 309 | } |
| 310 | log.Trace("Key info [native: %v]: %s-%d", conf.SSH.StartBuiltinServer, keyType, length) |
| 311 | |
| 312 | if minLen, found := conf.SSH.MinimumKeySizes[keyType]; found && length >= minLen { |
| 313 | return content, nil |
| 314 | } else if found && length < minLen { |
| 315 | return "", errors.Newf("key length is not enough: got %d, needs %d", length, minLen) |
| 316 | } |
| 317 | return "", errors.Newf("key type is not allowed: %s", keyType) |
| 318 | } |
| 319 | |
| 320 | // appendAuthorizedKeysToFile appends new SSH keys' content to authorized_keys file. |
| 321 | func appendAuthorizedKeysToFile(keys ...*PublicKey) error { |
no test coverage detected