-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
149 lines (110 loc) · 3.15 KB
/
main.go
File metadata and controls
149 lines (110 loc) · 3.15 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
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
package main
import "fmt"
import "net/http"
import "io/ioutil"
import "encoding/xml"
import "encoding/json"
import "os"
import "log"
import "crypto/tls"
import "runtime"
import "bytes"
import "github.com/jpgriffo/tapp-client/firewall"
import "github.com/jpgriffo/tapp-client/firewall/data"
import "./script"
type TappConfig struct {
XMLName xml.Name `xml:"tapp"`
ApiEndpoint string `xml:"server,attr"`
LogFile string `xml:"log_file,attr"`
LogLevel string `xml:"log_level,attr"`
Certificate Cert `xml:"ssl"`
}
type Cert struct {
Cert string `xml:"cert,attr"`
Key string `xml:"key,attr"`
Ca string `xml:"server_ca,attr"`
}
func openTappConfiguration(fileLocation string) (config TappConfig) {
xmlFile, err := os.Open(fileLocation)
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer xmlFile.Close()
b, _ := ioutil.ReadAll(xmlFile)
// var config TappConfig
xml.Unmarshal(b, &config)
return config
}
func createClient(config TappConfig) (client *http.Client) {
/**
* Loads Clients Certificates and creates and 509KeyPair
*/
cert, err := tls.LoadX509KeyPair(config.Certificate.Cert, config.Certificate.Key)
if err != nil {
log.Fatalln(err)
}
/**
* Creates a client with specific transport configurations
*/
transport := &http.Transport{
TLSClientConfig: &tls.Config { Certificates: []tls.Certificate{cert}, InsecureSkipVerify: true},
}
client = &http.Client{Transport: transport}
return client
}
func main() {
configPath := "./tapp/client.xml"
endPoint := "cloud/firewall_profile"
var policy data.Policy
// connection.Get(configPath, endPoint, policy)
config := openTappConfiguration(configPath)
client := createClient(config)
response, err := client.Get(config.ApiEndpoint + endPoint)
if err != nil {
log.Fatalln(err)
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatalln(err)
}
fmt.Printf("%s\n\n",body)
json.Unmarshal(body, &policy)
fmt.Println( runtime.GOOS)
if len(policy.Rules) > 0 {
firewall.Drop()
firewall.Apply(policy)
}
response, err = client.Get(config.ApiEndpoint + "blueprint/script_characterizations?type=boot")
if err != nil {
log.Fatalln(err)
}
defer response.Body.Close()
body, err = ioutil.ReadAll(response.Body)
if err != nil {
log.Fatalln(err)
}
fmt.Printf("%s\n\n",body)
var script_characterizations []script.ScriptCharacterization
json.Unmarshal(body, &script_characterizations)
fmt.Printf("%v\n", script_characterizations)
for _, script_characterization := range script_characterizations {
var b []byte
script_conclusion, err := script_characterization.Execute()
if err != nil {
fmt.Println("error:", err)
}
b, err = script_conclusion.ToJson()
if err != nil {
fmt.Println("error:", err)
}
fmt.Printf("%s\n", b)
post_body := "{\"script_conclusion\":" + string(b[:]) + "}"
response, err = client.Post(config.ApiEndpoint + "blueprint/script_conclusions", "application/json", bytes.NewBufferString(post_body))
if err != nil {
log.Fatalln(err)
}
defer response.Body.Close()
}
}