Skip to content

Commit 6f9c775

Browse files
authored
fix: invalid token and host for ProxyRest (#1234)
1 parent bea4a7c commit 6f9c775

File tree

3 files changed

+38
-28
lines changed

3 files changed

+38
-28
lines changed

Diff for: pkg/platform/proxy/client.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ func makeClientKey(username string, groups []string) string {
5656
return fmt.Sprintf("%s###%v", username, groups)
5757
}
5858

59-
func ClientSet(ctx context.Context, platformClient platforminternalclient.PlatformInterface) (*kubernetes.Clientset,
60-
error) {
59+
func GetConfig(ctx context.Context, platformClient platforminternalclient.PlatformInterface) (*rest.Config, error) {
6160
clusterName := filter.ClusterFrom(ctx)
6261
if clusterName == "" {
6362
return nil, errors.NewBadRequest("clusterName is required")
@@ -99,6 +98,15 @@ func ClientSet(ctx context.Context, platformClient platforminternalclient.Platfo
9998
}
10099
}
101100

101+
return config, nil
102+
}
103+
104+
func ClientSet(ctx context.Context, platformClient platforminternalclient.PlatformInterface) (*kubernetes.Clientset,
105+
error) {
106+
config, err := GetConfig(ctx, platformClient)
107+
if err != nil {
108+
return nil, err
109+
}
102110
return kubernetes.NewForConfig(config)
103111
}
104112

Diff for: pkg/platform/registry/cluster/storage/proxy.go

+25-24
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,23 @@ import (
2222
"context"
2323
"crypto/tls"
2424
"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"
3125
"net"
3226
"net/http"
3327
"net/http/httputil"
3428
"net/url"
3529
"strconv"
3630
"strings"
3731
"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"
3839
"tkestack.io/tke/api/platform"
39-
"tkestack.io/tke/pkg/apiserver/authentication/authenticator/localtrust"
4040
"tkestack.io/tke/pkg/platform/apiserver/filter"
41+
"tkestack.io/tke/pkg/platform/proxy"
4142
"tkestack.io/tke/pkg/platform/util"
4243
)
4344

@@ -46,6 +47,8 @@ type ProxyREST struct {
4647
rest.Storage
4748
store *registry.Store
4849
host string
50+
51+
platformClient platforminternalclient.PlatformInterface
4952
}
5053

5154
// ConnectMethods returns the list of HTTP methods that can be proxied
@@ -59,7 +62,7 @@ func (r *ProxyREST) NewConnectOptions() (runtime.Object, bool, string) {
5962
}
6063

6164
// 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) {
6366
clusterObject, err := r.store.Get(ctx, clusterName, &metav1.GetOptions{})
6467
if err != nil {
6568
return nil, err
@@ -78,22 +81,21 @@ func (r *ProxyREST) Connect(ctx context.Context, clusterName string, opts runtim
7881
return nil, errors.NewBadRequest("cycle dispatch")
7982
}
8083

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)
8685
if err != nil {
8786
return nil, errors.NewInternalError(err)
8887
}
88+
if config.BearerToken == "" {
89+
return nil, errors.NewInternalError(fmt.Errorf("%s has NO BearerToken", clusterName))
90+
}
8991

90-
uri, err := makeURL(r.host, proxyOpts.Path)
92+
uri, err := makeURL(config.Host, proxyOpts.Path)
9193
if err != nil {
9294
return nil, errors.NewBadRequest(err.Error())
9395
}
9496

9597
return &httputil.ReverseProxy{
96-
Director: makeDirector(cluster.ObjectMeta.Name, uri, token),
98+
Director: makeDirector(cluster.ObjectMeta.Name, uri, config.BearerToken),
9799
Transport: &http.Transport{
98100
DialContext: (&net.Dialer{
99101
Timeout: 30 * time.Second,
@@ -124,17 +126,16 @@ func makeDirector(clusterName string, uri *url.URL, token string) func(req *http
124126
func makeURL(host, path string) (*url.URL, error) {
125127
var port int64
126128
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])
135136
}
136137

137138
p := strings.TrimPrefix(path, "/")
138139

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))
140141
}

Diff for: pkg/platform/registry/cluster/storage/storage.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,9 @@ func NewStorage(optsGetter genericregistry.RESTOptionsGetter, platformClient pla
162162
platformClient: platformClient,
163163
},
164164
Proxy: &ProxyREST{
165-
store: store,
166-
host: host,
165+
store: store,
166+
host: host,
167+
platformClient: platformClient,
167168
},
168169
}
169170
}

0 commit comments

Comments
 (0)