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

Release/v0.7.3 #94

Merged
merged 25 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Update log msgs from spec updated in API mode
  • Loading branch information
Nikolay Tkachenko committed Apr 26, 2024
commit 193c8579d91de30b0cdbcfe2ba9223ee3356db42
26 changes: 20 additions & 6 deletions cmd/api-firewall/internal/updater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package updater
import (
"os"
"runtime/debug"
"slices"
"sync"
"time"

Expand All @@ -15,6 +16,10 @@ import (
"github.com/wallarm/api-firewall/internal/platform/database"
)

const (
logPrefix = "Regular OpenAPI specification updater"
)

type Updater interface {
Start() error
Shutdown() error
Expand Down Expand Up @@ -74,32 +79,41 @@ func (s *Specification) Run() {
// load new schemes
newSpecDB, err := s.Load()
if err != nil {
s.logger.WithFields(logrus.Fields{"error": err}).Error("Updating OpenAPI specification")
s.logger.WithFields(logrus.Fields{"error": err}).Errorf("%s: loading specifications", logPrefix)
continue
}

// do not downgrade the db version
if s.sqlLiteStorage.Version() > newSpecDB.Version() {
s.logger.Error("Regular update checker: version of the new DB structure is lower then current one (V2)")
s.logger.Errorf("%s: version of the new DB structure is lower then current one (V2)", logPrefix)
continue
}

if s.sqlLiteStorage.ShouldUpdate(newSpecDB) {
s.logger.Debugf("OpenAPI specifications has been updated. The schemas with the following IDs were updated: %v", newSpecDB.SchemaIDs())
s.logger.Debugf("%s: OpenAPI specifications with the following IDs were updated: %v", logPrefix, newSpecDB.SchemaIDs())

// find new IDs and log them
newScemaIDs := newSpecDB.SchemaIDs()
oldSchemaIDs := s.sqlLiteStorage.SchemaIDs()
for _, ns := range newScemaIDs {
if !slices.Contains(oldSchemaIDs, ns) {
s.logger.Infof("%s: fetched new OpenAPI specification from the database with id: %d", logPrefix, ns)
}
}

s.lock.Lock()
s.sqlLiteStorage = newSpecDB
s.api.Handler = handlersAPI.Handlers(s.lock, s.cfg, s.shutdown, s.logger, s.sqlLiteStorage, s.allowedIPCache, s.waf)
s.health.OpenAPIDB = s.sqlLiteStorage
if err := s.sqlLiteStorage.AfterLoad(s.cfg.PathToSpecDB); err != nil {
s.logger.WithFields(logrus.Fields{"error": err}).Error("Regular update checker: error in after specification loading function")
s.logger.WithFields(logrus.Fields{"error": err}).Errorf("%s: error in after specification loading function", logPrefix)
}
s.lock.Unlock()

continue
}

s.logger.Debugf("regular update checker: new OpenAPI specifications not found")
s.logger.Debugf("%s: new OpenAPI specifications not found", logPrefix)
case <-s.stop:
updateTicker.Stop()
return
Expand All @@ -117,7 +131,7 @@ func (s *Specification) Start() error {

// Shutdown function stops update process
func (s *Specification) Shutdown() error {
defer s.logger.Infof("specification updater: stopped")
defer s.logger.Infof("%s: stopped", logPrefix)

// close worker and finish Start function
for i := 0; i < 2; i++ {
Expand Down
Binary file modified cmd/api-firewall/internal/updater/wallarm_api2_update.db
Binary file not shown.