mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-11 04:52:08 +00:00
Initial support for TokenFile in the client config.
This commit is contained in:
parent
b5ce23c48d
commit
50089f6c81
@ -88,6 +88,8 @@ type AuthInfo struct {
|
|||||||
ClientKeyData []byte `json:"client-key-data,omitempty"`
|
ClientKeyData []byte `json:"client-key-data,omitempty"`
|
||||||
// Token is the bearer token for authentication to the kubernetes cluster.
|
// Token is the bearer token for authentication to the kubernetes cluster.
|
||||||
Token string `json:"token,omitempty"`
|
Token string `json:"token,omitempty"`
|
||||||
|
// TokenFile is a pointer to a file that contains a bearer token (as described above). If both Token and TokenFile are present, Token takes precedence.
|
||||||
|
TokenFile string `json:"tokenFile,omitempty"`
|
||||||
// Impersonate is the username to act-as.
|
// Impersonate is the username to act-as.
|
||||||
Impersonate string `json:"act-as,omitempty"`
|
Impersonate string `json:"act-as,omitempty"`
|
||||||
// Username is the username for basic authentication to the kubernetes cluster.
|
// Username is the username for basic authentication to the kubernetes cluster.
|
||||||
|
@ -82,6 +82,8 @@ type AuthInfo struct {
|
|||||||
ClientKeyData []byte `json:"client-key-data,omitempty"`
|
ClientKeyData []byte `json:"client-key-data,omitempty"`
|
||||||
// Token is the bearer token for authentication to the kubernetes cluster.
|
// Token is the bearer token for authentication to the kubernetes cluster.
|
||||||
Token string `json:"token,omitempty"`
|
Token string `json:"token,omitempty"`
|
||||||
|
// TokenFile is a pointer to a file that contains a bearer token (as described above). If both Token and TokenFile are present, Token takes precedence.
|
||||||
|
TokenFile string `json:"tokenFile,omitempty"`
|
||||||
// Impersonate is the username to imperonate. The name matches the flag.
|
// Impersonate is the username to imperonate. The name matches the flag.
|
||||||
Impersonate string `json:"as,omitempty"`
|
Impersonate string `json:"as,omitempty"`
|
||||||
// Username is the username for basic authentication to the kubernetes cluster.
|
// Username is the username for basic authentication to the kubernetes cluster.
|
||||||
|
@ -167,6 +167,12 @@ func getUserIdentificationPartialConfig(configAuthInfo clientcmdapi.AuthInfo, fa
|
|||||||
// blindly overwrite existing values based on precedence
|
// blindly overwrite existing values based on precedence
|
||||||
if len(configAuthInfo.Token) > 0 {
|
if len(configAuthInfo.Token) > 0 {
|
||||||
mergedConfig.BearerToken = configAuthInfo.Token
|
mergedConfig.BearerToken = configAuthInfo.Token
|
||||||
|
} else if len(configAuthInfo.TokenFile) > 0 {
|
||||||
|
tokenBytes, err := ioutil.ReadFile(configAuthInfo.TokenFile)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
mergedConfig.BearerToken = string(tokenBytes)
|
||||||
}
|
}
|
||||||
if len(configAuthInfo.Impersonate) > 0 {
|
if len(configAuthInfo.Impersonate) > 0 {
|
||||||
mergedConfig.Impersonate = configAuthInfo.Impersonate
|
mergedConfig.Impersonate = configAuthInfo.Impersonate
|
||||||
|
@ -17,6 +17,8 @@ limitations under the License.
|
|||||||
package clientcmd
|
package clientcmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -186,6 +188,80 @@ func TestBasicAuthData(t *testing.T) {
|
|||||||
matchStringArg(password, clientConfig.Password, t)
|
matchStringArg(password, clientConfig.Password, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBasicTokenFile(t *testing.T) {
|
||||||
|
token := "exampletoken"
|
||||||
|
f, err := ioutil.TempFile("", "tokenfile")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Unexpected error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer os.Remove(f.Name())
|
||||||
|
if err := ioutil.WriteFile(f.Name(), []byte(token), 0644); err != nil {
|
||||||
|
t.Errorf("Unexpected error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
config := clientcmdapi.NewConfig()
|
||||||
|
config.Clusters["clean"] = &clientcmdapi.Cluster{
|
||||||
|
Server: "https://localhost:8443",
|
||||||
|
}
|
||||||
|
config.AuthInfos["clean"] = &clientcmdapi.AuthInfo{
|
||||||
|
TokenFile: f.Name(),
|
||||||
|
}
|
||||||
|
config.Contexts["clean"] = &clientcmdapi.Context{
|
||||||
|
Cluster: "clean",
|
||||||
|
AuthInfo: "clean",
|
||||||
|
}
|
||||||
|
config.CurrentContext = "clean"
|
||||||
|
|
||||||
|
clientBuilder := NewNonInteractiveClientConfig(*config, "clean", &ConfigOverrides{}, nil)
|
||||||
|
|
||||||
|
clientConfig, err := clientBuilder.ClientConfig()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
matchStringArg(token, clientConfig.BearerToken, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrecedenceTokenFile(t *testing.T) {
|
||||||
|
token := "exampletoken"
|
||||||
|
f, err := ioutil.TempFile("", "tokenfile")
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Unexpected error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer os.Remove(f.Name())
|
||||||
|
if err := ioutil.WriteFile(f.Name(), []byte(token), 0644); err != nil {
|
||||||
|
t.Errorf("Unexpected error: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
config := clientcmdapi.NewConfig()
|
||||||
|
config.Clusters["clean"] = &clientcmdapi.Cluster{
|
||||||
|
Server: "https://localhost:8443",
|
||||||
|
}
|
||||||
|
expectedToken := "expected"
|
||||||
|
config.AuthInfos["clean"] = &clientcmdapi.AuthInfo{
|
||||||
|
Token: expectedToken,
|
||||||
|
TokenFile: f.Name(),
|
||||||
|
}
|
||||||
|
config.Contexts["clean"] = &clientcmdapi.Context{
|
||||||
|
Cluster: "clean",
|
||||||
|
AuthInfo: "clean",
|
||||||
|
}
|
||||||
|
config.CurrentContext = "clean"
|
||||||
|
|
||||||
|
clientBuilder := NewNonInteractiveClientConfig(*config, "clean", &ConfigOverrides{}, nil)
|
||||||
|
|
||||||
|
clientConfig, err := clientBuilder.ClientConfig()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
matchStringArg(expectedToken, clientConfig.BearerToken, t)
|
||||||
|
}
|
||||||
|
|
||||||
func TestCreateClean(t *testing.T) {
|
func TestCreateClean(t *testing.T) {
|
||||||
config := createValidTestConfig()
|
config := createValidTestConfig()
|
||||||
clientBuilder := NewNonInteractiveClientConfig(*config, "clean", &ConfigOverrides{}, nil)
|
clientBuilder := NewNonInteractiveClientConfig(*config, "clean", &ConfigOverrides{}, nil)
|
||||||
|
@ -215,7 +215,6 @@ func (rules *ClientConfigLoadingRules) Load() (*clientcmdapi.Config, error) {
|
|||||||
errlist = append(errlist, err)
|
errlist = append(errlist, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return config, utilerrors.NewAggregate(errlist)
|
return config, utilerrors.NewAggregate(errlist)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -530,7 +529,7 @@ func GetClusterFileReferences(cluster *clientcmdapi.Cluster) []*string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetAuthInfoFileReferences(authInfo *clientcmdapi.AuthInfo) []*string {
|
func GetAuthInfoFileReferences(authInfo *clientcmdapi.AuthInfo) []*string {
|
||||||
return []*string{&authInfo.ClientCertificate, &authInfo.ClientKey}
|
return []*string{&authInfo.ClientCertificate, &authInfo.ClientKey, &authInfo.TokenFile}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResolvePaths updates the given refs to be absolute paths, relative to the given base directory
|
// ResolvePaths updates the given refs to be absolute paths, relative to the given base directory
|
||||||
|
Loading…
Reference in New Issue
Block a user