Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [TKC-2802] use custom callback port #6035

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion cmd/kubectl-testkube/commands/common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ 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, cfg.CloudContext.CustomAuth)
port := config.CallbackPort
if cfg.CloudContext.CallbackPort != 0 {
port = cfg.CloudContext.CallbackPort
}

token, refreshToken, err = LoginUser(authURI, cfg.CloudContext.CustomAuth, port)
if err != nil {
return nil, "", fmt.Errorf("error logging in: %w", err)
}
Expand Down
1 change: 1 addition & 0 deletions cmd/kubectl-testkube/commands/common/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func PopulateMasterFlags(cmd *cobra.Command, opts *HelmOptions, isDockerCmd bool
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]")
cmd.Flags().IntVar(&opts.Master.CallbackPort, "callback-port", config.CallbackPort, "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")
Expand Down
12 changes: 10 additions & 2 deletions cmd/kubectl-testkube/commands/common/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,9 @@ func PopulateLoginDataToContext(orgID, envID, token, refreshToken, dockerContain
cfg.CloudContext.RefreshToken = refreshToken
}
cfg.CloudContext.DockerContainerName = dockerContainerName
if options.Master.CallbackPort != 0 {
cfg.CloudContext.CallbackPort = options.Master.CallbackPort
}

cfg, err := PopulateOrgAndEnvNames(cfg, orgID, envID, options.Master.URIs.Api)
if err != nil {
Expand Down Expand Up @@ -407,6 +410,10 @@ func PopulateAgentDataToContext(options HelmOptions, cfg config.Data) error {
cfg.CloudContext.OrganizationId = options.Master.OrgId
updated = true
}
if options.Master.CallbackPort != 0 {
cfg.CloudContext.CallbackPort = options.Master.CallbackPort
updated = true
}

if updated {
return config.Save(cfg)
Expand Down Expand Up @@ -488,19 +495,20 @@ func PopulateCloudConfig(cfg config.Data, apiKey string, dockerContainerName *st
if dockerContainerName != nil {
cfg.CloudContext.DockerContainerName = *dockerContainerName
}
cfg.CloudContext.CallbackPort = opts.Master.CallbackPort

return cfg
}

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

ui.Debug("Logging into cloud with parameters", authUri, connectorID)
authUrl, tokenChan, err := cloudlogin.CloudLogin(context.Background(), authUri, strings.ToLower(connectorID))
authUrl, tokenChan, err := cloudlogin.CloudLogin(context.Background(), authUri, strings.ToLower(connectorID), port)
if err != nil {
return "", "", fmt.Errorf("cloud login: %w", err)
}
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 @@ -121,7 +121,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, options.Master.CustomAuth)
token, refreshToken, err = common.LoginUser(options.Master.URIs.Auth, options.Master.CustomAuth, options.Master.CallbackPort)
sendErrTelemetry(cmd, cfg, "login", err)
ui.ExitOnError("user login", err)
}
Expand Down
5 changes: 2 additions & 3 deletions cmd/kubectl-testkube/commands/pro/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
)

const (
listenAddr = "127.0.0.1:8090"
docsUrl = "https://docs.testkube.io/testkube-pro/intro"
tokenQueryParam = "token"
)
Expand Down Expand Up @@ -80,7 +79,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, opts.Master.CustomAuth)
token, refreshToken, err = common.LoginUser(opts.Master.URIs.Auth, opts.Master.CustomAuth, opts.Master.CallbackPort)
ui.ExitOnError("login", err)

orgId, orgName, err := common.UiGetOrganizationId(opts.Master.URIs.Api, token)
Expand Down Expand Up @@ -162,7 +161,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, opts.Master.CustomAuth)
token, refreshToken, err = common.LoginUser(opts.Master.URIs.Auth, opts.Master.CustomAuth, opts.Master.CallbackPort)
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 @@ -108,7 +108,7 @@ func NewInitCmd() *cobra.Command {
ui.NL()
ui.H2("Launching web browser...")
ui.NL()
token, refreshToken, err = common.LoginUser(options.Master.URIs.Auth, options.Master.CustomAuth)
token, refreshToken, err = common.LoginUser(options.Master.URIs.Auth, options.Master.CustomAuth, options.Master.CallbackPort)
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, opts.Master.CustomAuth)
token, refreshToken, err := common.LoginUser(opts.Master.URIs.Auth, opts.Master.CustomAuth, opts.Master.CallbackPort)
ui.ExitOnError("getting token", err)

orgID := opts.Master.OrgId
Expand Down
3 changes: 3 additions & 0 deletions cmd/kubectl-testkube/config/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const (

TokenTypeOIDC = "oidc"
TokenTypeAPI = "api"

CallbackPort = 8090
)

type CloudContext struct {
Expand All @@ -26,6 +28,7 @@ type CloudContext struct {
TokenType string `json:"tokenType,omitempty"`
DockerContainerName string `json:"dockerContainerName,omitempty"`
CustomAuth bool `json:"customConnector,omitempty"`
CallbackPort int `json:"callbackPort,omitempty"`
}

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

URIs MasterURIs `json:"uris,omitempty"`
Expand Down
8 changes: 4 additions & 4 deletions pkg/cloudlogin/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import (

const (
clientID = "testkube-cloud-cli"
redirectURL = "http://127.0.0.1:8090/callback"
redirectURL = "http://127.0.0.1:%d/callback"
)

type Tokens struct {
IDToken string
RefreshToken string
}

func CloudLogin(ctx context.Context, providerURL, connectorID string) (string, chan Tokens, error) {
func CloudLogin(ctx context.Context, providerURL, connectorID string, port int) (string, chan Tokens, error) {
provider, err := oidc.NewProvider(ctx, providerURL)
if err != nil {
return "", nil, err
Expand All @@ -29,7 +29,7 @@ func CloudLogin(ctx context.Context, providerURL, connectorID string) (string, c
oauth2Config := oauth2.Config{
ClientID: clientID,
Endpoint: provider.Endpoint(),
RedirectURL: redirectURL,
RedirectURL: fmt.Sprintf(redirectURL, port),
Scopes: []string{oidc.ScopeOpenID, "profile", "email", "offline_access"},
}

Expand All @@ -44,7 +44,7 @@ func CloudLogin(ctx context.Context, providerURL, connectorID string) (string, c
fmt.Fprintln(w, "Authorization failed.")
}
})
go http.ListenAndServe(":8090", nil)
go http.ListenAndServe(fmt.Sprintf(":%d", port), nil)

// Redirect the user to the OIDC provider's login page.
opts := []oauth2.AuthCodeOption{oauth2.AccessTypeOffline}
Expand Down
Loading