Uploads the provided SSH key. Returns true if the key was uploaded, false if it was not.
(httpClient *http.Client, hostname string, keyFile io.Reader, title string)
| 15 | |
| 16 | // Uploads the provided SSH key. Returns true if the key was uploaded, false if it was not. |
| 17 | func SSHKeyUpload(httpClient *http.Client, hostname string, keyFile io.Reader, title string) (bool, error) { |
| 18 | u, err := safeurl.JoinPath("user", "keys") |
| 19 | if err != nil { |
| 20 | return false, err |
| 21 | } |
| 22 | |
| 23 | keyBytes, err := io.ReadAll(keyFile) |
| 24 | if err != nil { |
| 25 | return false, err |
| 26 | } |
| 27 | |
| 28 | fullUserKey := string(keyBytes) |
| 29 | splitKey := strings.Fields(fullUserKey) |
| 30 | if len(splitKey) < 2 { |
| 31 | return false, errors.New("provided key is not in a valid format") |
| 32 | } |
| 33 | |
| 34 | keyToCompare := splitKey[0] + " " + splitKey[1] |
| 35 | |
| 36 | keys, err := shared.UserKeys(httpClient, hostname, "") |
| 37 | if err != nil { |
| 38 | return false, err |
| 39 | } |
| 40 | |
| 41 | for _, k := range keys { |
| 42 | if k.Key == keyToCompare { |
| 43 | return false, nil |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | payload := map[string]string{ |
| 48 | "title": title, |
| 49 | "key": fullUserKey, |
| 50 | } |
| 51 | |
| 52 | err = keyUpload(httpClient, hostname, u, payload) |
| 53 | |
| 54 | if err != nil { |
| 55 | return false, err |
| 56 | } |
| 57 | |
| 58 | return true, nil |
| 59 | } |
| 60 | |
| 61 | // Uploads the provided SSH Signing key. Returns true if the key was uploaded, false if it was not. |
| 62 | func SSHSigningKeyUpload(httpClient *http.Client, hostname string, keyFile io.Reader, title string) (bool, error) { |