Skip to content

Commit

Permalink
fix: allow custom URLs and auth connector to support our Skaffold set…
Browse files Browse the repository at this point in the history
…up (#6003)

* fix: allow overriding all URLs, allow using custom auth connector for login
* fix: override namespace properly in `testkube dashboard` command
  • Loading branch information
rangoo94 authored Nov 5, 2024
1 parent 23a55eb commit 130be44
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 17 deletions.
2 changes: 1 addition & 1 deletion cmd/kubectl-testkube/commands/common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func GetClient(cmd *cobra.Command) (client.Client, string, error) {
token, refreshToken, err = cloudlogin.CheckAndRefreshToken(context.Background(), authURI, cfg.CloudContext.ApiKey, cfg.CloudContext.RefreshToken)
if err != nil {
// Error: failed refreshing, go thru login flow
token, refreshToken, err = LoginUser(authURI)
token, refreshToken, err = LoginUser(authURI, cfg.CloudContext.CustomAuth)
if err != nil {
return nil, "", fmt.Errorf("error logging in: %w", err)
}
Expand Down
16 changes: 16 additions & 0 deletions cmd/kubectl-testkube/commands/common/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ func PopulateMasterFlags(cmd *cobra.Command, opts *HelmOptions, isDockerCmd bool
cmd.Flags().StringVar(&opts.Master.LogsUrlPrefix, "logs-prefix", defaultLogsPrefix, "usually don't need to be changed [required for custom cloud mode]")
cmd.Flags().StringVar(&opts.Master.UiUrlPrefix, "ui-prefix", defaultUiPrefix, "usually don't need to be changed [required for custom cloud mode]")
cmd.Flags().StringVar(&opts.Master.RootDomain, "root-domain", defaultRootDomain, "usually don't need to be changed [required for custom cloud mode]")
cmd.Flags().BoolVar(&opts.Master.CustomAuth, "custom-auth", false, "usually don't need to be changed [required for custom cloud mode]")

// allow to override default values of all URIs
cmd.Flags().String("api-uri-override", "", "api uri override")
cmd.Flags().String("ui-uri-override", "", "ui uri override")
cmd.Flags().String("auth-uri-override", "", "auth uri override")
cmd.Flags().String("agent-uri-override", "", "agent uri override")
cmd.Flags().String("logs-uri-override", "", "logs service uri override")

agentURI := ""
if isDockerCmd {
Expand Down Expand Up @@ -165,6 +173,10 @@ func ProcessMasterFlags(cmd *cobra.Command, opts *HelmOptions, cfg *config.Data)
opts.Master.LogsUrlPrefix = cmd.Flag("logs-prefix").Value.String()
}

if cmd.Flags().Changed("custom-auth") {
opts.Master.CustomAuth = cmd.Flag("custom-auth").Value.String() == "true"
}

uris := NewMasterUris(opts.Master.ApiUrlPrefix,
opts.Master.UiUrlPrefix,
opts.Master.AgentUrlPrefix,
Expand All @@ -191,6 +203,10 @@ func ProcessMasterFlags(cmd *cobra.Command, opts *HelmOptions, cfg *config.Data)
uris.WithUiURI(cmd.Flag("ui-uri-override").Value.String())
}

if cmd.Flag("auth-uri-override") != nil && cmd.Flags().Changed("auth-uri-override") {
uris.WithAuthURI(cmd.Flag("auth-uri-override").Value.String())
}

opts.Master.URIs = uris

}
Expand Down
7 changes: 5 additions & 2 deletions cmd/kubectl-testkube/commands/common/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,12 @@ func PopulateCloudConfig(cfg config.Data, apiKey string, dockerContainerName *st
return cfg
}

func LoginUser(authUri string) (string, string, error) {
func LoginUser(authUri string, customConnector bool) (string, string, error) {
ui.H1("Login")
connectorID := ui.Select("Choose your login method", []string{github, gitlab})
connectorID := ""
if !customConnector {
connectorID = ui.Select("Choose your login method", []string{github, gitlab})
}

authUrl, tokenChan, err := cloudlogin.CloudLogin(context.Background(), authUri, strings.ToLower(connectorID))
if err != nil {
Expand Down
4 changes: 0 additions & 4 deletions cmd/kubectl-testkube/commands/context/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ func NewSetContextCmd() *cobra.Command {
cmd.Flags().StringVarP(&apiKey, "api-key", "k", "", "API Key for Testkube Pro")

// allow to override default values of all URIs
cmd.Flags().String("api-uri-override", "", "api uri override")
cmd.Flags().String("ui-uri-override", "", "ui uri override")
cmd.Flags().String("agent-uri-override", "", "agnet uri override")
cmd.Flags().String("logs-uri-override", "", "logs service uri override")
cmd.Flags().StringVar(&dockerContainerName, "docker-container", "testkube-agent", "Docker container name for Testkube Docker Agent")

common.PopulateMasterFlags(cmd, &opts, false)
Expand Down
6 changes: 3 additions & 3 deletions cmd/kubectl-testkube/commands/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ func NewDashboardCmd() *cobra.Command {
cfg, err := config.Load()
ui.ExitOnError("loading config file", err)

if namespace == "" {
namespace = cfg.Namespace
if namespace != "" {
cfg.Namespace = namespace
}

if cfg.ContextType != config.ContextTypeCloud {
isDashboardRunning, _ := k8sclient.IsPodOfServiceRunning(context.Background(), cfg.Namespace, config.EnterpriseUiName)
if isDashboardRunning {
openOnPremDashboard(cmd, cfg, verbose, skipBrowser, "")
} else {
ui.Warn("No dashboard found. Is it running in the " + namespace + " namespace?")
ui.Warn("No dashboard found. Is it running in the " + cfg.Namespace + " namespace?")
}
} else {
openCloudDashboard(cfg)
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubectl-testkube/commands/docker/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func NewInitCmd() *cobra.Command {
ui.H2("Saving Testkube CLI Pro context")
var token, refreshToken string
if !common.IsUserLoggedIn(cfg, options) {
token, refreshToken, err = common.LoginUser(options.Master.URIs.Auth)
token, refreshToken, err = common.LoginUser(options.Master.URIs.Auth, options.Master.CustomAuth)
sendErrTelemetry(cmd, cfg, "login", err)
ui.ExitOnError("user login", err)
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/kubectl-testkube/commands/pro/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func NewConnectCmd() *cobra.Command {
)
// if no agent is passed create new environment and get its token
if opts.Master.AgentToken == "" && opts.Master.OrgId == "" && opts.Master.EnvId == "" {
token, refreshToken, err = common.LoginUser(opts.Master.URIs.Auth)
token, refreshToken, err = common.LoginUser(opts.Master.URIs.Auth, opts.Master.CustomAuth)
ui.ExitOnError("login", err)

orgId, orgName, err := common.UiGetOrganizationId(opts.Master.URIs.Api, token)
Expand Down Expand Up @@ -162,7 +162,7 @@ func NewConnectCmd() *cobra.Command {

ui.H2("Saving Testkube CLI Pro context")
if token == "" && !common.IsUserLoggedIn(cfg, opts) {
token, refreshToken, err = common.LoginUser(opts.Master.URIs.Auth)
token, refreshToken, err = common.LoginUser(opts.Master.URIs.Auth, opts.Master.CustomAuth)
ui.ExitOnError("user login", err)
}
err = common.PopulateLoginDataToContext(opts.Master.OrgId, opts.Master.EnvId, token, refreshToken, "", opts, cfg)
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubectl-testkube/commands/pro/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func NewInitCmd() *cobra.Command {
ui.H2("Saving Testkube CLI Pro context")
var token, refreshToken string
if !common.IsUserLoggedIn(cfg, options) {
token, refreshToken, err = common.LoginUser(options.Master.URIs.Auth)
token, refreshToken, err = common.LoginUser(options.Master.URIs.Auth, options.Master.CustomAuth)
sendErrTelemetry(cmd, cfg, "login", err)
ui.ExitOnError("user login", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/kubectl-testkube/commands/pro/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewLoginCmd() *cobra.Command {

common.ProcessMasterFlags(cmd, &opts, &cfg)

token, refreshToken, err := common.LoginUser(opts.Master.URIs.Auth)
token, refreshToken, err := common.LoginUser(opts.Master.URIs.Auth, opts.Master.CustomAuth)
ui.ExitOnError("getting token", err)

orgID := opts.Master.OrgId
Expand Down
1 change: 1 addition & 0 deletions cmd/kubectl-testkube/config/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type CloudContext struct {
UiUri string `json:"uiUri,omitempty"`
TokenType string `json:"tokenType,omitempty"`
DockerContainerName string `json:"dockerContainerName,omitempty"`
CustomAuth bool `json:"customConnector,omitempty"`
}

type Data struct {
Expand Down
7 changes: 7 additions & 0 deletions cmd/kubectl-testkube/config/master.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Master struct {
LogsUrlPrefix string `json:"logsUrlPrefix,omitempty"`
ApiUrlPrefix string `json:"apiUrlPrefix,omitempty"`
RootDomain string `json:"rootDomain,omitempty"`
CustomAuth bool `json:"customAuth,omitempty"`
Features featureflags.FeatureFlags `json:"features,omitempty"`

URIs MasterURIs `json:"uris,omitempty"`
Expand Down Expand Up @@ -52,3 +53,9 @@ func (m *MasterURIs) WithUiURI(uri string) *MasterURIs {
m.Ui = uri
return m
}

// WithUi sets whole auth URI
func (m *MasterURIs) WithAuthURI(uri string) *MasterURIs {
m.Auth = uri
return m
}
2 changes: 1 addition & 1 deletion cmd/tcl/kubectl-testkube/devbox/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ This utility is used to help with development of the Agent features (like Test W
## Usage

* Login to Testkube CLI, like `testkube login`
* Run `go run cmd/kubectl-testkube/main.go devbox`
* For local development Testkube Enterprise (Skaffold), consider `testkube login --api-uri-override=http://localhost:8099 --agent-uri-override=http://testkube-enterprise-api.tk-dev.svc.local:8089 --auth-uri-override=http://localhost:5556 --custom-auth`
* It's worth to create alias for that in own `.bashrc` or `.bash_profile`
* It's worth to pass a devbox name, like `-n dawid`, so it's not using random name

Expand Down
6 changes: 5 additions & 1 deletion pkg/cloudlogin/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ func CloudLogin(ctx context.Context, providerURL, connectorID string) (string, c
go http.ListenAndServe(":8090", nil)

// Redirect the user to the OIDC provider's login page.
authURL := oauth2Config.AuthCodeURL("state", oauth2.AccessTypeOffline, oauth2.SetAuthURLParam("connector_id", connectorID))
opts := []oauth2.AuthCodeOption{oauth2.AccessTypeOffline}
if connectorID != "" {
opts = append(opts, oauth2.SetAuthURLParam("connector_id", connectorID))
}
authURL := oauth2Config.AuthCodeURL("state", opts...)

respCh := make(chan Tokens)

Expand Down

0 comments on commit 130be44

Please sign in to comment.