Skip to content

Commit

Permalink
Create DB from backup should check for restore permissions (#648)
Browse files Browse the repository at this point in the history
Signed-off-by: Mayank Shah <[email protected]>
  • Loading branch information
mayankshah1607 authored Sep 10, 2024
1 parent d911b86 commit 405ea1a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
13 changes: 13 additions & 0 deletions api/database_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,16 @@ func (e *EverestServer) canTakeBackups(user string, object string) (bool, error)
}
return ok, nil
}

// canRestore checks if a given user is allowed to restore.
func (e *EverestServer) canRestore(user string, object string) (bool, error) {
ok, err := e.rbacEnforcer.Enforce(
user, rbac.ResourceDatabaseClusterRestores,
rbac.ActionCreate,
object,
)
if err != nil {
return false, fmt.Errorf("failed to Enforce: %w", err)
}
return ok, nil
}
10 changes: 10 additions & 0 deletions api/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,16 @@ func (e *EverestServer) validateDatabaseClusterOnCreate(
return errors.Join(errInsufficientPermissions, errors.New("missing permission to take backups"))
}
}
// To be able to create a cluster from a backup (restore backup to new cluster), the user needs to explicitly
// have permissions to take restores.
sourceBackup := pointer.Get(pointer.Get(pointer.Get(databaseCluster.Spec).DataSource).DbClusterBackupName)
if sourceBackup != "" {
if can, err := e.canRestore(user, namespace+"/"+sourceBackup); err != nil {
return err
} else if !can {
return errors.Join(errInsufficientPermissions, errors.New("missing permission to restore"))
}
}
return nil
}

Expand Down

0 comments on commit 405ea1a

Please sign in to comment.