-
Notifications
You must be signed in to change notification settings - Fork 63
feat(api): expose go api for third party apps #282
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
Open
AndreyLevchenko
wants to merge
14
commits into
main
Choose a base branch
from
dev
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
17a69c9
Main & postee API merge (#263)
elad-da 098b20f
Merge branch 'main' of github.com:aquasecurity/postee into dev
AndreyLevchenko 8d548cb
fix tests and linter errors
AndreyLevchenko 2ac2f7d
add missed test
AndreyLevchenko 29e2528
remove PostgreSQL references from README.me
AndreyLevchenko 8768a4d
fix tests to avoid using default path '/server/..', remove router.bol…
AndreyLevchenko 2edb888
rename test methods, clean up not useful code
AndreyLevchenko f0f5488
fix db size limit measurement unit, fix test
AndreyLevchenko 75c2c11
fix typo
AndreyLevchenko e8977c4
move existing method to logger package
AndreyLevchenko bc4dfe1
Merge branch 'main' of github.com:aquasecurity/postee into dev
AndreyLevchenko b8cb265
adopted latest changes
AndreyLevchenko bfbacda
removed go.mod and go.sum from spell checking
AndreyLevchenko 1a3bb16
Embed rego templates (#291)
elad-da File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package router | ||
| package data | ||
|
|
||
| type OutputSettings struct { | ||
| Name string `json:"name,omitempty"` | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package router | ||
| package data | ||
|
|
||
| type Template struct { | ||
| Name string `json:"name"` | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package boltdb | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "github.com/aquasecurity/postee/v2/dbservice/dbparam" | ||
| ) | ||
|
|
||
| func (boltDb *BoltDb) MayBeStoreMessage(message []byte, messageKey string, expired *time.Time) (wasStored bool, err error) { | ||
| boltDb.mu.Lock() | ||
| defer boltDb.mu.Unlock() | ||
|
|
||
| db := boltDb.db | ||
|
|
||
| if err = Init(db, dbparam.DbBucketName); err != nil { | ||
| return false, err | ||
| } | ||
| if err = Init(db, dbparam.DbBucketExpiryDates); err != nil { | ||
| return false, err | ||
| } | ||
|
|
||
| currentValue, err := dbSelect(db, dbparam.DbBucketName, messageKey) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
|
|
||
| if currentValue != nil { | ||
| return false, nil | ||
| } else { | ||
| bMessageKey := []byte(messageKey) | ||
| err = dbInsert(db, dbparam.DbBucketName, bMessageKey, message) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| if expired != nil { | ||
| err = dbInsert(db, dbparam.DbBucketExpiryDates, []byte(expired.Format(dbparam.DateFmt)), bMessageKey) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| } | ||
| return true, nil | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package boltdb | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "sync" | ||
|
|
||
| "github.com/aquasecurity/postee/v2/log" | ||
| bolt "go.etcd.io/bbolt" | ||
| ) | ||
|
|
||
| const ( | ||
| DEFAULT_PATH = "/server/database/webhooks.db" | ||
| ) | ||
|
|
||
| type BoltDb struct { | ||
| mu sync.Mutex | ||
| DbPath string | ||
| db *bolt.DB | ||
| } | ||
|
|
||
| func NewBoltDb(paths ...string) (*BoltDb, error) { | ||
| dbPath := DEFAULT_PATH | ||
| if len(paths) > 0 { | ||
| if paths[0] != "" { | ||
| dbPath = paths[0] | ||
| } | ||
| } | ||
|
|
||
| log.Logger.Infof("Open Bolt DB at %s", dbPath) | ||
| dbConn, err := open(dbPath) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to open bolt DB file: (%s) %w", dbPath, err) | ||
| } | ||
|
|
||
| return &BoltDb{ | ||
| db: dbConn, | ||
| DbPath: dbPath, | ||
| }, nil | ||
| } | ||
|
|
||
| func open(path string) (*bolt.DB, error) { | ||
| if _, err := os.Stat(path); os.IsNotExist(err) { | ||
| err = os.MkdirAll(filepath.Dir(path), os.ModePerm) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
||
| return bolt.Open(path, 0666, nil) | ||
| } | ||
|
|
||
| func (boltDb *BoltDb) ChangeDbPath(newPath string) error { | ||
| boltDb.mu.Lock() | ||
| defer boltDb.mu.Unlock() | ||
| boltDb.DbPath = newPath | ||
|
|
||
| if boltDb.db != nil { | ||
| boltDb.db.Close() | ||
| } | ||
|
|
||
| dbConn, err := bolt.Open(newPath, 0666, nil) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to open bolt DB file: (%s) %w", newPath, err) | ||
| } | ||
|
|
||
| boltDb.db = dbConn | ||
| return nil | ||
| } | ||
|
|
||
| func (boltDb *BoltDb) Close() error { | ||
| boltDb.mu.Lock() | ||
| defer boltDb.mu.Unlock() | ||
| return boltDb.db.Close() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package boltdb | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestChangeDbPath(t *testing.T) { | ||
| boltDb, err := NewBoltDb() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| defer boltDb.Close() | ||
| testPath := "/tmp/test.db" | ||
| storedPath := boltDb.DbPath | ||
| _ = boltDb.ChangeDbPath(testPath) | ||
| defer func() { | ||
| _ = boltDb.ChangeDbPath(storedPath) | ||
| }() | ||
| if boltDb.DbPath != testPath { | ||
| t.Errorf("path is not configured correctly, expected: %s, got %s", testPath, boltDb.DbPath) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package boltdb | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "time" | ||
|
|
||
| "github.com/aquasecurity/postee/v2/dbservice/dbparam" | ||
| "github.com/aquasecurity/postee/v2/log" | ||
| bolt "go.etcd.io/bbolt" | ||
| ) | ||
|
|
||
| func (boltDb *BoltDb) CheckSizeLimit() { | ||
| if dbparam.DbSizeLimit == 0 { | ||
| return | ||
| } | ||
| boltDb.mu.Lock() | ||
| defer boltDb.mu.Unlock() | ||
|
|
||
| db := boltDb.db | ||
|
|
||
| if err := db.Update(func(tx *bolt.Tx) error { | ||
| b := tx.Bucket([]byte(dbparam.DbBucketName)) | ||
| if b == nil { | ||
| return nil | ||
| } | ||
| c := b.Cursor() | ||
| size := 0 | ||
| for k, v := c.First(); k != nil; k, v = c.Next() { | ||
| size += len(v) | ||
| } | ||
| if size > dbparam.DbSizeLimit { | ||
| return tx.DeleteBucket([]byte(dbparam.DbBucketName)) | ||
| } | ||
| return nil | ||
| }); err != nil { | ||
| log.Logger.Errorf("Unable to delete bucket: %v", err) | ||
| return | ||
| } | ||
| } | ||
|
|
||
| func (boltDb *BoltDb) CheckExpiredData() { | ||
| boltDb.mu.Lock() | ||
| defer boltDb.mu.Unlock() | ||
|
|
||
| db := boltDb.db | ||
|
|
||
| expired, err := boltDb.getExpired(db) | ||
| if err != nil { | ||
| log.Logger.Errorf("Can't select expired data: %v", err) | ||
| return | ||
| } | ||
|
|
||
| if err := dbDelete(db, dbparam.DbBucketName, expired); err != nil { | ||
| log.Logger.Errorf("Can't remove expired data: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func (boltDb *BoltDb) getExpired(db *bolt.DB) (keys [][]byte, err error) { | ||
| keys = [][]byte{} | ||
| ttlKeys := [][]byte{} | ||
|
|
||
| if err = db.View(func(tx *bolt.Tx) error { | ||
| b := tx.Bucket([]byte(dbparam.DbBucketExpiryDates)) | ||
| if b == nil { | ||
| return nil | ||
| } | ||
| c := b.Cursor() | ||
|
|
||
| max := []byte(time.Now().UTC().Format(dbparam.DateFmt)) //remove expired records | ||
| for k, v := c.First(); k != nil && bytes.Compare(k, max) <= 0; k, v = c.Next() { | ||
| keys = append(keys, v) | ||
| ttlKeys = append(ttlKeys, k) | ||
| } | ||
| return nil | ||
| }); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if err = dbDelete(db, dbparam.DbBucketExpiryDates, ttlKeys); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we get the size in bytes, but readme says Mb.