Sample code for 30+ languages & platforms
Tcl

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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

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

set tunnel [new_CkSshTunnel]

#  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.).
CkSshTunnel_put_EnableSecrets $tunnel 1

CkSshTunnel_put_DestHostname $tunnel "db.internal.example.com"
CkSshTunnel_put_DestPort $tunnel 5432

set sshPort 22
set success [CkSshTunnel_Connect $tunnel "ssh.example.com" $sshPort]
if {$success == 0} then {
    puts [CkSshTunnel_lastErrorText $tunnel]
    delete_CkSshTunnel $tunnel
    exit
}

#  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.
set password "!!my_ssh_password_secret"
set success [CkSshTunnel_AuthenticatePw $tunnel "mySshLogin" $password]
if {$success == 0} then {
    puts [CkSshTunnel_lastErrorText $tunnel]
    delete_CkSshTunnel $tunnel
    exit
}

set listenPort 1080
set success [CkSshTunnel_BeginAccepting $tunnel $listenPort]
if {$success == 0} then {
    puts [CkSshTunnel_lastErrorText $tunnel]
    delete_CkSshTunnel $tunnel
    exit
}

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

set waitForThreadExit 1
set success [CkSshTunnel_CloseTunnel $tunnel $waitForThreadExit]
if {$success == 0} then {
    puts [CkSshTunnel_lastErrorText $tunnel]
    delete_CkSshTunnel $tunnel
    exit
}


delete_CkSshTunnel $tunnel