Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
5bdc2077
Unverified
Commit
5bdc2077
authored
Dec 11, 2018
by
Kubernetes Prow Robot
Committed by
GitHub
Dec 11, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #71723 from liggitt/automated-cherry-pick-of-#71713-upstream-release-1.13
Automated cherry pick of #71713, #71857: Plumb token and token file through rest.Config
parents
aa277804
6b11adcc
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
73 additions
and
38 deletions
+73
-38
authentication.go
...g/src/k8s.io/apiserver/pkg/util/webhook/authentication.go
+1
-0
BUILD
staging/src/k8s.io/client-go/rest/BUILD
+0
-4
config.go
staging/src/k8s.io/client-go/rest/config.go
+16
-10
config_test.go
staging/src/k8s.io/client-go/rest/config_test.go
+1
-0
client_config.go
...ing/src/k8s.io/client-go/tools/clientcmd/client_config.go
+4
-3
client_config_test.go
...rc/k8s.io/client-go/tools/clientcmd/client_config_test.go
+1
-14
BUILD
staging/src/k8s.io/client-go/transport/BUILD
+4
-0
config.go
staging/src/k8s.io/client-go/transport/config.go
+6
-1
round_trippers.go
staging/src/k8s.io/client-go/transport/round_trippers.go
+36
-3
token_source.go
staging/src/k8s.io/client-go/transport/token_source.go
+1
-1
token_source_test.go
staging/src/k8s.io/client-go/transport/token_source_test.go
+1
-1
test_context.go
test/e2e/framework/test_context.go
+2
-1
No files found.
staging/src/k8s.io/apiserver/pkg/util/webhook/authentication.go
View file @
5bdc2077
...
...
@@ -177,6 +177,7 @@ func restConfigFromKubeconfig(configAuthInfo *clientcmdapi.AuthInfo) (*rest.Conf
return
nil
,
err
}
config
.
BearerToken
=
string
(
tokenBytes
)
config
.
BearerTokenFile
=
configAuthInfo
.
TokenFile
}
if
len
(
configAuthInfo
.
Impersonate
)
>
0
{
config
.
Impersonate
=
rest
.
ImpersonationConfig
{
...
...
staging/src/k8s.io/client-go/rest/BUILD
View file @
5bdc2077
...
...
@@ -13,7 +13,6 @@ go_test(
"config_test.go",
"plugin_test.go",
"request_test.go",
"token_source_test.go",
"url_utils_test.go",
"urlbackoff_test.go",
],
...
...
@@ -41,7 +40,6 @@ go_test(
"//staging/src/k8s.io/client-go/util/testing:go_default_library",
"//vendor/github.com/google/gofuzz:go_default_library",
"//vendor/github.com/stretchr/testify/assert:go_default_library",
"//vendor/golang.org/x/oauth2:go_default_library",
"//vendor/k8s.io/klog:go_default_library",
],
)
...
...
@@ -53,7 +51,6 @@ go_library(
"config.go",
"plugin.go",
"request.go",
"token_source.go",
"transport.go",
"url_utils.go",
"urlbackoff.go",
...
...
@@ -80,7 +77,6 @@ go_library(
"//staging/src/k8s.io/client-go/util/cert:go_default_library",
"//staging/src/k8s.io/client-go/util/flowcontrol:go_default_library",
"//vendor/golang.org/x/net/http2:go_default_library",
"//vendor/golang.org/x/oauth2:go_default_library",
"//vendor/k8s.io/klog:go_default_library",
],
)
...
...
staging/src/k8s.io/client-go/rest/config.go
View file @
5bdc2077
...
...
@@ -70,6 +70,11 @@ type Config struct {
// TODO: demonstrate an OAuth2 compatible client.
BearerToken
string
// Path to a file containing a BearerToken.
// If set, the contents are periodically read.
// The last successfully read value takes precedence over BearerToken.
BearerTokenFile
string
// Impersonate is the configuration that RESTClient will use for impersonation.
Impersonate
ImpersonationConfig
...
...
@@ -322,9 +327,8 @@ func InClusterConfig() (*Config, error) {
return
nil
,
ErrNotInCluster
}
ts
:=
NewCachedFileTokenSource
(
tokenFile
)
if
_
,
err
:=
ts
.
Token
();
err
!=
nil
{
token
,
err
:=
ioutil
.
ReadFile
(
tokenFile
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -340,7 +344,8 @@ func InClusterConfig() (*Config, error) {
// TODO: switch to using cluster DNS.
Host
:
"https://"
+
net
.
JoinHostPort
(
host
,
port
),
TLSClientConfig
:
tlsClientConfig
,
WrapTransport
:
TokenSourceWrapTransport
(
ts
),
BearerToken
:
string
(
token
),
BearerTokenFile
:
tokenFile
,
},
nil
}
...
...
@@ -430,12 +435,13 @@ func AnonymousClientConfig(config *Config) *Config {
// CopyConfig returns a copy of the given config
func
CopyConfig
(
config
*
Config
)
*
Config
{
return
&
Config
{
Host
:
config
.
Host
,
APIPath
:
config
.
APIPath
,
ContentConfig
:
config
.
ContentConfig
,
Username
:
config
.
Username
,
Password
:
config
.
Password
,
BearerToken
:
config
.
BearerToken
,
Host
:
config
.
Host
,
APIPath
:
config
.
APIPath
,
ContentConfig
:
config
.
ContentConfig
,
Username
:
config
.
Username
,
Password
:
config
.
Password
,
BearerToken
:
config
.
BearerToken
,
BearerTokenFile
:
config
.
BearerTokenFile
,
Impersonate
:
ImpersonationConfig
{
Groups
:
config
.
Impersonate
.
Groups
,
Extra
:
config
.
Impersonate
.
Extra
,
...
...
staging/src/k8s.io/client-go/rest/config_test.go
View file @
5bdc2077
...
...
@@ -264,6 +264,7 @@ func TestAnonymousConfig(t *testing.T) {
// is added to Config, update AnonymousClientConfig to preserve the field otherwise.
expected
.
Impersonate
=
ImpersonationConfig
{}
expected
.
BearerToken
=
""
expected
.
BearerTokenFile
=
""
expected
.
Username
=
""
expected
.
Password
=
""
expected
.
AuthProvider
=
nil
...
...
staging/src/k8s.io/client-go/tools/clientcmd/client_config.go
View file @
5bdc2077
...
...
@@ -229,11 +229,12 @@ func (config *DirectClientConfig) getUserIdentificationPartialConfig(configAuthI
if
len
(
configAuthInfo
.
Token
)
>
0
{
mergedConfig
.
BearerToken
=
configAuthInfo
.
Token
}
else
if
len
(
configAuthInfo
.
TokenFile
)
>
0
{
t
s
:=
restclient
.
NewCachedFileTokenSourc
e
(
configAuthInfo
.
TokenFile
)
if
_
,
err
:=
ts
.
Token
();
err
!=
nil
{
t
okenBytes
,
err
:=
ioutil
.
ReadFil
e
(
configAuthInfo
.
TokenFile
)
if
err
!=
nil
{
return
nil
,
err
}
mergedConfig
.
WrapTransport
=
restclient
.
TokenSourceWrapTransport
(
ts
)
mergedConfig
.
BearerToken
=
string
(
tokenBytes
)
mergedConfig
.
BearerTokenFile
=
configAuthInfo
.
TokenFile
}
if
len
(
configAuthInfo
.
Impersonate
)
>
0
{
mergedConfig
.
Impersonate
=
restclient
.
ImpersonationConfig
{
...
...
staging/src/k8s.io/client-go/tools/clientcmd/client_config_test.go
View file @
5bdc2077
...
...
@@ -18,7 +18,6 @@ package clientcmd
import
(
"io/ioutil"
"net/http"
"os"
"reflect"
"strings"
...
...
@@ -334,19 +333,7 @@ func TestBasicTokenFile(t *testing.T) {
t
.
Fatalf
(
"Unexpected error: %v"
,
err
)
}
var
out
*
http
.
Request
clientConfig
.
WrapTransport
(
fakeTransport
(
func
(
req
*
http
.
Request
)
(
*
http
.
Response
,
error
)
{
out
=
req
return
&
http
.
Response
{},
nil
}))
.
RoundTrip
(
&
http
.
Request
{})
matchStringArg
(
token
,
strings
.
TrimPrefix
(
out
.
Header
.
Get
(
"Authorization"
),
"Bearer "
),
t
)
}
type
fakeTransport
func
(
*
http
.
Request
)
(
*
http
.
Response
,
error
)
func
(
ft
fakeTransport
)
RoundTrip
(
req
*
http
.
Request
)
(
*
http
.
Response
,
error
)
{
return
ft
(
req
)
matchStringArg
(
token
,
clientConfig
.
BearerToken
,
t
)
}
func
TestPrecedenceTokenFile
(
t
*
testing
.
T
)
{
...
...
staging/src/k8s.io/client-go/transport/BUILD
View file @
5bdc2077
...
...
@@ -11,9 +11,11 @@ go_test(
srcs = [
"cache_test.go",
"round_trippers_test.go",
"token_source_test.go",
"transport_test.go",
],
embed = [":go_default_library"],
deps = ["//vendor/golang.org/x/oauth2:go_default_library"],
)
go_library(
...
...
@@ -22,12 +24,14 @@ go_library(
"cache.go",
"config.go",
"round_trippers.go",
"token_source.go",
"transport.go",
],
importmap = "k8s.io/kubernetes/vendor/k8s.io/client-go/transport",
importpath = "k8s.io/client-go/transport",
deps = [
"//staging/src/k8s.io/apimachinery/pkg/util/net:go_default_library",
"//vendor/golang.org/x/oauth2:go_default_library",
"//vendor/k8s.io/klog:go_default_library",
],
)
...
...
staging/src/k8s.io/client-go/transport/config.go
View file @
5bdc2077
...
...
@@ -39,6 +39,11 @@ type Config struct {
// Bearer token for authentication
BearerToken
string
// Path to a file containing a BearerToken.
// If set, the contents are periodically read.
// The last successfully read value takes precedence over BearerToken.
BearerTokenFile
string
// Impersonate is the config that this Config will impersonate using
Impersonate
ImpersonationConfig
...
...
@@ -80,7 +85,7 @@ func (c *Config) HasBasicAuth() bool {
// HasTokenAuth returns whether the configuration has token authentication or not.
func
(
c
*
Config
)
HasTokenAuth
()
bool
{
return
len
(
c
.
BearerToken
)
!=
0
return
len
(
c
.
BearerToken
)
!=
0
||
len
(
c
.
BearerTokenFile
)
!=
0
}
// HasCertAuth returns whether the configuration has certificate authentication or not.
...
...
staging/src/k8s.io/client-go/transport/round_trippers.go
View file @
5bdc2077
...
...
@@ -22,6 +22,7 @@ import (
"strings"
"time"
"golang.org/x/oauth2"
"k8s.io/klog"
utilnet
"k8s.io/apimachinery/pkg/util/net"
...
...
@@ -44,7 +45,11 @@ func HTTPWrappersForConfig(config *Config, rt http.RoundTripper) (http.RoundTrip
case
config
.
HasBasicAuth
()
&&
config
.
HasTokenAuth
()
:
return
nil
,
fmt
.
Errorf
(
"username/password or bearer token may be set, but not both"
)
case
config
.
HasTokenAuth
()
:
rt
=
NewBearerAuthRoundTripper
(
config
.
BearerToken
,
rt
)
var
err
error
rt
,
err
=
NewBearerAuthWithRefreshRoundTripper
(
config
.
BearerToken
,
config
.
BearerTokenFile
,
rt
)
if
err
!=
nil
{
return
nil
,
err
}
case
config
.
HasBasicAuth
()
:
rt
=
NewBasicAuthRoundTripper
(
config
.
Username
,
config
.
Password
,
rt
)
}
...
...
@@ -265,13 +270,35 @@ func (rt *impersonatingRoundTripper) WrappedRoundTripper() http.RoundTripper { r
type
bearerAuthRoundTripper
struct
{
bearer
string
source
oauth2
.
TokenSource
rt
http
.
RoundTripper
}
// NewBearerAuthRoundTripper adds the provided bearer token to a request
// unless the authorization header has already been set.
func
NewBearerAuthRoundTripper
(
bearer
string
,
rt
http
.
RoundTripper
)
http
.
RoundTripper
{
return
&
bearerAuthRoundTripper
{
bearer
,
rt
}
return
&
bearerAuthRoundTripper
{
bearer
,
nil
,
rt
}
}
// NewBearerAuthRoundTripper adds the provided bearer token to a request
// unless the authorization header has already been set.
// If tokenFile is non-empty, it is periodically read,
// and the last successfully read content is used as the bearer token.
// If tokenFile is non-empty and bearer is empty, the tokenFile is read
// immediately to populate the initial bearer token.
func
NewBearerAuthWithRefreshRoundTripper
(
bearer
string
,
tokenFile
string
,
rt
http
.
RoundTripper
)
(
http
.
RoundTripper
,
error
)
{
if
len
(
tokenFile
)
==
0
{
return
&
bearerAuthRoundTripper
{
bearer
,
nil
,
rt
},
nil
}
source
:=
NewCachedFileTokenSource
(
tokenFile
)
if
len
(
bearer
)
==
0
{
token
,
err
:=
source
.
Token
()
if
err
!=
nil
{
return
nil
,
err
}
bearer
=
token
.
AccessToken
}
return
&
bearerAuthRoundTripper
{
bearer
,
source
,
rt
},
nil
}
func
(
rt
*
bearerAuthRoundTripper
)
RoundTrip
(
req
*
http
.
Request
)
(
*
http
.
Response
,
error
)
{
...
...
@@ -280,7 +307,13 @@ func (rt *bearerAuthRoundTripper) RoundTrip(req *http.Request) (*http.Response,
}
req
=
utilnet
.
CloneRequest
(
req
)
req
.
Header
.
Set
(
"Authorization"
,
fmt
.
Sprintf
(
"Bearer %s"
,
rt
.
bearer
))
token
:=
rt
.
bearer
if
rt
.
source
!=
nil
{
if
refreshedToken
,
err
:=
rt
.
source
.
Token
();
err
==
nil
{
token
=
refreshedToken
.
AccessToken
}
}
req
.
Header
.
Set
(
"Authorization"
,
fmt
.
Sprintf
(
"Bearer %s"
,
token
))
return
rt
.
rt
.
RoundTrip
(
req
)
}
...
...
staging/src/k8s.io/client-go/
res
t/token_source.go
→
staging/src/k8s.io/client-go/
transpor
t/token_source.go
View file @
5bdc2077
...
...
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package
res
t
package
transpor
t
import
(
"fmt"
...
...
staging/src/k8s.io/client-go/
res
t/token_source_test.go
→
staging/src/k8s.io/client-go/
transpor
t/token_source_test.go
View file @
5bdc2077
...
...
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package
res
t
package
transpor
t
import
(
"fmt"
...
...
test/e2e/framework/test_context.go
View file @
5bdc2077
...
...
@@ -315,7 +315,8 @@ func createKubeConfig(clientCfg *restclient.Config) *clientcmdapi.Config {
config
:=
clientcmdapi
.
NewConfig
()
credentials
:=
clientcmdapi
.
NewAuthInfo
()
credentials
.
TokenFile
=
"/var/run/secrets/kubernetes.io/serviceaccount/token"
credentials
.
Token
=
clientCfg
.
BearerToken
credentials
.
TokenFile
=
clientCfg
.
BearerTokenFile
credentials
.
ClientCertificate
=
clientCfg
.
TLSClientConfig
.
CertFile
if
len
(
credentials
.
ClientCertificate
)
==
0
{
credentials
.
ClientCertificateData
=
clientCfg
.
TLSClientConfig
.
CertData
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment