@@ -7,19 +7,34 @@ import (
7
7
commandutil "github.com/choerodon/choerodon-cluster-agent/pkg/util/command"
8
8
"github.com/golang/glog"
9
9
"io/ioutil"
10
+ "net/url"
10
11
"os"
11
12
"strings"
12
13
)
13
14
14
15
const sshKeyPath = "/ssh-keys"
15
16
16
17
// 这个变量保存需要被替换的ssh地址
17
- var SshUrlMap = make (map [string ]string )
18
+ var SshRewriteUrlMap = make (map [string ]string )
19
+ var sshProxyMap = make (map [string ]string )
18
20
19
21
func init () {
20
22
rawOriginSshUrl := os .Getenv ("ORIGIN_SSH_URL" )
21
23
rawOverrideSshUrl := os .Getenv ("REWRITE_SSH_URL" )
22
24
if len (rawOriginSshUrl ) == 0 || len (rawOverrideSshUrl ) == 0 {
25
+ if sshProxy , exist := os .LookupEnv ("SSH_PROXY" ); exist && sshProxy != "" {
26
+ url , err := url .Parse (sshProxy )
27
+ if err != nil {
28
+ glog .Error (err .Error ())
29
+ os .Exit (1 )
30
+ }
31
+ sshProxyMap ["schema" ] = url .Scheme
32
+ if url .User != nil {
33
+ sshProxyMap ["username" ] = url .User .Username ()
34
+ sshProxyMap ["password" ], _ = url .User .Password ()
35
+ }
36
+ sshProxyMap ["host" ] = url .Host
37
+ }
23
38
return
24
39
}
25
40
@@ -29,7 +44,7 @@ func init() {
29
44
return
30
45
}
31
46
for index , originSshUrl := range originSshUrls {
32
- SshUrlMap [originSshUrl ] = overrideSshUrls [index ]
47
+ SshRewriteUrlMap [originSshUrl ] = overrideSshUrls [index ]
33
48
}
34
49
}
35
50
@@ -79,8 +94,8 @@ func writeSSHkey(fileName, key string) error {
79
94
}
80
95
81
96
func config (host , namespace string ) string {
82
- if rewriteUrl , ok := SshUrlMap [host ]; ok {
83
- glog .Infof ("origin host %s has been rewrited to %s" ,host ,rewriteUrl )
97
+ if rewriteUrl , ok := SshRewriteUrlMap [host ]; ok {
98
+ glog .Infof ("origin host %s has been rewrited to %s" , host , rewriteUrl )
84
99
host = rewriteUrl
85
100
}
86
101
@@ -99,6 +114,13 @@ func config(host, namespace string) string {
99
114
result = result + fmt .Sprintf (" UserKnownHostsFile /dev/null\n " )
100
115
result = result + fmt .Sprintf (" IdentityFile %s/rsa-%s\n " , sshKeyPath , namespace )
101
116
result = result + fmt .Sprintf (" LogLevel error\n " )
117
+ if len (SshRewriteUrlMap ) == 0 && len (sshProxyMap ) != 0 {
118
+ result = result + fmt .Sprintf (" ProxyCommand ncat --proxy-type %s --proxy-auth %s:%s --proxy %s %%h %%p\n " ,
119
+ sshProxyMap ["schema" ],
120
+ sshProxyMap ["username" ],
121
+ sshProxyMap ["password" ],
122
+ sshProxyMap ["host" ])
123
+ }
102
124
return result
103
125
}
104
126
0 commit comments