11package validations
22
33import (
4+ "bytes"
5+ "encoding/json"
6+ "fmt"
7+ "io"
48 "net/http"
59 "net/url"
10+ "time"
611
7- "github.com/threatwinds/go-sdk/catcher"
8- "github.com/threatwinds/go-sdk/utils"
912 "github.com/utmstack/UTMStack/plugins/modules-config/config"
1013)
1114
1215const (
13- sophosAuthURL = "https://id.sophos.com/api/v2/oauth2/token"
16+ sophosAuthURL = "https://id.sophos.com/api/v2/oauth2/token"
17+ sophosWhoamiURL = "https://api.central.sophos.com/whoami/v1"
1418)
1519
1620func ValidateSophosConfig (config * config.ModuleGroup ) error {
1721 var clientID , clientSecret string
22+
1823 if config == nil {
19- return catcher . Error ("Sophos configuration is nil" , nil , nil )
24+ return fmt . Errorf ("Sophos configuration is nil" )
2025 }
2126
2227 for _ , cnf := range config .ModuleGroupConfigurations {
23- switch cnf .ConfName {
24- case "Client Id " :
28+ switch cnf .ConfKey {
29+ case "sophos_client_id " :
2530 clientID = cnf .ConfValue
26- case "Client Secret " :
31+ case "sophos_x_api_key " :
2732 clientSecret = cnf .ConfValue
2833 }
2934 }
3035
3136 if clientID == "" {
32- return catcher . Error ("Client ID is required in Sophos configuration" , nil , nil )
37+ return fmt . Errorf ("Client ID is required in Sophos configuration" )
3338 }
3439 if clientSecret == "" {
35- return catcher . Error ("Client Secret is required in Sophos configuration" , nil , nil )
40+ return fmt . Errorf ("Client Secret is required in Sophos configuration" )
3641 }
3742
3843 data := url.Values {}
@@ -41,30 +46,64 @@ func ValidateSophosConfig(config *config.ModuleGroup) error {
4146 data .Set ("client_secret" , clientSecret )
4247 data .Set ("scope" , "token" )
4348
44- headers := map [string ]string {
45- "Content-Type" : "application/x-www-form-urlencoded" ,
49+ req , err := http .NewRequest (http .MethodPost , sophosAuthURL , bytes .NewBufferString (data .Encode ()))
50+ if err != nil {
51+ return fmt .Errorf ("failed to create request: %w" , err )
4652 }
4753
48- response , status , err := utils .DoReq [map [string ]any ](sophosAuthURL , []byte (data .Encode ()), http .MethodPost , headers )
54+ req .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
55+
56+ client := & http.Client {
57+ Timeout : 10 * time .Second ,
58+ }
59+
60+ resp , err := client .Do (req )
4961 if err != nil {
50- return catcher .Error ("error validating Sophos credentials" , err , map [string ]any {
51- "status" : status ,
52- })
62+ return fmt .Errorf ("Sophos authentication request failed: %w" , err )
5363 }
64+ defer resp .Body .Close ()
5465
55- if status != http .StatusOK {
56- return catcher .Error ("Sophos authentication failed" , nil , map [string ]any {
57- "status" : status ,
58- "response" : response ,
59- })
66+ body , err := io .ReadAll (resp .Body )
67+ if err != nil {
68+ return fmt .Errorf ("failed to read response: %w" , err )
69+ }
70+
71+ var response map [string ]interface {}
72+ if err := json .Unmarshal (body , & response ); err != nil {
73+ return fmt .Errorf ("failed to parse response: %w" , err )
74+ }
75+
76+ if resp .StatusCode != http .StatusOK {
77+ if errorCode , hasError := response ["errorCode" ]; hasError {
78+ message := ""
79+ if msg , ok := response ["message" ].(string ); ok {
80+ message = msg
81+ }
82+ if errorCode == "oauth.invalid_client_secret" {
83+ return fmt .Errorf ("Sophos authentication failed: Invalid Client Secret" )
84+ }
85+ if errorCode == "oauth.invalid_client_id" {
86+ return fmt .Errorf ("Sophos authentication failed: Invalid Client ID" )
87+ }
88+ return fmt .Errorf ("Sophos authentication failed: %v - %s" , errorCode , message )
89+ }
90+ if errorCode , hasError := response ["error" ]; hasError {
91+ errorDesc := ""
92+ if desc , ok := response ["error_description" ].(string ); ok {
93+ errorDesc = desc
94+ }
95+ return fmt .Errorf ("Sophos authentication failed: %v - %s" , errorCode , errorDesc )
96+ }
97+ return fmt .Errorf ("Sophos authentication failed with status %d" , resp .StatusCode )
6098 }
6199
62100 accessToken , ok := response ["access_token" ].(string )
63101 if ! ok || accessToken == "" {
64- return catcher .Error ("Sophos credentials are invalid - no access token received" , nil , map [string ]any {
65- "response" : response ,
66- "status" : status ,
67- })
102+ var fields []string
103+ for k := range response {
104+ fields = append (fields , k )
105+ }
106+ return fmt .Errorf ("Sophos authentication failed: no access token received. Response fields: %v" , fields )
68107 }
69108
70109 return nil
0 commit comments