Skip to content

Commit 79a4180

Browse files
committed
feat(backup): secret storage
1 parent b9a931a commit 79a4180

File tree

4 files changed

+80
-14
lines changed

4 files changed

+80
-14
lines changed

db/SecretStorage.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ const (
1010
)
1111

1212
type SecretStorage struct {
13-
ID int `db:"id" json:"id"`
14-
ProjectID int `db:"project_id" json:"project_id"`
13+
ID int `db:"id" json:"id" backup:"-"`
14+
ProjectID int `db:"project_id" json:"project_id" backup:"-"`
1515
Name string `db:"name" json:"name"`
1616
Type SecretStorageType `db:"type" json:"type"`
1717
Params MapStringAnyField `db:"params" json:"params"`

services/project/backup.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ func (b *BackupDB) load(projectID int, store db.Store) (err error) {
174174
}
175175
//b.schedules = getSchedulesByProject(projectID, schedules)
176176

177+
b.secretStorages, err = store.GetSecretStorages(projectID)
178+
if err != nil {
179+
return
180+
}
181+
177182
b.meta, err = store.GetProject(projectID)
178183
if err != nil {
179184
return
@@ -214,6 +219,7 @@ func (b *BackupDB) load(projectID int, store db.Store) (err error) {
214219
}
215220

216221
func (b *BackupDB) format() (*BackupFormat, error) {
222+
217223
schedules := make([]BackupSchedule, len(b.schedules))
218224
for i, o := range b.schedules {
219225

@@ -236,7 +242,27 @@ func (b *BackupDB) format() (*BackupFormat, error) {
236242
keys := make([]BackupAccessKey, len(b.keys))
237243
for i, o := range b.keys {
238244
keys[i] = BackupAccessKey{
239-
o,
245+
AccessKey: o,
246+
}
247+
}
248+
249+
secretStorages := make([]BackupSecretStorage, len(b.secretStorages))
250+
for i, o := range b.secretStorages {
251+
secretStorages[i] = BackupSecretStorage{
252+
SecretStorage: o,
253+
}
254+
255+
for _, key := range keys {
256+
if *key.StorageID == o.ID {
257+
secretStorages[i].VaultTokenKeyName = key.Name
258+
break
259+
}
260+
}
261+
262+
for k := range keys {
263+
if *keys[k].SourceStorageID == o.ID {
264+
keys[k].SourceStorage = &o.Name
265+
}
240266
}
241267
}
242268

@@ -388,6 +414,7 @@ func (b *BackupDB) format() (*BackupFormat, error) {
388414
Integration: integrations,
389415
IntegrationAliases: integrationAliases,
390416
Schedules: schedules,
417+
SecretStorages: secretStorages,
391418
}, nil
392419
}
393420

@@ -405,7 +432,7 @@ func (b *BackupFormat) Marshal() (res string, err error) {
405432
return
406433
}
407434

408-
bytes, err := json.Marshal(data)
435+
bytes, err := json.MarshalIndent(data, "", " ")
409436
if err != nil {
410437
return
411438
}

services/project/restore.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ func verifyDuplicate[T BackupEntry](name string, items []T) error {
3232
return nil
3333
}
3434

35+
func (e BackupSecretStorage) Verify(backup *BackupFormat) error {
36+
return verifyDuplicate[BackupSecretStorage](e.Name, backup.SecretStorages)
37+
}
38+
39+
func (e BackupSecretStorage) Restore(store db.Store, b *BackupDB) error {
40+
st := e.SecretStorage
41+
st.ProjectID = b.meta.ID
42+
newStorage, err := store.CreateSecretStorage(st)
43+
if err != nil {
44+
return err
45+
}
46+
b.secretStorages = append(b.secretStorages, newStorage)
47+
return nil
48+
}
49+
3550
func (e BackupEnvironment) Verify(backup *BackupFormat) error {
3651
return verifyDuplicate[BackupEnvironment](e.Name, backup.Environments)
3752
}
@@ -412,6 +427,11 @@ func (backup *BackupFormat) Verify() error {
412427
return fmt.Errorf("error at inventories[%d]: %s", i, err.Error())
413428
}
414429
}
430+
for i, o := range backup.SecretStorages {
431+
if err := o.Verify(backup); err != nil {
432+
return fmt.Errorf("error at secret storage[%d]: %s", i, err.Error())
433+
}
434+
}
415435
for i, o := range backup.Templates {
416436
if err := o.Verify(backup); err != nil {
417437
return fmt.Errorf("error at templates[%d]: %s", i, err.Error())
@@ -509,5 +529,11 @@ func (backup *BackupFormat) Restore(user db.User, store db.Store) (*db.Project,
509529
}
510530
}
511531

532+
for i, o := range backup.SecretStorages {
533+
if err := o.Restore(store, &b); err != nil {
534+
return nil, fmt.Errorf("error at secret storage[%d]: %s", i, err.Error())
535+
}
536+
}
537+
512538
return &newProject, nil
513539
}

services/project/types.go

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,22 @@ type BackupDB struct {
1919
integrationAliases map[int][]db.IntegrationAlias
2020
integrationMatchers map[int][]db.IntegrationMatcher
2121
integrationExtractValues map[int][]db.IntegrationExtractValue
22+
23+
secretStorages []db.SecretStorage
2224
}
2325

2426
type BackupFormat struct {
25-
Meta BackupMeta `backup:"meta"`
26-
Templates []BackupTemplate `backup:"templates"`
27-
Repositories []BackupRepository `backup:"repositories"`
28-
Keys []BackupAccessKey `backup:"keys"`
29-
Views []BackupView `backup:"views"`
30-
Inventories []BackupInventory `backup:"inventories"`
31-
Environments []BackupEnvironment `backup:"environments"`
32-
Integration []BackupIntegration `backup:"integrations"`
33-
IntegrationAliases []string `backup:"integration_aliases"`
34-
Schedules []BackupSchedule `backup:"schedules"`
27+
Meta BackupMeta `backup:"meta"`
28+
Templates []BackupTemplate `backup:"templates"`
29+
Repositories []BackupRepository `backup:"repositories"`
30+
Keys []BackupAccessKey `backup:"keys"`
31+
Views []BackupView `backup:"views"`
32+
Inventories []BackupInventory `backup:"inventories"`
33+
Environments []BackupEnvironment `backup:"environments"`
34+
Integration []BackupIntegration `backup:"integrations"`
35+
IntegrationAliases []string `backup:"integration_aliases"`
36+
Schedules []BackupSchedule `backup:"schedules"`
37+
SecretStorages []BackupSecretStorage `backup:"secret_storages"`
3538
}
3639

3740
type BackupMeta struct {
@@ -44,6 +47,7 @@ type BackupEnvironment struct {
4447

4548
type BackupAccessKey struct {
4649
db.AccessKey
50+
SourceStorage *string `backup:"source_storage"`
4751
}
4852

4953
type BackupSchedule struct {
@@ -95,6 +99,11 @@ type BackupIntegration struct {
9599
AuthSecret *string `backup:"auth_secret"`
96100
}
97101

102+
type BackupSecretStorage struct {
103+
db.SecretStorage
104+
VaultTokenKeyName string `backup:"vault_token_key_name"`
105+
}
106+
98107
type BackupEntry interface {
99108
GetName() string
100109
Verify(backup *BackupFormat) error
@@ -124,3 +133,7 @@ func (e BackupView) GetName() string {
124133
func (e BackupTemplate) GetName() string {
125134
return e.Name
126135
}
136+
137+
func (e BackupSecretStorage) GetName() string {
138+
return e.Name
139+
}

0 commit comments

Comments
 (0)