mirror of
https://github.com/0x1d/rcond.git
synced 2025-12-14 18:25:21 +01:00
feat: implement file upload
This commit is contained in:
19
README.md
19
README.md
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
A distributed management daemon designed to remotely configure system components, including:
|
A distributed management daemon designed to remotely configure system components, including:
|
||||||
- Network connections: Manage network connections through NetworkManager's D-Bus interface
|
- Network connections: Manage network connections through NetworkManager's D-Bus interface
|
||||||
|
- Files: Manage files on the system
|
||||||
- System hostname: Update the system's hostname
|
- System hostname: Update the system's hostname
|
||||||
- Authorized SSH keys: Manage the user's authorized_keys file to add, remove, or modify authorized SSH keys
|
- Authorized SSH keys: Manage the user's authorized_keys file to add, remove, or modify authorized SSH keys
|
||||||
- System state: Restart and shutdown the system
|
- System state: Restart and shutdown the system
|
||||||
@@ -143,6 +144,7 @@ All endpoints except `/health` require authentication via an API token passed in
|
|||||||
| POST | `/hostname` | Set the hostname |
|
| POST | `/hostname` | Set the hostname |
|
||||||
| POST | `/users/{user}/keys` | Add an authorized SSH key |
|
| POST | `/users/{user}/keys` | Add an authorized SSH key |
|
||||||
| DELETE | `/users/{user}/keys/{fingerprint}` | Remove an authorized SSH key |
|
| DELETE | `/users/{user}/keys/{fingerprint}` | Remove an authorized SSH key |
|
||||||
|
| POST | `/system/file` | Upload a file to the system |
|
||||||
| POST | `/system/restart` | Restart the system |
|
| POST | `/system/restart` | Restart the system |
|
||||||
| POST | `/system/shutdown` | Shutdown the system |
|
| POST | `/system/shutdown` | Shutdown the system |
|
||||||
| GET | `/cluster/members` | Get the cluster members |
|
| GET | `/cluster/members` | Get the cluster members |
|
||||||
@@ -150,6 +152,7 @@ All endpoints except `/health` require authentication via an API token passed in
|
|||||||
| POST | `/cluster/leave` | Leave the cluster |
|
| POST | `/cluster/leave` | Leave the cluster |
|
||||||
| POST | `/cluster/event` | Send a cluster event |
|
| POST | `/cluster/event` | Send a cluster event |
|
||||||
|
|
||||||
|
|
||||||
### Response Codes
|
### Response Codes
|
||||||
|
|
||||||
- 200: Success
|
- 200: Success
|
||||||
@@ -230,3 +233,19 @@ curl -X POST "http://rpi-test:8080/cluster/event" \
|
|||||||
"name": "restart"
|
"name": "restart"
|
||||||
}'
|
}'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Upload a file
|
||||||
|
|
||||||
|
This example will store Base64 encoded content to the target path.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl -X 'POST' \
|
||||||
|
'http://localhost:8080/system/file' \
|
||||||
|
-H 'accept: application/json' \
|
||||||
|
-H 'X-API-Token: 1234567890' \
|
||||||
|
-H 'Content-Type: application/json' \
|
||||||
|
-d '{
|
||||||
|
"path": "/tmp/somefile",
|
||||||
|
"content": "Zm9vCg=="
|
||||||
|
}'
|
||||||
|
```
|
||||||
@@ -494,6 +494,48 @@ paths:
|
|||||||
application/json:
|
application/json:
|
||||||
schema:
|
schema:
|
||||||
$ref: '#/components/schemas/Error'
|
$ref: '#/components/schemas/Error'
|
||||||
|
/system/file:
|
||||||
|
post:
|
||||||
|
summary: Upload a file
|
||||||
|
description: Uploads a file to the system
|
||||||
|
requestBody:
|
||||||
|
required: true
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
path:
|
||||||
|
type: string
|
||||||
|
description: Path where the file will be stored
|
||||||
|
example: "/path/to/file"
|
||||||
|
content:
|
||||||
|
type: string
|
||||||
|
description: Base64 encoded content of the file
|
||||||
|
example: "SGVsbG8gV29ybGQh"
|
||||||
|
responses:
|
||||||
|
'200':
|
||||||
|
description: File uploaded successfully
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
type: object
|
||||||
|
properties:
|
||||||
|
status:
|
||||||
|
type: string
|
||||||
|
example: "success"
|
||||||
|
'400':
|
||||||
|
description: Invalid request payload
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: '#/components/schemas/Error'
|
||||||
|
'500':
|
||||||
|
description: Internal server error
|
||||||
|
content:
|
||||||
|
application/json:
|
||||||
|
schema:
|
||||||
|
$ref: '#/components/schemas/Error'
|
||||||
/system/restart:
|
/system/restart:
|
||||||
post:
|
post:
|
||||||
summary: Restart system
|
summary: Restart system
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package http
|
package http
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -26,3 +27,31 @@ func HandleShutdown(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(map[string]string{"status": "success"})
|
json.NewEncoder(w).Encode(map[string]string{"status": "success"})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func HandleFileUpload(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Parse the request body
|
||||||
|
var fileUpload struct {
|
||||||
|
Path string `json:"path"`
|
||||||
|
Content string `json:"content"`
|
||||||
|
}
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&fileUpload); err != nil {
|
||||||
|
WriteError(w, "Invalid request body", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Decode base64 encoded content to bytes
|
||||||
|
contentBytes, err := base64.StdEncoding.DecodeString(fileUpload.Content)
|
||||||
|
if err != nil {
|
||||||
|
WriteError(w, "Failed to decode base64 content", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store the file
|
||||||
|
if err := system.StoreFile(fileUpload.Path, contentBytes); err != nil {
|
||||||
|
WriteError(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
json.NewEncoder(w).Encode(map[string]string{"status": "success"})
|
||||||
|
}
|
||||||
|
|||||||
@@ -80,6 +80,7 @@ func (s *Server) RegisterRoutes() {
|
|||||||
s.router.HandleFunc("/cluster/join", s.verifyToken(ClusterAgentHandler(s.clusterAgent, HandleClusterJoin))).Methods(http.MethodPost)
|
s.router.HandleFunc("/cluster/join", s.verifyToken(ClusterAgentHandler(s.clusterAgent, HandleClusterJoin))).Methods(http.MethodPost)
|
||||||
s.router.HandleFunc("/cluster/leave", s.verifyToken(ClusterAgentHandler(s.clusterAgent, HandleClusterLeave))).Methods(http.MethodPost)
|
s.router.HandleFunc("/cluster/leave", s.verifyToken(ClusterAgentHandler(s.clusterAgent, HandleClusterLeave))).Methods(http.MethodPost)
|
||||||
s.router.HandleFunc("/cluster/event", s.verifyToken(ClusterAgentHandler(s.clusterAgent, HandleClusterEvent))).Methods(http.MethodPost)
|
s.router.HandleFunc("/cluster/event", s.verifyToken(ClusterAgentHandler(s.clusterAgent, HandleClusterEvent))).Methods(http.MethodPost)
|
||||||
|
s.router.HandleFunc("/system/file", s.verifyToken(HandleFileUpload)).Methods(http.MethodPost)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) healthHandler(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) healthHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|||||||
25
pkg/system/file.go
Normal file
25
pkg/system/file.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package system
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
// StoreFile stores a file on the file system at the given path.
|
||||||
|
// If the path does not exist, it will be created.
|
||||||
|
func StoreFile(path string, content []byte) error {
|
||||||
|
// Ensure the directory exists
|
||||||
|
dir := filepath.Dir(path)
|
||||||
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
||||||
|
return fmt.Errorf("failed to create directory: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write the file
|
||||||
|
if err := ioutil.WriteFile(path, content, 0644); err != nil {
|
||||||
|
return fmt.Errorf("failed to write file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user