@@ -93,23 +93,24 @@ func (c *etcdChecker) doCheck(hostport string) utils.State {
9393// findDestination finds a reachable destination for the sshd server according
9494// to the etcd database if available or the routes and route_select algorithm.
9595// It returns a string with the service name, a string with host:port, a string
96- // containing ForceCommand value and a bool containing CommandMustMatch; a
96+ // containing ForceCommand value, a bool containing CommandMustMatch, an int64
97+ // with etcd_keyttl and a map of strings with the environment variables; a
9798// string with the service name and an empty string if no destination is found
9899// or an error if any.
99- func findDestination (cli * utils.Client , username string , routes map [string ]* utils.RouteConfig , sshdHostport string , checkInterval utils.Duration ) (string , string , string , bool , error ) {
100+ func findDestination (cli * utils.Client , username string , routes map [string ]* utils.RouteConfig , sshdHostport string , checkInterval utils.Duration ) (string , string , string , bool , int64 , map [ string ] string , error ) {
100101 checker := & etcdChecker {
101102 checkInterval : checkInterval ,
102103 cli : cli ,
103104 }
104105
105106 service , err := findService (routes , sshdHostport )
106107 if err != nil {
107- return "" , "" , "" , false , err
108+ return "" , "" , "" , false , 0 , nil , err
108109 }
109110 key := fmt .Sprintf ("%s@%s" , username , service )
110111
111112 if routes [service ].Mode == "sticky" && cli != nil && cli .IsAlive () {
112- dest , err := cli .GetDestination (key )
113+ dest , err := cli .GetDestination (key , routes [ service ]. EtcdKeyTTL )
113114 if err != nil {
114115 if err != utils .ErrKeyNotFound {
115116 log .Errorf ("problem with etcd: %v" , err )
@@ -118,7 +119,7 @@ func findDestination(cli *utils.Client, username string, routes map[string]*util
118119 if utils .IsDestinationInRoutes (dest , routes [service ].Dest ) {
119120 if checker .Check (dest ) {
120121 log .Debugf ("found destination in etcd: %s" , dest )
121- return service , dest , routes [service ].ForceCommand , routes [service ].CommandMustMatch , nil
122+ return service , dest , routes [service ].ForceCommand , routes [service ].CommandMustMatch , routes [ service ]. EtcdKeyTTL , routes [ service ]. Environment , nil
122123 }
123124 log .Infof ("cannot connect %s to already existing connection(s) to %s: host %s" , key , dest , checker .LastState )
124125 } else {
@@ -129,10 +130,10 @@ func findDestination(cli *utils.Client, username string, routes map[string]*util
129130
130131 if len (routes [service ].Dest ) > 0 {
131132 selected , err := utils .SelectRoute (routes [service ].RouteSelect , routes [service ].Dest , checker , cli , key )
132- return service , selected , routes [service ].ForceCommand , routes [service ].CommandMustMatch , err
133+ return service , selected , routes [service ].ForceCommand , routes [service ].CommandMustMatch , routes [ service ]. EtcdKeyTTL , routes [ service ]. Environment , err
133134 }
134135
135- return service , "" , "" , false , fmt .Errorf ("no destination set for service %s" , service )
136+ return service , "" , "" , false , 0 , nil , fmt .Errorf ("no destination set for service %s" , service )
136137}
137138
138139// findService finds the first service containing a suitable source in the conf,
@@ -326,7 +327,7 @@ func mainExitCode() int {
326327 log .Errorf ("Cannot contact etcd cluster to update state: %v" , err )
327328 }
328329
329- service , hostport , forceCommand , commandMustMatch , err := findDestination (cli , username , config .Routes , sshInfos .Dst (), config .CheckInterval )
330+ service , hostport , forceCommand , commandMustMatch , etcdKeyTTL , environment , err := findDestination (cli , username , config .Routes , sshInfos .Dst (), config .CheckInterval )
330331 switch {
331332 case err != nil :
332333 log .Fatalf ("Finding destination: %s" , err )
@@ -345,6 +346,8 @@ func mainExitCode() int {
345346 log .Fatalf ("Invalid destination '%s': %s" , hostport , err )
346347 }
347348
349+ setEnvironment (environment )
350+
348351 // waitgroup and channel to stop our background command when exiting.
349352 var wg sync.WaitGroup
350353 ctx , cancel := context .WithCancel (context .Background ())
@@ -366,7 +369,7 @@ func mainExitCode() int {
366369 // Register destination in etcd and keep it alive while running.
367370 if cli != nil && cli .IsAlive () {
368371 key := fmt .Sprintf ("%s@%s" , username , service )
369- keepAliveChan , eP , err := cli .SetDestination (ctx , key , sshInfos .Dst (), hostport )
372+ keepAliveChan , eP , err := cli .SetDestination (ctx , key , sshInfos .Dst (), hostport , etcdKeyTTL )
370373 etcdPath = eP
371374 if err != nil {
372375 log .Warningf ("setting destination in etcd: %v" , err )
0 commit comments