-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
371 lines (316 loc) · 10.7 KB
/
main.go
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package main
import (
"context"
"crypto/tls"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"sync"
"time"
"gopkg.in/yaml.v2"
)
// Config represents the application configuration
type Config struct {
APIKey string `yaml:"api_key"` // The API key used to authenticate requests
ConcurrentLimit int `yaml:"concurrent_limit"` // The maximum number of concurrent executions
Port int `yaml:"port"` // The port number the server listens on
ServerTLS bool `yaml:"server_tls"` // Whether to use TLS for the server
ServerCertPath string `yaml:"server_cert_path"` // The path to the server's SSL/TLS certificate
ServerKeyPath string `yaml:"server_key_path"` // The path to the server's SSL/TLS private key
AllowedCommands []string `yaml:"allowed_commands"` // The list of allowed shell commands
}
// LoadConfig reads a configuration file and returns a Config struct.
// filename: path to the YAML configuration file
// Returns Config struct and error if any occurred.
func LoadConfig(filename string) (Config, error) {
// First, check if the file exists
if _, err := os.Stat(filename); os.IsNotExist(err) {
return Config{}, errors.New("config file does not exist")
}
// If the file exists, read and parse it
data, err := ioutil.ReadFile(filename)
if err != nil {
return Config{}, err
}
var config Config
err = yaml.Unmarshal(data, &config)
if err != nil {
return Config{}, err
}
return config, nil
}
// Execution represents a command execution request.
type Execution struct {
Command string `json:"command"` // Command to execute
Args []string `json:"args"` // Arguments to pass to the command
}
// ExecutionResult holds the result of a command execution.
type ExecutionResult struct {
Stdout string `json:"stdout"` // Standard output of the command
Stderr string `json:"stderr"` // Standard error output of the command
ReturnCode int `json:"returnCode"` // Return code of the command
StartTime string `json:"startTime"` // Execution start time (UTC)
EndTime string `json:"endTime"` // Execution end time (UTC)
Elapsed string `json:"elapsed"` // Elapsed time (duration)
Status string `json:"status"` // Execution status ("success" or "failure")
}
// Global variables for managing command executions and results
var (
config Config // Server configuration
mu sync.Mutex // Mutex to protect concurrent access to the maps
executions = make(map[int]*exec.Cmd) // Map of running command executions
results = make(map[int]*ExecutionResult) // Map of command execution results
apiKey = "your-api-key" // Server configuration
concurrentLimit = 5 // Concurrent executions limit
semaphore = make(chan struct{}, concurrentLimit) // Semaphore for limiting concurrent executions
)
// main initializes the server configuration, sets up the API routes, and starts the HTTP server.
//
// main function performs the following steps:
// 1. Load configuration from "config.yaml" into the global `config` variable.
// 2. Set global variables `apiKey` and `concurrentLimit` from the loaded configuration.
// 3. Initialize the `semaphore` with the configured concurrent limit.
// 4. Register the following API route handlers with the necessary authentication and method checks:
// - POST /execute: Executes a command (handled by executeHandler).
// - GET /execution/: Retrieves information about a specific command execution (handled by executionHandler).
// - GET /execution/list: Retrieves a list of all command executions (handled by listHandler).
// - POST /execution/cancel/: Cancels an ongoing command execution (handled by cancelHandler).
//
// 5. Start the HTTP server on the configured port.
func main() {
var err error
var configFile string
flag.StringVar(&configFile, "c", "config.yaml", "path to config file")
flag.Parse()
config, err := LoadConfig(configFile)
if err != nil {
log.Fatalf("Failed to load configuration: %v", err)
}
apiKey = config.APIKey
concurrentLimit = config.ConcurrentLimit
semaphore = make(chan struct{}, config.ConcurrentLimit)
http.HandleFunc("/execute", authenticate(withMethod("POST", executeHandler)))
http.HandleFunc("/execution/", authenticate(withMethod("GET", executionHandler)))
http.HandleFunc("/execution/list", authenticate(withMethod("GET", listHandler)))
http.HandleFunc("/execution/cancel/", authenticate(withMethod("POST", cancelHandler)))
serverTLS := config.ServerTLS
if serverTLS {
tlsConfig := createTLSConfig()
server := &http.Server{
Addr: fmt.Sprintf(":%d", config.Port),
TLSConfig: tlsConfig,
}
log.Fatal(server.ListenAndServeTLS("", ""))
} else {
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil))
}
}
func createTLSConfig() *tls.Config {
cert, err := tls.LoadX509KeyPair(config.ServerCertPath, config.ServerKeyPath)
if err != nil {
log.Fatal(err)
}
return &tls.Config{
MinVersion: tls.VersionTLS12,
PreferServerCipherSuites: true,
CipherSuites: []uint16{
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_RSA_WITH_AES_128_CBC_SHA256,
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
},
CurvePreferences: []tls.CurveID{
tls.CurveP521,
tls.CurveP384,
tls.CurveP256,
},
Certificates: []tls.Certificate{cert},
ClientAuth: tls.NoClientCert,
}
}
func authenticate(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
key := r.Header.Get("X-API-Key")
if key != apiKey {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
next(w, r)
}
}
// isAllowedCommand checks if the given command is allowed to be executed.
// command: the command to check
// Returns true if the command is allowed, false otherwise.
func isAllowedCommand(command string) bool {
for _, allowedCommand := range config.AllowedCommands {
if command == allowedCommand {
return true
}
}
return false
}
func executeHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var e Execution
err := json.NewDecoder(r.Body).Decode(&e)
if err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
if !isAllowedCommand(e.Command) {
http.Error(w, "Command not allowed", http.StatusForbidden)
return
}
ctx, cancelFunc := context.WithCancel(context.Background())
cmd := exec.CommandContext(ctx, "/bin/bash", "-c", fmt.Sprintf("%s %s", e.Command, strings.Join(e.Args, " ")))
stdout, _ := cmd.StdoutPipe()
stderr, _ := cmd.StderrPipe()
err = cmd.Start()
if err != nil {
http.Error(w, "Failed to start command", http.StatusInternalServerError)
return
}
id := cmd.Process.Pid
mu.Lock()
executions[id] = cmd
results[id] = &ExecutionResult{
StartTime: time.Now().Format(time.RFC3339),
Status: "running",
}
mu.Unlock()
semaphore <- struct{}{}
go func() {
defer func() { <-semaphore }()
stdoutBytes, _ := ioutil.ReadAll(stdout)
stderrBytes, _ := ioutil.ReadAll(stderr)
mu.Lock()
results[id].Stdout = string(stdoutBytes)
results[id].Stderr = string(stderrBytes)
results[id].ReturnCode = cmd.ProcessState.ExitCode()
results[id].EndTime = time.Now().Format(time.RFC3339)
results[id].Status = "completed"
mu.Unlock()
cmd.Wait()
cancelFunc()
}()
w.WriteHeader(http.StatusOK)
w.Write([]byte(fmt.Sprintf("%d", id)))
}
func executionHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
isResult := strings.HasSuffix(r.URL.Path, "/result")
isStdout := strings.HasSuffix(r.URL.Path, "/stdout")
isStderr := strings.HasSuffix(r.URL.Path, "/stderr")
if !isResult && !isStdout && !isStderr {
http.Error(w, "Invalid endpoint", http.StatusBadRequest)
return
}
idStr := r.URL.Path[len("/execution/") : len(r.URL.Path)-len("/result")]
if isStdout {
idStr = r.URL.Path[len("/execution/") : len(r.URL.Path)-len("/stdout")]
} else if isStderr {
idStr = r.URL.Path[len("/execution/") : len(r.URL.Path)-len("/stderr")]
}
id, err := strconv.Atoi(idStr)
if err != nil {
http.Error(w, "Invalid execution ID", http.StatusBadRequest)
return
}
mu.Lock()
result := results[id]
mu.Unlock()
if result == nil {
http.Error(w, "Execution not found", http.StatusNotFound)
return
}
if isStdout {
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte(result.Stdout))
} else if isStderr {
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte(result.Stderr))
} else {
if result.EndTime != "" {
startTime, _ := time.Parse(time.RFC3339, result.StartTime)
endTime, _ := time.Parse(time.RFC3339, result.EndTime)
result.Elapsed = endTime.Sub(startTime).String()
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(result)
}
}
func listHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
mu.Lock()
defer mu.Unlock()
executionList := make(map[int]string)
for id, result := range results {
executionList[id] = result.Status
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(executionList)
}
func cancelHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
idStr := r.URL.Path[len("/execution/cancel/"):]
id, err := strconv.Atoi(idStr)
if err != nil {
http.Error(w, "Invalid execution ID", http.StatusBadRequest)
return
}
mu.Lock()
cmd := executions[id]
result := results[id]
mu.Unlock()
if cmd == nil || result == nil {
http.Error(w, "Execution not found", http.StatusNotFound)
return
}
if cmd.ProcessState != nil && cmd.ProcessState.Exited() {
http.Error(w, "Execution already finished", http.StatusConflict)
return
}
err = cmd.Process.Kill()
if err != nil {
http.Error(w, "Failed to cancel execution", http.StatusInternalServerError)
return
}
mu.Lock()
result.Status = "canceled"
mu.Unlock()
w.WriteHeader(http.StatusOK)
w.Write([]byte("Execution canceled"))
}
func withMethod(method string, handlerFunc http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != method {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
handlerFunc(w, r)
}
}