Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
672d4ed
feat(api): add API draft
AndreyLevchenko Aug 9, 2021
1a6b8dd
feat(api): add API draft
AndreyLevchenko Aug 12, 2021
7964586
added output api implementation
AndreyLevchenko Aug 14, 2021
cf1389d
misc clean up
AndreyLevchenko Aug 14, 2021
1358020
added routes and templates
AndreyLevchenko Aug 14, 2021
581bbb9
switched API to synchronous mode
AndreyLevchenko Aug 18, 2021
4c74bff
fixed typo
AndreyLevchenko Aug 19, 2021
a2d28de
added simple integration test
AndreyLevchenko Aug 19, 2021
da3dae1
updated go version
AndreyLevchenko Aug 19, 2021
e47211b
fixed tests
AndreyLevchenko Aug 19, 2021
26ba2db
added fix to set dbpath through API
AndreyLevchenko Aug 23, 2021
31c55ca
added input callbacks & tests
AndreyLevchenko Aug 24, 2021
3088306
fixed race condition
AndreyLevchenko Aug 24, 2021
b75ef77
removed not needed test
AndreyLevchenko Aug 24, 2021
d4a68d7
added misc fix to clean up input callbacks
AndreyLevchenko Aug 24, 2021
3256a6f
fixed pointer reference error in loop
AndreyLevchenko Nov 30, 2021
a41a408
Merge branch 'main' of github.com:aquasecurity/postee into postee-as-…
AndreyLevchenko Nov 30, 2021
aea2507
added default db path
AndreyLevchenko Nov 30, 2021
75895b1
fixed TODO in Jira outputs
AndreyLevchenko Nov 30, 2021
01e893d
feat(postgres): added support postgres
DmitriyLewen Nov 30, 2021
c0099b0
feat(postgresDb): add test connect psql, change cfg.yaml
DmitriyLewen Dec 1, 2021
f267246
feat(psql) changed configurate psql
DmitriyLewen Dec 1, 2021
0a36fb0
combined Webhooktable and WebhookExpiryDates
DmitriyLewen Dec 3, 2021
bc3573a
Refactor: code review notes are corrected
DmitriyLewen Dec 3, 2021
04f8f21
Refactor: code review notes are corrected
DmitriyLewen Dec 6, 2021
90c2311
Refactor : added вудуеув temp dir in configureDb test
DmitriyLewen Dec 6, 2021
ccfec36
Docs(psql): added psql env info to readme
DmitriyLewen Dec 6, 2021
f9473c3
refactor: change parametrs for deleteRowsByIdAndTime
DmitriyLewen Dec 7, 2021
ec36e3b
Refactor: code review notes are corrected
DmitriyLewen Dec 7, 2021
d1a3768
Merge commit 'refs/pull/207/head' of github.com:aquasecurity/postee i…
AndreyLevchenko Dec 7, 2021
38d1929
clean up of db connection config
AndreyLevchenko Dec 7, 2021
ac3f836
exposed API to use postgres
AndreyLevchenko Dec 8, 2021
de297df
feat:added copy of cfgFile in psql
DmitriyLewen Dec 13, 2021
f40b5b7
test: changed tests for insert into psql
DmitriyLewen Dec 13, 2021
47b581b
test: added tests for api
DmitriyLewen Dec 14, 2021
e04c940
test: added tests for configure psql
DmitriyLewen Dec 15, 2021
aca7bf3
Refactor: code review notes are corrected
DmitriyLewen Dec 15, 2021
abd381f
Merge branch 'main' of github.com:aquasecurity/postee into postee-as-…
AndreyLevchenko Dec 15, 2021
164c3f7
added make target for golang-lint
AndreyLevchenko Dec 15, 2021
909bdcf
Merge pull request #215 from DmitriyLewen/add-config-in-psql
AndreyLevchenko Dec 15, 2021
c3e2a86
Refactor: code review notes are corrected
DmitriyLewen Dec 16, 2021
d19f583
Merge branch 'postee-as-api-2' into api-2-tests
DmitriyLewen Dec 16, 2021
4011466
Refactor: code review notes are corrected
DmitriyLewen Dec 17, 2021
b3877b1
refactor: changed func load
DmitriyLewen Dec 17, 2021
5018fa0
Merge pull request #221 from DmitriyLewen/change-load-tenant
AndreyLevchenko Dec 21, 2021
b726ea1
misc api cleanup
AndreyLevchenko Dec 21, 2021
529f714
Merge branch 'main' of github.com:aquasecurity/postee into postee-as-…
AndreyLevchenko Dec 21, 2021
e4d3d8f
Merge pull request #219 from DmitriyLewen/api-2-tests
AndreyLevchenko Dec 21, 2021
7321b5b
Merge branch 'postee-as-api-2' of github.com:aquasecurity/postee into…
AndreyLevchenko Dec 21, 2021
77e762c
test: fixed api tests
DmitriyLewen Dec 21, 2021
4671663
test: fixed api tests
DmitriyLewen Dec 21, 2021
a504849
Merge pull request #223 from DmitriyLewen/postee-as-api-2
AndreyLevchenko Dec 24, 2021
2e2c32c
feat(logger): added default logger
DmitriyLewen Dec 24, 2021
2b73ecc
fix: fixed golangci-lint test errors
DmitriyLewen Dec 24, 2021
6e628c2
Merge pull request #225 from DmitriyLewen/fix-lint
AndreyLevchenko Dec 24, 2021
983832a
fix: fixed golangci-lint test errors
DmitriyLewen Dec 24, 2021
9e833da
Merge pull request #226 from DmitriyLewen/fix-lint
AndreyLevchenko Dec 24, 2021
15f4b9d
feat: added custom logger
DmitriyLewen Dec 28, 2021
20bb346
Merge branch 'postee-as-api-2' into add-logger
DmitriyLewen Dec 28, 2021
ff2528f
fix: fixed load psql cfg with empty table
DmitriyLewen Dec 29, 2021
cc9e31d
fix: fixed load psql cfg with empty table
DmitriyLewen Dec 29, 2021
8385ca4
Merge pull request #230 from DmitriyLewen/fix-load-empty-cfg-table
AndreyLevchenko Dec 29, 2021
9e40f3d
test: added test for errors to getCfgCacheSource
DmitriyLewen Dec 29, 2021
0d663fc
test: added test for errors to getCfgCacheSource
DmitriyLewen Dec 29, 2021
57a0e79
fix: fixed lint error
DmitriyLewen Dec 30, 2021
17864ab
Refactor: code review notes are corrected
DmitriyLewen Dec 30, 2021
dc501c6
Refactor: code review notes are corrected
DmitriyLewen Dec 30, 2021
31032c2
refactor: removed func debug
DmitriyLewen Dec 30, 2021
58bfd4a
refactor: removed func debug
DmitriyLewen Dec 30, 2021
e18162b
Merge pull request #231 from DmitriyLewen/fix-load-empty-cfg-table
AndreyLevchenko Jan 4, 2022
ca2bad4
chore(deps): added zap dependency
DmitriyLewen Jan 12, 2022
74112e7
Merge branch 'postee-as-api-2' into add-logger
DmitriyLewen Jan 12, 2022
ab5cd21
Merge pull request #229 from DmitriyLewen/add-logger
AndreyLevchenko Jan 17, 2022
50f31c9
fix: fixed test errors (#228)
DmitriyLewen Jan 26, 2022
b6d6300
reuse postgres db conncetion instance
elad-da Jan 30, 2022
17027cc
adjust unit test for Postgres singleton and retrying functionality
elad-da Jan 30, 2022
a68683d
Merge pull request #251 from elad-da/reuse-pg-conn
Jan 31, 2022
e1fae4c
Router | remove redundant rego match check
Jan 30, 2022
a788cf7
Evaluate rego rule once for every route
Jan 31, 2022
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
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ fmt :
test :
go test -race -coverprofile=coverage.txt -covermode=atomic ./router ./msgservice ./dbservice ./formatting ./data ./regoservice ./routes

lint :
golangci-lint run

cover :
go test ./msgservice ./dbservice ./router ./formatting ./data ./regoservice ./routes -v -coverprofile=cover.out
go tool cover -html=cover.out
Expand Down
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -448,15 +448,18 @@ See [Postee UI](PosteeUI.md) for details how to setup the Postee UI.
## Misc

### Data Persistency
The Postee container uses BoltDB to store information about previously scanned images.
The Postee container uses BoltDB or PostgreSQL to store information about previously scanned images.
This is used to prevent resending messages that were already sent before.
The size of the database can grow over time. Every image that is saved in the database uses 20K of storage.

Postee supports ‘PATH_TO_DB’ environment variable to change the database directory. To use, set the ‘PATH_TO_DB’ environment variable to point to the database file, for example: PATH_TO_DB="./database/webhook.db". By default, the directory for the database file is “/server/database/webhook.db”.
The size of the database can grow over time. Every image that is saved in the Bolt database uses 20K of storage.
The default Postee Database is BoltDb.

Postee supports ‘PATH_TO_DB’ environment variable to change the bolt database directory. To use, set the ‘PATH_TO_DB’ environment variable to point to the bolt database file, for example: PATH_TO_DB="./database/webhook.db". By default, the directory for the bolt database file is “/server/database/webhook.db”.

If you would like to persist the database file between restarts of the Postee container, then you should
use a persistent storage option to mount the "/server/database" directory of the container.
The "deploy/kubernetes" directory in this project contains an example deployment that includes a basic Host Persistency.

To use PostgreSQL set the 'POSTGRES_URL' environment variable to your [connection URI](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING). If you would like to connect 2 or more Postee to 1 PostgreSQL to use unique tenant name in postee config file.

### Using environment variables in Postee Configuration File
Postee supports use of environment variables for *Output* fields: **User**, **Password** and **Token**. Add preffix `$` to the environment variable name in the configuration file, for example:
Expand Down
2 changes: 1 addition & 1 deletion router/integrations.go → data/integrations.go
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"`
Expand Down
2 changes: 1 addition & 1 deletion router/template.go → data/template.go
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"`
Expand Down
3 changes: 2 additions & 1 deletion router/tenants.go → data/tenants.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package router
package data

import (
"github.com/aquasecurity/postee/routes"
)

type TenantSettings struct {
Name string `json:"name,omitempty"`
AquaServer string `json:"aqua-server,omitempty"`
DBMaxSize int `json:"max-db-size,omitempty"`
DBRemoveOldData int `json:"delete-old-data,omitempty"`
Expand Down
6 changes: 6 additions & 0 deletions data/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ func ClearField(source string) string {
re := regexp.MustCompile(`[[:cntrl:]]|[\x{FFFD}]`)
return re.ReplaceAllString(source, "")
}

func CopyStringArray(src []string) []string {
dst := make([]string, len(src))
copy(dst, src)
return dst
}
10 changes: 10 additions & 0 deletions data/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package data

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestClearField(t *testing.T) {
Expand All @@ -19,3 +21,11 @@ func TestClearField(t *testing.T) {
}
}
}

func TestCopyStringArray(t *testing.T) {
src := []string{"a", "b", "c"}
dst := CopyStringArray(src)
dst[0] = "x"
assert.Equal(t, "a", src[0], "TestCopyStringArray")
assert.Equal(t, "x", dst[0], "TestCopyStringArray")
}
48 changes: 0 additions & 48 deletions dbservice/actions.go

This file was deleted.

49 changes: 49 additions & 0 deletions dbservice/boltdb/actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package boltdb

import (
"time"

"github.com/aquasecurity/postee/dbservice/dbparam"
bolt "go.etcd.io/bbolt"
)

func (boltDb *BoltDb) MayBeStoreMessage(message []byte, messageKey string, expired *time.Time) (wasStored bool, err error) {
mutex.Lock()
defer mutex.Unlock()

db, err := bolt.Open(boltDb.DbPath, 0666, nil)
if err != nil {
return false, err
}
defer db.Close()

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
}

}
49 changes: 49 additions & 0 deletions dbservice/boltdb/boltdb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package boltdb

import (
"os"
"path/filepath"
"sync"
)

var (
mutex sync.Mutex
)

type BoltDb struct {
DbPath string
}

func NewBoltDb() *BoltDb {
return &BoltDb{
DbPath: "/server/database/webhooks.db",
}
}

func (boltDb *BoltDb) ChangeDbPath(newPath string) {
mutex.Lock()
boltDb.DbPath = newPath
mutex.Unlock()
}

func (boltDb *BoltDb) SetNewDbPath(newPath string) error {
if newPath != "" {
if _, err := os.Stat(newPath); err != nil {
if os.IsNotExist(err) {
err = os.MkdirAll(filepath.Dir(newPath), os.ModePerm)
if err != nil {
return err
}
} else {
return err
}
}
boltDb.ChangeDbPath(newPath)
}
return nil
}

// unimplemented
func (boltDb *BoltDb) Close() error {
return nil
}
18 changes: 18 additions & 0 deletions dbservice/boltdb/changedbpath_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package boltdb

import (
"testing"
)

func TestChangeDbPath(t *testing.T) {
boltDb := NewBoltDb()
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)
}
}
94 changes: 94 additions & 0 deletions dbservice/boltdb/checker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package boltdb

import (
"bytes"
"time"

"github.com/aquasecurity/postee/dbservice/dbparam"
"github.com/aquasecurity/postee/log"
bolt "go.etcd.io/bbolt"
)

func (boltDb *BoltDb) CheckSizeLimit() {
if dbparam.DbSizeLimit == 0 {
return
}
mutex.Lock()
defer mutex.Unlock()

db, err := bolt.Open(boltDb.DbPath, 0666, nil)
if err != nil {
log.Logger.Errorf("CheckSizeLimit: Can't open db: %s", boltDb.DbPath)
return
}
defer db.Close()

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() {
mutex.Lock()
defer mutex.Unlock()

db, err := bolt.Open(boltDb.DbPath, 0666, nil)
if err != nil {
log.Logger.Errorf("CheckExpiredData: Can't open db: %s", boltDb.DbPath)
return
}
defer db.Close()

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
}
Loading