Skip to content

Commit 680a941

Browse files
author
xdonggao
committed
fix(platform): compatible with certificate authentication when the target cluster is global the user and tenantID need to be set in the header
1 parent 3f86e94 commit 680a941

File tree

1 file changed

+23
-8
lines changed
  • pkg/platform/registry/cluster/storage

1 file changed

+23
-8
lines changed

pkg/platform/registry/cluster/storage/proxy.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import (
2929
"strconv"
3030
"strings"
3131
"time"
32+
"tkestack.io/tke/pkg/apiserver/authentication"
33+
"tkestack.io/tke/pkg/platform/proxy"
3234

3335
"k8s.io/apimachinery/pkg/api/errors"
3436
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -38,7 +40,6 @@ import (
3840
platforminternalclient "tkestack.io/tke/api/client/clientset/internalversion/typed/platform/internalversion"
3941
"tkestack.io/tke/api/platform"
4042
"tkestack.io/tke/pkg/platform/apiserver/filter"
41-
"tkestack.io/tke/pkg/platform/proxy"
4243
"tkestack.io/tke/pkg/platform/util"
4344
)
4445

@@ -85,17 +86,27 @@ func (r *ProxyREST) Connect(ctx context.Context, clusterName string, opts runtim
8586
if err != nil {
8687
return nil, errors.NewInternalError(err)
8788
}
88-
if config.BearerToken == "" {
89-
return nil, errors.NewInternalError(fmt.Errorf("%s has NO BearerToken", clusterName))
90-
}
9189

90+
userName, tenantID := authentication.UsernameAndTenantID(ctx)
9291
uri, err := makeURL(config.Host, proxyOpts.Path)
9392
if err != nil {
9493
return nil, errors.NewBadRequest(err.Error())
9594
}
95+
TLSClientConfig := &tls.Config{}
96+
TLSClientConfig.InsecureSkipVerify = true
97+
98+
if config.TLSClientConfig.CertData != nil && config.TLSClientConfig.KeyData != nil {
99+
cert, err := tls.X509KeyPair(config.TLSClientConfig.CertData, config.TLSClientConfig.KeyData)
100+
if err != nil {
101+
return nil, err
102+
}
103+
TLSClientConfig.Certificates = []tls.Certificate{cert}
104+
} else if config.BearerToken == "" {
105+
return nil, errors.NewInternalError(fmt.Errorf("%s has NO BearerToken", clusterName))
106+
}
96107

97108
return &httputil.ReverseProxy{
98-
Director: makeDirector(cluster.ObjectMeta.Name, uri, config.BearerToken),
109+
Director: makeDirector(cluster.ObjectMeta.Name, userName, tenantID, uri, config.BearerToken),
99110
Transport: &http.Transport{
100111
DialContext: (&net.Dialer{
101112
Timeout: 30 * time.Second,
@@ -105,7 +116,7 @@ func (r *ProxyREST) Connect(ctx context.Context, clusterName string, opts runtim
105116
IdleConnTimeout: 90 * time.Second,
106117
TLSHandshakeTimeout: 10 * time.Second,
107118
ExpectContinueTimeout: 1 * time.Second,
108-
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
119+
TLSClientConfig: TLSClientConfig,
109120
},
110121
}, nil
111122
}
@@ -115,10 +126,14 @@ func (r *ProxyREST) New() runtime.Object {
115126
return &platform.HelmProxyOptions{}
116127
}
117128

118-
func makeDirector(clusterName string, uri *url.URL, token string) func(req *http.Request) {
129+
func makeDirector(clusterName, userName, tenantID string, uri *url.URL, token string) func(req *http.Request) {
119130
return func(req *http.Request) {
120131
req.Header.Set(filter.ClusterNameHeaderKey, clusterName)
121-
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
132+
req.Header.Set("X-Remote-User", userName)
133+
req.Header.Set("X-Remote-Extra-TenantID", tenantID)
134+
if token != "" {
135+
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
136+
}
122137
req.URL = uri
123138
}
124139
}

0 commit comments

Comments
 (0)