Files
zeekayandHanzo Dev 1ab6e799c1 cd: the module is github.com/hanzoai/cd, where it is actually hosted
go.mod still declared `module github.com/hanzoai/deploy` after the repo was
renamed. A Go module path is not cosmetic — it is the resolvable location. So
`go get github.com/hanzoai/cd` could not work, and anything importing this
module had to spell a name that no longer exists at that address. The build log
of v3.6.0 is where it surfaced: the compiler was still emitting
github.com/hanzoai/deploy/cmd/cd/commands.

3541 import paths across 690 .go, 19 .proto, 4 .yaml, 3 .sh, 2 .mod, Procfile
and Makefile. Plus the 62 ghcr.io/hanzoai/deploy image references, moved to
ghcr.io/hanzoai/cd to match hanzo.yml's build target — a distinct string from
the module path, replaced separately so neither could clobber the other.

go mod tidy clean, CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build ./... clean.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-28 21:16:24 -07:00

139 lines
4.2 KiB
Go

package commands
import (
"context"
"crypto/tls"
"errors"
"fmt"
"net/http"
"os"
"time"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/hanzoai/cd/cmd/cd/commands/utils"
"github.com/hanzoai/cd/common"
cdclient "github.com/hanzoai/cd/pkg/apiclient"
"github.com/hanzoai/cd/util/cli"
errutil "github.com/hanzoai/cd/util/errors"
grpc_util "github.com/hanzoai/cd/util/grpc"
"github.com/hanzoai/cd/util/localconfig"
)
// NewLogoutCommand returns a new instance of `cd logout` command
func NewLogoutCommand(clientOpts *cdclient.ClientOptions) *cobra.Command {
command := &cobra.Command{
Use: "logout CONTEXT",
Short: "Log out from Hanzo CD",
Long: "Log out from Hanzo CD",
Example: `# Logout from the active Hanzo CD context
# This can be helpful for security reasons or when you want to switch between different Hanzo CD contexts or accounts.
cd logout CONTEXT
# Logout from a specific context named 'cd.hanzo.ai'
cd logout cd.hanzo.ai
`,
Run: func(c *cobra.Command, args []string) {
if len(args) == 0 {
c.HelpFunc()(c, args)
os.Exit(1)
}
context := args[0]
localCfg, err := localconfig.ReadLocalConfig(clientOpts.ConfigPath)
errutil.CheckError(err)
if localCfg == nil {
log.Fatal("Nothing to logout from")
}
promptUtil := utils.NewPrompt(clientOpts.PromptsEnabled)
canLogout := promptUtil.Confirm(fmt.Sprintf("Are you sure you want to log out from '%s'?", context))
if canLogout {
if tlsTestResult, err := grpc_util.TestTLS(context, common.BearerTokenTimeout); err != nil {
log.Warnf("failed to check the TLS config settings for the server : %v.", err)
clientOpts.PlainText = true
} else {
if !tlsTestResult.TLS {
if !clientOpts.PlainText {
if !cli.AskToProceed("WARNING: server is not configured with TLS. Proceed (y/n)? ") {
os.Exit(1)
}
clientOpts.PlainText = true
}
} else if tlsTestResult.InsecureErr != nil {
if !clientOpts.Insecure {
if !cli.AskToProceed(fmt.Sprintf("WARNING: server certificate had error: %s. Proceed insecurely (y/n)? ", tlsTestResult.InsecureErr)) {
os.Exit(1)
}
clientOpts.Insecure = true
}
}
}
scheme := "https"
if clientOpts.PlainText {
scheme = "http"
}
if res, err := revokeServerToken(scheme, context, localCfg.GetToken(context), clientOpts.Insecure); err != nil {
log.Warnf("failed to invalidate token on server: %v.", err)
} else {
_ = res.Body.Close()
if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusSeeOther {
log.Warnf("server returned unexpected status code %d during logout", res.StatusCode)
} else {
log.Infof("token successfully invalidated on server")
}
}
// Remove token from local config
ok := localCfg.RemoveToken(context)
if !ok {
log.Fatalf("Context %s does not exist", context)
}
err = localconfig.ValidateLocalConfig(*localCfg)
if err != nil {
log.Fatalf("Error in logging out: %s", err)
}
err = localconfig.WriteLocalConfig(*localCfg, clientOpts.ConfigPath)
errutil.CheckError(err)
fmt.Printf("Logged out from '%s'\n", context)
} else {
log.Infof("Logout from '%s' cancelled", context)
}
},
}
return command
}
// revokeServerToken makes a call to the server logout endpoint to revoke the token server side
func revokeServerToken(scheme, hostName, token string, insecure bool) (res *http.Response, err error) {
if token == "" {
return nil, errors.New("error getting token from local context file")
}
logoutURL := fmt.Sprintf("%s://%s%s", scheme, hostName, common.LogoutEndpoint)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, logoutURL, http.NoBody)
if err != nil {
return nil, err
}
cookie := &http.Cookie{
Name: common.AuthCookieName,
Value: token,
}
req.AddCookie(cookie)
client := &http.Client{Timeout: common.TokenRevocationClientTimeout}
if insecure {
tr := http.DefaultTransport.(*http.Transport).Clone()
tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
client.Transport = tr
}
return client.Do(req)
}