Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

master #2692

Merged
merged 6 commits into from
Nov 24, 2023
Merged

master #2692

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pro/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func InitPro() {
)
logic.EnterpriseCheckFuncs = append(logic.EnterpriseCheckFuncs, func() {
// == License Handling ==
ClearLicenseCache()
if err := ValidateLicense(); err != nil {
slog.Error(err.Error())
return
Expand Down
35 changes: 19 additions & 16 deletions pro/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ func AddLicenseHooks() {
Hook: ValidateLicense,
Interval: time.Hour,
}
logic.HookManagerCh <- models.HookDetails{
Hook: ClearLicenseCache,
Interval: time.Hour,
}
// logic.HookManagerCh <- models.HookDetails{
// Hook: ClearLicenseCache,
// Interval: time.Hour,
// }
}

// ValidateLicense - the initial and periodic license check for netmaker server
Expand Down Expand Up @@ -97,11 +97,14 @@ func ValidateLicense() (err error) {
return err
}

validationResponse, err := validateLicenseKey(encryptedData, tempPubKey)
validationResponse, timedOut, err := validateLicenseKey(encryptedData, tempPubKey)
if err != nil {
err = fmt.Errorf("failed to validate license key: %w", err)
return err
}
if timedOut {
return
}
if len(validationResponse) == 0 {
err = errors.New("empty validation response")
return err
Expand Down Expand Up @@ -185,12 +188,11 @@ func getLicensePublicKey(licensePubKeyEncoded string) (*[32]byte, error) {
return ncutils.ConvertBytesToKey(decodedPubKey)
}

func validateLicenseKey(encryptedData []byte, publicKey *[32]byte) ([]byte, error) {
func validateLicenseKey(encryptedData []byte, publicKey *[32]byte) ([]byte, bool, error) {
publicKeyBytes, err := ncutils.ConvertKeyToBytes(publicKey)
if err != nil {
return nil, err
return nil, false, err
}

msg := ValidateLicenseRequest{
LicenseKey: servercfg.GetLicenseKey(),
NmServerPubKey: base64encode(publicKeyBytes),
Expand All @@ -199,7 +201,7 @@ func validateLicenseKey(encryptedData []byte, publicKey *[32]byte) ([]byte, erro

requestBody, err := json.Marshal(msg)
if err != nil {
return nil, err
return nil, false, err
}

req, err := http.NewRequest(
Expand All @@ -208,15 +210,16 @@ func validateLicenseKey(encryptedData []byte, publicKey *[32]byte) ([]byte, erro
bytes.NewReader(requestBody),
)
if err != nil {
return nil, err
return nil, false, err
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
client := &http.Client{}
validateResponse, err := client.Do(req)
if err != nil { // check cache
slog.Warn("proceeding with cached response, Netmaker API may be down")
return getCachedResponse()
cachedResp, err := getCachedResponse()
return cachedResp, false, err
}
defer validateResponse.Body.Close()
code := validateResponse.StatusCode
Expand All @@ -226,12 +229,12 @@ func validateLicenseKey(encryptedData []byte, publicKey *[32]byte) ([]byte, erro
body, err := io.ReadAll(validateResponse.Body)
if err != nil {
slog.Warn("failed to parse response", "error", err)
return nil, err
return nil, false, err
}
if err := cacheResponse(body); err != nil {
slog.Warn("failed to cache response", "error", err)
}
return body, nil
return body, false, nil
}

// at this point the backend returned some undesired state
Expand All @@ -244,12 +247,12 @@ func validateLicenseKey(encryptedData []byte, publicKey *[32]byte) ([]byte, erro

// 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()
slog.Warn("Netmaker API may be down, will retry later...", "code", code)
return nil, true, nil
}

// at this point the error is irreversible, return it
return nil, err
return nil, false, err
}

func getAccountsHost() string {
Expand Down