Pārlūkot izejas kodu

feat(go): add idp sync interval setting;

Vishal Dalwadi 4 mēneši atpakaļ
vecāks
revīzija
d769c83d76
6 mainītis faili ar 34 papildinājumiem un 23 dzēšanām
  1. 9 0
      controllers/server.go
  2. 11 0
      logic/settings.go
  3. 1 0
      models/settings.go
  4. 12 2
      pro/auth/sync.go
  5. 1 1
      pro/initialize.go
  6. 0 20
      servercfg/serverconf.go

+ 9 - 0
controllers/server.go

@@ -3,6 +3,7 @@ package controller
 import (
 	"encoding/json"
 	"errors"
+	"github.com/gravitl/netmaker/pro/auth"
 	"net/http"
 	"os"
 	"strings"
@@ -264,11 +265,19 @@ func updateSettings(w http.ResponseWriter, r *http.Request) {
 		logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("invalid settings"), "badrequest"))
 		return
 	}
+
+	currSettings := logic.GetServerSettings()
+
 	err := logic.UpsertServerSettings(req)
 	if err != nil {
 		logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("failed to udpate server settings "+err.Error()), "internal"))
 		return
 	}
+
+	if currSettings.IDPSyncInterval != req.IDPSyncInterval {
+		auth.ResetSyncHook()
+	}
+
 	go mq.PublishPeerUpdate(false)
 	logic.ReturnSuccessResponseWithJson(w, r, req, "updated server settings successfully")
 }

+ 11 - 0
logic/settings.go

@@ -271,6 +271,17 @@ func GetAzureTenant() string {
 	return GetServerSettings().AzureTenant
 }
 
+// GetIDPSyncInterval returns the interval at which the netmaker should sync
+// data from IDP.
+func GetIDPSyncInterval() time.Duration {
+	syncInterval, err := time.ParseDuration(GetServerSettings().IDPSyncInterval)
+	if err != nil {
+		return 24 * time.Hour
+	}
+
+	return syncInterval
+}
+
 // GetMetricsPort - get metrics port
 func GetMetricsPort() int {
 	return GetServerSettings().MetricsPort

+ 1 - 0
models/settings.go

@@ -21,6 +21,7 @@ type ServerSettings struct {
 	AzureTenant                string   `json:"azure_tenant"`
 	UserFilters                []string `json:"user_filters"`
 	GroupFilters               []string `json:"group_filters"`
+	IDPSyncInterval            string   `json:"idp_sync_interval"`
 	Telemetry                  string   `json:"telemetry"`
 	BasicAuth                  bool     `json:"basic_auth"`
 	JwtValidityDuration        int      `json:"jwt_validity_duration"`

+ 12 - 2
pro/auth/sync.go

@@ -9,13 +9,16 @@ import (
 	"github.com/gravitl/netmaker/pro/idp/azure"
 	"github.com/gravitl/netmaker/pro/idp/google"
 	proLogic "github.com/gravitl/netmaker/pro/logic"
-	"github.com/gravitl/netmaker/servercfg"
 	"strings"
 	"time"
 )
 
+var syncTicker *time.Ticker
+
 func StartSyncHook() {
-	for range time.Tick(servercfg.GetIDPSyncInterval()) {
+	syncTicker = time.NewTicker(logic.GetIDPSyncInterval())
+
+	for range syncTicker.C {
 		err := SyncFromIDP()
 		if err != nil {
 			logger.Log(0, "failed to sync from idp: ", err.Error())
@@ -25,6 +28,13 @@ func StartSyncHook() {
 	}
 }
 
+func ResetSyncHook() {
+	if syncTicker != nil {
+		syncTicker.Stop()
+		go StartSyncHook()
+	}
+}
+
 func SyncFromIDP() error {
 	settings := logic.GetServerSettings()
 	if !settings.SyncEnabled {

+ 1 - 1
pro/initialize.go

@@ -91,7 +91,7 @@ func InitPro() {
 			slog.Error("no OAuth provider found or not configured, continuing without OAuth")
 		}
 
-		auth.StartSyncHook()
+		go auth.StartSyncHook()
 
 		proLogic.LoadNodeMetricsToCache()
 		proLogic.InitFailOverCache()

+ 0 - 20
servercfg/serverconf.go

@@ -623,26 +623,6 @@ func GetAzureTenant() string {
 	return azureTenant
 }
 
-// GetIDPSyncInterval returns the interval at which the netmaker should sync
-// data from IDP.
-func GetIDPSyncInterval() time.Duration {
-	if os.Getenv("IDP_SYNC_INTERVAL") != "" {
-		syncInterval, err := time.ParseDuration(os.Getenv("IDP_SYNC_INTERVAL"))
-		if err != nil {
-			return 24 * time.Hour
-		}
-
-		return syncInterval
-	} else {
-		syncInterval, err := time.ParseDuration(config.Config.Server.IDPSyncInterval)
-		if err != nil {
-			return 24 * time.Hour
-		}
-
-		return syncInterval
-	}
-}
-
 // GetMqPassword - fetches the MQ password
 func GetMqPassword() string {
 	password := ""