Skip to content

Commit d199937

Browse files
committed
[auth] fix --check by hitting /version instead of listing proxies
1 parent 8355495 commit d199937

2 files changed

Lines changed: 26 additions & 23 deletions

File tree

config/auth.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package config
22

33
import (
4-
"context"
4+
"errors"
55
"fmt"
6+
"io"
67
"log/slog"
78
"net"
89
"net/http"
@@ -15,7 +16,6 @@ import (
1516
"github.com/getsentry/sentry-go"
1617
"github.com/google/uuid"
1718
"github.com/pkg/browser"
18-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1919

2020
configv1alpha1 "github.com/apoxy-dev/apoxy/api/config/v1alpha1"
2121
"github.com/apoxy-dev/apoxy/pkg/log"
@@ -53,36 +53,39 @@ func NewAuthenticator(cfg *configv1alpha1.Config, opts ...AuthenticatorOption) *
5353
return a
5454
}
5555

56-
func (a *Authenticator) Check() (bool, error) {
57-
log.Debugf("checking Apoxy authentication")
56+
// ErrUnauthenticated is returned by Check when the server explicitly rejects
57+
// the credentials (HTTP 401/403). Other errors mean the check could not be
58+
// completed (network, missing config, server error).
59+
var ErrUnauthenticated = errors.New("not authenticated")
60+
61+
func (a *Authenticator) Check() error {
62+
log.Debugf("Checking Apoxy authentication")
5863
c, err := DefaultAPIClient()
5964
if err != nil {
60-
log.Debugf("error creating API client: %v", err)
61-
return true, err
65+
return err
6266
}
6367

6468
if c.BaseHost != "" {
6569
resp, err := c.SendRequest(http.MethodPost, "/v1/terra/check", nil)
6670
if err != nil {
67-
log.Debugf("API request error: %v", err)
68-
return true, err
71+
return err
6972
}
73+
defer resp.Body.Close()
74+
_, _ = io.Copy(io.Discard, resp.Body)
7075

7176
log.Debugf("/v1/terra/check returned status=%d", resp.StatusCode)
72-
if resp.StatusCode != 200 {
73-
return false, nil
77+
switch resp.StatusCode {
78+
case http.StatusOK:
79+
return nil
80+
case http.StatusUnauthorized, http.StatusForbidden:
81+
return ErrUnauthenticated
82+
default:
83+
return fmt.Errorf("/v1/terra/check returned status %d", resp.StatusCode)
7484
}
7585
}
7686

77-
log.Debugf("checking API server authentication")
78-
_, err = c.ControllersV1alpha1().Proxies().List(context.Background(), metav1.ListOptions{})
79-
if err != nil {
80-
return false, err
81-
}
82-
log.Debugf("API server authentication successful")
83-
84-
return true, nil
85-
87+
_, err = c.Discovery().ServerVersion()
88+
return err
8689
}
8790
func (a *Authenticator) healthzHandler(w http.ResponseWriter, r *http.Request) {
8891
fmt.Fprintf(w, "OK")

pkg/cmd/auth.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ If your CLI is already authenticated this will return information about your ses
4040
}
4141

4242
auth := config.NewAuthenticator(cfg, opts...)
43-
ok, err := auth.Check()
43+
err = auth.Check()
4444

45-
if ok && err == nil {
45+
if err == nil {
4646
fmt.Println("Authenticated")
4747
os.Exit(0)
48-
} else if checkOnly { // If we're only checking, exit with an error.
49-
fmt.Println("Invalid authentication")
48+
} else if checkOnly {
49+
fmt.Fprintf(os.Stderr, "Invalid authentication: %v\n", err)
5050
os.Exit(1)
5151
}
5252

0 commit comments

Comments
 (0)