11package config
22
33import (
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}
8790func (a * Authenticator ) healthzHandler (w http.ResponseWriter , r * http.Request ) {
8891 fmt .Fprintf (w , "OK" )
0 commit comments