Skip to content

Commit 3ce8889

Browse files
committed
adjust login
1 parent 01dbe01 commit 3ce8889

5 files changed

Lines changed: 21 additions & 39 deletions

File tree

backend/auth/handler.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ func (ah *AuthHandler) HandleAuthorize(w http.ResponseWriter, r *http.Request) {
9393

9494
func (ah *AuthHandler) HandleCallback(w http.ResponseWriter, r *http.Request) {
9595
logger := klog.FromContext(r.Context()).WithValues("method", r.Method, "url", r.URL.String())
96-
9796
if errMsg := r.Form.Get("error"); errMsg != "" {
9897
logger.Error(errors.New(errMsg), "failed to authorize")
9998
http.Error(w, errMsg+": "+r.Form.Get("error_description"), http.StatusBadRequest)

cli/pkg/kubectl/bind-apiservice/plugin/binder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func (b *Binder) BindFromFile(ctx context.Context) ([]*kubebindv1alpha2.APIServi
151151

152152
// BindFromResponse processes a BindingResourceResponse and creates all necessary bindings
153153
func (b *Binder) BindFromResponse(ctx context.Context, response *kubebindv1alpha2.BindingResourceResponse) ([]*kubebindv1alpha2.APIServiceBinding, error) {
154-
if response.Authentication.OAuth2CodeGrant == nil {
154+
if response == nil || response.Authentication.OAuth2CodeGrant == nil {
155155
return nil, fmt.Errorf("unexpected response: authentication.oauth2CodeGrant is nil")
156156
}
157157

cli/pkg/kubectl/bind-login/plugin/login.go

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,7 @@ func (o *LoginOptions) Complete(args []string) error {
103103
return err
104104
}
105105

106-
o.loginClient = &http.Client{
107-
Transport: &http.Transport{
108-
DialContext: (&net.Dialer{
109-
Timeout: 5 * time.Second,
110-
KeepAlive: 30 * time.Second,
111-
}).DialContext,
112-
},
113-
}
106+
o.loginClient = http.DefaultClient
114107

115108
return nil
116109
}
@@ -134,19 +127,20 @@ func (o *LoginOptions) Run(ctx context.Context, authURLCh chan<- string) error {
134127
tokenCh := make(chan *TokenResponse, 1)
135128
errCh := make(chan error, 1)
136129

137-
server, localCallbackURL, err := o.startCallbackServerWithRandomPort(sessionID, tokenCh, errCh)
138-
if err != nil {
139-
return fmt.Errorf("failed to start callback server: %w", err)
140-
}
141-
defer server.Close()
142-
143130
// Get provider information
144131
fmt.Fprintf(o.Streams.ErrOut, "Connecting to kube-bind server %s...\n", o.Options.Server)
145132
provider, err := o.getProvider(ctx)
146133
if err != nil {
147134
return fmt.Errorf("failed to get provider information: %w", err)
148135
}
149136

137+
server, localCallbackURL, err := o.startCallbackServerWithRandomPort(tokenCh, errCh)
138+
if err != nil {
139+
return fmt.Errorf("failed to start callback server: %w", err)
140+
}
141+
defer server.Close()
142+
fmt.Fprintf(o.Streams.ErrOut, "Started local callback server at %s\n", localCallbackURL)
143+
150144
// Start authentication flow
151145
authURL, err := o.buildAuthURL(provider, localCallbackURL, sessionID)
152146
if err != nil {
@@ -156,7 +150,6 @@ func (o *LoginOptions) Run(ctx context.Context, authURLCh chan<- string) error {
156150
authURLCh <- authURL
157151
}
158152

159-
fmt.Fprintf(o.Streams.ErrOut, "\nStarted callback server at %s\n", localCallbackURL)
160153
if !o.SkipBrowser {
161154
fmt.Fprintf(o.Streams.ErrOut, "Opening browser for authentication... %s\n", authURL)
162155
err = base.OpenBrowser(authURL)
@@ -308,25 +301,20 @@ func (o *LoginOptions) buildAuthURL(provider *kubebindv1alpha2.BindingProvider,
308301
return u.String(), nil
309302
}
310303

311-
func (o *LoginOptions) startCallbackServerWithRandomPort(sessionID string, tokenCh chan<- *TokenResponse, errCh chan<- error) (*http.Server, string, error) {
312-
expectedSessionID := sessionID
304+
func (o *LoginOptions) startCallbackServerWithRandomPort(tokenCh chan<- *TokenResponse, errCh chan<- error) (*http.Server, string, error) {
313305
listener, err := net.Listen("tcp", "127.0.0.1:0")
314306
if err != nil {
315307
return nil, "", fmt.Errorf("failed to find available port: %w", err)
316308
}
317309

318310
port := listener.Addr().(*net.TCPAddr).Port
311+
listener.Close()
319312

320313
callbackURL := fmt.Sprintf("http://127.0.0.1:%d/callback", port)
321314

322315
// Setup HTTP handler
323316
mux := http.NewServeMux()
324317
mux.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
325-
receivedSessionID := r.URL.Query().Get("session_id")
326-
if receivedSessionID != expectedSessionID {
327-
http.Error(w, "Invalid session", http.StatusBadRequest)
328-
return
329-
}
330318
token := &TokenResponse{
331319
Error: r.URL.Query().Get("error"),
332320
ErrorMessage: r.URL.Query().Get("error_description"),
@@ -361,7 +349,8 @@ func (o *LoginOptions) startCallbackServerWithRandomPort(sessionID string, token
361349
<p>You can now close this window and return to the CLI.</p>
362350
</div>
363351
</body>
364-
</html>`, map[bool]string{true: "success", false: "error"}[token.Error == ""],
352+
</html>`,
353+
map[bool]string{true: "success", false: "error"}[token.Error == ""],
365354
map[bool]string{true: "Authentication Successful!", false: "Authentication Failed"}[token.Error == ""])
366355

367356
select {
@@ -371,9 +360,8 @@ func (o *LoginOptions) startCallbackServerWithRandomPort(sessionID string, token
371360
})
372361

373362
server := &http.Server{
374-
ReadTimeout: 5 * time.Minute,
375-
Addr: fmt.Sprintf(":%d", port),
376-
Handler: mux,
363+
Addr: fmt.Sprintf(":%d", port),
364+
Handler: mux,
377365
}
378366

379367
go func() {

cli/pkg/kubectl/bind/plugin/bind.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"net/url"
2828
"os/exec"
2929
"strconv"
30-
"time"
3130

3231
"github.com/spf13/cobra"
3332
"github.com/spf13/pflag"
@@ -158,11 +157,7 @@ func (b *BindOptions) runWithCallback(ctx context.Context, _ chan<- string) erro
158157
if err != nil {
159158
return fmt.Errorf("failed to start callback server: %w", err)
160159
}
161-
defer func() {
162-
shutdownCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
163-
defer cancel()
164-
_ = callbackServer.Shutdown(shutdownCtx)
165-
}()
160+
defer callbackServer.Close()
166161

167162
// Build the UI URL with callback parameters
168163
uiURL, err := b.buildUIURL(callbackPort, sessionID, b.Cluster)
@@ -360,9 +355,8 @@ func (b *BindOptions) startCallbackServer(resultCh chan<- *BindResult, errCh cha
360355
})
361356

362357
server := &http.Server{
363-
ReadTimeout: time.Minute * 5,
364-
Addr: fmt.Sprintf(":%d", port),
365-
Handler: mux,
358+
Addr: fmt.Sprintf(":%d", port),
359+
Handler: mux,
366360
}
367361

368362
go func() {

contrib/kcp/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,16 @@ kubectl ws create consumer --enter
124124
10. Bind the thing:
125125

126126
```bash
127-
./bin/kubectl-bind http://127.0.0.1:8080/clusters/n9enfzyxpqujqqwk/exports --dry-run -o yaml > apiserviceexport.yaml
127+
./bin/kubectl-bind login http://127.0.0.1:8080 --cluster qs427lvg0y86m0ka
128+
./bin/kubectl-bind --dry-run -o yaml > apiserviceexport.yaml
128129

129130
# Extract secret for binding process. Note that secret name is not the same as output from command above. Check secret
130131
# name by running `kubectl get secret -n kube-bind`
131132
kubectl get secrets -n kube-bind -o jsonpath='{.items[0].data.kubeconfig}' | base64 -d > remote.kubeconfig
132133

133134
namespace=$(yq '.contexts[0].context.namespace' remote.kubeconfig)
134135

135-
./bin/kubectl-bind apiservice -v 6 --remote-kubeconfig remote.kubeconfig -f apiserviceexport.yaml --skip-konnector --remote-namespace "$namespace"
136+
./bin/kubectl-bind apiservice -v 6 --remote-kubeconfig remote.kubeconfig -f apiserviceexport.yaml --skip-konnector --remote-namespace "$namespace"
136137
```
137138

138139
This will keep running, so switch to a new terminal.

0 commit comments

Comments
 (0)