-
Notifications
You must be signed in to change notification settings - Fork 5k
Allow disable audit log to DB when initialize #22350
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,12 +22,14 @@ import ( | |
"net/url" | ||
"os" | ||
"os/signal" | ||
"strconv" | ||
"strings" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/beego/beego/v2/server/web" | ||
|
||
"github.com/goharbor/harbor/src/common" | ||
"github.com/goharbor/harbor/src/common/dao" | ||
common_http "github.com/goharbor/harbor/src/common/http" | ||
configCtl "github.com/goharbor/harbor/src/controller/config" | ||
|
@@ -222,6 +224,11 @@ func main() { | |
log.Error(err) | ||
} | ||
|
||
// Allow user to disable writing audit log to db by env while initialize | ||
if err := initSkipAuditDBbyEnv(ctx); err != nil { | ||
log.Errorf("Failed to initialize SkipAuditDB by ENV: %v", err) | ||
} | ||
|
||
// Init API handler | ||
if err := api.Init(); err != nil { | ||
log.Fatalf("Failed to initialize API handlers with error: %s", err.Error()) | ||
|
@@ -356,3 +363,34 @@ func getDefaultScannerName() string { | |
} | ||
return "" | ||
} | ||
|
||
func initSkipAuditDBbyEnv(ctx context.Context) error { | ||
var err error | ||
skipAuditEnv := false | ||
s := os.Getenv("SKIP_LOG_AUDIT_DATABASE") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggest to add _INIT suffix so that it can be self explained. |
||
if s != "" { | ||
skipAuditEnv, err = strconv.ParseBool(s) | ||
if err != nil { | ||
log.Warningf("Failed to parse SKIP_LOG_AUDIT_DATABASE to bool with error: %v, Will use SKIP_LOG_AUDIT_DATABASE env as false", err) | ||
} | ||
} | ||
log.Debugf("get SKIP_LOG_AUDIT_DATABASE from Env is %v", skipAuditEnv) | ||
|
||
// get from db | ||
mgr := config.GetCfgManager(ctx) | ||
cfg, err := mgr.GetItemFromDriver(ctx, common.SkipAuditLogDatabase) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It can use the mgr.Get(), if the value is not set, this method will return a pre-defined error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mgr.Get() get the k-v from the store map instead of the db driver directly. And it will always has a default values once cfgValues loaded There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should implement the function to check if k = common.SkipAuditLogDatabase exists in the properties table. not GetItermFromDriver() There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For sure I could implement either check the key existence in the db or like now return k-v from db. |
||
if err != nil { | ||
return err | ||
} | ||
// if key not exist in the db, set default ENV value | ||
if val, ok := cfg[common.SkipAuditLogDatabase]; !ok { | ||
log.Debugf("key SkipAuditLogDatabase do not exist in the db, will initialize as %v", skipAuditEnv) | ||
cfg[common.SkipAuditLogDatabase] = skipAuditEnv | ||
if err := mgr.UpdateConfig(ctx, cfg); err != nil { | ||
return err | ||
} | ||
} else { | ||
log.Debugf("key SkipAuditLogDatabase aleady exist in the db with value %v", val) | ||
} | ||
return nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.