Skip to content

Commit

Permalink
ClientConfig should not default to http://localhost:8080
Browse files Browse the repository at this point in the history
This changes clientcmd to skip the default cluster, but preserves the
behavior in kubectl. This prevents the possibility of an administrator
misconfiguration in kubelet or other server component from allowing a
third party who can bind to 8080 on that host from potentially
impersonating an API server and gaining root access.
  • Loading branch information
smarterclayton committed Aug 30, 2016
1 parent 3ccb99d commit 06cbb29
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pkg/client/unversioned/clientcmd/client_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func (config *DirectClientConfig) getCluster() clientcmdapi.Cluster {
clusterInfoName := config.getClusterName()

var mergedClusterInfo clientcmdapi.Cluster
mergo.Merge(&mergedClusterInfo, DefaultCluster)
mergo.Merge(&mergedClusterInfo, config.overrides.ClusterDefaults)
mergo.Merge(&mergedClusterInfo, EnvVarCluster)
if configClusterInfo, exists := clusterInfos[clusterInfoName]; exists {
mergo.Merge(&mergedClusterInfo, configClusterInfo)
Expand Down
41 changes: 35 additions & 6 deletions pkg/client/unversioned/clientcmd/client_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,13 @@ func TestCreateCleanWithPrefix(t *testing.T) {
cleanConfig.Server = tc.server
config.Clusters["clean"] = cleanConfig

clientBuilder := NewNonInteractiveClientConfig(*config, "clean", &ConfigOverrides{}, nil)
clientBuilder := NewNonInteractiveClientConfig(*config, "clean", &ConfigOverrides{
ClusterDefaults: DefaultCluster,
}, nil)

clientConfig, err := clientBuilder.ClientConfig()
if err != nil {
t.Errorf("Unexpected error: %v", err)
t.Fatalf("Unexpected error: %v", err)
}

matchStringArg(tc.host, clientConfig.Host, t)
Expand All @@ -321,30 +323,57 @@ func TestCreateCleanDefault(t *testing.T) {

clientConfig, err := clientBuilder.ClientConfig()
if err != nil {
t.Errorf("Unexpected error: %v", err)
t.Fatalf("Unexpected error: %v", err)
}

matchStringArg(config.Clusters["clean"].Server, clientConfig.Host, t)
matchBoolArg(config.Clusters["clean"].InsecureSkipTLSVerify, clientConfig.Insecure, t)
matchStringArg(config.AuthInfos["clean"].Token, clientConfig.BearerToken, t)
}

func TestCreateMissingContext(t *testing.T) {
func TestCreateCleanDefaultCluster(t *testing.T) {
config := createValidTestConfig()
clientBuilder := NewDefaultClientConfig(*config, &ConfigOverrides{
ClusterDefaults: DefaultCluster,
})

clientConfig, err := clientBuilder.ClientConfig()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

matchStringArg(config.Clusters["clean"].Server, clientConfig.Host, t)
matchBoolArg(config.Clusters["clean"].InsecureSkipTLSVerify, clientConfig.Insecure, t)
matchStringArg(config.AuthInfos["clean"].Token, clientConfig.BearerToken, t)
}

func TestCreateMissingContextNoDefault(t *testing.T) {
const expectedErrorContains = "Context was not found for specified context"
config := createValidTestConfig()
clientBuilder := NewNonInteractiveClientConfig(*config, "not-present", &ConfigOverrides{}, nil)

_, err := clientBuilder.ClientConfig()
if err == nil {
t.Fatalf("Unexpected error: %v", err)
}
}
func TestCreateMissingContext(t *testing.T) {
const expectedErrorContains = "Context was not found for specified context"
config := createValidTestConfig()
clientBuilder := NewNonInteractiveClientConfig(*config, "not-present", &ConfigOverrides{
ClusterDefaults: DefaultCluster,
}, nil)

clientConfig, err := clientBuilder.ClientConfig()
if err != nil {
t.Errorf("Unexpected error: %v", err)
t.Fatalf("Unexpected error: %v", err)
}

expectedConfig := &restclient.Config{Host: clientConfig.Host}

if !reflect.DeepEqual(expectedConfig, clientConfig) {
t.Errorf("Expected %#v, got %#v", expectedConfig, clientConfig)
}

}

func matchBoolArg(expected, got bool, t *testing.T) {
Expand Down
10 changes: 6 additions & 4 deletions pkg/client/unversioned/clientcmd/overrides.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ import (
// ConfigOverrides holds values that should override whatever information is pulled from the actual Config object. You can't
// simply use an actual Config object, because Configs hold maps, but overrides are restricted to "at most one"
type ConfigOverrides struct {
AuthInfo clientcmdapi.AuthInfo
ClusterInfo clientcmdapi.Cluster
Context clientcmdapi.Context
CurrentContext string
AuthInfo clientcmdapi.AuthInfo
// ClusterDefaults are applied before the configured cluster info is loaded.
ClusterDefaults clientcmdapi.Cluster
ClusterInfo clientcmdapi.Cluster
Context clientcmdapi.Context
CurrentContext string
}

// ConfigOverrideFlags holds the flag names to be used for binding command line flags. Notice that this structure tightly
Expand Down
4 changes: 4 additions & 0 deletions pkg/kubectl/cmd/util/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"time"

"github.com/emicklei/go-restful/swagger"
"github.com/imdario/mergo"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

Expand Down Expand Up @@ -1219,6 +1220,9 @@ func DefaultClientConfig(flags *pflag.FlagSet) clientcmd.ClientConfig {
flags.StringVar(&loadingRules.ExplicitPath, "kubeconfig", "", "Path to the kubeconfig file to use for CLI requests.")

overrides := &clientcmd.ConfigOverrides{}
// use the standard defaults for this client config
mergo.Merge(&overrides.ClusterDefaults, clientcmd.DefaultCluster)

flagNames := clientcmd.RecommendedConfigOverrideFlags("")
// short flagnames are disabled by default. These are here for compatibility with existing scripts
flagNames.ClusterOverrideFlags.APIServer.ShortName = "s"
Expand Down
4 changes: 2 additions & 2 deletions plugin/pkg/admission/imagepolicy/admission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ users:
client-certificate: {{ .Cert }}
client-key: {{ .Key }}
`,
wantErr: false,
wantErr: true,
},
{
msg: "multiple clusters with no context",
Expand All @@ -135,7 +135,7 @@ users:
client-certificate: {{ .Cert }}
client-key: {{ .Key }}
`,
wantErr: false,
wantErr: true,
},
{
msg: "multiple clusters with a context",
Expand Down
4 changes: 2 additions & 2 deletions plugin/pkg/auth/authorizer/webhook/webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ users:
client-certificate: {{ .Cert }}
client-key: {{ .Key }}
`,
wantErr: false,
wantErr: true,
},
{
msg: "multiple clusters with no context",
Expand All @@ -109,7 +109,7 @@ users:
client-certificate: {{ .Cert }}
client-key: {{ .Key }}
`,
wantErr: false,
wantErr: true,
},
{
msg: "multiple clusters with a context",
Expand Down

0 comments on commit 06cbb29

Please sign in to comment.