2020-12-20 19:00:59 +01:00
|
|
|
package sql
|
|
|
|
|
2021-09-04 08:39:16 +02:00
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
|
|
|
)
|
2020-12-20 19:00:59 +01:00
|
|
|
|
2021-09-09 23:31:06 +02:00
|
|
|
func (d *SqlDb) GetAccessKey(projectID int, accessKeyID int, deserializeSecret bool) (key db.AccessKey, err error) {
|
2021-09-01 16:38:28 +02:00
|
|
|
err = d.getObject(projectID, db.AccessKeyProps, accessKeyID, &key)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-09 23:31:06 +02:00
|
|
|
if deserializeSecret {
|
|
|
|
err = key.DeserializeSecret()
|
|
|
|
}
|
2021-09-01 16:38:28 +02:00
|
|
|
|
|
|
|
return
|
2020-12-20 19:00:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SqlDb) GetAccessKeys(projectID int, params db.RetrieveQueryParams) ([]db.AccessKey, error) {
|
|
|
|
var keys []db.AccessKey
|
2021-05-13 21:45:54 +02:00
|
|
|
err := d.getObjects(projectID, db.AccessKeyProps, params, &keys)
|
2020-12-20 19:00:59 +01:00
|
|
|
return keys, err
|
|
|
|
}
|
|
|
|
|
2021-09-09 23:31:06 +02:00
|
|
|
func (d *SqlDb) 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
|
|
|
err = key.SerializeSecret()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var res sql.Result
|
|
|
|
|
|
|
|
var args []interface{}
|
|
|
|
query := "update access_key set name=?"
|
|
|
|
args = append(args, key.Name)
|
|
|
|
|
|
|
|
if key.OverrideSecret {
|
|
|
|
query += ", type=?, secret=?"
|
|
|
|
args = append(args, key.Type)
|
|
|
|
args = append(args, key.Secret)
|
|
|
|
}
|
|
|
|
|
|
|
|
query += " where id=?"
|
|
|
|
args = append(args, key.ID)
|
|
|
|
|
2021-09-09 23:31:06 +02:00
|
|
|
query += " and project_id=?"
|
|
|
|
args = append(args, key.ProjectID)
|
2021-09-04 08:39:16 +02:00
|
|
|
|
|
|
|
res, err = d.exec(query, args...)
|
2020-12-20 19:00:59 +01:00
|
|
|
|
|
|
|
return validateMutationResult(res, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SqlDb) CreateAccessKey(key db.AccessKey) (newKey db.AccessKey, err error) {
|
2021-09-01 16:38:28 +02:00
|
|
|
err = key.SerializeSecret()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-24 19:52:35 +02:00
|
|
|
insertID, err := d.insert(
|
|
|
|
"id",
|
2021-08-31 07:47:19 +02:00
|
|
|
"insert into access_key (name, type, project_id, secret) values (?, ?, ?, ?)",
|
2020-12-20 19:00:59 +01:00
|
|
|
key.Name,
|
|
|
|
key.Type,
|
|
|
|
key.ProjectID,
|
|
|
|
key.Secret)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
newKey = key
|
2021-08-24 19:52:35 +02:00
|
|
|
newKey.ID = insertID
|
2020-12-20 19:00:59 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SqlDb) DeleteAccessKey(projectID int, accessKeyID int) error {
|
2021-05-13 21:45:54 +02:00
|
|
|
return d.deleteObject(projectID, db.AccessKeyProps, accessKeyID)
|
2020-12-20 19:00:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *SqlDb) DeleteAccessKeySoft(projectID int, accessKeyID int) error {
|
2021-05-13 21:45:54 +02:00
|
|
|
return d.deleteObjectSoft(projectID, db.AccessKeyProps, accessKeyID)
|
2021-09-09 23:31:06 +02:00
|
|
|
}
|