-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
116 lines (99 loc) · 2.65 KB
/
main.go
File metadata and controls
116 lines (99 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"flag"
"html"
"log"
"net/http"
"os"
"os/exec"
"slices"
"strings"
"github.com/gorilla/mux"
"github.com/kataras/jwt"
)
var (
jwtKey *string
allowedOperations = []string{"start", "restart"}
)
func runVirshCommand(operation, vm string) (bool, string) {
// Construct command
cmd := exec.Command("virsh", "-c", "qemu:///system", operation, vm)
var stdout strings.Builder
var stderr strings.Builder
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil {
log.Println("Could not run command:", err)
}
stderrOut := stderr.String()
log.Println("Run operation:", operation, vm)
log.Println("stdout:", stdout.String())
log.Println("stderr:", stderrOut)
// Check if operation finished successfully
if strings.Contains(stderrOut, "error") || strings.Contains(stderrOut, "fail") {
return false, stderrOut
}
return true, ""
}
func handler(w http.ResponseWriter, r *http.Request) {
// Get token
token := r.URL.Query().Get("token")
token = html.EscapeString(token)
// Check if token is valid
verfiedToken, err := jwt.Verify(jwt.HS256, []byte(*jwtKey), []byte(token))
if err != nil {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
// Get claims
var claims = struct {
VMName string `json:"vm"`
Operation string `json:"operation"`
}{}
err = verfiedToken.Claims(&claims)
if err != nil || len(claims.VMName) == 0 || len(claims.Operation) == 0 {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
if !slices.Contains(allowedOperations, claims.Operation) {
log.Println("Forbidden operation requested: ", claims.Operation)
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
return
}
success := true
stderr := ""
if claims.Operation == "restart" {
success, stderr = runVirshCommand("destroy", claims.VMName)
success2, stderr2 := runVirshCommand("start", claims.VMName)
if !success || !success2 {
success = false
stderr = stderr + "\r\n" + stderr2
}
} else {
success, stderr = runVirshCommand(claims.Operation, claims.VMName)
}
/* Send error message on failure */
if !success {
http.Error(w, stderr, http.StatusInternalServerError)
return
}
}
func init() {
jwtKey = flag.String("key", "", "JWT key used to sign the exchanged data")
}
func main() {
flag.Parse()
log.SetFlags(0)
if len(*jwtKey) == 0 {
log.Println("No JWT key provided, aborting.")
os.Exit(0)
}
r := mux.NewRouter()
r.HandleFunc("/", handler)
log.Println("VM control listens on port 25000")
err := http.ListenAndServe(":25000", r)
if err != nil {
log.Fatalln("VM control failed: ", err)
}
}