-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
250 lines (211 loc) · 9.57 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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
"github.com/joho/godotenv"
"os"
)
type TokenResponse struct {
AccessToken string `json:"access_token"`
}
type GraphAPIResponse struct {
Value []map[string]interface{} `json:"value"`
}
// auditlog struct with fields to support events
type AuditLog struct {
Level string `json:"level"`
Job string `json:"job"`
Log string `json:"log"`
ActivityDateTime string `json:"activityDateTime"`
Category string `json:"category"`
CorrelationID string `json:"correlationId"`
Result string `json:"result"`
UserPrincipal string `json:"userPrincipalName"`
ClientApp string `json:"clientAppUsed"`
IPAddress string `json:"ipAddress"`
}
// get access token from Microsoft Graph API
func getAccessToken(tenantID, clientID, clientSecret string) string {
tokenURL := "https://login.microsoftonline.com/" + tenantID + "/oauth2/v2.0/token"
data := "client_id=" + clientID + "&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default&client_secret=" + clientSecret + "&grant_type=client_credentials"
req, err := http.NewRequest("POST", tokenURL, bytes.NewBuffer([]byte(data)))
if err != nil {
log.Fatalf("Error creating token request: %v", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatalf("Error getting token: %v", err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
var tokenResponse TokenResponse
json.Unmarshal(body, &tokenResponse)
return tokenResponse.AccessToken
}
// retrieve logs from a given Microsoft Graph API endpoint
func getLogsFromEndpoint(accessToken, apiURL string) []map[string]interface{} {
req, _ := http.NewRequest("GET", apiURL, nil)
req.Header.Set("Authorization", "Bearer "+accessToken)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatalf("Error fetching logs: %v", err)
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
var graphResponse GraphAPIResponse
err = json.Unmarshal(body, &graphResponse)
if err != nil {
log.Fatalf("Error unmarshalling logs: %v", err)
}
return graphResponse.Value
}
// map audit logs for consent approvals to AuditLog struct
func mapToConsentLogs(rawLogs []map[string]interface{}) []AuditLog {
var consentLogs []AuditLog
for _, logEntry := range rawLogs {
if logEntry["activityDisplayName"] == "Consent to application" {
consentLogs = append(consentLogs, AuditLog{
Level: "info",
Job: "user_consent",
Log: fmt.Sprintf("User %s approved consent for %s", logEntry["initiatedBy"].(map[string]interface{})["user"].(map[string]interface{})["userPrincipalName"], logEntry["targetResources"].([]interface{})[0].(map[string]interface{})["displayName"]),
ActivityDateTime: logEntry["activityDateTime"].(string),
Category: "Consent",
CorrelationID: logEntry["correlationId"].(string),
Result: logEntry["result"].(string),
UserPrincipal: logEntry["initiatedBy"].(map[string]interface{})["user"].(map[string]interface{})["userPrincipalName"].(string),
ClientApp: "N/A", // Not relevant for consent logs
IPAddress: "N/A", // Not relevant for consent logs
})
}
}
return consentLogs
}
// map sign-in logs to AuditLog struct
func mapToSignInLogs(rawLogs []map[string]interface{}) []AuditLog {
var signInLogs []AuditLog
for _, logEntry := range rawLogs {
signInLogs = append(signInLogs, AuditLog{
Level: "info",
Job: "user_signin",
Log: fmt.Sprintf("User %s signed in using %s from IP %s", logEntry["userPrincipalName"], logEntry["clientAppUsed"], logEntry["ipAddress"]),
ActivityDateTime: logEntry["createdDateTime"].(string),
Category: "SignIn",
CorrelationID: logEntry["correlationId"].(string),
Result: logEntry["status"].(map[string]interface{})["value"].(string),
UserPrincipal: logEntry["userPrincipalName"].(string),
ClientApp: logEntry["clientAppUsed"].(string),
IPAddress: logEntry["ipAddress"].(string),
})
}
return signInLogs
}
// to map raw logs to AuditLog struct
func mapToAuditLogs(rawLogs []map[string]interface{}, jobName string) []AuditLog {
var auditLogs []AuditLog
for _, logEntry := range rawLogs {
auditLogs = append(auditLogs, AuditLog{
Level: "info", // Default log level, adjust as necessary
Job: jobName,
Log: fmt.Sprintf("Action: %s | Result: %s", logEntry["activityDisplayName"], logEntry["result"]),
ActivityDateTime: logEntry["activityDateTime"].(string),
Category: logEntry["category"].(string),
CorrelationID: logEntry["correlationId"].(string),
Result: logEntry["result"].(string),
UserPrincipal: "N/A", // Only relevant for certain logs
ClientApp: "N/A", // Only relevant for certain logs
IPAddress: "N/A", // Only relevant for certain logs
})
}
return auditLogs
}
// to push logs to OpenObserve
func pushLogsToOpenObserve(logs []AuditLog, orgID, streamName, openObserveHost, base64Creds string) {
jsonData, err := json.Marshal(logs)
if err != nil {
log.Fatalf("Error marshalling logs: %v", err)
}
// Define o2 endpoint and credentials
openObserveURL := fmt.Sprintf("http://%s/api/%s/%s/_json", openObserveHost, orgID, streamName)
req, err := http.NewRequest("POST", openObserveURL, bytes.NewBuffer(jsonData))
if err != nil {
log.Fatalf("Error creating request: %v", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Basic "+base64Creds)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatalf("Error sending logs to OpenObserve: %v", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalf("Error reading response body: %v", err)
}
fmt.Printf("Response Status: %s\n", resp.Status)
fmt.Printf("Response Body: %s\n", string(body))
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
fmt.Println("Logs successfully pushed to OpenObserve")
} else {
fmt.Printf("Failed to push logs: %s\n", string(body))
}
}
// run the connector logic continuously
func runConnector(tenantID, clientID, clientSecret, orgID, streamName, openObserveHost, base64Creds string) {
for {
fmt.Println("Fetching logs from Microsoft Graph API...")
// e access token from Microsoft Graph API
accessToken := getAccessToken(tenantID, clientID, clientSecret)
// fetch audit logs
auditLogs := getLogsFromEndpoint(accessToken, "https://graph.microsoft.com/v1.0/auditLogs/directoryAudits")
mappedAuditLogs := mapToAuditLogs(auditLogs, "microsoft_audit")
// directory logs
directoryLogs := getLogsFromEndpoint(accessToken, "https://graph.microsoft.com/v1.0/auditLogs/directoryAudits")
mappedDirectoryLogs := mapToAuditLogs(directoryLogs, "microsoft_directory")
// sign-in activity logs (existing logic - i will improve)
activityLogs := getLogsFromEndpoint(accessToken, "https://graph.microsoft.com/v1.0/auditLogs/signIns")
mappedActivityLogs := mapToAuditLogs(activityLogs, "microsoft_signin")
// map user sign-ins
signInLogs := getLogsFromEndpoint(accessToken, "https://graph.microsoft.com/v1.0/auditLogs/signIns")
mappedSignInLogs := mapToSignInLogs(signInLogs)
// map user consent approvals
consentLogs := mapToConsentLogs(auditLogs) // Consent approvals come from audit logs
// Combine all logs into a single array
allLogs := append(mappedAuditLogs, mappedDirectoryLogs...)
allLogs = append(allLogs, mappedActivityLogs...)
allLogs = append(allLogs, mappedSignInLogs...)
allLogs = append(allLogs, consentLogs...)
// push all logs to OpenObserve
pushLogsToOpenObserve(allLogs, orgID, streamName, openObserveHost, base64Creds)
// wait for 5 seconds before the next fetch
fmt.Println("Waiting for 5 seconds...")
time.Sleep(5 * time.Second)
}
}
func main() {
err := godotenv.Load()
if err != nil {
log.Fatalf("Error loading .env file")
}
tenantID := os.Getenv("TENANT_ID")
clientID := os.Getenv("CLIENT_ID")
clientSecret := os.Getenv("CLIENT_SECRET")
orgID := os.Getenv("ORG_ID")
streamName := os.Getenv("STREAM_NAME")
openObserveHost := os.Getenv("OPEN_OBSERVE_HOST")
base64Creds := os.Getenv("BASE64_CREDS")
// Validate that environment variables are set
if tenantID == "" || clientID == "" || clientSecret == "" || orgID == "" || streamName == "" || openObserveHost == "" || base64Creds == "" {
log.Fatal("One or more required environment variables are missing.")
}
// Run the connector continuously
runConnector(tenantID, clientID, clientSecret, orgID, streamName, openObserveHost, base64Creds)
}