github.com/jaylevin/jenkins-library@v1.230.4/pkg/cloudfoundry/Authentication.go (about)

     1  package cloudfoundry
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/SAP/jenkins-library/pkg/command"
     8  	"github.com/SAP/jenkins-library/pkg/log"
     9  )
    10  
    11  //LoginCheck checks if user is logged in to Cloud Foundry with the receiver provided
    12  //to the function call.
    13  func (cf *CFUtils) LoginCheck(options LoginOptions) (bool, error) {
    14  	return cf.loggedIn, nil
    15  }
    16  
    17  //Login logs user in to Cloud Foundry via cf cli.
    18  //Checks if user is logged in first, if not perform 'cf login' command with appropriate parameters
    19  func (cf *CFUtils) Login(options LoginOptions) error {
    20  	var err error
    21  
    22  	_c := cf.Exec
    23  
    24  	if _c == nil {
    25  		_c = &command.Command{}
    26  	}
    27  
    28  	if options.CfAPIEndpoint == "" || options.CfOrg == "" || options.CfSpace == "" || options.Username == "" || options.Password == "" {
    29  		return fmt.Errorf("Failed to login to Cloud Foundry: %w", errors.New("Parameters missing. Please provide the Cloud Foundry Endpoint, Org, Space, Username and Password"))
    30  	}
    31  
    32  	var loggedIn bool
    33  
    34  	loggedIn, err = cf.LoginCheck(options)
    35  
    36  	if loggedIn == true {
    37  		return err
    38  	}
    39  
    40  	if err == nil {
    41  		log.Entry().Info("Logging in to Cloud Foundry")
    42  
    43  		var cfLoginScript = append([]string{
    44  			"login",
    45  			"-a", options.CfAPIEndpoint,
    46  			"-o", options.CfOrg,
    47  			"-s", options.CfSpace,
    48  			"-u", options.Username,
    49  			"-p", options.Password,
    50  		}, options.CfLoginOpts...)
    51  
    52  		log.Entry().WithField("cfAPI:", options.CfAPIEndpoint).WithField("cfOrg", options.CfOrg).WithField("space", options.CfSpace).Info("Logging into Cloud Foundry..")
    53  
    54  		err = _c.RunExecutable("cf", cfLoginScript...)
    55  	}
    56  
    57  	if err != nil {
    58  		return fmt.Errorf("Failed to login to Cloud Foundry: %w", err)
    59  	}
    60  	log.Entry().Info("Logged in successfully to Cloud Foundry..")
    61  	cf.loggedIn = true
    62  	return nil
    63  }
    64  
    65  //Logout logs User out of Cloud Foundry
    66  //Logout can be perforned via 'cf logout' command regardless if user is logged in or not
    67  func (cf *CFUtils) Logout() error {
    68  
    69  	_c := cf.Exec
    70  
    71  	if _c == nil {
    72  		_c = &command.Command{}
    73  	}
    74  
    75  	var cfLogoutScript = "logout"
    76  
    77  	log.Entry().Info("Logging out of Cloud Foundry")
    78  
    79  	err := _c.RunExecutable("cf", cfLogoutScript)
    80  	if err != nil {
    81  		return fmt.Errorf("Failed to Logout of Cloud Foundry: %w", err)
    82  	}
    83  	log.Entry().Info("Logged out successfully")
    84  	cf.loggedIn = false
    85  	return nil
    86  }
    87  
    88  //LoginOptions for logging in to CF
    89  type LoginOptions struct {
    90  	CfAPIEndpoint string
    91  	CfOrg         string
    92  	CfSpace       string
    93  	Username      string
    94  	Password      string
    95  	CfLoginOpts   []string
    96  }
    97  
    98  // CFUtils ...
    99  type CFUtils struct {
   100  	// In order to avoid clashes between parallel workflows requiring cf login/logout
   101  	// this instance of command.ExecRunner can be configured accordingly by settings the
   102  	// environment variables CF_HOME to distict directories.
   103  	// In order to ensure plugins installed to the cf cli are found environment variables
   104  	// CF_PLUGIN_HOME can be set accordingly.
   105  	Exec     command.ExecRunner
   106  	loggedIn bool
   107  }
   108  
   109  // AuthenticationUtils - interface for cloud foundry login and logout
   110  type AuthenticationUtils interface {
   111  	Login(options LoginOptions) error
   112  	Logout() error
   113  }
   114  
   115  // CfUtilsMock - mock for CfUtils
   116  type CfUtilsMock struct {
   117  	LoginError  error
   118  	LogoutError error
   119  }
   120  
   121  // Login mock implementation
   122  func (cf *CfUtilsMock) Login(options LoginOptions) error {
   123  	return cf.LoginError
   124  }
   125  
   126  // Logout mock implementation
   127  func (cf *CfUtilsMock) Logout() error {
   128  	return cf.LogoutError
   129  }
   130  
   131  // Cleanup for CfUtilsMock
   132  func (cf *CfUtilsMock) Cleanup() {
   133  	cf.LoginError = nil
   134  	cf.LogoutError = nil
   135  }