Skip to content

[v1] add examples of SFTP over TLS without SSH - #689

Merged
puellanivis merged 3 commits into
pkg:masterfrom
christhomas:example-tls
Nov 5, 2026
Merged

puellanivis merged 3 commits into
pkg:masterfrom
christhomas:example-tls

Conversation

@christhomas

Copy link
Copy Markdown
Contributor

For #90.

The NewClientPipe docs say it can be used to connect to an SFTP server over TCP/TLS, but the only example runs the ssh command. This adds ExampleNewClientPipe_tls (tls.Dial and pass the connection to NewClientPipe) and ExampleNewServer_tls (tls.Listen and NewServer on each accepted connection).

Since SFTP has no authentication of its own, both use client certificates (RequireAndVerifyClientCert on the server), and the comments say that plain TCP has neither encryption nor authentication. The server example also points out that NewServer gives clients the local filesystem as the process's user, and suggests NewRequestServer to restrict that.

I ran the same code end to end with a throwaway CA: listing a directory over TLS works, and a client without a certificate is rejected during the handshake.

These are added at the end of example_test.go, so they don't overlap with #688, which adds another NewClientPipe example after the existing one.

The NewClientPipe docs say it can be used over TCP/TLS, but the only
example runs the ssh command. Add a client and a server example using
crypto/tls with client certificates, since SFTP has no authentication
of its own, and point out that NewServer exposes the local filesystem.
Comment thread example_test.go Resolved
Comment thread example_test.go Outdated
if err := server.Serve(); err != nil && err != io.EOF {
log.Print("sftp server completed with error: ", err)
}
server.Close()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This receiver method does not seem to exist:

Image

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Changed the cleanup to call conn.Close() after server.Serve() returns. This closes the accepted TLS connection explicitly and makes the example’s connection cleanup clear.

@christhomas

Copy link
Copy Markdown
Contributor Author

The pipeline failure came from go vet treating ExampleNewClientPipeTLS and ExampleNewServerTLS as examples for nonexistent APIs. The client example now uses the supported _tls suffix because ExampleNewClientPipe already exists; the unique server example is named ExampleNewServer. The latest change is commit 3b72e6a.

@puellanivis
puellanivis merged commit 3dcb47e into pkg:master Nov 5, 2026
4 checks passed
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.

2 participants