Skip to content

Commit

Permalink
PMM-13132 Add consts.
Browse files Browse the repository at this point in the history
  • Loading branch information
JiriCtvrtka committed Sep 23, 2024
1 parent 8d9d4cc commit 999c4c8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
4 changes: 3 additions & 1 deletion managed/cmd/pmm-encryption-rotation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"github.com/percona/pmm/utils/logger"
)

const codeDBConnectionFailed = 1

func main() {
signal.Ignore(syscall.SIGINT, syscall.SIGTERM) // to prevent any interuptions during process

Expand All @@ -35,7 +37,7 @@ func main() {
sqlDB, err := models.OpenDB(setupParams())
if err != nil {
logrus.Error(err)
os.Exit(1)
os.Exit(codeDBConnectionFailed)
}

statusCode := models.RotateEncryptionKey(sqlDB, "pmm-managed")
Expand Down
25 changes: 17 additions & 8 deletions managed/models/encryption_rotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,42 @@ import (
"github.com/percona/pmm/managed/utils/encryption"
)

const (
statusRunning = "RUNNING"
statusStopped = "STOPPED"
codeOK = 0
codePMMStopFailed = 2
codeEncryptionFailed = 3
codePMMStartFailed = 4
)

// RotateEncryptionKey will stop PMM server, decrypt data, create new encryption key and encrypt them and start PMM Server again.
func RotateEncryptionKey(sqlDB *sql.DB, dbName string) int {
db := reform.NewDB(sqlDB, postgresql.Dialect, nil)

err := stopPMMServer()
if err != nil {
logrus.Errorf("Failed to stop PMM Server: %+v", err)
return 2
return codePMMStopFailed
}

err = rotateEncryptionKey(db, dbName)
if err != nil {
logrus.Errorf("Failed to rotate encryption key: %+v", err)
return 3
return codeEncryptionFailed
}

err = startPMMServer()
if err != nil {
logrus.Errorf("Failed to start PMM Server: %+v", err)
return 4
return codePMMStartFailed
}

return 0
return codeOK
}

func startPMMServer() error {
if isPMMServerStatus("RUNNING") {
if isPMMServerStatus(statusRunning) {
return nil
}

Expand All @@ -65,15 +74,15 @@ func startPMMServer() error {
return fmt.Errorf("%w: %s", err, output)
}

if !isPMMServerStatus("RUNNING") {
if !isPMMServerStatus(statusRunning) {
return errors.New("cannot start pmm-managed")
}

return nil
}

func stopPMMServer() error {
if isPMMServerStatus("STOPPED") {
if isPMMServerStatus(statusStopped) {
return nil
}

Expand All @@ -83,7 +92,7 @@ func stopPMMServer() error {
return fmt.Errorf("%w: %s", err, output)
}

if !isPMMServerStatus("STOPPED") {
if !isPMMServerStatus(statusStopped) {
return errors.New("cannot stop pmm-managed")
}

Expand Down

0 comments on commit 999c4c8

Please sign in to comment.