github.com/arunkumar7540/cli@v6.45.0+incompatible/integration/helpers/login.go (about) 1 package helpers 2 3 import ( 4 "fmt" 5 "os" 6 "strconv" 7 "strings" 8 "time" 9 10 . "github.com/onsi/ginkgo" 11 . "github.com/onsi/gomega" 12 . "github.com/onsi/gomega/gexec" 13 ) 14 15 // SetAPI sets the API endpoint to the value of the CF_INT_API environment variable, 16 // or "https://api.bosh-lite.com" if not set. If the SKIP_SSL_VALIDATION environment 17 // variable is set, it will use the '--skip-ssl-validation' flag. It returns the API 18 // URL and a boolean indicating if SSL validation was skipped. 19 func SetAPI() (string, bool) { 20 apiURL := GetAPI() 21 skipSSLValidation := skipSSLValidation() 22 if skipSSLValidation { 23 Eventually(CF("api", apiURL, "--skip-ssl-validation")).Should(Exit(0)) 24 } else { 25 Eventually(CF("api", apiURL)).Should(Exit(0)) 26 } 27 return apiURL, skipSSLValidation 28 } 29 30 // UnsetAPI unsets the currently set API endpoint for the CLI. 31 func UnsetAPI() { 32 Eventually(CF("api", "--unset")).Should(Exit(0)) 33 } 34 35 func skipSSLValidation() bool { 36 if skip, err := strconv.ParseBool(os.Getenv("SKIP_SSL_VALIDATION")); err == nil && !skip { 37 return false 38 } 39 return true 40 } 41 42 // GetAPI gets the value of the CF_INT_API environment variable, if set, and prefixes 43 // it with "https://" if the value doesn't already start with "http". If the variable 44 // is not set, returns "https://api.bosh-lite.com". 45 func GetAPI() string { 46 apiURL := os.Getenv("CF_INT_API") 47 if apiURL == "" { 48 return "https://api.bosh-lite.com" 49 } 50 if !strings.HasPrefix(apiURL, "http") { 51 apiURL = fmt.Sprintf("https://%s", apiURL) 52 } 53 54 return apiURL 55 } 56 57 // LoginAs logs in to the CLI with 'cf auth' and the given username and password, 58 // retrying up to 3 times on failures. 59 func LoginAs(username, password string) { 60 env := map[string]string{ 61 "CF_USERNAME": username, 62 "CF_PASSWORD": password, 63 } 64 65 for i := 0; i < 3; i++ { 66 session := CFWithEnv(env, "auth") 67 Eventually(session).Should(Exit()) 68 if session.ExitCode() == 0 { 69 break 70 } 71 time.Sleep(3 * time.Second) 72 } 73 } 74 75 // LoginCF logs in to the CLI using the username and password from the CF_INT_USERNAME 76 // and CF_INT_PASSWORD environment variables, respectively, defaulting to "admin" for 77 // each if either is not set. 78 func LoginCF() string { 79 username, password := GetCredentials() 80 LoginAs(username, password) 81 return username 82 } 83 84 // LoginCFWithClientCredentials logs in to the CLI using client credentials from the CF_INT_CLIENT_ID and 85 // CF_INT_CLIENT_SECRET environment variables and returns the client ID. If these environment variables 86 // are not set, it skips the current test. 87 func LoginCFWithClientCredentials() string { 88 username, password := SkipIfClientCredentialsNotSet() 89 env := map[string]string{ 90 "CF_USERNAME": username, 91 "CF_PASSWORD": password, 92 } 93 Eventually(CFWithEnv(env, "auth", "--client-credentials")).Should(Exit(0)) 94 95 return username 96 } 97 98 // GetCredentials returns back the username and the password from the CF_INT_USERNAME and CF_INT_PASSWORD 99 // environment variables, defaulting to "admin" for 100 // each if either is not set. 101 func GetCredentials() (string, string) { 102 username := os.Getenv("CF_INT_USERNAME") 103 if username == "" { 104 username = "admin" 105 } 106 password := os.Getenv("CF_INT_PASSWORD") 107 if password == "" { 108 password = "admin" 109 } 110 return username, password 111 } 112 113 // SkipIfOIDCCredentialsNotSet returns back the username and the password for 114 // OIDC origin, or skips the test if those values are not set. 115 func SkipIfOIDCCredentialsNotSet() (string, string) { 116 oidcUsername := os.Getenv("CF_INT_OIDC_USERNAME") 117 oidcPassword := os.Getenv("CF_INT_OIDC_PASSWORD") 118 119 if oidcUsername == "" || oidcPassword == "" { 120 Skip("CF_INT_OIDC_USERNAME or CF_INT_OIDC_PASSWORD is not set") 121 } 122 123 return oidcUsername, oidcPassword 124 } 125 126 // LogoutCF logs out of the CLI. 127 func LogoutCF() { 128 Eventually(CF("logout")).Should(Exit(0)) 129 } 130 131 // TargetOrgAndSpace targets the given org and space with 'cf target'. 132 func TargetOrgAndSpace(org string, space string) { 133 Eventually(CF("target", "-o", org, "-s", space)).Should(Exit(0)) 134 } 135 136 // TargetOrg targets the given org with 'cf target'. 137 func TargetOrg(org string) { 138 Eventually(CF("target", "-o", org)).Should(Exit(0)) 139 } 140 141 // ClearTarget logs out and logs back in to the CLI using LogoutCF and LoginCF. 142 func ClearTarget() { 143 LogoutCF() 144 LoginCF() 145 } 146 147 // SetupCF logs in to the CLI with LoginCF, creates the given org and space, and targets that 148 // org and space. 149 func SetupCF(org string, space string) { 150 LoginCF() 151 CreateOrgAndSpace(org, space) 152 TargetOrgAndSpace(org, space) 153 } 154 155 // SwitchToNoRole logs out of the CLI and logs back in as a newly-created user without a role. 156 func SwitchToNoRole() string { 157 username, password := CreateUser() 158 LogoutCF() 159 LoginAs(username, password) 160 return username 161 } 162 163 // SwitchToOrgRole logs out of the CLI and logs back in as a newly-created user with the given 164 // org role in the given org. 165 func SwitchToOrgRole(org, role string) string { 166 username, password := CreateUserInOrgRole(org, role) 167 LogoutCF() 168 LoginAs(username, password) 169 return username 170 } 171 172 // SwitchToSpaceRole logs out of the CLI and logs back in as a newly-created user with the given 173 // space role in the given space and org. 174 func SwitchToSpaceRole(org, space, role string) string { 175 username, password := CreateUserInSpaceRole(org, space, role) 176 LogoutCF() 177 LoginAs(username, password) 178 return username 179 }