Skip to content

Commit a1df8ee

Browse files
authored
Fix unhandled errors (#732)
1 parent f4da96a commit a1df8ee

File tree

6 files changed

+30
-24
lines changed

6 files changed

+30
-24
lines changed

office365/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ func main() {
2727
startTime := st.UTC().Format("2006-01-02T15:04:05")
2828
endTime := et.UTC().Format("2006-01-02T15:04:05")
2929

30+
utils.Logger.Info("syncing logs from %s to %s", startTime, endTime)
31+
3032
moduleConfig, err := client.GetUTMConfig(enum.O365)
3133
if err != nil {
3234
if strings.Contains(err.Error(), "invalid character '<'") {
@@ -35,10 +37,10 @@ func main() {
3537
}
3638
if (err.Error() != "") && (err.Error() != " ") {
3739
utils.Logger.ErrorF("error getting configuration of the O365 module: %v", err)
40+
} else {
41+
utils.Logger.Info("program not configured yet")
3842
}
3943

40-
utils.Logger.Info("sync complete waiting %v seconds", delayCheck)
41-
4244
time.Sleep(time.Second * delayCheck)
4345

4446
st = et.Add(1)
@@ -72,8 +74,6 @@ func main() {
7274
}(group)
7375
}
7476

75-
utils.Logger.Info("waiting %d seconds until sync completes", delayCheck)
76-
7777
wg.Wait()
7878

7979
utils.Logger.Info("sync complete waiting %d seconds", delayCheck)

office365/processor/processor.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"fmt"
66
"net/http"
77
"net/url"
8+
"strings"
89

9-
"github.com/threatwinds/logger"
1010
"github.com/utmstack/UTMStack/office365/configuration"
1111
"github.com/utmstack/UTMStack/office365/utils"
1212
"github.com/utmstack/config-client-go/types"
@@ -43,7 +43,7 @@ func GetOfficeProcessor(group types.ModuleGroup) OfficeProcessor {
4343
return offProc
4444
}
4545

46-
func (o *OfficeProcessor) GetAuth() *logger.Error {
46+
func (o *OfficeProcessor) GetAuth() error {
4747
requestUrl := configuration.GetMicrosoftLoginLink(o.TenantId)
4848

4949
data := url.Values{}
@@ -68,7 +68,7 @@ func (o *OfficeProcessor) GetAuth() *logger.Error {
6868
return nil
6969
}
7070

71-
func (o *OfficeProcessor) StartSubscriptions() *logger.Error {
71+
func (o *OfficeProcessor) StartSubscriptions() error {
7272
for _, subscription := range o.Subscriptions {
7373
utils.Logger.Info("starting subscription: %s...", subscription)
7474
url := configuration.GetStartSubscriptionLink(o.TenantId) + "?contentType=" + subscription
@@ -79,15 +79,16 @@ func (o *OfficeProcessor) StartSubscriptions() *logger.Error {
7979

8080
resp, status, e := utils.DoReq[StartSubscriptionResponse](url, []byte("{}"), http.MethodPost, headers)
8181
if e != nil || status != http.StatusOK {
82-
if e.Is("subscription is already enabled") {
83-
continue
82+
if strings.Contains(e.Error(), "subscription is already enabled") {
83+
// utils.Logger.Info("subscription is already enabled") // Debug
84+
return nil
8485
}
8586
return e
8687
}
8788

8889
respJson, err := json.Marshal(resp)
8990
if err != nil || status != http.StatusOK {
90-
return utils.Logger.ErrorF("failed to unmarshal response: %v", err)
91+
return fmt.Errorf("failed to unmarshal response: %v", err)
9192
}
9293

9394
utils.Logger.Info("starting subscription response: %v", respJson)
@@ -96,7 +97,7 @@ func (o *OfficeProcessor) StartSubscriptions() *logger.Error {
9697
return nil
9798
}
9899

99-
func (o *OfficeProcessor) GetContentList(subscription string, startTime string, endTime string, group types.ModuleGroup) ([]ContentList, *logger.Error) {
100+
func (o *OfficeProcessor) GetContentList(subscription string, startTime string, endTime string, group types.ModuleGroup) ([]ContentList, error) {
100101
url := configuration.GetContentLink(o.TenantId) + fmt.Sprintf("?startTime=%s&endTime=%s&contentType=%s", startTime, endTime, subscription)
101102

102103
headers := map[string]string{
@@ -105,15 +106,15 @@ func (o *OfficeProcessor) GetContentList(subscription string, startTime string,
105106
}
106107

107108
respBody, status, err := utils.DoReq[[]ContentList](url, nil, http.MethodGet, headers)
108-
if err != nil && status != http.StatusOK {
109+
if err != nil || status != http.StatusOK {
109110
return []ContentList{}, err
110111
}
111112

112113
return respBody, nil
113114

114115
}
115116

116-
func (o *OfficeProcessor) GetContentDetails(url string) (ContentDetailsResponse, *logger.Error) {
117+
func (o *OfficeProcessor) GetContentDetails(url string) (ContentDetailsResponse, error) {
117118
headers := map[string]string{
118119
"Content-Type": "application/json",
119120
"Authorization": fmt.Sprintf("%s %s", o.Credentials.TokenType, o.Credentials.AccessToken),
@@ -131,20 +132,23 @@ func (o *OfficeProcessor) GetLogs(startTime string, endTime string, group types.
131132
for _, subscription := range o.Subscriptions {
132133
contentList, err := o.GetContentList(subscription, startTime, endTime, group)
133134
if err != nil {
135+
utils.Logger.ErrorF("error getting content list: %v", err) // Debug
134136
continue
135137
}
136138
logsCounter := 0
137139
if len(contentList) > 0 {
138140
for _, log := range contentList {
139141
details, err := o.GetContentDetails(log.ContentUri)
140142
if err != nil {
143+
utils.Logger.ErrorF("error getting content details: %v", err) // Debug
141144
continue
142145
}
143146
if len(details) > 0 {
144147
logsCounter += len(details)
145148
cleanLogs := ETLProcess(details, group)
146149
err = SendToCorrelation(cleanLogs)
147150
if err != nil {
151+
utils.Logger.ErrorF("error sending logs to correlation: %v", err) // Debug
148152
continue
149153
}
150154
}

office365/processor/pull.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ func PullLogs(startTime string, endTime string, group types.ModuleGroup) {
1212

1313
err := agent.GetAuth()
1414
if err != nil {
15+
utils.Logger.ErrorF("error getting auth token: %v", err)
1516
return
1617
}
1718

1819
err = agent.StartSubscriptions()
1920
if err != nil {
21+
utils.Logger.ErrorF("error starting subscriptions: %v", err)
2022
return
2123
}
2224

office365/processor/sendData.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import (
44
"encoding/json"
55
"net/http"
66

7-
"github.com/threatwinds/logger"
87
"github.com/utmstack/UTMStack/office365/configuration"
98
"github.com/utmstack/UTMStack/office365/utils"
109
)
1110

12-
func SendToCorrelation(data []TransformedLog) *logger.Error {
11+
func SendToCorrelation(data []TransformedLog) error {
1312
utils.Logger.Info("uploading %d logs...", len(data))
1413

1514
for _, log := range data {
@@ -21,8 +20,10 @@ func SendToCorrelation(data []TransformedLog) *logger.Error {
2120

2221
_, status, e := utils.DoReq[map[string]interface{}](configuration.CORRELATIONURL, body, http.MethodPost, map[string]string{})
2322
if e != nil {
23+
utils.Logger.ErrorF("error sending log to correlation engine: %v", e)
2424
continue
2525
} else if status != http.StatusOK && status != http.StatusCreated {
26+
utils.Logger.ErrorF("error sending log to correlation engine: status code %d", status)
2627
continue
2728
}
2829

office365/utils/req.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@ package utils
33
import (
44
"bytes"
55
"encoding/json"
6+
"fmt"
67
"io"
78
"net/http"
8-
9-
"github.com/threatwinds/logger"
109
)
1110

12-
func DoReq[response any](url string, data []byte, method string, headers map[string]string) (response, int, *logger.Error) {
11+
func DoReq[response any](url string, data []byte, method string, headers map[string]string) (response, int, error) {
1312
var result response
1413

1514
req, err := http.NewRequest(method, url, bytes.NewBuffer(data))
1615
if err != nil {
17-
return result, http.StatusInternalServerError, Logger.ErrorF(err.Error())
16+
return result, http.StatusInternalServerError, err
1817
}
1918

2019
for k, v := range headers {
@@ -25,22 +24,22 @@ func DoReq[response any](url string, data []byte, method string, headers map[str
2524

2625
resp, err := client.Do(req)
2726
if err != nil {
28-
return result, http.StatusInternalServerError, Logger.ErrorF(err.Error())
27+
return result, http.StatusInternalServerError, err
2928
}
3029
defer resp.Body.Close()
3130

3231
body, err := io.ReadAll(resp.Body)
3332
if err != nil {
34-
return result, http.StatusInternalServerError, Logger.ErrorF(err.Error())
33+
return result, http.StatusInternalServerError, err
3534
}
3635

3736
if resp.StatusCode != http.StatusAccepted && resp.StatusCode != http.StatusOK {
38-
return result, resp.StatusCode, Logger.ErrorF("while sending request to %s received status code: %d and response body: %s", url, resp.StatusCode, body)
37+
return result, resp.StatusCode, fmt.Errorf("while sending request to %s received status code: %d and response body: %s", url, resp.StatusCode, body)
3938
}
4039

4140
err = json.Unmarshal(body, &result)
4241
if err != nil {
43-
return result, http.StatusInternalServerError, Logger.ErrorF(err.Error())
42+
return result, http.StatusInternalServerError, err
4443
}
4544

4645
return result, resp.StatusCode, nil

version.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version: 10.5.2
1+
version: 10.5.3

0 commit comments

Comments
 (0)