2021-05-08 19:25:00 +02:00
|
|
|
package bolt
|
|
|
|
|
2021-05-08 22:25:31 +02:00
|
|
|
import (
|
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
2023-09-09 14:41:41 +02:00
|
|
|
"go.etcd.io/bbolt"
|
2021-05-08 22:25:31 +02:00
|
|
|
)
|
2021-05-08 19:25:00 +02:00
|
|
|
|
2021-09-10 00:41:36 +02:00
|
|
|
func (d *BoltDb) GetAccessKey(projectID int, accessKeyID int) (key db.AccessKey, err error) {
|
2021-09-01 16:38:28 +02:00
|
|
|
err = d.getObject(projectID, db.AccessKeyProps, intObjectID(accessKeyID), &key)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-09-09 23:31:06 +02:00
|
|
|
|
2021-09-01 16:38:28 +02:00
|
|
|
return
|
2021-05-08 19:25:00 +02:00
|
|
|
}
|
|
|
|
|
2022-02-03 08:05:13 +01:00
|
|
|
func (d *BoltDb) GetAccessKeyRefs(projectID int, accessKeyID int) (db.ObjectReferrers, error) {
|
|
|
|
return d.getObjectRefs(projectID, db.AccessKeyProps, accessKeyID)
|
|
|
|
}
|
|
|
|
|
2021-05-08 19:25:00 +02:00
|
|
|
func (d *BoltDb) GetAccessKeys(projectID int, params db.RetrieveQueryParams) ([]db.AccessKey, error) {
|
|
|
|
var keys []db.AccessKey
|
2024-07-02 19:26:59 +02:00
|
|
|
err := d.getObjects(projectID, db.AccessKeyProps, params, func(i interface{}) bool {
|
|
|
|
k := i.(db.AccessKey)
|
|
|
|
return k.EnvironmentID == nil
|
|
|
|
}, &keys)
|
2021-05-08 19:25:00 +02:00
|
|
|
return keys, err
|
|
|
|
}
|
|
|
|
|
2021-09-09 23:31:06 +02:00
|
|
|
func (d *BoltDb) UpdateAccessKey(key db.AccessKey) error {
|
2021-09-04 08:39:16 +02:00
|
|
|
err := key.Validate(key.OverrideSecret)
|
|
|
|
|
2021-09-01 16:38:28 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-04 08:39:16 +02:00
|
|
|
|
|
|
|
if key.OverrideSecret {
|
|
|
|
err = key.SerializeSecret()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else { // accept only new name, ignore other changes
|
2021-09-10 00:41:36 +02:00
|
|
|
oldKey, err2 := d.GetAccessKey(*key.ProjectID, key.ID)
|
2021-09-04 08:39:16 +02:00
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
oldKey.Name = key.Name
|
|
|
|
key = oldKey
|
|
|
|
}
|
|
|
|
|
2021-09-09 23:31:06 +02:00
|
|
|
return d.updateObject(*key.ProjectID, db.AccessKeyProps, key)
|
2021-05-08 19:25:00 +02:00
|
|
|
}
|
|
|
|
|
2022-01-31 23:16:00 +01:00
|
|
|
func (d *BoltDb) CreateAccessKey(key db.AccessKey) (db.AccessKey, error) {
|
2021-09-01 21:17:28 +02:00
|
|
|
err := key.SerializeSecret()
|
|
|
|
if err != nil {
|
|
|
|
return db.AccessKey{}, err
|
|
|
|
}
|
2021-05-14 13:47:32 +02:00
|
|
|
newKey, err := d.createObject(*key.ProjectID, db.AccessKeyProps, key)
|
2021-05-08 22:25:31 +02:00
|
|
|
return newKey.(db.AccessKey), err
|
2021-05-08 19:25:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *BoltDb) DeleteAccessKey(projectID int, accessKeyID int) error {
|
2022-01-31 23:16:00 +01:00
|
|
|
return d.deleteObject(projectID, db.AccessKeyProps, intObjectID(accessKeyID), nil)
|
2021-05-08 19:25:00 +02:00
|
|
|
}
|
2023-09-09 14:41:41 +02:00
|
|
|
|
|
|
|
func (d *BoltDb) RekeyAccessKeys(oldKey string) error {
|
|
|
|
return d.db.Update(func(tx *bbolt.Tx) error {
|
|
|
|
var allProjects []db.Project
|
|
|
|
|
|
|
|
err := d.getObjectsTx(tx, 0, db.ProjectProps, db.RetrieveQueryParams{}, nil, &allProjects)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, project := range allProjects {
|
|
|
|
var keys []db.AccessKey
|
|
|
|
err = d.getObjectsTx(tx, project.ID, db.AccessKeyProps, db.RetrieveQueryParams{}, nil, &keys)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, key := range keys {
|
|
|
|
err = key.DeserializeSecret2(oldKey)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = key.SerializeSecret()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = d.updateObjectTx(tx, *key.ProjectID, db.AccessKeyProps, key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|