Sample code for 30+ languages & platforms
Xbase++

SSH Tunnel Credentials from OS Secure Storage

See more SSH Tunnel Examples

Demonstrates the EnableSecrets property, which enables automatic resolution of credentials from the operating system's secure storage. When enabled, password properties and methods may receive a "secret specification string" beginning with !! instead of a literal password.

Background: This is a cleaner alternative to reading a secret yourself and passing it in: with EnableSecrets on, you hand Chilkat a reference like !!my_secret_name and it fetches the actual value from the platform's secure store — Windows Credential Manager, Apple Keychain, and so on. The literal password never appears in your source or configuration, which is exactly the practice recommended throughout these examples.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oTunnel
LOCAL nSshPort
LOCAL cPassword
LOCAL nListenPort
LOCAL nWaitForThreadExit

nSuccess := 0

//  Demonstrates the SshTunnel.EnableSecrets property, which enables automatic resolution of
//  credentials from the operating system's secure storage.

oTunnel := CreateObject("Chilkat.SshTunnel")

//  When EnableSecrets is 1, supported password properties and methods may receive a "secret
//  specification string" beginning with "!!" instead of a literal password.  Chilkat resolves the
//  secret from the OS secure store (Windows Credential Manager, Apple Keychain, etc.).
oTunnel:EnableSecrets := 1

oTunnel:DestHostname := "db.internal.example.com"
oTunnel:DestPort := 5432

nSshPort := 22
nSuccess := oTunnel:Connect("ssh.example.com", nSshPort)
IF (nSuccess == 0)
    ? oTunnel:LastErrorText
    oTunnel:destroy()
    RETURN
ENDIF

//  Pass a secret specification instead of a literal password.  Chilkat looks up the named secret
//  in the OS secure store rather than using this text directly.
cPassword := "!!my_ssh_password_secret"
nSuccess := oTunnel:AuthenticatePw("mySshLogin", cPassword)
IF (nSuccess == 0)
    ? oTunnel:LastErrorText
    oTunnel:destroy()
    RETURN
ENDIF

nListenPort := 1080
nSuccess := oTunnel:BeginAccepting(nListenPort)
IF (nSuccess == 0)
    ? oTunnel:LastErrorText
    oTunnel:destroy()
    RETURN
ENDIF

? "Authenticated using a secret from OS secure storage."

nWaitForThreadExit := 1
nSuccess := oTunnel:CloseTunnel(nWaitForThreadExit)
IF (nSuccess == 0)
    ? oTunnel:LastErrorText
    oTunnel:destroy()
    RETURN
ENDIF

oTunnel:destroy()