2022-04-13 08:35:35 -05:00
|
|
|
package rbac
|
|
|
|
|
|
2023-02-14 08:27:06 -06:00
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
|
|
"github.com/open-policy-agent/opa/rego"
|
|
|
|
|
)
|
2022-04-13 08:35:35 -05:00
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
// errUnauthorized is the error message that should be returned to
|
|
|
|
|
// clients when an action is forbidden. It is intentionally vague to prevent
|
|
|
|
|
// disclosing information that a client should not have access to.
|
2022-05-17 13:43:19 -05:00
|
|
|
errUnauthorized = "forbidden"
|
2022-04-13 08:35:35 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// UnauthorizedError is the error type for authorization errors
|
|
|
|
|
type UnauthorizedError struct {
|
|
|
|
|
// internal is the internal error that should never be shown to the client.
|
|
|
|
|
// It is only for debugging purposes.
|
|
|
|
|
internal error
|
2023-02-09 13:47:17 -06:00
|
|
|
|
|
|
|
|
// These fields are for debugging purposes.
|
|
|
|
|
subject Subject
|
|
|
|
|
action Action
|
|
|
|
|
// Note only the object type is set for partial execution.
|
|
|
|
|
object Object
|
|
|
|
|
|
|
|
|
|
output rego.ResultSet
|
2022-04-13 08:35:35 -05:00
|
|
|
}
|
|
|
|
|
|
2023-02-14 08:27:06 -06:00
|
|
|
// IsUnauthorizedError is a convenience function to check if err is UnauthorizedError.
|
|
|
|
|
// It is equivalent to errors.As(err, &UnauthorizedError{}).
|
|
|
|
|
func IsUnauthorizedError(err error) bool {
|
|
|
|
|
return errors.As(err, &UnauthorizedError{})
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-13 08:35:35 -05:00
|
|
|
// ForbiddenWithInternal creates a new error that will return a simple
|
|
|
|
|
// "forbidden" to the client, logging internally the more detailed message
|
|
|
|
|
// provided.
|
2023-02-09 13:47:17 -06:00
|
|
|
func ForbiddenWithInternal(internal error, subject Subject, action Action, object Object, output rego.ResultSet) *UnauthorizedError {
|
2022-04-13 08:35:35 -05:00
|
|
|
return &UnauthorizedError{
|
|
|
|
|
internal: internal,
|
2023-02-09 13:47:17 -06:00
|
|
|
subject: subject,
|
|
|
|
|
action: action,
|
|
|
|
|
object: object,
|
2022-04-13 08:35:35 -05:00
|
|
|
output: output,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-14 08:27:06 -06:00
|
|
|
func (e UnauthorizedError) Unwrap() error {
|
|
|
|
|
return e.internal
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-13 08:35:35 -05:00
|
|
|
// Error implements the error interface.
|
|
|
|
|
func (UnauthorizedError) Error() string {
|
|
|
|
|
return errUnauthorized
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Internal allows the internal error message to be logged.
|
|
|
|
|
func (e *UnauthorizedError) Internal() error {
|
|
|
|
|
return e.internal
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-14 08:27:06 -06:00
|
|
|
func (e *UnauthorizedError) SetInternal(err error) {
|
|
|
|
|
e.internal = err
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-13 08:35:35 -05:00
|
|
|
func (e *UnauthorizedError) Input() map[string]interface{} {
|
2023-02-09 13:47:17 -06:00
|
|
|
return map[string]interface{}{
|
|
|
|
|
"subject": e.subject,
|
|
|
|
|
"action": e.action,
|
|
|
|
|
"object": e.object,
|
|
|
|
|
}
|
2022-04-13 08:35:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Output contains the results of the Rego query for debugging.
|
|
|
|
|
func (e *UnauthorizedError) Output() rego.ResultSet {
|
|
|
|
|
return e.output
|
|
|
|
|
}
|
2023-02-14 08:27:06 -06:00
|
|
|
|
|
|
|
|
// As implements the errors.As interface.
|
|
|
|
|
func (*UnauthorizedError) As(target interface{}) bool {
|
|
|
|
|
if _, ok := target.(*UnauthorizedError); ok {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|