Skip to content

Commit fc5ac5e

Browse files
committed
fix: fix DoUserLogin. First use v3 API instead V1 (support more provider)
1 parent f875b88 commit fc5ac5e

2 files changed

Lines changed: 25 additions & 11 deletions

File tree

rancher2/util.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,22 @@ func DoUserLogin(url, provider, user, pass, ttl, desc, cacert string, insecure b
145145
return "", "", fmt.Errorf("Invalid ttl value: %s", ttl)
146146
}
147147

148+
authType := map[string]string{
149+
"local": "local",
150+
"activedirectory": "activeDirectory",
151+
"adfs": "adfs",
152+
"azuread": "azureAD",
153+
"freeipa": "freeIpa",
154+
"generic_oidc": "generic_oidc",
155+
"github": "github",
156+
"keycloak": "keyCloak",
157+
"okta": "okta",
158+
"openldap": "openLdap",
159+
"ping": "ping",
160+
}
161+
148162
payload, err := json.Marshal(map[string]any{
149-
"type": fmt.Sprintf("%sProvider", provider),
163+
"type": fmt.Sprintf("%sProvider", authType[provider]),
150164
"username": user,
151165
"password": pass,
152166
"ttl": TTL,
@@ -157,7 +171,7 @@ func DoUserLogin(url, provider, user, pass, ttl, desc, cacert string, insecure b
157171
}
158172

159173
loginURL := url + "/v1-public/login"
160-
v3loginURL := fmt.Sprintf("%s/v3-public/%sProviders/%s?action=login", url, provider, provider)
174+
v3loginURL := fmt.Sprintf("%s/v3-public/%sProviders/%s?action=login", url, authType[provider], provider)
161175

162176
loginHead := map[string]string{
163177
"Accept": "application/json",
@@ -167,11 +181,11 @@ func DoUserLogin(url, provider, user, pass, ttl, desc, cacert string, insecure b
167181
errPrefix := "Doing user login"
168182

169183
// Login with user and pass
170-
respBody, resp, err := DoPost(loginURL, string(payload), cacert, insecure, loginHead)
184+
respBody, resp, err := DoPost(v3loginURL, string(payload), cacert, insecure, loginHead)
171185
if resp != nil && resp.StatusCode == http.StatusNotFound {
172-
// /v1-public/login endpoint is not available
173-
// try to fall back to /v3-public endpoint.
174-
respBody, _, err = DoPost(v3loginURL, string(payload), cacert, insecure, loginHead)
186+
// /v3-public/login endpoint is not available
187+
// try to fall back to /v1-public endpoint.
188+
respBody, _, err = DoPost(loginURL, string(payload), cacert, insecure, loginHead)
175189
if err != nil {
176190
return "", "", fmt.Errorf("%s: %v", errPrefix, err)
177191
}

rancher2/util_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestDoUserLogin(t *testing.T) {
2929

3030
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
3131
assert.Equal(t, http.MethodPost, r.Method)
32-
assert.Equal(t, "/v1-public/login", r.URL.Path)
32+
assert.Equal(t, "/v3-public/localProviders/local", r.URL.Path)
3333
assert.Equal(t, "application/json", r.Header.Get("Content-Type"))
3434

3535
var reqBody loginInput
@@ -125,15 +125,15 @@ func TestDoUserLogin(t *testing.T) {
125125
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
126126
callCount++
127127

128-
if r.URL.Path == "/v1-public/login" {
129-
// Simulate v1 endpoint not available
128+
if r.URL.Path == "/v3-public/localProviders/local" {
129+
assert.Equal(t, "login", r.URL.Query().Get("action"))
130+
// Simulate v3 endpoint not available
130131
w.WriteHeader(http.StatusNotFound)
131132
w.Write([]byte(http.StatusText(http.StatusNotFound)))
132133
return
133134
}
134135

135-
if r.URL.Path == "/v3-public/localProviders/local" {
136-
assert.Equal(t, "login", r.URL.Query().Get("action"))
136+
if r.URL.Path == "/v1-public/login" {
137137

138138
var reqBody loginInput
139139
err := json.NewDecoder(r.Body).Decode(&reqBody)

0 commit comments

Comments
 (0)