gitsshkey

package
v0.9.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package gitsshkey holds the SSH deploy key path for private git repos: key generation, strict pinned host key checking, and the go-git auth method. Nothing here shells out or writes the private key to disk.

Index

Constants

View Source
const (
	PrivateKeyField = "ssh_private_key"
	HostKeyField    = "ssh_host_key"
)

Secret field names of an app's stored deploy key and pinned host key.

Variables

View Source
var ErrAuthRequired = errors.New("gitsshkey: ssh repository needs a deploy key with a pinned host key")

ErrAuthRequired is returned for an ssh session with no deploy key, so the transport never falls back to the host's ssh-agent or known_hosts.

View Source
var ErrBlockedScanTarget = errors.New("gitsshkey: scan target is an internal address")

ErrBlockedScanTarget marks a host key scan refused because the resolved address is internal and not on the operator allowlist.

Functions

func AuthMethod

func AuthMethod(privatePEM []byte, pinnedHostKey string) (transport.AuthMethod, error)

AuthMethod builds the go-git auth for a clone: the deploy key held in memory plus the pinned host key check. User is "git" for every major forge.

func DeployKeyAuth

func DeployKeyAuth(ctx context.Context, s SecretReader, secretsKey string) (transport.AuthMethod, error)

DeployKeyAuth loads the deploy key stored under secretsKey. No stored key is ErrAuthRequired; a stored but unreadable key is an error without detail.

func Fingerprint

func Fingerprint(authorizedKey string) (string, error)

Fingerprint returns the SHA256 fingerprint of an authorized_keys line.

func Host

func Host(repoURL string) (string, error)

Host returns the host:port an SSH repo URL dials, defaulting to port 22. It uses go-git's own endpoint parser so the scanner and the cloner always agree on the target.

func HostKeyCallback

func HostKeyCallback(pinned string) (ssh.HostKeyCallback, error)

HostKeyCallback accepts only the pinned key(s). An empty pin is an error, never a fallback to accepting anything.

func InstallGuard

func InstallGuard(o GuardOptions)

InstallGuard makes every go-git ssh fetch in the process go through the guarded transport, and removes the agent and ssh_config fallbacks.

func IsSSHURL

func IsSSHURL(repoURL string) bool

IsSSHURL reports whether go-git would use its ssh transport for repoURL. It is deliberately broader than Host's validation so a malformed ssh URL still counts as ssh and cannot slip past a fail-closed auth check.

func NewGuardedTransport

func NewGuardedTransport(o GuardOptions) transport.Transport

NewGuardedTransport wraps go-git's ssh transport: it requires an explicit deploy key with a pinned host key and dials only a checked, pinned address.

func PublicKeyOf

func PublicKeyOf(privatePEM []byte) (string, error)

PublicKeyOf derives the shareable public key from a stored private key.

func ScanHostKey

func ScanHostKey(ctx context.Context, hostPort string, timeout time.Duration, allow []string) (string, error)

ScanHostKey fetches the server's host key for an explicit trust on first use. The caller records the returned key and shows its fingerprint. allow lists internal CIDRs or IPs the operator permits scanning.

Types

type GuardOptions

type GuardOptions struct {
	// Allow lists internal IPs or CIDRs a fetch may reach, like ScanHostKey's.
	Allow func() []string
	// AllowPrivate opens loopback, private and CGNAT ranges; metadata stays refused.
	AllowPrivate   func() bool
	ResolveTimeout time.Duration
	// Next is the real ssh transport; tests replace it to observe sessions.
	Next transport.Transport
}

GuardOptions configures the guarded ssh transport. Allow and AllowPrivate are read on every session so a settings change applies without a restart.

type Pair

type Pair struct {
	PrivatePEM []byte
	PublicKey  string
}

Pair is a freshly generated deploy key. PrivatePEM must go straight into the secrets store and never into a response or log line.

func Generate

func Generate() (Pair, error)

Generate makes a new ed25519 deploy key. The public half is in authorized_keys format, ready to paste into the repository's deploy keys.

type SecretReader

type SecretReader interface {
	Exists(ctx context.Context, key, field string) (bool, error)
	Resolve(ctx context.Context, key, field string) (string, error)
}

SecretReader is the slice of the secrets store a deploy key lookup needs.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL