From 5308285cbd4bc5c76b7e6f2f3de12f21b70661db Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Wed, 4 Jun 2025 16:41:46 -0400 Subject: [PATCH 1/6] parameterize zrokdir (#976) --- environment/api.go | 6 ++++++ environment/env_v0_4/dirs.go | 8 +++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/environment/api.go b/environment/api.go index 54addb7e..9bc8d263 100644 --- a/environment/api.go +++ b/environment/api.go @@ -7,6 +7,12 @@ import ( "github.com/pkg/errors" ) +// SetRootDirName allows setting a custom name for the root directory. +// This should be called before any other environment operations. +func SetRootDirName(name string) { + env_v0_4.SetRootDirName(name) +} + func LoadRoot() (env_core.Root, error) { if assert, err := env_v0_4.Assert(); assert && err == nil { return env_v0_4.Load() diff --git a/environment/env_v0_4/dirs.go b/environment/env_v0_4/dirs.go index baf23be8..d0b9b65e 100644 --- a/environment/env_v0_4/dirs.go +++ b/environment/env_v0_4/dirs.go @@ -6,12 +6,18 @@ import ( "path/filepath" ) +var rootDirName = ".zrok" + +func SetRootDirName(name string) { + rootDirName = ".zrok" +} + func rootDir() (string, error) { home, err := os.UserHomeDir() if err != nil { return "", err } - return filepath.Join(home, ".zrok"), nil + return filepath.Join(home, rootDirName), nil } func metadataFile() (string, error) { From 7a5b460790ba52e2126651053bda107484457af0 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Wed, 4 Jun 2025 16:54:47 -0400 Subject: [PATCH 2/6] separate out host/user interrogation functions into a library function (#976) --- cmd/zrok/enable.go | 38 +++++++--------------------------- util/host.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 31 deletions(-) create mode 100644 util/host.go diff --git a/cmd/zrok/enable.go b/cmd/zrok/enable.go index f9476f9d..0cd8f814 100644 --- a/cmd/zrok/enable.go +++ b/cmd/zrok/enable.go @@ -2,6 +2,9 @@ package main import ( "fmt" + "os" + "time" + "github.com/charmbracelet/bubbles/spinner" tea "github.com/charmbracelet/bubbletea" httptransport "github.com/go-openapi/runtime/client" @@ -9,12 +12,9 @@ import ( "github.com/openziti/zrok/environment/env_core" restEnvironment "github.com/openziti/zrok/rest_client_zrok/environment" "github.com/openziti/zrok/tui" - "github.com/shirou/gopsutil/v3/host" + "github.com/openziti/zrok/util" "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "os" - user2 "os/user" - "time" ) func init() { @@ -51,26 +51,12 @@ func (cmd *enableCommand) run(_ *cobra.Command, args []string) { tui.Error(fmt.Sprintf("you already have an enabled environment, %v first before you %v", tui.Code.Render("zrok disable"), tui.Code.Render("zrok enable")), nil) } - hostName, hostDetail, err := getHost() + hostName, hostDetail, username, err := util.GetHostDetails() if err != nil { panic(err) } - var username string - userObj, err := user2.Current() - if err == nil && userObj.Username != "" { - username = userObj.Username - } else { - username = os.Getenv("USER") - if username == "" { - euid := os.Geteuid() - username = fmt.Sprintf("user-%d", euid) - logrus.Warnf("unable to determine the current user, using effective UID: %v", euid) - } - } - hostDetail = fmt.Sprintf("%v; %v", username, hostDetail) - if cmd.description == "@" { - cmd.description = fmt.Sprintf("%v@%v", username, hostName) - } + hostDetail, cmd.description = util.FormatHostDetailsWithUser(username, hostName, hostDetail, cmd.description) + zrok, err := env.Client() if err != nil { cmd.endpointError(env.ApiEndpoint()) @@ -169,16 +155,6 @@ func (cmd *enableCommand) endpointError(apiEndpoint, _ string) { fmt.Printf("(where newEndpoint is something like: %v)\n\n", tui.Code.Render("https://some.zrok.io")) } -func getHost() (string, string, error) { - info, err := host.Info() - if err != nil { - return "", "", err - } - thisHost := fmt.Sprintf("%v; %v; %v; %v; %v; %v; %v", - info.Hostname, info.OS, info.Platform, info.PlatformFamily, info.PlatformVersion, info.KernelVersion, info.KernelArch) - return info.Hostname, thisHost, nil -} - type enableTuiModel struct { spinner spinner.Model msg string diff --git a/util/host.go b/util/host.go new file mode 100644 index 00000000..c7253b5d --- /dev/null +++ b/util/host.go @@ -0,0 +1,51 @@ +package util + +import ( + "fmt" + "github.com/shirou/gopsutil/v3/host" + "github.com/sirupsen/logrus" + "os" + "os/user" +) + +// GetHostDetails returns the hostname, detailed host information, and username. +// The detailed host information includes OS, platform, kernel details, and username. +func GetHostDetails() (hostname string, hostDetail string, username string, err error) { + info, err := host.Info() + if err != nil { + return "", "", "", err + } + hostname = info.Hostname + + userObj, err := user.Current() + if err == nil && userObj.Username != "" { + username = userObj.Username + } else { + username = os.Getenv("USER") + if username == "" { + euid := os.Geteuid() + username = fmt.Sprintf("user-%d", euid) + logrus.Warnf("unable to determine the current user, using effective UID: %v", euid) + } + } + + hostDetail = fmt.Sprintf("%v; %v; %v; %v; %v; %v; %v", + info.Hostname, info.OS, info.Platform, info.PlatformFamily, + info.PlatformVersion, info.KernelVersion, info.KernelArch) + + return hostname, hostDetail, username, nil +} + +// FormatHostDetailsWithUser combines the username and host details into a single string. +// It also returns a formatted description string if the input description is the default "@". +func FormatHostDetailsWithUser(username, hostname, hostDetail, description string) (formattedHostDetail, formattedDescription string) { + formattedHostDetail = fmt.Sprintf("%v; %v", username, hostDetail) + + if description == "@" { + formattedDescription = fmt.Sprintf("%v@%v", username, hostname) + } else { + formattedDescription = description + } + + return formattedHostDetail, formattedDescription +} \ No newline at end of file From 6e1c36ca1e76fd02cd01584f5b30324d17e466ff Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Wed, 4 Jun 2025 17:14:19 -0400 Subject: [PATCH 3/6] actually set the provided name (#976) --- environment/env_v0_4/dirs.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/environment/env_v0_4/dirs.go b/environment/env_v0_4/dirs.go index d0b9b65e..e9605247 100644 --- a/environment/env_v0_4/dirs.go +++ b/environment/env_v0_4/dirs.go @@ -9,7 +9,7 @@ import ( var rootDirName = ".zrok" func SetRootDirName(name string) { - rootDirName = ".zrok" + rootDirName = name } func rootDir() (string, error) { From c161e68c68e14171dd0e9e3ef2b7dc6611a7a3eb Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Wed, 4 Jun 2025 17:18:08 -0400 Subject: [PATCH 4/6] test endpoint lint (#976) --- cmd/zrok/testEndpoint.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/zrok/testEndpoint.go b/cmd/zrok/testEndpoint.go index 2b49b679..291ff754 100644 --- a/cmd/zrok/testEndpoint.go +++ b/cmd/zrok/testEndpoint.go @@ -15,6 +15,7 @@ import ( "github.com/openziti/sdk-golang/ziti" "github.com/openziti/zrok/cmd/zrok/endpointUi" "github.com/openziti/zrok/tui" + "github.com/openziti/zrok/util" "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -200,7 +201,7 @@ func newEndpointData(r *http.Request) *endpointData { } func (ed *endpointData) getHostInfo() { - host, hostDetail, err := getHost() + host, hostDetail, _, err := util.GetHostDetails() if err != nil { logrus.Errorf("error getting host detail: %v", err) } From bb506cf01a45fe1f2fb984c79bb0472a821ffcac Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Thu, 5 Jun 2025 14:46:35 -0400 Subject: [PATCH 5/6] disable the agent console (#976) --- agent/agent.go | 11 +++++++---- agent/config.go | 2 ++ cmd/zrok/agentStart.go | 9 +++++---- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/agent/agent.go b/agent/agent.go index d6825cf9..9b747ef7 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -2,6 +2,10 @@ package agent import ( "context" + "net" + "net/http" + "os" + "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" "github.com/openziti/zrok/agent/agentGrpc" "github.com/openziti/zrok/agent/agentUi" @@ -13,9 +17,6 @@ import ( "github.com/sirupsen/logrus" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" - "net" - "net/http" - "os" ) type Agent struct { @@ -66,7 +67,9 @@ func (a *Agent) Run() error { a.agentSocket = agentSocket go a.manager() - go a.gateway() + if a.cfg.ConsoleEnabled { + go a.gateway() + } go a.remoteAgent() a.persistRegistry = false diff --git a/agent/config.go b/agent/config.go index e44e37a8..1bb379fb 100644 --- a/agent/config.go +++ b/agent/config.go @@ -4,6 +4,7 @@ type AgentConfig struct { ConsoleAddress string ConsoleStartPort uint16 ConsoleEndPort uint16 + ConsoleEnabled bool } func DefaultConfig() *AgentConfig { @@ -11,5 +12,6 @@ func DefaultConfig() *AgentConfig { ConsoleAddress: "127.0.0.1", ConsoleStartPort: 8080, ConsoleEndPort: 8181, + ConsoleEnabled: true, } } diff --git a/cmd/zrok/agentStart.go b/cmd/zrok/agentStart.go index 965199f3..0df78286 100644 --- a/cmd/zrok/agentStart.go +++ b/cmd/zrok/agentStart.go @@ -1,13 +1,14 @@ package main import ( + "os" + "os/signal" + "syscall" + "github.com/openziti/zrok/agent" "github.com/openziti/zrok/environment" "github.com/openziti/zrok/tui" "github.com/spf13/cobra" - "os" - "os/signal" - "syscall" ) func init() { @@ -54,7 +55,7 @@ func (cmd *agentStartCommand) run(_ *cobra.Command, _ []string) { tui.Error("error creating agent", err) } - c := make(chan os.Signal) + c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt, syscall.SIGTERM) go func() { <-c From 35d99a7d0d1f0ccdeb15563971d3afc7b0000342 Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Mon, 9 Jun 2025 13:40:19 -0400 Subject: [PATCH 6/6] changelog (#976) --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50770025..79fcf37f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # CHANGELOG +## v1.0.6 + +CHANGE: Adjusted core framework entry points to support changing zrokdir, and host interrogation functions to better support embedded zrok functionality (https://github.com/openziti/zrok/issues/976) + ## v1.0.5 FEATURE: Initial support for zrok Agent remoting; new `zrok agent enroll` and `zrok agent unenroll` commands that establish opt-in remote Agent management facilities on a per-environment basis. The central API has been augmented to allow for remote control (creating shares and private access instances) of these agents; see the [remoting guide](https://docs.zrok.io/docs/guides/agent/remoting) for details (https://github.com/openziti/zrok/issues/967)