github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/internal/credentials/access_error.go (about)

     1  package credentials
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"reflect"
     7  	"strconv"
     8  
     9  	"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
    10  	grpcCodes "google.golang.org/grpc/codes"
    11  
    12  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
    13  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xstring"
    14  )
    15  
    16  type authErrorOption interface {
    17  	applyAuthErrorOption(w io.Writer)
    18  }
    19  
    20  var (
    21  	_ authErrorOption = nodeIDAuthErrorOption(0)
    22  	_ authErrorOption = addressAuthErrorOption("")
    23  	_ authErrorOption = endpointAuthErrorOption("")
    24  	_ authErrorOption = databaseAuthErrorOption("")
    25  )
    26  
    27  type addressAuthErrorOption string
    28  
    29  func (address addressAuthErrorOption) applyAuthErrorOption(w io.Writer) {
    30  	fmt.Fprint(w, "address:")
    31  	fmt.Fprintf(w, "%q", address)
    32  }
    33  
    34  func WithAddress(address string) addressAuthErrorOption {
    35  	return addressAuthErrorOption(address)
    36  }
    37  
    38  type endpointAuthErrorOption string
    39  
    40  func (endpoint endpointAuthErrorOption) applyAuthErrorOption(w io.Writer) {
    41  	fmt.Fprint(w, "endpoint:")
    42  	fmt.Fprintf(w, "%q", endpoint)
    43  }
    44  
    45  func WithEndpoint(endpoint string) endpointAuthErrorOption {
    46  	return endpointAuthErrorOption(endpoint)
    47  }
    48  
    49  type databaseAuthErrorOption string
    50  
    51  func (address databaseAuthErrorOption) applyAuthErrorOption(w io.Writer) {
    52  	fmt.Fprint(w, "database:")
    53  	fmt.Fprintf(w, "%q", address)
    54  }
    55  
    56  func WithDatabase(database string) databaseAuthErrorOption {
    57  	return databaseAuthErrorOption(database)
    58  }
    59  
    60  type nodeIDAuthErrorOption uint32
    61  
    62  func (id nodeIDAuthErrorOption) applyAuthErrorOption(w io.Writer) {
    63  	fmt.Fprint(w, "nodeID:")
    64  	fmt.Fprint(w, strconv.FormatUint(uint64(id), 10))
    65  }
    66  
    67  func WithNodeID(id uint32) authErrorOption {
    68  	return nodeIDAuthErrorOption(id)
    69  }
    70  
    71  type credentialsUnauthenticatedErrorOption struct {
    72  	credentials Credentials
    73  }
    74  
    75  func (opt credentialsUnauthenticatedErrorOption) applyAuthErrorOption(w io.Writer) {
    76  	fmt.Fprint(w, "credentials:")
    77  	if stringer, has := opt.credentials.(fmt.Stringer); has {
    78  		fmt.Fprintf(w, "%q", stringer.String())
    79  	} else {
    80  		t := reflect.TypeOf(opt.credentials)
    81  		fmt.Fprintf(w, "%q", t.PkgPath()+"."+t.Name())
    82  	}
    83  }
    84  
    85  func WithCredentials(credentials Credentials) credentialsUnauthenticatedErrorOption {
    86  	return credentialsUnauthenticatedErrorOption{
    87  		credentials: credentials,
    88  	}
    89  }
    90  
    91  func AccessError(msg string, err error, opts ...authErrorOption) error {
    92  	buffer := xstring.Buffer()
    93  	defer buffer.Free()
    94  	buffer.WriteString(msg)
    95  	buffer.WriteString(" (")
    96  	for i, opt := range opts {
    97  		if opt != nil {
    98  			if i != 0 {
    99  				buffer.WriteString(",")
   100  			}
   101  			opt.applyAuthErrorOption(buffer)
   102  		}
   103  	}
   104  	buffer.WriteString("): %w")
   105  
   106  	return xerrors.WithStackTrace(fmt.Errorf(buffer.String(), err), xerrors.WithSkipDepth(1))
   107  }
   108  
   109  func IsAccessError(err error) bool {
   110  	if xerrors.IsTransportError(err,
   111  		grpcCodes.Unauthenticated,
   112  		grpcCodes.PermissionDenied,
   113  	) {
   114  		return true
   115  	}
   116  	if xerrors.IsOperationError(err,
   117  		Ydb.StatusIds_UNAUTHORIZED,
   118  	) {
   119  		return true
   120  	}
   121  
   122  	return false
   123  }