Skip to content

Commit 342b081

Browse files
committed
Allow disable audit log to DB by Env when initialize
Signed-off-by: my036811 <miner.yang@broadcom.com>
1 parent c122380 commit 342b081

File tree

11 files changed

+136
-0
lines changed

11 files changed

+136
-0
lines changed

src/core/main.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ import (
2222
"net/url"
2323
"os"
2424
"os/signal"
25+
"strconv"
2526
"strings"
2627
"syscall"
2728
"time"
2829

2930
"github.com/beego/beego/v2/server/web"
3031

32+
"github.com/goharbor/harbor/src/common"
3133
"github.com/goharbor/harbor/src/common/dao"
3234
common_http "github.com/goharbor/harbor/src/common/http"
3335
configCtl "github.com/goharbor/harbor/src/controller/config"
@@ -222,6 +224,11 @@ func main() {
222224
log.Error(err)
223225
}
224226

227+
// Allow user to disable writing audit log to db by env while initialize
228+
if err := initSkipAuditDBbyEnv(ctx); err != nil {
229+
log.Errorf("Failed to initialize SkipAuditDB by ENV: %v", err)
230+
}
231+
225232
// Init API handler
226233
if err := api.Init(); err != nil {
227234
log.Fatalf("Failed to initialize API handlers with error: %s", err.Error())
@@ -356,3 +363,34 @@ func getDefaultScannerName() string {
356363
}
357364
return ""
358365
}
366+
367+
func initSkipAuditDBbyEnv(ctx context.Context) error {
368+
var err error
369+
skipAuditEnv := false
370+
s := os.Getenv("SKIP_LOG_AUDIT_DATABASE")
371+
if s != "" {
372+
skipAuditEnv, err = strconv.ParseBool(s)
373+
if err != nil {
374+
log.Warningf("Failed to parse SKIP_LOG_AUDIT_DATABASE to bool with error: %v, Will use SKIP_LOG_AUDIT_DATABASE env as false", err)
375+
}
376+
}
377+
log.Debugf("get SKIP_LOG_AUDIT_DATABASE from Env is %v", skipAuditEnv)
378+
379+
// get from db
380+
mgr := config.GetCfgManager(ctx)
381+
cfg, err := mgr.GetItemFromDriver(ctx, common.SkipAuditLogDatabase)
382+
if err != nil {
383+
return err
384+
}
385+
// if key not exist in the db, set default ENV value
386+
if val, ok := cfg[common.SkipAuditLogDatabase]; !ok {
387+
log.Debugf("key SkipAuditLogDatabase do not exist in the db, will initialize as %v", skipAuditEnv)
388+
cfg[common.SkipAuditLogDatabase] = skipAuditEnv
389+
if err := mgr.UpdateConfig(ctx, cfg); err != nil {
390+
return err
391+
}
392+
} else {
393+
log.Debugf("key SkipAuditLogDatabase aleady exist in the db with value %v", val)
394+
}
395+
return nil
396+
}

src/lib/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type Manager interface {
4848
Set(ctx context.Context, key string, value any)
4949
Save(ctx context.Context) error
5050
Get(ctx context.Context, key string) *metadata.ConfigureValue
51+
GetItemFromDriver(ctx context.Context, key string) (map[string]any, error)
5152
UpdateConfig(ctx context.Context, cfgs map[string]any) error
5253
GetUserCfgs(ctx context.Context) map[string]any
5354
ValidateCfg(ctx context.Context, cfgs map[string]any) error

src/pkg/config/db/cache.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ func (d *Cache) Save(ctx context.Context, cfg map[string]any) error {
6161
return nil
6262
}
6363

64+
// Get - delegate to driver
65+
func (d *Cache) Get(ctx context.Context, key string) (map[string]any, error) {
66+
return d.driver.Get(ctx, key)
67+
}
68+
6469
// NewCacheDriver returns driver with cache
6570
func NewCacheDriver(cache cache.Cache, driver store.Driver) store.Driver {
6671
return &Cache{

src/pkg/config/db/dao/dao.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/goharbor/harbor/src/lib/config/models"
2323
"github.com/goharbor/harbor/src/lib/errors"
2424
"github.com/goharbor/harbor/src/lib/orm"
25+
"github.com/goharbor/harbor/src/lib/q"
2526
)
2627

2728
// DAO the dao for configure items
@@ -30,6 +31,8 @@ type DAO interface {
3031
GetConfigEntries(ctx context.Context) ([]*models.ConfigEntry, error)
3132
// SaveConfigEntries save configure items provided
3233
SaveConfigEntries(ctx context.Context, entries []models.ConfigEntry) error
34+
// GetConfigItem get configure item by key
35+
GetConfigItem(ctx context.Context, query *q.Query) ([]*models.ConfigEntry, error)
3336
}
3437

3538
type dao struct {
@@ -59,6 +62,21 @@ func (d *dao) GetConfigEntries(ctx context.Context) ([]*models.ConfigEntry, erro
5962
return p, nil
6063
}
6164

65+
// GetConfigItem get configure item by query
66+
func (d *dao) GetConfigItem(ctx context.Context, query *q.Query) ([]*models.ConfigEntry, error) {
67+
query = q.MustClone(query)
68+
qs, err := orm.QuerySetter(ctx, &models.ConfigEntry{}, query)
69+
if err != nil {
70+
return nil, err
71+
}
72+
var configs []*models.ConfigEntry
73+
if _, err := qs.All(&configs); err != nil {
74+
return nil, err
75+
}
76+
return configs, nil
77+
78+
}
79+
6280
func (d *dao) SaveConfigEntries(ctx context.Context, entries []models.ConfigEntry) error {
6381
o, err := orm.FromContext(ctx)
6482
if err != nil {

src/pkg/config/db/db.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/goharbor/harbor/src/lib/config/models"
2424
"github.com/goharbor/harbor/src/lib/encrypt"
2525
"github.com/goharbor/harbor/src/lib/log"
26+
"github.com/goharbor/harbor/src/lib/q"
2627
"github.com/goharbor/harbor/src/pkg/config/db/dao"
2728
)
2829

@@ -84,3 +85,18 @@ func (d *Database) Save(ctx context.Context, cfgs map[string]any) error {
8485
}
8586
return d.cfgDAO.SaveConfigEntries(ctx, configEntries)
8687
}
88+
89+
// Get - Get config item from db
90+
func (d *Database) Get(ctx context.Context, key string) (map[string]any, error) {
91+
resultMap := map[string]any{}
92+
configEntries, err := d.cfgDAO.GetConfigItem(ctx, q.New(q.KeyWords{"k": key}))
93+
if err != nil {
94+
log.Debugf("get config db error: %v", err)
95+
return resultMap, err
96+
}
97+
// convert to map if there's any record
98+
for _, item := range configEntries {
99+
resultMap[item.Key] = item.Value
100+
}
101+
return resultMap, nil
102+
}

src/pkg/config/inmemory/manager.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package inmemory
1616

1717
import (
1818
"context"
19+
"errors"
1920
"maps"
2021
"sync"
2122

@@ -54,6 +55,11 @@ func (d *Driver) Save(_ context.Context, cfg map[string]any) error {
5455
return nil
5556
}
5657

58+
// TODO
59+
func (d *Driver) Get(ctx context.Context, key string) (map[string]any, error) {
60+
return nil, errors.ErrUnsupported
61+
}
62+
5763
// NewInMemoryManager create a manager for unit testing, doesn't involve database or REST
5864
func NewInMemoryManager() *config.CfgManager {
5965
manager := &config.CfgManager{Store: store.NewConfigStore(&Driver{cfgMap: map[string]any{}})}

src/pkg/config/manager.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ func (c *CfgManager) UpdateConfig(ctx context.Context, cfgs map[string]any) erro
180180
return c.Store.Update(ctx, cfgs)
181181
}
182182

183+
func (c *CfgManager) GetItemFromDriver(ctx context.Context, key string) (map[string]any, error) {
184+
return c.Store.GetFromDriver(ctx, key)
185+
}
186+
183187
// ValidateCfg validate config by metadata. return the first error if exist.
184188
func (c *CfgManager) ValidateCfg(ctx context.Context, cfgs map[string]any) error {
185189
for key, value := range cfgs {

src/pkg/config/rest/rest.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,8 @@ func (h *Driver) Load(_ context.Context) (map[string]any, error) {
6262
func (h *Driver) Save(_ context.Context, cfg map[string]any) error {
6363
return h.client.Put(h.configRESTURL, cfg)
6464
}
65+
66+
// TODO
67+
func (d *Driver) Get(ctx context.Context, key string) (map[string]any, error) {
68+
return nil, errors.ErrUnsupported
69+
}

src/pkg/config/store/driver.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ type Driver interface {
2323
Load(ctx context.Context) (map[string]any, error)
2424
// Save - save config item into config driver
2525
Save(ctx context.Context, cfg map[string]any) error
26+
// Load - load config item from config driver
27+
Get(ctx context.Context, key string) (map[string]any, error)
2628
}

src/pkg/config/store/store.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ func (c *ConfigStore) Get(key string) (*metadata.ConfigureValue, error) {
5151
return nil, metadata.ErrValueNotSet
5252
}
5353

54+
func (c *ConfigStore) GetFromDriver(ctx context.Context, key string) (map[string]any, error) {
55+
if c.cfgDriver == nil {
56+
return nil, errors.New("failed to load store, cfgDriver is nil")
57+
}
58+
cfgs, err := c.cfgDriver.Get(ctx, key)
59+
if err != nil {
60+
return nil, err
61+
}
62+
return cfgs, nil
63+
}
64+
5465
// GetAnyType get any type for config items
5566
func (c *ConfigStore) GetAnyType(key string) (any, error) {
5667
if value, ok := c.cfgValues.Load(key); ok {

0 commit comments

Comments
 (0)