Uploads the provided SSH Signing key. Returns true if the key was uploaded, false if it was not.
(httpClient *http.Client, hostname string, keyFile io.Reader, title string)
| 57 | |
| 58 | // Uploads the provided SSH Signing key. Returns true if the key was uploaded, false if it was not. |
| 59 | func SSHSigningKeyUpload(httpClient *http.Client, hostname string, keyFile io.Reader, title string) (bool, error) { |
| 60 | url := ghinstance.RESTPrefix(hostname) + "user/ssh_signing_keys" |
| 61 | |
| 62 | keyBytes, err := io.ReadAll(keyFile) |
| 63 | if err != nil { |
| 64 | return false, err |
| 65 | } |
| 66 | |
| 67 | fullUserKey := string(keyBytes) |
| 68 | splitKey := strings.Fields(fullUserKey) |
| 69 | if len(splitKey) < 2 { |
| 70 | return false, errors.New("provided key is not in a valid format") |
| 71 | } |
| 72 | |
| 73 | keyToCompare := splitKey[0] + " " + splitKey[1] |
| 74 | |
| 75 | keys, err := shared.UserSigningKeys(httpClient, hostname, "") |
| 76 | if err != nil { |
| 77 | return false, err |
| 78 | } |
| 79 | |
| 80 | for _, k := range keys { |
| 81 | if k.Key == keyToCompare { |
| 82 | return false, nil |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | payload := map[string]string{ |
| 87 | "title": title, |
| 88 | "key": fullUserKey, |
| 89 | } |
| 90 | |
| 91 | err = keyUpload(httpClient, url, payload) |
| 92 | |
| 93 | if err != nil { |
| 94 | return false, err |
| 95 | } |
| 96 | |
| 97 | return true, nil |
| 98 | } |
| 99 | |
| 100 | func keyUpload(httpClient *http.Client, url string, payload map[string]string) error { |
| 101 | payloadBytes, err := json.Marshal(payload) |
no test coverage detected