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

NET-1842:initialize cache in startup #3231

Merged
merged 1 commit into from
Dec 4, 2024
Merged
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
2 changes: 1 addition & 1 deletion controllers/acls.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func getAcls(w http.ResponseWriter, r *http.Request) {
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
return
}
acls, err := logic.ListAcls(models.NetworkID(netID))
acls, err := logic.ListAclsByNetwork(models.NetworkID(netID))
if err != nil {
logger.Log(0, r.Header.Get("user"), "failed to get all network acl entries: ", err.Error())
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
Expand Down
18 changes: 9 additions & 9 deletions logic/acls.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func CreateDefaultAclNetworkPolicies(netID models.NetworkID) {
if netID.String() == "" {
return
}
_, _ = ListAcls(netID)
_, _ = ListAclsByNetwork(netID)
if !IsAclExists(fmt.Sprintf("%s.%s", netID, "all-nodes")) {
defaultDeviceAcl := models.Acl{
ID: fmt.Sprintf("%s.%s", netID, "all-nodes"),
Expand Down Expand Up @@ -106,7 +106,7 @@ func CreateDefaultAclNetworkPolicies(netID models.NetworkID) {

// DeleteDefaultNetworkPolicies - deletes all default network acl policies
func DeleteDefaultNetworkPolicies(netId models.NetworkID) {
acls, _ := ListAcls(netId)
acls, _ := ListAclsByNetwork(netId)
for _, acl := range acls {
if acl.NetworkID == netId && acl.Default {
DeleteAcl(acl)
Expand Down Expand Up @@ -347,7 +347,7 @@ func GetDefaultPolicy(netID models.NetworkID, ruleType models.AclPolicyType) (mo
return acl, nil
}
// check if there are any custom all policies
policies, _ := ListAcls(netID)
policies, _ := ListAclsByNetwork(netID)
for _, policy := range policies {
if !policy.Enabled {
continue
Expand All @@ -367,7 +367,7 @@ func GetDefaultPolicy(netID models.NetworkID, ruleType models.AclPolicyType) (mo
return acl, nil
}

func listAcls() (acls []models.Acl) {
func ListAcls() (acls []models.Acl) {
if servercfg.CacheEnabled() && len(aclCacheMap) > 0 {
return listAclFromCache()
}
Expand All @@ -393,7 +393,7 @@ func listAcls() (acls []models.Acl) {

// ListUserPolicies - lists all acl policies enforced on an user
func ListUserPolicies(u models.User) []models.Acl {
allAcls := listAcls()
allAcls := ListAcls()
userAcls := []models.Acl{}
for _, acl := range allAcls {

Expand All @@ -418,7 +418,7 @@ func ListUserPolicies(u models.User) []models.Acl {

// listPoliciesOfUser - lists all user acl policies applied to user in an network
func listPoliciesOfUser(user models.User, netID models.NetworkID) []models.Acl {
allAcls := listAcls()
allAcls := ListAcls()
userAcls := []models.Acl{}
for _, acl := range allAcls {
if acl.NetworkID == netID && acl.RuleType == models.UserPolicy {
Expand Down Expand Up @@ -447,7 +447,7 @@ func listPoliciesOfUser(user models.User, netID models.NetworkID) []models.Acl {

// listDevicePolicies - lists all device policies in a network
func listDevicePolicies(netID models.NetworkID) []models.Acl {
allAcls := listAcls()
allAcls := ListAcls()
deviceAcls := []models.Acl{}
for _, acl := range allAcls {
if acl.NetworkID == netID && acl.RuleType == models.DevicePolicy {
Expand All @@ -458,9 +458,9 @@ func listDevicePolicies(netID models.NetworkID) []models.Acl {
}

// ListAcls - lists all acl policies
func ListAcls(netID models.NetworkID) ([]models.Acl, error) {
func ListAclsByNetwork(netID models.NetworkID) ([]models.Acl, error) {

allAcls := listAcls()
allAcls := ListAcls()
netAcls := []models.Acl{}
for _, acl := range allAcls {
if acl.NetworkID == netID {
Expand Down
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ func initialize() { // Client Mode Prereq Check
logger.FatalLog("Error connecting to database: ", err.Error())
}
logger.Log(0, "database successfully connected")

//initialize cache
_, _ = logic.GetNetworks()
_, _ = logic.GetAllNodes()
_, _ = logic.GetAllHosts()
_, _ = logic.GetAllExtClients()
_ = logic.ListAcls()
_, _ = logic.GetAllEnrollmentKeys()

migrate.Run()

logic.SetJWTSecret()
Expand Down
2 changes: 0 additions & 2 deletions migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (

// Run - runs all migrations
func Run() {
_, _ = logic.GetAllNodes()
_, _ = logic.GetAllHosts()
updateEnrollmentKeys()
assignSuperAdmin()
createDefaultTagsAndPolicies()
Expand Down