@@ -3,12 +3,13 @@ package ws1
33import (
44 "bytes"
55 "context"
6- "encoding/base64"
76 "encoding/json"
7+ "fmt"
88 "github.com/hazcod/crowdstrike-spotlight-slacker/config"
99 "github.com/pkg/errors"
1010 "github.com/sirupsen/logrus"
11- "io/ioutil"
11+ "golang.org/x/oauth2/clientcredentials"
12+ "io"
1213 "net/http"
1314 "strconv"
1415 "strings"
@@ -31,46 +32,41 @@ type UserDeviceFinding struct {
3132 ComplianceName string
3233}
3334
34- func basicAuth (username , password string ) string {
35- auth := username + ":" + password
36- return base64 .StdEncoding .EncodeToString ([]byte (auth ))
37- }
38-
39- func doAuthRequest (user , pass , apiKey , url , method string , payload interface {}) (respBytes []byte , err error ) {
35+ func doAuthRequest (ctx context.Context , ws1AuthLocation , clientID , secret , url , method string , payload interface {}) (respBytes []byte , err error ) {
4036 var reqPayload []byte
4137 if payload != nil {
4238 if reqPayload , err = json .Marshal (& payload ); err != nil {
4339 return nil , errors .Wrap (err , "coult not encode request body" )
4440 }
4541 }
4642
43+ oauth2Config := clientcredentials.Config {ClientID : clientID , ClientSecret : secret ,
44+ TokenURL : fmt .Sprintf ("https://%s.uemauth.vmwservices.com/connect/token" , ws1AuthLocation )}
45+ httpClient := oauth2Config .Client (ctx )
46+ httpClient .Timeout = time .Second * 10
47+
4748 req , err := http .NewRequest (method , url , bytes .NewReader (reqPayload ))
49+ req = req .WithContext (ctx )
4850 if err != nil {
4951 return nil , errors .Wrap (err , "request failed" )
5052 }
5153
5254 req .Header .Set ("Accept" , "application/json" )
53- req .Header .Set ("aw-tenant-code" , apiKey )
54- req .Header .Set ("Authorization" , "Basic " + basicAuth (user , pass ))
55-
56- httpClient := http.Client {
57- Timeout : time .Second * 10 ,
58- }
5955
6056 resp , err := httpClient .Do (req )
6157 if err != nil {
6258 return nil , errors .Wrap (err , "http request failed" )
6359 }
6460
6561 if resp .StatusCode > 399 {
66- respB , _ := ioutil .ReadAll (resp .Body )
62+ respB , _ := io .ReadAll (resp .Body )
6763 logrus .WithField ("response" , string (respB )).Warn ("invalid response" )
6864 return nil , errors .New ("invalid response code: " + strconv .Itoa (resp .StatusCode ))
6965 }
7066
7167 defer resp .Body .Close ()
7268
73- if respBytes , err = ioutil .ReadAll (resp .Body ); err != nil {
69+ if respBytes , err = io .ReadAll (resp .Body ); err != nil {
7470 return nil , errors .New ("could not read response body" )
7571 }
7672
@@ -79,7 +75,8 @@ func doAuthRequest(user, pass, apiKey, url, method string, payload interface{})
7975
8076func GetMessages (config * config.Config , ctx context.Context ) (map [string ]WS1Result , []string , error ) {
8177 deviceResponseB , err := doAuthRequest (
82- config .WS1 .User , config .WS1 .Password , config .WS1 .APIKey ,
78+ ctx ,
79+ config .WS1 .AuthLocation , config .WS1 .ClientID , config .WS1 .ClientSecret ,
8380 strings .TrimRight (config .WS1 .Endpoint , "/" )+ "/mdm/devices/search?compliance_status=NonCompliant" ,
8481 http .MethodGet ,
8582 nil ,
@@ -89,7 +86,7 @@ func GetMessages(config *config.Config, ctx context.Context) (map[string]WS1Resu
8986 return nil , nil , errors .Wrap (err , "could not fetch WS1 devices" )
9087 }
9188
92- usersWithDevices := []string {}
89+ usersWithDevices := make ( []string , 0 )
9390
9491 var devicesResponse DevicesResponse
9592 if err := json .Unmarshal (deviceResponseB , & devicesResponse ); err != nil {
0 commit comments