Skip to content

fix(auth): support in-memory private key content in goph.Key (Issue #48)#77

Draft
ljluestc wants to merge 2 commits into
melbahja:masterfrom
ljluestc:fix/48-private-key-content
Draft

fix(auth): support in-memory private key content in goph.Key (Issue #48)#77
ljluestc wants to merge 2 commits into
melbahja:masterfrom
ljluestc:fix/48-private-key-content

Conversation

@ljluestc

@ljluestc ljluestc commented Apr 6, 2026

Copy link
Copy Markdown

fix(auth): support in-memory private key content in goph.Key (Issue #48)

Related Issue

Closes #48

Summary

This change fixes SSH key authentication when the private key is provided as in-memory PEM content instead of a filesystem path.
After this update, goph.Key(...) supports both:

  • a private key file path (existing behavior),
  • raw private key content in memory (new behavior).

Problem

Users reported that they keep private keys in memory and do not write them to disk, but goph.Key(...) failed because it always treated its input as a file path.
Example failing pattern before:

auth, err := goph.Key(serverData.SshPrivateKey, "")

When serverData.SshPrivateKey contained PEM text, the implementation attempted ReadFile(...) and failed.

Root Cause

  • Key(...) calls GetSigner(...).
  • GetSigner(...) previously always read from disk and did not detect raw PEM content.

What Changed

1) Auth behavior fix

  • Updated GetSigner(prvFile, passphrase) in auth.go:
    • Detects PEM-like content (-----BEGIN ...).
    • If PEM content is detected, it parses directly via GetSignerForRawKey(...).
    • Otherwise, it continues using the existing file-path flow.

2) Tests

  • Added auth_test.go with coverage for:
    • GetSigner using in-memory raw private key content,
    • Key using in-memory raw private key content,
    • GetSigner using a private key file path (compatibility regression check).

3) Documentation

  • Updated README.md with an explicit example for in-memory private key usage:
    • auth, err := goph.Key(privateKeyPEM, "")

Backward Compatibility

  • Backward compatible: Yes
  • Existing file path usage: Unchanged
  • Existing passphrase behavior: Unchanged

Validation

go test ./...

Result: pass.

Files Changed

  • auth.go
  • auth_test.go
  • README.md

Change Type

  • Bug fix
  • Tests
  • Documentation

ljluestc added 2 commits April 5, 2026 23:11
Detect PEM private key content in GetSigner and parse it directly while preserving file-path behavior; add tests and README example.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

how to connect with private key content, without file path

1 participant