Skip to content
Merged
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
2 changes: 1 addition & 1 deletion cmd/companion/companion_desktop.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build windows || linux
//go:build windows || linux || darwin

package companioncmd

Expand Down
2 changes: 1 addition & 1 deletion cmd/companion/companion_other.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !windows && !linux
//go:build !windows && !linux && !darwin

package companioncmd

Expand Down
10 changes: 10 additions & 0 deletions cmd/up/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"time"

"github.com/fosrl/cli/internal/api"
"github.com/fosrl/cli/internal/companion"
"github.com/fosrl/cli/internal/config"
"github.com/fosrl/cli/internal/fingerprint"
"github.com/fosrl/cli/internal/logger"
Expand Down Expand Up @@ -464,6 +465,15 @@ func clientUpMain(cmd *cobra.Command, opts *ClientUpCmdOpts, extraArgs []string)
opts.MatchDomains = cfg.GetStringSlice("up.match_domains_dns")
}

if opts.ID == "" && opts.Secret == "" {
if state := companion.StateFromContext(cmd.Context()); state.Enabled {
err := fmt.Errorf("companion mode is enabled, so connect from %s", state.ProviderName)
logger.Error("Error: %v", err)
logger.Info("To run the tunnel from the CLI, pass --id and --secret, or run 'pangolin companion disable'.")
return err
}
}

// Check if a client is already running
olmClient := olm.NewClient(olm.CLISocketPath())
if olmClient.IsRunning() {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,6 @@ require (
// If changes to Olm or Newt are required, use these
// replace directives during development.
//
//replace github.com/fosrl/olm => ../olm
// replace github.com/fosrl/olm => ../olm

//replace github.com/fosrl/newt => ../newt
// replace github.com/fosrl/newt => ../newt
2 changes: 1 addition & 1 deletion internal/companion/accounts_hint_desktop.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build windows || linux
//go:build windows || linux || darwin

package companion

Expand Down
2 changes: 1 addition & 1 deletion internal/companion/accounts_hint_other.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build !windows && !linux
//go:build !windows && !linux && !darwin

package companion

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build windows || linux
//go:build windows || linux || darwin

package companion

Expand All @@ -14,11 +14,15 @@ import (

const accountsFileName = "accounts.json"

// desktopProvider reads the Pangolin desktop client's session, on Windows and
// Linux alike: account details from its accounts.json, and secrets from its
// manager service, which only the manager can read.
// desktopProvider loads the session of the Pangolin desktop app (Windows,
// Linux, or macOS). Account details live in an accounts.json of the same
// shape; secrets use the framed protocol in secrets_desktop.go. Platforms
// differ only in where the data lives and how secrets are fetched.
type desktopProvider struct {
secrets SecretsClient
defaultDataDir string
// secrets returns the client that fetches secrets for the app whose
// data is in dataDir.
secrets func(dataDir string) SecretsClient
}

type desktopAccountsFile struct {
Expand All @@ -39,8 +43,8 @@ func (desktopProvider) Name() string {
return olm.CompanionAgentName
}

func (desktopProvider) DefaultDataDir() string {
return defaultDesktopDataDir()
func (p desktopProvider) DefaultDataDir() string {
return p.defaultDataDir
}

func (p desktopProvider) LoadSession(dataDir string) (*Session, error) {
Expand All @@ -65,9 +69,10 @@ func (p desktopProvider) LoadSession(dataDir string) (*Session, error) {
return nil, nil
}

secretsClient := p.secrets(dataDir)
accounts := make(map[string]config.Account, len(file.Accounts))
for userID, appAccount := range file.Accounts {
secrets, err := p.secrets.GetUserSecrets(userID)
for userID, desktopAccount := range file.Accounts {
secrets, err := secretsClient.GetUserSecrets(userID)
if err != nil {
logger.Debug("companion: failed to load secrets for %s: %v", userID, err)
continue
Expand All @@ -76,7 +81,7 @@ func (p desktopProvider) LoadSession(dataDir string) (*Session, error) {
continue
}

account := mapDesktopAccount(appAccount, secrets)
account := mapDesktopAccount(desktopAccount, secrets)
accounts[userID] = account
}

Expand Down Expand Up @@ -107,20 +112,20 @@ func (p desktopProvider) LoadSession(dataDir string) (*Session, error) {
return session, nil
}

func mapDesktopAccount(app desktopAccount, secrets UserSecrets) config.Account {
func mapDesktopAccount(src desktopAccount, secrets UserSecrets) config.Account {
account := config.Account{
UserID: app.UserID,
Host: app.Hostname,
Email: app.Email,
OrgID: app.OrgID,
UserID: src.UserID,
Host: src.Hostname,
Email: src.Email,
OrgID: src.OrgID,
SessionToken: secrets.SessionToken,
}
if app.Username != "" {
username := app.Username
if src.Username != "" {
username := src.Username
account.Username = &username
}
if app.Name != "" {
name := app.Name
if src.Name != "" {
name := src.Name
account.Name = &name
}
if secrets.OlmID != "" || secrets.OlmSecret != "" {
Expand Down
19 changes: 17 additions & 2 deletions internal/companion/provider_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,22 @@

package companion

// macOS companion mode is not implemented yet. Return nil so the CLI uses standalone auth.
import (
"path/filepath"

"github.com/fosrl/cli/internal/config"
)

const darwinAppName = "Pangolin"

func platformProvider() Provider {
return nil
// The original user's home, so the CLI under sudo still finds their app.
dataDir := ""
if home, err := config.UserHomeDir(); err == nil {
dataDir = filepath.Join(home, "Library", "Application Support", darwinAppName)
}
return desktopProvider{
defaultDataDir: dataDir,
secrets: newSocketSecretsClient,
}
}
7 changes: 6 additions & 1 deletion internal/companion/provider_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ func platformProvider() Provider {
if !desktopAppInstalled() {
return nil
}
return desktopProvider{secrets: newSocketSecretsClient()}
return desktopProvider{
defaultDataDir: defaultDesktopDataDir(),
secrets: func(string) SecretsClient {
return newSocketSecretsClient()
},
}
}

func desktopAppInstalled() bool {
Expand Down
13 changes: 7 additions & 6 deletions internal/companion/provider_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (

const windowsAppName = "Pangolin"

func defaultDesktopDataDir() string {
func platformProvider() Provider {
appData := os.Getenv("LOCALAPPDATA")
if appData == "" {
appData = os.Getenv("APPDATA")
}
return filepath.Join(appData, windowsAppName)
}

func platformProvider() Provider {
return desktopProvider{secrets: newPipeSecretsClient()}
return desktopProvider{
defaultDataDir: filepath.Join(appData, windowsAppName),
secrets: func(string) SecretsClient {
return newPipeSecretsClient()
},
}
}
38 changes: 38 additions & 0 deletions internal/companion/secrets_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//go:build darwin

package companion

import (
"fmt"
"net"
"path/filepath"
"time"
)

// cliSecretsSocketName is the Unix socket Pangolin for macOS listens on, next
// to its accounts.json. The app checks that the caller runs as the same user
// (or root, for the CLI under sudo).
const cliSecretsSocketName = "cli.sock"

const cliSecretsTimeout = 3 * time.Second

// socketSecretsClient asks the running Pangolin macOS app for secrets, which
// it keeps in its own Keychain items.
type socketSecretsClient struct {
socketPath string
}

func newSocketSecretsClient(dataDir string) SecretsClient {
return &socketSecretsClient{socketPath: filepath.Join(dataDir, cliSecretsSocketName)}
}

func (c *socketSecretsClient) GetUserSecrets(userID string) (UserSecrets, error) {
conn, err := net.DialTimeout("unix", c.socketPath, cliSecretsTimeout)
if err != nil {
return UserSecrets{}, fmt.Errorf("connect to desktop app secrets socket: %w", err)
}
defer conn.Close()
_ = conn.SetDeadline(time.Now().Add(cliSecretsTimeout))

return requestSecrets(conn, userID)
}
14 changes: 9 additions & 5 deletions internal/companion/secrets_desktop.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build windows || linux
//go:build windows || linux || darwin

package companion

Expand All @@ -9,26 +9,30 @@ import (
"io"
)

// The desktop app serves secrets with a small framed protocol, the same on
// every platform: the request is a little-endian uint32 length and the
// Pangolin user id; the reply is a uint32 status, a uint32 length and a
// payload (secrets JSON, or an error message). Only the transport differs.

const (
secretsStatusOK uint32 = 0
secretsStatusNotFound uint32 = 1
secretsStatusError uint32 = 2
)

// UserSecrets matches the desktop client manager's secret store JSON shape.
// UserSecrets matches the desktop app's secret store JSON shape.
type UserSecrets struct {
SessionToken string `json:"sessionToken,omitempty"`
OlmID string `json:"olmId,omitempty"`
OlmSecret string `json:"olmSecret,omitempty"`
}

// SecretsClient fetches user secrets from the desktop client's manager service.
// SecretsClient fetches user secrets from the desktop app.
type SecretsClient interface {
GetUserSecrets(userID string) (UserSecrets, error)
}

// requestSecrets sends a secrets request for userID on a connection to the
// manager and reads the reply. The framing is the same on every platform.
// requestSecrets sends one secrets request on conn and decodes the reply.
func requestSecrets(conn io.ReadWriter, userID string) (UserSecrets, error) {
userIDBytes := []byte(userID)
if err := binary.Write(conn, binary.LittleEndian, uint32(len(userIDBytes))); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions internal/companion/secrets_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

const cliSecretsPipePath = `\\.\pipe\pangolin-manager-cli-secrets`

// pipeSecretsClient asks the Pangolin Manager service for secrets over its
// named pipe.
type pipeSecretsClient struct {
pipePath string
}
Expand Down
2 changes: 1 addition & 1 deletion internal/companion/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "runtime"

const (
windowsMinDesktopAppVersion = "0.11.0"
darwinMinDesktopAppVersion = ""
darwinMinDesktopAppVersion = "0.14.0"
// Every Pangolin Linux client release supports companion mode.
linuxMinDesktopAppVersion = ""
)
Expand Down
6 changes: 6 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,12 @@ func GetPangolinConfigDir() (string, error) {
return pangolinDir, nil
}

// UserHomeDir returns the home directory of the user who invoked the CLI,
// even under sudo. See userHomeDir.
func UserHomeDir() (string, error) {
return userHomeDir()
}

// userHomeDir returns the home directory of the original user
// (the user who invoked the command, not the effective user when running with sudo).
// This ensures that config files work both with and without sudo.
Expand Down
16 changes: 13 additions & 3 deletions internal/notice/notices.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import (
)

const (
companionModeIntroID = "companion-mode-intro-windows-v1"
companionModeIntroLinuxID = "companion-mode-intro-linux-v1"
desktopClientLinuxID = "desktop-client-available-linux-v1"
companionModeIntroID = "companion-mode-intro-windows-v1"
companionModeIntroDarwinID = "companion-mode-intro-darwin-v1"
companionModeIntroLinuxID = "companion-mode-intro-linux-v1"
desktopClientLinuxID = "desktop-client-available-linux-v1"
)

const downloadsURL = "https://pangolin.net/downloads"
Expand All @@ -25,6 +26,15 @@ var registeredNotices = []Notice{
},
Lines: companionModeIntroLines,
},
{
ID: companionModeIntroDarwinID,
MinVersion: "0.23.0",
MaxVersion: "0.23.0",
Condition: func(cfg *config.Config) bool {
return runtime.GOOS == "darwin" && companion.PlatformAvailable()
},
Lines: companionModeIntroLines,
},
{
// Not tied to a CLI version: Linux offers companion mode only once
// the desktop client is installed, which can happen at any time.
Expand Down
7 changes: 7 additions & 0 deletions internal/olm/companion_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build darwin

package olm

// CompanionAgentName is the OLM agent identifier used by Pangolin for macOS.
// Must match the agent the macOS app's packet tunnel passes to OLM.
const CompanionAgentName = "Pangolin macOS"