-
Notifications
You must be signed in to change notification settings - Fork 85
/
onboarding.go
115 lines (94 loc) · 2.36 KB
/
onboarding.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
// SPDX-License-Identifier: Apache-2.0
// Copyright 2022 Authors of KubeArmor
package vm
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
tp "github.com/kubearmor/KVMService/src/types"
kg "github.com/kubearmor/KubeArmor/KubeArmor/log"
"sigs.k8s.io/yaml"
)
func postHTTPRequest(eventData []byte, vmAction string, address string) (string, error) {
timeout := time.Duration(5 * time.Second)
client := http.Client{
Timeout: timeout,
}
request, err := http.NewRequest("POST", address+"/"+vmAction, bytes.NewBuffer(eventData))
request.Header.Set("Content-type", "application/json")
if err != nil {
return "", err
}
resp, err := client.Do(request)
if err != nil {
return "", err
}
defer func() {
if err := resp.Body.Close(); err != nil {
kg.Warnf("Error closing http stream %s\n", err)
}
}()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(respBody), err
}
// List - Lists all configured VMs
func List(address string) error {
var endpoints []tp.KVMSEndpoint
vmlist, err := postHTTPRequest(nil, "vmlist", address)
if err != nil {
fmt.Println("Failed to get vm list")
return err
}
err = json.Unmarshal([]byte(vmlist), &endpoints)
if err != nil {
fmt.Println("Failed to parse vm list")
return err
}
if len(endpoints) == 0 {
fmt.Println("No VMs configured")
} else {
fmt.Println("-------------------------------------------")
fmt.Printf(" %-3s| %-15s| %-10s| %s\n", "", "VM Name", "Identity", "Labels")
fmt.Println("-------------------------------------------")
for idx, vm := range endpoints {
fmt.Printf(" %-3s| %-15s| %-10s| %s\n", strconv.Itoa(idx+1),
vm.VMName, strconv.Itoa(int(vm.Identity)), strings.Join(vm.Labels, "; "))
}
}
return nil
}
// Onboarding - onboards a vm
func Onboarding(eventType string, path string, address string) error {
var vm tp.KubeArmorVirtualMachinePolicy
vmFile, err := os.ReadFile(filepath.Clean(path))
if err != nil {
return err
}
err = yaml.Unmarshal(vmFile, &vm)
if err != nil {
return err
}
vmEvent := tp.KubeArmorVirtualMachinePolicyEvent{
Type: eventType,
Object: vm,
}
vmEventData, err := json.Marshal(vmEvent)
if err != nil {
return err
}
if _, err = postHTTPRequest(vmEventData, "vm", address); err != nil {
return err
}
fmt.Println("Success")
return nil
}