github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/provider/vsphere/errors.go (about)

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package vsphere
     5  
     6  import (
     7  	"strings"
     8  
     9  	"github.com/juju/errors"
    10  	"github.com/vmware/govmomi/vim25/soap"
    11  	"github.com/vmware/govmomi/vim25/types"
    12  
    13  	"github.com/juju/juju/environs/context"
    14  	"github.com/juju/juju/provider/common"
    15  )
    16  
    17  const (
    18  	// serverFaultCode is the code on the SOAP fault for an
    19  	// authentication error.
    20  	serverFaultCode = "ServerFaultCode"
    21  
    22  	// loginErrorFragment is what we look for in the message string to
    23  	// determine whether this is a login failure. (Using a fragment
    24  	// instead of the exact string to try to avoid breaking if a
    25  	// 'cosmetic' is made to the message.)
    26  	loginErrorFragment = "incorrect user name or password"
    27  )
    28  
    29  // IsAuthorisationFailure determines whether the given error indicates
    30  // that the vsphere credential used is bad.
    31  func IsAuthorisationFailure(err error) bool {
    32  	baseErr := errors.Cause(err)
    33  	if !soap.IsSoapFault(baseErr) {
    34  		return false
    35  	}
    36  	fault := soap.ToSoapFault(baseErr)
    37  	if fault.Code != serverFaultCode {
    38  		return false
    39  	}
    40  	_, isPermissionError := fault.Detail.Fault.(types.NoPermission)
    41  	if isPermissionError {
    42  		return true
    43  	}
    44  	// Otherwise it could be a login error.
    45  	return strings.Contains(fault.String, loginErrorFragment)
    46  }
    47  
    48  // HandleCredentialError marks the current credential as invalid if
    49  // the passed vsphere error indicates it should be.
    50  func HandleCredentialError(err error, ctx context.ProviderCallContext) {
    51  	common.HandleCredentialError(IsAuthorisationFailure, err, ctx)
    52  }