forked from cloudfoundry-community/cloudfoundry-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.go
149 lines (127 loc) · 3.06 KB
/
login.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package helpers
import (
"fmt"
"os"
"strconv"
"strings"
"time"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gexec"
)
func SetAPI() (string, bool) {
apiURL := GetAPI()
skipSSLValidation := skipSSLValidation()
if skipSSLValidation {
Eventually(CF("api", apiURL, "--skip-ssl-validation")).Should(Exit(0))
} else {
Eventually(CF("api", apiURL)).Should(Exit(0))
}
return apiURL, skipSSLValidation
}
func UnsetAPI() {
Eventually(CF("api", "--unset")).Should(Exit(0))
}
func skipSSLValidation() bool {
if skip, err := strconv.ParseBool(os.Getenv("SKIP_SSL_VALIDATION")); err == nil && !skip {
return false
}
return true
}
func GetAPI() string {
apiURL := os.Getenv("CF_INT_API")
if apiURL == "" {
return "https://api.bosh-lite.com"
}
if !strings.HasPrefix(apiURL, "http") {
apiURL = fmt.Sprintf("https://%s", apiURL)
}
return apiURL
}
func LoginAs(username, password string) {
env := map[string]string{
"CF_USERNAME": username,
"CF_PASSWORD": password,
}
for i := 0; i < 3; i++ {
session := CFWithEnv(env, "auth")
Eventually(session).Should(Exit())
if session.ExitCode() == 0 {
break
}
time.Sleep(3 * time.Second)
}
}
func LoginCF() string {
username, password := GetCredentials()
LoginAs(username, password)
return username
}
func LoginCFWithClientCredentials() string {
username, password := SkipIfClientCredentialsNotSet()
env := map[string]string{
"CF_USERNAME": username,
"CF_PASSWORD": password,
}
Eventually(CFWithEnv(env, "auth", "--client-credentials")).Should(Exit(0))
return username
}
// GetCredentials returns back the username and the password.
func GetCredentials() (string, string) {
username := os.Getenv("CF_INT_USERNAME")
if username == "" {
username = "admin"
}
password := os.Getenv("CF_INT_PASSWORD")
if password == "" {
password = "admin"
}
return username, password
}
// GetOIDCCredentials returns back the username and the password for OIDC origin.
func GetOIDCCredentials() (string, string) {
username := os.Getenv("CF_INT_OIDC_USERNAME")
if username == "" {
username = "admin_oidc"
}
password := os.Getenv("CF_INT_OIDC_PASSWORD")
if password == "" {
password = "admin"
}
return username, password
}
func LogoutCF() {
Eventually(CF("logout")).Should(Exit(0))
}
func TargetOrgAndSpace(org string, space string) {
Eventually(CF("target", "-o", org, "-s", space)).Should(Exit(0))
}
func TargetOrg(org string) {
Eventually(CF("target", "-o", org)).Should(Exit(0))
}
func ClearTarget() {
LogoutCF()
LoginCF()
}
func SetupCF(org string, space string) {
LoginCF()
CreateOrgAndSpace(org, space)
TargetOrgAndSpace(org, space)
}
func SwitchToNoRole() string {
username, password := CreateUser()
LogoutCF()
LoginAs(username, password)
return username
}
func SwitchToOrgRole(org, role string) string {
username, password := CreateUserInOrgRole(org, role)
LogoutCF()
LoginAs(username, password)
return username
}
func SwitchToSpaceRole(org, space, role string) string {
username, password := CreateUserInSpaceRole(org, space, role)
LogoutCF()
LoginAs(username, password)
return username
}