github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/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 var session *Session 66 67 for i := 0; i < 3; i++ { 68 session = CFWithEnv(env, "auth") 69 Eventually(session).Should(Exit()) 70 if session.ExitCode() == 0 { 71 return 72 } 73 time.Sleep(3 * time.Second) 74 } 75 Expect(session.ExitCode()).To(Equal(0)) 76 } 77 78 // LoginCF logs in to the CLI using the username and password from the CF_INT_USERNAME 79 // and CF_INT_PASSWORD environment variables, respectively, defaulting to "admin" for 80 // each if either is not set. 81 func LoginCF() string { 82 if ClientCredentialsTestMode() { 83 return LoginCFWithClientCredentials() 84 } 85 username, password := GetCredentials() 86 LoginAs(username, password) 87 return username 88 } 89 90 // LoginCFWithClientCredentials logs in to the CLI using client credentials from the CF_INT_CLIENT_ID and 91 // CF_INT_CLIENT_SECRET environment variables and returns the client ID. If these environment variables 92 // are not set, it skips the current test. 93 func LoginCFWithClientCredentials() string { 94 username, password := SkipIfClientCredentialsNotSet() 95 env := map[string]string{ 96 "CF_USERNAME": username, 97 "CF_PASSWORD": password, 98 } 99 Eventually(CFWithEnv(env, "auth", "--client-credentials")).Should(Exit(0)) 100 101 return username 102 } 103 104 // GetCredentials returns back the credentials for the user or client to authenticate with Cloud Foundry. 105 func GetCredentials() (string, string) { 106 if ClientCredentialsTestMode() { 107 return SkipIfClientCredentialsNotSet() 108 } 109 110 username := os.Getenv("CF_INT_USERNAME") 111 if username == "" { 112 username = "admin" 113 } 114 password := os.Getenv("CF_INT_PASSWORD") 115 if password == "" { 116 password = "admin" 117 } 118 return username, password 119 } 120 121 // SkipIfOIDCCredentialsNotSet returns back the username and the password for 122 // OIDC origin, or skips the test if those values are not set. 123 func SkipIfOIDCCredentialsNotSet() (string, string) { 124 oidcUsername := os.Getenv("CF_INT_OIDC_USERNAME") 125 oidcPassword := os.Getenv("CF_INT_OIDC_PASSWORD") 126 127 if oidcUsername == "" || oidcPassword == "" { 128 Skip("CF_INT_OIDC_USERNAME or CF_INT_OIDC_PASSWORD is not set") 129 } 130 131 return oidcUsername, oidcPassword 132 } 133 134 // LogoutCF logs out of the CLI. 135 func LogoutCF() { 136 Eventually(CF("logout")).Should(Exit(0)) 137 } 138 139 // TargetOrgAndSpace targets the given org and space with 'cf target'. 140 func TargetOrgAndSpace(org string, space string) { 141 Eventually(CF("target", "-o", org, "-s", space)).Should(Exit(0)) 142 } 143 144 // TargetOrg targets the given org with 'cf target'. 145 func TargetOrg(org string) { 146 Eventually(CF("target", "-o", org)).Should(Exit(0)) 147 } 148 149 // ClearTarget logs out and logs back in to the CLI using LogoutCF and LoginCF. 150 func ClearTarget() { 151 LogoutCF() 152 LoginCF() 153 } 154 155 // SetupCF logs in to the CLI with LoginCF, creates the given org and space, and targets that 156 // org and space. 157 func SetupCF(org string, space string) { 158 LoginCF() 159 CreateOrgAndSpace(org, space) 160 TargetOrgAndSpace(org, space) 161 } 162 163 // SetupCFWithOrgOnly logs in to the CLI with LoginCF, creates the given org, and targets it. 164 func SetupCFWithOrgOnly(org string) { 165 LoginCF() 166 CreateOrg(org) 167 TargetOrg(org) 168 } 169 170 // SetupCFWithGeneratedOrgAndSpaceNames logs in to the CLI with LoginCF, creates the org and 171 // space with generated names, and targets that org and space. Returns the generated org so 172 // that it can be deleted easily in cleanup step of the test. 173 func SetupCFWithGeneratedOrgAndSpaceNames() string { 174 org := NewOrgName() 175 space := NewSpaceName() 176 177 SetupCF(org, space) 178 return org 179 } 180 181 // SwitchToNoRole logs out of the CLI and logs back in as a newly-created user without a role. 182 func SwitchToNoRole() string { 183 username, password := CreateUser() 184 LogoutCF() 185 LoginAs(username, password) 186 return username 187 } 188 189 // SwitchToOrgRole logs out of the CLI and logs back in as a newly-created user with the given 190 // org role in the given org. 191 func SwitchToOrgRole(org, role string) string { 192 username, password := CreateUserInOrgRole(org, role) 193 LogoutCF() 194 LoginAs(username, password) 195 return username 196 } 197 198 // SwitchToSpaceRole logs out of the CLI and logs back in as a newly-created user with the given 199 // space role in the given space and org. 200 func SwitchToSpaceRole(org, space, role string) string { 201 username, password := CreateUserInSpaceRole(org, space, role) 202 LogoutCF() 203 LoginAs(username, password) 204 return username 205 }