Skip to content

Commit 60f646b

Browse files
dilyevskyclaude
andcommitted
[cli] Add --dashboard-url and --api-base-url flags to apoxy auth
Allow authentication against custom environments (e.g., staging) by adding flags to override the dashboard and API URLs. The API base URL is stored in the project config for subsequent commands. Also fix a bug where an empty kubernetesConfig: {} in the config would incorrectly trigger Kubernetes auth mode instead of using API key auth. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 5d5db87 commit 60f646b

File tree

3 files changed

+47
-9
lines changed

3 files changed

+47
-9
lines changed

config/auth.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,29 @@ type authContext struct {
2828
}
2929

3030
type Authenticator struct {
31-
cfg *configv1alpha1.Config
32-
authCh chan authContext
31+
cfg *configv1alpha1.Config
32+
authCh chan authContext
33+
apiBaseURL string // optional override
3334
}
3435

35-
func NewAuthenticator(cfg *configv1alpha1.Config) *Authenticator {
36-
return &Authenticator{
36+
// AuthenticatorOption is a functional option for configuring the Authenticator.
37+
type AuthenticatorOption func(*Authenticator)
38+
39+
// WithAPIBaseURL sets the API base URL for the authenticated project.
40+
func WithAPIBaseURL(url string) AuthenticatorOption {
41+
return func(a *Authenticator) {
42+
a.apiBaseURL = url
43+
}
44+
}
45+
46+
func NewAuthenticator(cfg *configv1alpha1.Config, opts ...AuthenticatorOption) *Authenticator {
47+
a := &Authenticator{
3748
cfg: cfg,
3849
}
50+
for _, opt := range opts {
51+
opt(a)
52+
}
53+
return a
3954
}
4055

4156
func (a *Authenticator) Check() (bool, error) {
@@ -159,15 +174,22 @@ func (a *Authenticator) Authenticate() {
159174
if p.ID == a.cfg.CurrentProject {
160175
a.cfg.Projects[i].APIKey = key.APIKey
161176
a.cfg.Projects[i].ID = key.ProjectID
177+
if a.apiBaseURL != "" {
178+
a.cfg.Projects[i].APIBaseURL = a.apiBaseURL
179+
}
162180
projectUpdated = true
163181
break
164182
}
165183
}
166184
if !projectUpdated {
167-
a.cfg.Projects = append(a.cfg.Projects, configv1alpha1.Project{
185+
newProject := configv1alpha1.Project{
168186
ID: key.ProjectID,
169187
APIKey: key.APIKey,
170-
})
188+
}
189+
if a.apiBaseURL != "" {
190+
newProject.APIBaseURL = a.apiBaseURL
191+
}
192+
a.cfg.Projects = append(a.cfg.Projects, newProject)
171193
log.Debugf("Appended new project. ProjectID=%q", key.ProjectID)
172194
}
173195
a.cfg.CurrentProject = key.ProjectID

config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func DefaultAPIClient() (*rest.APIClient, error) {
250250
}
251251
project := cfg.Projects[idx]
252252

253-
if project.KubernetesConfig != nil {
253+
if project.KubernetesConfig != nil && (project.KubernetesConfig.InCluster || project.KubernetesConfig.KubeconfigPath != "" || project.KubernetesConfig.Context != "") {
254254
var restConfig *k8srest.Config
255255
if project.KubernetesConfig.InCluster {
256256
restConfig, err = k8srest.InClusterConfig()

pkg/cmd/auth.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import (
99
"github.com/apoxy-dev/apoxy/config"
1010
)
1111

12-
var checkOnly bool
12+
var (
13+
checkOnly bool
14+
dashboardURL string
15+
apiBaseURL string
16+
)
1317

1418
// authCmd represents the auth command
1519
var authCmd = &cobra.Command{
@@ -25,7 +29,17 @@ If your CLI is already authenticated this will return information about your ses
2529
return
2630
}
2731

28-
auth := config.NewAuthenticator(cfg)
32+
// Override dashboard URL if provided
33+
if dashboardURL != "" {
34+
cfg.DashboardURL = dashboardURL
35+
}
36+
37+
var opts []config.AuthenticatorOption
38+
if apiBaseURL != "" {
39+
opts = append(opts, config.WithAPIBaseURL(apiBaseURL))
40+
}
41+
42+
auth := config.NewAuthenticator(cfg, opts...)
2943
ok, err := auth.Check()
3044

3145
if ok && err == nil {
@@ -46,5 +60,7 @@ If your CLI is already authenticated this will return information about your ses
4660

4761
func init() {
4862
authCmd.PersistentFlags().BoolVar(&checkOnly, "check", false, "only check the authentication status")
63+
authCmd.PersistentFlags().StringVar(&dashboardURL, "dashboard-url", "", "dashboard URL for authentication (default: https://dashboard.apoxy.dev)")
64+
authCmd.PersistentFlags().StringVar(&apiBaseURL, "api-base-url", "", "API base URL (default: https://api.apoxy.dev)")
4965
RootCmd.AddCommand(authCmd)
5066
}

0 commit comments

Comments
 (0)