sshkey_handler.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package handlers
  2. import (
  3. "byom-infra-api/config"
  4. "encoding/json"
  5. "log"
  6. "net/http"
  7. "github.com/gorilla/mux"
  8. )
  9. type SSHKey struct {
  10. KeyName string `json:"keyName"`
  11. Key string `json:"key"`
  12. }
  13. type SSHKeyHandler struct{}
  14. func NewSSHKeyHandler() *SSHKeyHandler {
  15. return &SSHKeyHandler{}
  16. }
  17. // GetSSHKeys handles the retrieval of all SSH keys
  18. func (h *SSHKeyHandler) GetSSHKeys(w http.ResponseWriter, r *http.Request) {
  19. log.Printf("Handling GET request for all SSH keys from %s", r.RemoteAddr)
  20. // First, get the list of SSH key IDs
  21. var keyIDs []string
  22. err := config.OVHClient.Get("/me/sshKey", &keyIDs)
  23. if err != nil {
  24. log.Printf("Error fetching SSH key IDs: %v", err)
  25. http.Error(w, "Failed to fetch SSH keys: "+err.Error(), http.StatusInternalServerError)
  26. return
  27. }
  28. // Then fetch details for each key
  29. var sshKeys []SSHKey
  30. for _, id := range keyIDs {
  31. var key SSHKey
  32. err := config.OVHClient.Get("/me/sshKey/"+id, &key)
  33. if err != nil {
  34. log.Printf("Error fetching details for SSH key %s: %v", id, err)
  35. continue
  36. }
  37. sshKeys = append(sshKeys, key)
  38. }
  39. log.Printf("Successfully retrieved %d SSH keys", len(sshKeys))
  40. w.Header().Set("Content-Type", "application/json")
  41. if err := json.NewEncoder(w).Encode(sshKeys); err != nil {
  42. log.Printf("Error encoding SSH keys response: %v", err)
  43. http.Error(w, "Failed to encode response", http.StatusInternalServerError)
  44. return
  45. }
  46. }
  47. // GetSSHKeyByID handles the retrieval of a specific SSH key by ID
  48. func (h *SSHKeyHandler) GetSSHKeyByID(w http.ResponseWriter, r *http.Request) {
  49. vars := mux.Vars(r)
  50. keyID := vars["id"]
  51. log.Printf("Handling GET request for SSH key ID: %s from %s", keyID, r.RemoteAddr)
  52. var sshKey SSHKey
  53. err := config.OVHClient.Get("/me/sshKey/"+keyID, &sshKey)
  54. if err != nil {
  55. log.Printf("Error fetching SSH key %s: %v", keyID, err)
  56. http.Error(w, "Failed to fetch SSH key: "+err.Error(), http.StatusInternalServerError)
  57. return
  58. }
  59. log.Printf("Successfully retrieved SSH key: %s", keyID)
  60. w.Header().Set("Content-Type", "application/json")
  61. if err := json.NewEncoder(w).Encode(sshKey); err != nil {
  62. log.Printf("Error encoding SSH key response: %v", err)
  63. http.Error(w, "Failed to encode response", http.StatusInternalServerError)
  64. return
  65. }
  66. }
  67. // CreateSSHKey handles the creation of a new SSH key
  68. func (h *SSHKeyHandler) CreateSSHKey(w http.ResponseWriter, r *http.Request) {
  69. log.Printf("Handling POST request for new SSH key from %s", r.RemoteAddr)
  70. // Parse request body
  71. var newKey struct {
  72. KeyName string `json:"keyName"`
  73. Key string `json:"key"`
  74. }
  75. if err := json.NewDecoder(r.Body).Decode(&newKey); err != nil {
  76. log.Printf("Error decoding request body: %v", err)
  77. http.Error(w, "Invalid request body", http.StatusBadRequest)
  78. return
  79. }
  80. // Validate input
  81. if newKey.KeyName == "" || newKey.Key == "" {
  82. log.Printf("Invalid input: keyName or key is empty")
  83. http.Error(w, "KeyName and Key are required", http.StatusBadRequest)
  84. return
  85. }
  86. // Prepare creation payload
  87. sshKey := SSHKey{
  88. KeyName: newKey.KeyName,
  89. Key: newKey.Key,
  90. }
  91. // Create SSH key via OVH API
  92. err := config.OVHClient.Post("/me/sshKey", sshKey, &sshKey)
  93. if err != nil {
  94. log.Printf("Error creating SSH key: %v", err)
  95. http.Error(w, "Failed to create SSH key: "+err.Error(), http.StatusInternalServerError)
  96. return
  97. }
  98. log.Printf("Successfully created SSH key: %s", sshKey.KeyName)
  99. // Return created key details
  100. w.Header().Set("Content-Type", "application/json")
  101. w.WriteHeader(http.StatusCreated)
  102. if err := json.NewEncoder(w).Encode(sshKey); err != nil {
  103. log.Printf("Error encoding response: %v", err)
  104. http.Error(w, "Failed to encode response", http.StatusInternalServerError)
  105. return
  106. }
  107. }
  108. // // GenerateED25519Key generates a new ED25519 SSH key pair and returns public and private keys
  109. // func GenerateED25519Key(keyName string) (string, string, error) {
  110. // // Generate the key pair
  111. // pubKey, privKey, err := ed25519.GenerateKey(rand.Reader)
  112. // if err != nil {
  113. // return "", "", fmt.Errorf("failed to generate ED25519 key: %v", err)
  114. // }
  115. // // Convert to OpenSSH format
  116. // // ED25519 public key format: ssh-ed25519 <base64-encoded-public-key> <comment>
  117. // publicKeyStr := "ssh-ed25519 " + base64.StdEncoding.EncodeToString(pubKey) + " OVH_" + keyName
  118. // // Convert private key to base64
  119. // privateKeyStr := base64.StdEncoding.EncodeToString(privKey)
  120. // return publicKeyStr, privateKeyStr, nil
  121. // }
  122. // // Example usage in CreateSSHKey:
  123. // func (h *SSHKeyHandler) CreateSSHKeyWithGeneration(w http.ResponseWriter, r *http.Request) {
  124. // var request struct {
  125. // KeyName string `json:"keyName"`
  126. // }
  127. // if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
  128. // http.Error(w, "Invalid request body", http.StatusBadRequest)
  129. // return
  130. // }
  131. // publicKey, privateKey, err := GenerateED25519Key(request.KeyName)
  132. // if err != nil {
  133. // http.Error(w, err.Error(), http.StatusInternalServerError)
  134. // return
  135. // }
  136. // sshKey := SSHKey{
  137. // KeyName: request.KeyName,
  138. // Key: publicKey,
  139. // }
  140. // // print private key to console
  141. // fmt.Printf("-----BEGIN OPENSSH PRIVATE KEY-----\n")
  142. // fmt.Printf("%s\n", privateKey)
  143. // fmt.Printf("-----END OPENSSH PRIVATE KEY-----\n")
  144. // // Create SSH key via OVH API
  145. // err = config.OVHClient.Post("/me/sshKey", sshKey, &sshKey)
  146. // if err != nil {
  147. // log.Printf("Error creating SSH key: %v", err)
  148. // http.Error(w, "Failed to create SSH key: "+err.Error(), http.StatusInternalServerError)
  149. // return
  150. // }
  151. // // Return created key details
  152. // w.Header().Set("Content-Type", "application/json")
  153. // w.WriteHeader(http.StatusCreated)
  154. // if err := json.NewEncoder(w).Encode(sshKey); err != nil {
  155. // log.Printf("Error encoding response: %v", err)
  156. // http.Error(w, "Failed to encode response", http.StatusInternalServerError)
  157. // return
  158. // }
  159. // }
  160. // DeleteSSHKey handles the deletion of an SSH key
  161. func (h *SSHKeyHandler) DeleteSSHKey(w http.ResponseWriter, r *http.Request) {
  162. vars := mux.Vars(r)
  163. keyId := vars["id"]
  164. log.Printf("Handling DELETE request for SSH key: %s from %s", keyId, r.RemoteAddr)
  165. err := config.OVHClient.Delete("/me/sshKey/"+keyId, nil)
  166. if err != nil {
  167. log.Printf("Error deleting SSH key %s: %v", keyId, err)
  168. http.Error(w, "Failed to delete SSH key: "+err.Error(), http.StatusInternalServerError)
  169. return
  170. }
  171. log.Printf("Successfully deleted SSH key: %s", keyId)
  172. w.WriteHeader(http.StatusNoContent)
  173. }