github.com/jojicluka/consul-api@v1.4.5/acl/errors.go (about)

     1  package acl
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  )
     7  
     8  // These error constants define the standard ACL error types. The values
     9  // must not be changed since the error values are sent via RPC calls
    10  // from older clients and may not have the correct type.
    11  const (
    12  	errNotFound         = "ACL not found"
    13  	errRootDenied       = "Cannot resolve root ACL"
    14  	errDisabled         = "ACL support disabled"
    15  	errPermissionDenied = "Permission denied"
    16  	errInvalidParent    = "Invalid Parent"
    17  )
    18  
    19  var (
    20  	// ErrNotFound indicates there is no matching ACL.
    21  	ErrNotFound = errors.New(errNotFound)
    22  
    23  	// ErrRootDenied is returned when attempting to resolve a root ACL.
    24  	ErrRootDenied = errors.New(errRootDenied)
    25  
    26  	// ErrDisabled is returned when ACL changes are not permitted since
    27  	// they are disabled.
    28  	ErrDisabled = errors.New(errDisabled)
    29  
    30  	// ErrPermissionDenied is returned when an ACL based rejection
    31  	// happens.
    32  	ErrPermissionDenied = PermissionDeniedError{}
    33  
    34  	// ErrInvalidParent is returned when a remotely resolve ACL
    35  	// token claims to have a non-root parent
    36  	ErrInvalidParent = errors.New(errInvalidParent)
    37  )
    38  
    39  // IsErrNotFound checks if the given error message is comparable to
    40  // ErrNotFound.
    41  func IsErrNotFound(err error) bool {
    42  	return err != nil && strings.Contains(err.Error(), errNotFound)
    43  }
    44  
    45  // IsErrRootDenied checks if the given error message is comparable to
    46  // ErrRootDenied.
    47  func IsErrRootDenied(err error) bool {
    48  	return err != nil && strings.Contains(err.Error(), errRootDenied)
    49  }
    50  
    51  // IsErrDisabled checks if the given error message is comparable to
    52  // ErrDisabled.
    53  func IsErrDisabled(err error) bool {
    54  	return err != nil && strings.Contains(err.Error(), errDisabled)
    55  }
    56  
    57  // IsErrPermissionDenied checks if the given error message is comparable
    58  // to ErrPermissionDenied.
    59  func IsErrPermissionDenied(err error) bool {
    60  	return err != nil && strings.Contains(err.Error(), errPermissionDenied)
    61  }
    62  
    63  type PermissionDeniedError struct {
    64  	Cause string
    65  }
    66  
    67  func (e PermissionDeniedError) Error() string {
    68  	if e.Cause != "" {
    69  		return errPermissionDenied + ": " + e.Cause
    70  	}
    71  	return errPermissionDenied
    72  }