mirror of
https://github.com/gravitl/netmaker.git
synced 2024-11-10 17:48:25 +08:00
[NET-687] On license validation fail, parse and store the message of the response (#2669)
* On license validation fail, parse and store the message of the response * Return cached response body when necessary * Have license validation return proper val,err
This commit is contained in:
parent
27378b9c52
commit
0a88fdebd9
1 changed files with 31 additions and 20 deletions
|
@ -213,35 +213,46 @@ func validateLicenseKey(encryptedData []byte, publicKey *[32]byte) ([]byte, erro
|
|||
req.Header.Add("Content-Type", "application/json")
|
||||
req.Header.Add("Accept", "application/json")
|
||||
client := &http.Client{}
|
||||
var body []byte
|
||||
validateResponse, err := client.Do(req)
|
||||
if err != nil { // check cache
|
||||
body, err = getCachedResponse()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
slog.Warn("proceeding with cached response, Netmaker API may be down")
|
||||
} else {
|
||||
defer validateResponse.Body.Close()
|
||||
if validateResponse.StatusCode != http.StatusOK {
|
||||
err := fmt.Errorf("could not validate license, got status code %d", validateResponse.StatusCode)
|
||||
// if it's a temp error, just log it, don't consider license invalid
|
||||
if validateResponse.StatusCode == http.StatusServiceUnavailable ||
|
||||
validateResponse.StatusCode == http.StatusGatewayTimeout {
|
||||
slog.Warn(err.Error())
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
} // if you received a 200 cache the response locally
|
||||
return getCachedResponse()
|
||||
}
|
||||
defer validateResponse.Body.Close()
|
||||
code := validateResponse.StatusCode
|
||||
|
||||
body, err = io.ReadAll(validateResponse.Body)
|
||||
// if we received a 200, cache the response locally
|
||||
if code == http.StatusOK {
|
||||
body, err := io.ReadAll(validateResponse.Body)
|
||||
if err != nil {
|
||||
slog.Warn("failed to parse response", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
cacheResponse(body)
|
||||
if err := cacheResponse(body); err != nil {
|
||||
slog.Warn("failed to cache response", "error", err)
|
||||
}
|
||||
return body, nil
|
||||
}
|
||||
|
||||
return body, err
|
||||
// at this point the backend returned some undesired state
|
||||
|
||||
// inform failure via logs
|
||||
err = fmt.Errorf("could not validate license with validation backend, got status code %d", validateResponse.StatusCode)
|
||||
slog.Warn(err.Error())
|
||||
body, err := io.ReadAll(validateResponse.Body)
|
||||
if err != nil {
|
||||
slog.Warn(err.Error())
|
||||
}
|
||||
slog.Warn("license-validation backend response: %s", string(body))
|
||||
|
||||
// try to use cache if we had a temporary error
|
||||
if code == http.StatusServiceUnavailable || code == http.StatusGatewayTimeout {
|
||||
slog.Warn("proceeding with cached response, Netmaker API may be down")
|
||||
return getCachedResponse()
|
||||
}
|
||||
|
||||
// at this point the error is irreversible, return it
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func getAccountsHost() string {
|
||||
|
|
Loading…
Reference in a new issue