Skip to content
Open
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
68 changes: 67 additions & 1 deletion cmd/finch-credential-helper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"net"
"net/http"
"os"
"path/filepath"
"strings"
"time"

dockertypes "github.com/docker/cli/cli/config/types"
Expand All @@ -24,6 +26,7 @@ import (
const (
// ConnectionTimeout is the timeout for connecting to the credential socket.
ConnectionTimeout = 5 * time.Second
BufferSize = 4096
)

// CredentialSocketPath is the path to the credential socket.
Expand Down Expand Up @@ -51,6 +54,10 @@ func (h FinchCredentialHelper) List() (map[string]string, error) {

// Get retrieves credentials from the Finch daemon.
func (h FinchCredentialHelper) Get(serverURL string) (string, string, error) {
if os.Getenv("USE_NATIVE_CREDSTORE") != "" {
return h.getFromCredSocket(serverURL)
Copy link
Contributor

@Shubhranshu153 Shubhranshu153 Dec 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we are not able to reuse any of it, i would say just create a different binary with finch.
Using this main doent seem to be helpful and creates a dependency on a different package.

}

buildID := os.Getenv("FINCH_BUILD_ID")
if buildID == "" {
return "", "", credentials.NewErrCredentialsNotFound()
Expand Down Expand Up @@ -111,6 +118,65 @@ func (h FinchCredentialHelper) Get(serverURL string) (string, string, error) {
return authConfig.Username, authConfig.Password, nil
}

func (h FinchCredentialHelper) getFromCredSocket(serverURL string) (string, string, error) {
credentialSocketPath := os.Getenv("FINCH_CREDENTIAL_SOCKET")
if credentialSocketPath == "" {
// detect WSL; Windows Finch uses direct fs mount instead of port forwarding
if strings.Contains(os.Getenv("PATH"), "/mnt/c") || os.Getenv("WSL_DISTRO_NAME") != "" {
finchDir := os.Getenv("FINCH_DIR")
if finchDir != "" {
credentialSocketPath = filepath.Join(finchDir, "lima", "data", "finch", "sock", "creds.sock")
} else {
return "", "", fmt.Errorf("FINCH_DIR environment variable not set; cannot find mounted socket")
}
} else {
// reverse port forwarded sock from mac.yaml
credentialSocketPath = "/run/finch-user-sockets/creds.sock"
}
}

// connect to socket
conn, err := net.Dial("unix", credentialSocketPath)
if err != nil {
return "", "", credentials.NewErrCredentialsNotFound()
}
defer conn.Close()

// simple sanitize to fight injection
serverURL = strings.ReplaceAll(serverURL, "\n", "")
serverURL = strings.ReplaceAll(serverURL, "\r", "")

// send get command with URL through socket
_, err = conn.Write([]byte("get\n" + serverURL + "\n"))
if err != nil {
return "", "", credentials.NewErrCredentialsNotFound()
}

// read response
response := make([]byte, BufferSize)
n, err := conn.Read(response)
if err != nil {
return "", "", credentials.NewErrCredentialsNotFound()
}

// parse response into credential struct (auth config breaking)
var cred struct {
ServerURL string `json:"ServerURL"`
Username string `json:"Username"`
Secret string `json:"Secret"`
}
if err := json.Unmarshal(response[:n], &cred); err != nil {
return "", "", credentials.NewErrCredentialsNotFound()
}

// Return empty credentials if no credentials found
if cred.Username == "" && cred.Secret == "" {
return "", "", credentials.NewErrCredentialsNotFound()
}

return cred.Username, cred.Secret, nil
}

func main() {
credentials.Serve(FinchCredentialHelper{})
}
}
Loading