@@ -22,22 +22,23 @@ import (
22
22
"context"
23
23
"crypto/tls"
24
24
"fmt"
25
- "k8s.io/apimachinery/pkg/api/errors"
26
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27
- "k8s.io/apimachinery/pkg/runtime"
28
- "k8s.io/apiserver/pkg/endpoints/request"
29
- "k8s.io/apiserver/pkg/registry/generic/registry"
30
- "k8s.io/apiserver/pkg/registry/rest"
31
25
"net"
32
26
"net/http"
33
27
"net/http/httputil"
34
28
"net/url"
35
29
"strconv"
36
30
"strings"
37
31
"time"
32
+
33
+ "k8s.io/apimachinery/pkg/api/errors"
34
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
35
+ "k8s.io/apimachinery/pkg/runtime"
36
+ "k8s.io/apiserver/pkg/registry/generic/registry"
37
+ "k8s.io/apiserver/pkg/registry/rest"
38
+ platforminternalclient "tkestack.io/tke/api/client/clientset/internalversion/typed/platform/internalversion"
38
39
"tkestack.io/tke/api/platform"
39
- "tkestack.io/tke/pkg/apiserver/authentication/authenticator/localtrust"
40
40
"tkestack.io/tke/pkg/platform/apiserver/filter"
41
+ "tkestack.io/tke/pkg/platform/proxy"
41
42
"tkestack.io/tke/pkg/platform/util"
42
43
)
43
44
@@ -46,6 +47,8 @@ type ProxyREST struct {
46
47
rest.Storage
47
48
store * registry.Store
48
49
host string
50
+
51
+ platformClient platforminternalclient.PlatformInterface
49
52
}
50
53
51
54
// ConnectMethods returns the list of HTTP methods that can be proxied
@@ -59,7 +62,7 @@ func (r *ProxyREST) NewConnectOptions() (runtime.Object, bool, string) {
59
62
}
60
63
61
64
// Connect returns a handler for the native api proxy
62
- func (r * ProxyREST ) Connect (ctx context.Context , clusterName string , opts runtime.Object , responder rest.Responder ) (http.Handler , error ) {
65
+ func (r * ProxyREST ) Connect (ctx context.Context , clusterName string , opts runtime.Object , _ rest.Responder ) (http.Handler , error ) {
63
66
clusterObject , err := r .store .Get (ctx , clusterName , & metav1.GetOptions {})
64
67
if err != nil {
65
68
return nil , err
@@ -78,22 +81,21 @@ func (r *ProxyREST) Connect(ctx context.Context, clusterName string, opts runtim
78
81
return nil , errors .NewBadRequest ("cycle dispatch" )
79
82
}
80
83
81
- u , ok := request .UserFrom (ctx )
82
- if ! ok {
83
- return nil , errors .NewUnauthorized ("unknown user" )
84
- }
85
- token , err := localtrust .GenerateToken (u )
84
+ config , err := proxy .GetConfig (ctx , r .platformClient )
86
85
if err != nil {
87
86
return nil , errors .NewInternalError (err )
88
87
}
88
+ if config .BearerToken == "" {
89
+ return nil , errors .NewInternalError (fmt .Errorf ("%s has NO BearerToken" , clusterName ))
90
+ }
89
91
90
- uri , err := makeURL (r . host , proxyOpts .Path )
92
+ uri , err := makeURL (config . Host , proxyOpts .Path )
91
93
if err != nil {
92
94
return nil , errors .NewBadRequest (err .Error ())
93
95
}
94
96
95
97
return & httputil.ReverseProxy {
96
- Director : makeDirector (cluster .ObjectMeta .Name , uri , token ),
98
+ Director : makeDirector (cluster .ObjectMeta .Name , uri , config . BearerToken ),
97
99
Transport : & http.Transport {
98
100
DialContext : (& net.Dialer {
99
101
Timeout : 30 * time .Second ,
@@ -124,17 +126,16 @@ func makeDirector(clusterName string, uri *url.URL, token string) func(req *http
124
126
func makeURL (host , path string ) (* url.URL , error ) {
125
127
var port int64
126
128
hostSegment := strings .Split (host , ":" )
127
- if len (hostSegment ) == 0 {
128
- port = 443
129
- } else {
130
- var err error
131
- port , err = strconv .ParseInt (hostSegment [len (hostSegment )- 1 ], 10 , 32 )
132
- if err != nil {
133
- port = 443
134
- }
129
+ if len (hostSegment ) != 2 {
130
+ return nil , fmt .Errorf ("invalid host %s" , host )
131
+ }
132
+ var err error
133
+ port , err = strconv .ParseInt (hostSegment [1 ], 10 , 32 )
134
+ if err != nil {
135
+ return nil , fmt .Errorf ("invalid host port %s" , hostSegment [1 ])
135
136
}
136
137
137
138
p := strings .TrimPrefix (path , "/" )
138
139
139
- return url .Parse (fmt .Sprintf ("https://127.0.0.1 :%d/%s" , port , p ))
140
+ return url .Parse (fmt .Sprintf ("https://%s :%d/%s" , hostSegment [ 0 ] , port , p ))
140
141
}
0 commit comments