mirror of
https://github.com/fleetdm/fleet
synced 2026-05-18 06:28:40 +00:00
#8129 Apart from fixing the issue in #8129, this change also introduces UUIDs to Fleet errors. To be able to match a returned error from the API to a error in the Fleet logs. See https://fleetdm.slack.com/archives/C019WG4GH0A/p1677780622769939 for more context. Samples with the changes in this PR: ``` curl -k -H "Authorization: Bearer $TEST_TOKEN" -H 'Content-Type:application/json' "https://localhost:8080/api/v1/fleet/sso" -d '' { "message": "Bad request", "errors": [ { "name": "base", "reason": "Expected JSON Body" } ], "uuid": "a01f6e10-354c-4ff0-b96e-1f64adb500b0" } ``` ``` curl -k -H "Authorization: Bearer $TEST_TOKEN" -H 'Content-Type:application/json' "https://localhost:8080/api/v1/fleet/sso" -d 'asd' { "message": "Bad request", "errors": [ { "name": "base", "reason": "json decoder error" } ], "uuid": "5f716a64-7550-464b-a1dd-e6a505a9f89d" } ``` ``` curl -k -X GET -H "Authorization: Bearer badtoken" "https://localhost:8080/api/latest/fleet/teams" { "message": "Authentication required", "errors": [ { "name": "base", "reason": "Authentication required" } ], "uuid": "efe45bc0-f956-4bf9-ba4f-aa9020a9aaaf" } ``` ``` curl -k -X PATCH -H "Authorization: Bearer $TEST_TOKEN" "https://localhost:8080/api/latest/fleet/users/14" -d '{"name": "Manuel2", "password": "what", "new_password": "p4ssw0rd.12345"}' { "message": "Authorization header required", "errors": [ { "name": "base", "reason": "Authorization header required" } ], "uuid": "57f78cd0-4559-464f-9df7-36c9ef7c89b3" } ``` ``` curl -k -X PATCH -H "Authorization: Bearer $TEST_TOKEN" "https://localhost:8080/api/latest/fleet/users/14" -d '{"name": "Manuel2", "password": "what", "new_password": "p4ssw0rd.12345"}' { "message": "Permission Denied", "uuid": "7f0220ad-6de7-4faf-8b6c-8d7ff9d2ca06" } ``` - [X] Changes file added for user-visible changes in `changes/` or `orbit/changes/`. See [Changes files](https://fleetdm.com/docs/contributing/committing-changes#changes-files) for more information. - [X] Documented any API changes (docs/Using-Fleet/REST-API.md or docs/Contributing/API-for-contributors.md) - ~[ ] Documented any permissions changes~ - ~[ ] Input data is properly validated, `SELECT *` is avoided, SQL injection is prevented (using placeholders for values in statements)~ - ~[ ] Added support on fleet's osquery simulator `cmd/osquery-perf` for new osquery data ingestion features.~ - [X] Added/updated tests - [X] Manual QA for all new/changed functionality - For Orbit and Fleet Desktop changes: - [X] Manual QA must be performed in the three main OSs, macOS, Windows and Linux. - ~[ ] Auto-update manual QA, from released version of component to new version (see [tools/tuf/test](../tools/tuf/test/README.md)).~
145 lines
3 KiB
Go
145 lines
3 KiB
Go
package mysql
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"github.com/VividCortex/mysqlerr"
|
|
"github.com/fleetdm/fleet/v4/server/contexts/ctxerr"
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
"github.com/go-sql-driver/mysql"
|
|
)
|
|
|
|
type notFoundError struct {
|
|
ID uint
|
|
Name string
|
|
Message string
|
|
ResourceType string
|
|
|
|
fleet.ErrorWithUUID
|
|
}
|
|
|
|
var _ fleet.NotFoundError = (*notFoundError)(nil)
|
|
|
|
func notFound(kind string) *notFoundError {
|
|
return ¬FoundError{
|
|
ResourceType: kind,
|
|
}
|
|
}
|
|
|
|
func (e *notFoundError) Error() string {
|
|
if e.ID != 0 {
|
|
return fmt.Sprintf("%s %d was not found in the datastore", e.ResourceType, e.ID)
|
|
}
|
|
if e.Name != "" {
|
|
return fmt.Sprintf("%s %s was not found in the datastore", e.ResourceType, e.Name)
|
|
}
|
|
if e.Message != "" {
|
|
return fmt.Sprintf("%s %s was not found in the datastore", e.ResourceType, e.Message)
|
|
}
|
|
return fmt.Sprintf("%s was not found in the datastore", e.ResourceType)
|
|
}
|
|
|
|
func (e *notFoundError) WithID(id uint) error {
|
|
e.ID = id
|
|
return e
|
|
}
|
|
|
|
func (e *notFoundError) WithName(name string) error {
|
|
e.Name = name
|
|
return e
|
|
}
|
|
|
|
func (e *notFoundError) WithMessage(msg string) error {
|
|
e.Message = msg
|
|
return e
|
|
}
|
|
|
|
func (e *notFoundError) IsNotFound() bool {
|
|
return true
|
|
}
|
|
|
|
// Implement Is so that errors.Is(err, sql.ErrNoRows) returns true for an
|
|
// error of type *notFoundError, without having to wrap sql.ErrNoRows
|
|
// explicitly.
|
|
func (e *notFoundError) Is(other error) bool {
|
|
return other == sql.ErrNoRows
|
|
}
|
|
|
|
type existsError struct {
|
|
Identifier interface{}
|
|
ResourceType string
|
|
TeamID *uint
|
|
|
|
fleet.ErrorWithUUID
|
|
}
|
|
|
|
func alreadyExists(kind string, identifier interface{}) error {
|
|
if s, ok := identifier.(string); ok {
|
|
identifier = strconv.Quote(s)
|
|
}
|
|
return &existsError{
|
|
Identifier: identifier,
|
|
ResourceType: kind,
|
|
}
|
|
}
|
|
|
|
func (e *existsError) WithTeamID(teamID uint) error {
|
|
e.TeamID = &teamID
|
|
return e
|
|
}
|
|
|
|
func (e *existsError) Error() string {
|
|
msg := fmt.Sprintf("%s %v already exists", e.ResourceType, e.Identifier)
|
|
if e.TeamID != nil {
|
|
msg += fmt.Sprintf(" with TeamID %d", *e.TeamID)
|
|
}
|
|
return msg
|
|
}
|
|
|
|
func (e *existsError) IsExists() bool {
|
|
return true
|
|
}
|
|
|
|
func isDuplicate(err error) bool {
|
|
err = ctxerr.Cause(err)
|
|
if driverErr, ok := err.(*mysql.MySQLError); ok {
|
|
if driverErr.Number == mysqlerr.ER_DUP_ENTRY {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
type foreignKeyError struct {
|
|
Name string
|
|
ResourceType string
|
|
|
|
fleet.ErrorWithUUID
|
|
}
|
|
|
|
func foreignKey(kind string, name string) error {
|
|
return &foreignKeyError{
|
|
Name: name,
|
|
ResourceType: kind,
|
|
}
|
|
}
|
|
|
|
func (e *foreignKeyError) Error() string {
|
|
return fmt.Sprintf("the operation violates a foreign key constraint on %s: %s", e.ResourceType, e.Name)
|
|
}
|
|
|
|
func (e *foreignKeyError) IsForeignKey() bool {
|
|
return true
|
|
}
|
|
|
|
func isMySQLForeignKey(err error) bool {
|
|
err = ctxerr.Cause(err)
|
|
if driverErr, ok := err.(*mysql.MySQLError); ok {
|
|
if driverErr.Number == mysqlerr.ER_ROW_IS_REFERENCED_2 {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|