-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
refactor/sync flag #3793
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
refactor/sync flag #3793
Changes from all commits
455b16e
58edfc7
bc0ccad
0425b60
d005d7c
c298199
d596292
cfcb5b5
3689f0e
ccfb873
909fe13
dcbb50a
3de58f3
0e17669
8d0aa83
f9cfed3
6b97a77
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 |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package db | ||
|
|
||
| import "time" | ||
|
|
||
| // SecretSync describes a unit of remote-storage synchronization. | ||
| // A row with EnvironmentID == nil represents storage-level sync (imports | ||
| // access keys). A row with EnvironmentID set represents env-scoped sync | ||
| // (imports environment variables for that variable group). | ||
| type SecretSync struct { | ||
| ID int `db:"id" json:"id" backup:"-"` | ||
| ProjectID int `db:"project_id" json:"project_id" backup:"-"` | ||
| StorageID int `db:"storage_id" json:"storage_id" backup:"-"` | ||
| EnvironmentID *int `db:"environment_id" json:"environment_id,omitempty" backup:"-"` | ||
|
|
||
| SyncEnabled bool `db:"sync_enabled" json:"sync_enabled"` | ||
| // SyncInterval is the auto-sync period in minutes. Zero disables auto-sync. | ||
| SyncInterval int `db:"sync_interval" json:"sync_interval"` | ||
| LastSyncedAt *time.Time `db:"last_synced_at" json:"last_synced_at,omitempty"` | ||
| LastSyncFailedAt *time.Time `db:"last_sync_failed_at" json:"last_sync_failed_at,omitempty"` | ||
|
|
||
| Paths []SecretSyncPath `db:"-" json:"paths"` | ||
| } | ||
|
|
||
| type SecretSyncPath struct { | ||
| ID int `db:"id" json:"id" backup:"-"` | ||
| SyncID int `db:"sync_id" json:"sync_id" backup:"-"` | ||
| Path string `db:"path" json:"path"` | ||
| Prefix string `db:"prefix" json:"prefix"` | ||
| Separator string `db:"separator" json:"separator"` | ||
| } | ||
|
|
||
| // SecretStorageSyncPath is retained as an alias for backward compatibility | ||
| // with callers that predate the SecretSync refactor. | ||
| type SecretStorageSyncPath = SecretSyncPath |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,11 @@ import ( | |
|
|
||
| func (d *SqlDb) GetEnvironment(projectID int, environmentID int) (environment db.Environment, err error) { | ||
| err = d.getObject(projectID, db.EnvironmentProps, environmentID, &environment) | ||
| if err != nil { | ||
| return | ||
| } | ||
|
|
||
| err = d.fillEnvironmentSync(&environment) | ||
| return | ||
| } | ||
|
|
||
|
|
@@ -14,9 +19,19 @@ func (d *SqlDb) GetEnvironmentRefs(projectID int, environmentID int) (db.ObjectR | |
| } | ||
|
|
||
| func (d *SqlDb) GetEnvironments(projectID int, params db.RetrieveQueryParams) ([]db.Environment, error) { | ||
| var environment []db.Environment | ||
| err := d.getObjects(projectID, db.EnvironmentProps, params, nil, &environment) | ||
| return environment, err | ||
| var environments []db.Environment | ||
| err := d.getObjects(projectID, db.EnvironmentProps, params, nil, &environments) | ||
| if err != nil { | ||
| return environments, err | ||
| } | ||
|
|
||
| for i := range environments { | ||
| if err = d.fillEnvironmentSync(&environments[i]); err != nil { | ||
| return environments, err | ||
| } | ||
| } | ||
|
|
||
| return environments, nil | ||
| } | ||
|
|
||
| func (d *SqlDb) UpdateEnvironment(env db.Environment) error { | ||
|
|
@@ -33,7 +48,12 @@ func (d *SqlDb) UpdateEnvironment(env db.Environment) error { | |
| env.ENV, | ||
| env.Password, | ||
| env.ID) | ||
| return err | ||
|
|
||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return d.saveEnvironmentSync(env) | ||
| } | ||
|
|
||
| func (d *SqlDb) CreateEnvironment(env db.Environment) (newEnv db.Environment, err error) { | ||
|
|
@@ -62,6 +82,12 @@ func (d *SqlDb) CreateEnvironment(env db.Environment) (newEnv db.Environment, er | |
|
|
||
| newEnv = env | ||
| newEnv.ID = insertID | ||
|
|
||
| if err = d.saveEnvironmentSync(newEnv); err != nil { | ||
| return | ||
| } | ||
|
|
||
| err = d.fillEnvironmentSync(&newEnv) | ||
| return | ||
| } | ||
|
|
||
|
|
@@ -90,3 +116,45 @@ func (d *SqlDb) GetEnvironmentSecrets(projectID int, environmentID int) (keys [] | |
|
|
||
| return | ||
| } | ||
|
|
||
| func (d *SqlDb) fillEnvironmentSync(env *db.Environment) error { | ||
| sync, err := d.GetEnvironmentSecretSync(env.ID) | ||
| if err == db.ErrNotFound { | ||
| env.SyncEnabled = false | ||
| env.SyncInterval = 0 | ||
| env.LastSyncedAt = nil | ||
| env.LastSyncFailedAt = nil | ||
| env.SyncPaths = []db.SecretSyncPath{} | ||
| return nil | ||
| } | ||
| if err != nil { | ||
| return err | ||
| } | ||
| env.SyncEnabled = sync.SyncEnabled | ||
| env.SyncInterval = sync.SyncInterval | ||
| env.LastSyncedAt = sync.LastSyncedAt | ||
| env.LastSyncFailedAt = sync.LastSyncFailedAt | ||
| env.SyncPaths = sync.Paths | ||
| if env.SyncPaths == nil { | ||
| env.SyncPaths = []db.SecretSyncPath{} | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // saveEnvironmentSync persists sync settings for an environment. Syncs | ||
| // require a linked SecretStorage; without one, any pending sync row is | ||
| // removed. | ||
| func (d *SqlDb) saveEnvironmentSync(env db.Environment) error { | ||
| envID := env.ID | ||
| sync := db.SecretSync{ | ||
| ProjectID: env.ProjectID, | ||
| EnvironmentID: &envID, | ||
| } | ||
| if env.SecretStorageID != nil { | ||
| sync.StorageID = *env.SecretStorageID | ||
|
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. High — IDOR / cross-project secret storage binding
Fix: Before persisting, load the storage with |
||
| sync.SyncEnabled = env.SyncEnabled | ||
| sync.SyncInterval = env.SyncInterval | ||
| sync.Paths = env.SyncPaths | ||
| } | ||
| return d.SaveSecretSync(sync) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| drop table `project__secret_sync_path`; | ||
| drop table `project__secret_sync`; | ||
| alter table `access_key` drop column `synchronized`; |
Uh oh!
There was an error while loading. Please reload this page.