/
prisma.go
136 lines (124 loc) · 3.62 KB
/
prisma.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
package hatchery
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"strconv"
)
type Token struct {
Token string `json:"token"`
}
type InstallBundle struct {
WsAddress string `json:"wsAddress"`
Bundle string `json:"installBundle"`
}
func getPrismaToken(username string, password string) (*string, error) {
postBody, _ := json.Marshal(map[string]string{
"username": username,
"password": password,
})
reqBody := bytes.NewBuffer(postBody)
authEndpoint := Config.Config.PrismaConfig.ConsoleAddress + "/api/v1/authenticate"
resp, err := http.Post(authEndpoint, "application/json", reqBody)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
b, _ := io.ReadAll(resp.Body)
Config.Logger.Print(string(b))
return nil, errors.New("Error authenticating with Prisma Cloud: " + string(b))
}
//We Read the response body on the line below.
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var result Token
if err := json.Unmarshal(body, &result); err != nil {
fmt.Println("Invalid response from prisma auth endpoint: " + authEndpoint)
}
return &result.Token, nil
}
func getInstallBundle() (*InstallBundle, error) {
username := os.Getenv("PRISMA_ACCESS_KEY_ID")
password := os.Getenv("PRISMA_SECRET_KEY")
token, err := getPrismaToken(username, password)
if err != nil {
return nil, err
}
installBundleEndpoint := Config.Config.PrismaConfig.ConsoleAddress + fmt.Sprintf("/api/%s/defenders/install-bundle?consoleaddr=", Config.Config.PrismaConfig.ConsoleVersion) + Config.Config.PrismaConfig.ConsoleAddress + "&defenderType=appEmbedded"
var bearer = "Bearer " + *token
// Create a new request using http
req, err := http.NewRequest("GET", installBundleEndpoint, nil)
if err != nil {
return nil, err
}
// add authorization header to the req
req.Header.Add("Authorization", bearer)
// Send req using http Client
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
b, _ := io.ReadAll(resp.Body)
Config.Logger.Print(string(b))
return nil, errors.New("Error getting install bundle: " + string(b))
}
//We Read the response body on the line below.
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var result InstallBundle
if err := json.Unmarshal(body, &result); err != nil {
fmt.Println("Invalid response from prisma install_bundle endpoint: " + installBundleEndpoint)
}
return &result, nil
}
func getPrismaImage() (*string, error) {
username := os.Getenv("PRISMA_ACCESS_KEY_ID")
password := os.Getenv("PRISMA_SECRET_KEY")
token, err := getPrismaToken(username, password)
if err != nil {
return nil, err
}
imageEndpoint := Config.Config.PrismaConfig.ConsoleAddress + fmt.Sprintf("/api/%s/defenders/image-name", Config.Config.PrismaConfig.ConsoleVersion)
var bearer = "Bearer " + *token
// Create a new request using http
req, err := http.NewRequest("GET", imageEndpoint, nil)
if err != nil {
return nil, err
}
// add authorization header to the req
req.Header.Add("Authorization", bearer)
// Send req using http Client
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
b, _ := io.ReadAll(resp.Body)
Config.Logger.Print(string(b))
return nil, errors.New("Error getting install bundle: " + string(b))
}
//We Read the response body on the line below.
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
sb, err := strconv.Unquote(string(body))
if err != nil {
return nil, err
}
return &sb, nil
}