Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unauthorized after update to v1.10 using token auth #679

Closed
msegura3 opened this issue Nov 9, 2019 · 29 comments · Fixed by #690
Closed

Unauthorized after update to v1.10 using token auth #679

msegura3 opened this issue Nov 9, 2019 · 29 comments · Fixed by #690

Comments

@msegura3
Copy link

msegura3 commented Nov 9, 2019

Hi there,
After upgrading to version 1.10, token authentication stopped working.

Terraform Version

0.12.11

Affected Resource(s)

  • Seems to be a provider issue on version 1.10.0

Terraform Configuration Files

provider "kubernetes" {
  version = "1.10"
  cluster_ca_certificate = base64decode(
    data.google_container_cluster.this.master_auth[0].cluster_ca_certificate,
  )
  host             = data.google_container_cluster.this.endpoint
  token            = data.external.get_token.result["token"]
  load_config_file = false
}

data "google_container_cluster" "this" {
  name     = var.cluster_name
  location = var.cluster_location
  project  = var.project_id
}

data "external" "get_token" {
  program = ["/bin/bash", "${path.module}/get_token.sh"]
  query = {
    cluster_name     = data.google_container_cluster.this.name
    cluster_location = var.cluster_location
    project_id       = var.project_id
    credentials      = var.credentials
  }
}

resource "kubernetes_namespace" "this" {
  metadata {
    name = var.namespace
  }
}

Expected Behavior

Get a success authentication.

Actual Behavior

It fail, getting a "Unauthorized "

Important Factoids

Using version 1.9 there is no problem

@dchelm
Copy link

dchelm commented Nov 9, 2019

Having the same issue with authentication in version 1.10

Terraform Configuration File

data "google_client_config" "default" {}

data "google_container_cluster" "my_cluster" {
  name   = "my-cluster"
  zone   = "us-east1-a"
}

provider "kubernetes" {
  load_config_file = false

  host = "https://${data.google_container_cluster.my_cluster.endpoint}"
  token = "${data.google_client_config.default.access_token}"
  cluster_ca_certificate = "${base64decode(data.google_container_cluster.my_cluster.master_auth.0.cluster_ca_certificate)}"
}

Terraform Version: v0.11.14

for what it's worth it seemed to always use a static Authorization: Bearer token independent of the value in the provider

I was having this issue in linux_amd64/terraform-provider-kubernetes_v1.10.0_x4 and not in darwin_amd64/terraform-provider-kubernetes_v1.10.0_x4 for some weird reason

@joshk0
Copy link

joshk0 commented Nov 9, 2019

We see this too. It seems like the smoking gun is this PR. If change is still desired, should this release have been an x-release (2.0)?

There could be a way to reimplemented that change using resource syntax that is additive and thus would only constitute a y release like you have it now.

For now, we are telling our developers to pin to 1.9.0.

@iciclespider
Copy link

iciclespider commented Nov 9, 2019

I also just ran into this issue and have figured out what I believe is the exact cause.

When the terraform is run in a Kubernetes pod, the Kubernetes configuration is defaulted to be the in cluster configuration, which sets all of these fields in the cfg https://github.com/terraform-providers/terraform-provider-kubernetes/blob/master/vendor/k8s.io/client-go/rest/config.go#L422-L428

	return &Config{
		// TODO: switch to using cluster DNS.
		Host:            "https://" + net.JoinHostPort(host, port),
		TLSClientConfig: tlsClientConfig,
		BearerToken:     string(token),
		BearerTokenFile: tokenFile,
	}, nil

It is the BearerTokenFile field that was not accounted for in this PR Support in-cluster configuration with service accounts. The BearerTokenFile value should be cleared when there is a token value from the kubernetes provider config here https://github.com/terraform-providers/terraform-provider-kubernetes/blob/master/kubernetes/provider.go#L242-L244

	if v, ok := d.GetOk("token"); ok {
		cfg.BearerToken = v.(string)
	}

Instead, it should do:

	if v, ok := d.GetOk("token"); ok {
		cfg.BearerToken = v.(string)
		cfg.BearerTokenFile = nil
	}

From https://github.com/terraform-providers/terraform-provider-kubernetes/blob/master/vendor/k8s.io/client-go/rest/config.go#L74-L77

	// 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

@iciclespider
Copy link

iciclespider commented Nov 9, 2019

In our builds, we tip toed around this issue by setting the KUBERNETES_SERVICE_HOST environment variable to an empty string before running terraform.

$ export KUBERNETES_SERVICE_HOST=
$ terraform init
$ terraform plan

This causes the following check in the kubernetes rest client to think it is not running in a cluster https://github.com/terraform-providers/terraform-provider-kubernetes/blob/master/vendor/k8s.io/client-go/rest/config.go#L404-L407

	host, port := os.Getenv("KUBERNETES_SERVICE_HOST"), os.Getenv("KUBERNETES_SERVICE_PORT")
	if len(host) == 0 || len(port) == 0 {
		return nil, ErrNotInCluster
	}

@caquino
Copy link

caquino commented Nov 10, 2019

I think I'm having a similar issue, but with a different result.

I'm using Google Container Cluster to both run atlantis and to configure other clusters, using the following configuration:

data "google_client_config" "current" {}

provider "kubernetes" {
  load_config_file = false
  host             = "https://${google_container_cluster.cluster.endpoint}"

  cluster_ca_certificate = "${base64decode(google_container_cluster.cluster.master_auth.0.cluster_ca_certificate)}"
  token                  = "${data.google_client_config.current.access_token}"
}

I'm seeing the following error:

Error: Failed to configure: open /var/run/secrets/kubernetes.io/serviceaccount/token: no such file or directory

  on providers.tf line 34, in provider "kubernetes":
  34: provider "kubernetes" {

Reverting back to version 1.9.0 seems to fix the problem.

One thing to note, this configuration is not managing the local cluster, so having a service account will not fix the issue, actually if I configure a service account it starts to fail with the Unauthorized error as it's managing other clusters and not the local one.

A service account is not configured as it's expected to use GCP service account to access the remote clusters to do the management.

@iciclespider
Copy link

@caquino Does setting the KUBERNETES_SERVICE_HOST environment variable to an empty string when running terraform still have the error you are enountering?

@alexsomesan
Copy link
Member

When seeing the Unauthorized error with provider version 1.10 are you positively sure that the supplied cluster service account has the necessary permissions?

I have tried to reproduce this with the configuration mentioned in the issue description. I had to replace the get_token.sh data source with the KUBE_TOKEN env var since the script is not included.

I am getting the same Unauthorized error with both 1.9 and 1.10 provider versions when using the default service account of a new GKE cluster.

@alexsomesan
Copy link
Member

Follow-up

Using the following configuration works as expected.
For reference, configuring the Kubernetes provider for GKE via Service Account tokens is documented here

data "google_client_config" "current" {}

data "google_container_cluster" "this" {
  name     = var.cluster_name
  location = var.cluster_location
  project  = var.project_id
}

provider "kubernetes" {
  version = "1.10"
  cluster_ca_certificate = base64decode(
    data.google_container_cluster.this.master_auth[0].cluster_ca_certificate,
  )
  host             = "https://${data.google_container_cluster.this.endpoint}"
  token            = data.google_client_config.current.access_token
  load_config_file = false
}

resource "kubernetes_namespace" "this" {
  metadata {
    name = var.namespace
  }
}

Versions used:

Terraform v0.12.13
+ provider.google v2.19.0
+ provider.kubernetes v1.10.0

Provider request log to Kubernetes API:

2019-11-10T21:52:10.446+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: 2019/11/10 21:52:10 [INFO] Creating new namespace: v1.Namespace{TypeMeta:v1.TypeMeta{Kind:"", APIVersion:""}, ObjectMeta:v1.ObjectMeta{Name:"alextest", GenerateName:"", Namespace:"", SelfLink:"", UID:"", ResourceVersion:"", Generation:0, CreationTimestamp:v1.Time{Time:time.Time{wall:0x0, ext:0, loc:(*time.Location)(nil)}}, DeletionTimestamp:(*v1.Time)(nil), DeletionGracePeriodSeconds:(*int64)(nil), Labels:map[string]string(nil), Annotations:map[string]string(nil), OwnerReferences:[]v1.OwnerReference(nil), Finalizers:[]string(nil), ClusterName:"", ManagedFields:[]v1.ManagedFieldsEntry(nil)}, Spec:v1.NamespaceSpec{Finalizers:[]v1.FinalizerName(nil)}, Status:v1.NamespaceStatus{Phase:"", Conditions:[]v1.NamespaceCondition(nil)}}
2019-11-10T21:52:10.447+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: 2019/11/10 21:52:10 [DEBUG] Kubernetes API Request Details:
2019-11-10T21:52:10.447+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: ---[ REQUEST ]---------------------------------------
2019-11-10T21:52:10.447+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: POST /api/v1/namespaces HTTP/1.1
2019-11-10T21:52:10.447+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: Host: 35.241.132.213
2019-11-10T21:52:10.447+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: User-Agent: HashiCorp/1.0 Terraform/0.12.13
2019-11-10T21:52:10.447+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: Content-Length: 117
2019-11-10T21:52:10.447+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: Accept: application/json, */*
2019-11-10T21:52:10.447+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: Authorization: Bearer ya29.c.KmSwB3qZ3n9oiLHdyVjnalieO_qeGm0-TZ2JyV25EG_5RCa43_m7OTivch6tTPLoEo_m5OweaA0rxEFYSLxzd5rZwt90DsK3pjOM9Uafwqo9cQFjiuOpvx1X3RHAx3-eSigUzRbQ
2019-11-10T21:52:10.447+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: Content-Type: application/json
2019-11-10T21:52:10.447+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: Accept-Encoding: gzip
2019-11-10T21:52:10.447+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:
2019-11-10T21:52:10.447+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: {
2019-11-10T21:52:10.447+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:  "kind": "Namespace",
2019-11-10T21:52:10.447+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:  "apiVersion": "v1",
2019-11-10T21:52:10.447+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:  "metadata": {
2019-11-10T21:52:10.447+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:   "name": "alextest",
2019-11-10T21:52:10.447+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:   "creationTimestamp": null
2019-11-10T21:52:10.447+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:  },
2019-11-10T21:52:10.447+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:  "spec": {},
2019-11-10T21:52:10.447+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:  "status": {}
2019-11-10T21:52:10.447+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: }
2019-11-10T21:52:10.447+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:
2019-11-10T21:52:10.447+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: -----------------------------------------------------
2019-11-10T21:52:10.617+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: 2019/11/10 21:52:10 [DEBUG] Kubernetes API Response Details:
2019-11-10T21:52:10.617+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: ---[ RESPONSE ]--------------------------------------
2019-11-10T21:52:10.617+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: HTTP/2.0 201 Created
2019-11-10T21:52:10.617+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: Content-Length: 290
2019-11-10T21:52:10.617+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: Audit-Id: e1a8b830-edf5-499a-a503-53fe826a7474
2019-11-10T21:52:10.617+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: Content-Type: application/json
2019-11-10T21:52:10.617+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: Date: Sun, 10 Nov 2019 20:52:10 GMT
2019-11-10T21:52:10.617+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:
2019-11-10T21:52:10.617+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: {
2019-11-10T21:52:10.617+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:  "kind": "Namespace",
2019-11-10T21:52:10.617+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:  "apiVersion": "v1",
2019-11-10T21:52:10.617+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:  "metadata": {
2019-11-10T21:52:10.617+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:   "name": "alextest",
2019-11-10T21:52:10.617+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:   "selfLink": "/api/v1/namespaces/alextest",
2019-11-10T21:52:10.617+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:   "uid": "f720fc8e-03fb-11ea-9ad7-42010a8400d5",
2019-11-10T21:52:10.617+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:   "resourceVersion": "56186",
2019-11-10T21:52:10.617+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:   "creationTimestamp": "2019-11-10T20:52:10Z"
2019-11-10T21:52:10.617+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:  },
2019-11-10T21:52:10.617+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:  "spec": {
2019-11-10T21:52:10.617+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:   "finalizers": [
2019-11-10T21:52:10.617+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:    "kubernetes"
2019-11-10T21:52:10.618+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:   ]
2019-11-10T21:52:10.618+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:  },
2019-11-10T21:52:10.618+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:  "status": {
2019-11-10T21:52:10.618+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:   "phase": "Active"
2019-11-10T21:52:10.618+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:  }
2019-11-10T21:52:10.618+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: }
2019-11-10T21:52:10.618+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:
2019-11-10T21:52:10.618+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: -----------------------------------------------------
2019-11-10T21:52:10.619+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: 2019/11/10 21:52:10 [INFO] Submitted new namespace: &v1.Namespace{TypeMeta:v1.TypeMeta{Kind:"", APIVersion:""}, ObjectMeta:v1.ObjectMeta{Name:"alextest", GenerateName:"", Namespace:"", SelfLink:"/api/v1/namespaces/alextest", UID:"f720fc8e-03fb-11ea-9ad7-42010a8400d5", ResourceVersion:"56186", Generation:0, CreationTimestamp:v1.Time{Time:time.Time{wall:0x0, ext:63709015930, loc:(*time.Location)(0x40f4d80)}}, DeletionTimestamp:(*v1.Time)(nil), DeletionGracePeriodSeconds:(*int64)(nil), Labels:map[string]string(nil), Annotations:map[string]string(nil), OwnerReferences:[]v1.OwnerReference(nil), Finalizers:[]string(nil), ClusterName:"", ManagedFields:[]v1.ManagedFieldsEntry(nil)}, Spec:v1.NamespaceSpec{Finalizers:[]v1.FinalizerName{"kubernetes"}}, Status:v1.NamespaceStatus{Phase:"Active", Conditions:[]v1.NamespaceCondition(nil)}}
2019-11-10T21:52:10.619+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: 2019/11/10 21:52:10 [INFO] Reading namespace alextest
2019-11-10T21:52:10.619+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: 2019/11/10 21:52:10 [DEBUG] Kubernetes API Request Details:
2019-11-10T21:52:10.619+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: ---[ REQUEST ]---------------------------------------
2019-11-10T21:52:10.619+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: GET /api/v1/namespaces/alextest HTTP/1.1
2019-11-10T21:52:10.619+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: Host: 35.241.132.213
2019-11-10T21:52:10.620+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: User-Agent: HashiCorp/1.0 Terraform/0.12.13
2019-11-10T21:52:10.620+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: Accept: application/json, */*
2019-11-10T21:52:10.620+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: Authorization: Bearer ya29.c.KmSwB3qZ3n9oiLHdyVjnalieO_qeGm0-TZ2JyV25EG_5RCa43_m7OTivch6tTPLoEo_m5OweaA0rxEFYSLxzd5rZwt90DsK3pjOM9Uafwqo9cQFjiuOpvx1X3RHAx3-eSigUzRbQ
2019-11-10T21:52:10.620+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: Accept-Encoding: gzip
2019-11-10T21:52:10.620+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:
2019-11-10T21:52:10.620+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:
2019-11-10T21:52:10.620+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: -----------------------------------------------------
2019-11-10T21:52:10.673+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: 2019/11/10 21:52:10 [DEBUG] Kubernetes API Response Details:
2019-11-10T21:52:10.673+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: ---[ RESPONSE ]--------------------------------------
2019-11-10T21:52:10.673+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: HTTP/2.0 200 OK
2019-11-10T21:52:10.673+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: Content-Length: 290
2019-11-10T21:52:10.673+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: Audit-Id: c362d6c4-3964-4014-9547-927b107318ad
2019-11-10T21:52:10.673+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: Content-Type: application/json
2019-11-10T21:52:10.673+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: Date: Sun, 10 Nov 2019 20:52:10 GMT
2019-11-10T21:52:10.673+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:
2019-11-10T21:52:10.673+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: {
2019-11-10T21:52:10.673+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:  "kind": "Namespace",
2019-11-10T21:52:10.673+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:  "apiVersion": "v1",
2019-11-10T21:52:10.673+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:  "metadata": {
2019-11-10T21:52:10.673+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:   "name": "alextest",
2019-11-10T21:52:10.673+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:   "selfLink": "/api/v1/namespaces/alextest",
2019-11-10T21:52:10.673+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:   "uid": "f720fc8e-03fb-11ea-9ad7-42010a8400d5",
2019-11-10T21:52:10.673+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:   "resourceVersion": "56186",
2019-11-10T21:52:10.673+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:   "creationTimestamp": "2019-11-10T20:52:10Z"
2019-11-10T21:52:10.673+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:  },
2019-11-10T21:52:10.673+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:  "spec": {
2019-11-10T21:52:10.673+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:   "finalizers": [
2019-11-10T21:52:10.673+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:    "kubernetes"
2019-11-10T21:52:10.673+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:   ]
2019-11-10T21:52:10.673+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:  },
2019-11-10T21:52:10.673+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:  "status": {
2019-11-10T21:52:10.673+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:   "phase": "Active"
2019-11-10T21:52:10.673+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:  }
2019-11-10T21:52:10.673+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: }
2019-11-10T21:52:10.673+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4:
2019-11-10T21:52:10.673+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: -----------------------------------------------------
2019-11-10T21:52:10.673+0100 [DEBUG] plugin.terraform-provider-kubernetes_v1.10.0_x4: 2019/11/10 21:52:10 [INFO] Received namespace: &v1.Namespace{TypeMeta:v1.TypeMeta{Kind:"", APIVersion:""}, ObjectMeta:v1.ObjectMeta{Name:"alextest", GenerateName:"", Namespace:"", SelfLink:"/api/v1/namespaces/alextest", UID:"f720fc8e-03fb-11ea-9ad7-42010a8400d5", ResourceVersion:"56186", Generation:0, CreationTimestamp:v1.Time{Time:time.Time{wall:0x0, ext:63709015930, loc:(*time.Location)(0x40f4d80)}}, DeletionTimestamp:(*v1.Time)(nil), DeletionGracePeriodSeconds:(*int64)(nil), Labels:map[string]string(nil), Annotations:map[string]string(nil), OwnerReferences:[]v1.OwnerReference(nil), Finalizers:[]string(nil), ClusterName:"", ManagedFields:[]v1.ManagedFieldsEntry(nil)}, Spec:v1.NamespaceSpec{Finalizers:[]v1.FinalizerName{"kubernetes"}}, Status:v1.NamespaceStatus{Phase:"Active", Conditions:[]v1.NamespaceCondition(nil)}}
2019/11/10 21:52:10 [WARN] Provider "kubernetes" produced an unexpected new value for kubernetes_namespace.this, but we are tolerating it because it is using the legacy plugin SDK.
    The following problems may be the cause of any confusing errors from downstream operations:
      - .metadata[0].generate_name: was null, but now cty.StringVal("")
2019/11/10 21:52:10 [TRACE] <root>: eval: *terraform.EvalMaybeTainted
2019/11/10 21:52:10 [TRACE] <root>: eval: *terraform.EvalWriteState
kubernetes_namespace.this: Creation complete after 1s [id=alextest]
2019-11-10T21:52:10.694+0100 [DEBUG] plugin: plugin process exited: path=/Users/alex/test-679/.terraform/plugins/darwin_amd64/terraform-provider-kubernetes_v1.10.0_x4 pid=77072
2019-11-10T21:52:10.694+0100 [DEBUG] plugin: plugin exited

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
------------------------------------------------------------

@alexsomesan
Copy link
Member

Please provide more details about your cluster configuration (SA used, version) and I can attempt to reproduce the supposed issue again.

@alexsomesan alexsomesan self-assigned this Nov 10, 2019
@iciclespider
Copy link

@alexsomesan The problem only occurs when terraform is run on a different Kubernetes cluster than the cluster being updated by the terraform script.

@alexsomesan
Copy link
Member

@iciclespider Do you mean "inside a different cluster" ?

@iciclespider
Copy link

iciclespider commented Nov 10, 2019

@iciclespider Do you mean "inside a different cluster" ?

@alexsomesan Yes. Terraform is executed inside a different cluster.

In my case, I am running Jenkins in a private, on premise Kubernetes cluster, and the build jobs are attempting to update an AWS EKS cluster. When the kubernetes rest api is called, the correct host and ca certificate is used, but the token used is the token found in the /var/run/secrets/kubernetes.io/serviceaccount/token file, even though the kubernetes provider is properly configured with the AWS EKS token.

@msegura3
Copy link
Author

@iciclespider Do you mean "inside a different cluster" ?

@alexsomesan Yes. Terraform is executed inside a different cluster.

In my case, I am running Jenkins in a private, on premise Kubernetes cluster, and the build jobs are attempting to update an AWS EKS cluster. When the kubernetes rest api is called, the correct host and ca certificate is used, but the token used is the token found in the /var/run/secrets/kubernetes.io/serviceaccount/token file, even though the kubernetes provider is properly configured with the AWS EKS token.

My case is similar, I running terraform inside of a cluster and I'm trying to update another gke cluster.

I have the right permission for the SA and using the same SA with 1.9 I don't have any issue.

I hope this help.

@nilium
Copy link

nilium commented Nov 11, 2019

We also run Terraform (via Atlantis) from inside a Kubernetes cluster. Change #497 appears to break our existing configuration, as we're creating a provider for a cluster from inside a different cluster. The end result is that Terraform has new implicitly loaded values for a Kubernetes provider's arguments, based on it being inside of a cluster, that we did not set ourselves.

Pinning to 1.9 helps, but it seems like we need an option to disable in-cluster configuration, or an opt-in to enable in-cluster configuration (considering this appears to be a breaking change). The other option is to document that in-Kubernetes users may need to hide Kubernetes environment variables from Terraform, which seems like the worst choice here.

Would it be best to only load the in-cluster config if no arguments are set on a Kubernetes provider?

@yuankunzhang
Copy link

Same case here. We are using a Google service account for authenticating against GKE, and we provision the token to the Kubernetes provider via token = data.google_client_config.current.access_token. Out of a sudden, our Atlantis server stopped to work and kept throwing 401 Unauthorized error.

We found that, instead of using the passed in token, Atlantis is using the token found in the /var/run/secrets/kubernetes.io/serviceaccount/token file.

I agree with @nilium , It would be better to only load the in-cluster config if no arguments are explicitly set on Kubernetes provider. For now, we reverted the version to v1.9.0 to keep our Atlantis server happy. Hope this issue gets fixed soon.

@nicolasbelanger
Copy link

nicolasbelanger commented Nov 12, 2019

Same here... From gitlab-runner inside EKS, connecting to another EKS === Unauthorized...
Rolling-back to 1.9 works in the meantime...

@conet
Copy link

conet commented Nov 12, 2019

Same here, #497 seems like the cause, maybe it would be best to have a load_in_cluster_config similar to load_config_file.

@conet
Copy link

conet commented Nov 12, 2019

@alexsomesan the problem is the following: if terraform runs in a kubernetes cluster in a pod that has service account credentials mounted and a config file is not used (as in the above examples) there is no way to ignore the pod service account credentials.

@triangletodd
Copy link

I'm going to solve this by moving our Atlantis installation to a Workload Identity enabled cluster in a namespace where we can control the default GSA, but, IMO, InClusterConfig shouldn't be forced if we're passing in an access token.

I believe the default logic should be to respect what is being passed in, check for InClusterConfig if nothing is being passed in and then fail.

@pdecat
Copy link
Contributor

pdecat commented Nov 12, 2019

For those encountering this issue when running terraform in kubernetes pods, there's a work-around provided by @iciclespider:

Setting the KUBERNETES_SERVICE_HOST to an empty value right before invoking terraform, this breaks the in-cluster detection logic.

A fix is being worked on. I believe @iciclespider also nailed the root cause and the solution.

@houqp
Copy link
Contributor

houqp commented Nov 13, 2019

Ran into the exact same issue with v1.10. As @pdecat mentioned above, the fix provided by @iciclespider should fix the root cause. Given that it's just an one line patch, @alexsomesan could you push a quick commit to have it fixed? Let us know if you think it's better for any of us to submit a PR for it.

This is affecting all terraform runs within k8s pods that are using token auth, so it's rather serious. At least it's breaking all of our Jenkins pipelines.

@pdecat
Copy link
Contributor

pdecat commented Nov 13, 2019

Hi @houqp, after more investigation, it is not the single line patch I expected as this would have only corrected the case where a service account token is available at /var/run/secrets/kubernetes.io/serviceaccount/token (and the symptom is Error: Unauthorized), but not the case where no token is mounted in the pod at /var/run/secrets/kubernetes.io/serviceaccount/token (symptom Error: Failed to configure: open /var/run/secrets/kubernetes.io/serviceaccount/token: no such file or directory).

I've submitted #686 which should fix both cases. Obviously, feedback is welcome, you may even build the provider yourself to test the fix.

I understand this is causing issues but as there is a work-around, it should be avoided to rush a fix that would only make things worse.

@houqp
Copy link
Contributor

houqp commented Nov 14, 2019

good catch @pdecat and thanks for creating that PR!

@mtranter
Copy link

Thanks so much for this @iciclespider !!

@Krishna1408
Copy link

So even I am getting the same error. I am getting it with Kubernetes Provider and EKS. I got it for :

  • Terraform pod running in the same Kubernetes Cluster
  • Terraform pod running in different kubernetes cluster

The mentioned workaround fixed the issue for me. Happy to see that root cause and fix both are available. Thanks a lot @iciclespider @pdecat

@sleenen
Copy link

sleenen commented Jan 30, 2020

When will we see a new release?

I'm running into Error: Failed to configure: open /var/run/secrets/kubernetes.io/serviceaccount/token: no such file or directory issues in my pipeline (bitbucket pipelines) too when using load_config_file = false

With 1.9 it works, so downgraded for now, but something seems very off when telling the provider to not load the config file.

I'm curious to see if the PR merged to solve this issue will also solve mine.

@rafaelmagu
Copy link

I'm having the exact same issue, however I'm not running Terraform inside an EKS cluster. Even weirder is that I can apply changes fine, but import fails at the stage where TF tries to refresh the state.

@rafaelmagu
Copy link

UPDATE: Once I apply some changes to the cluster, import works. Very odd.

@rivernews
Copy link

rivernews commented Feb 8, 2020

Looking forward to have @pdecat work included in the next release! (Or I guess specifying version = "1.10.1-dev6" for now will also do?)

Want to add a data point about the issue. I got Error: Unauthorized during tf refreshing itself as well. This happens to plan and apply. Weirdly, it sometimes will succeed w/o error, like what @rafaelmagu mentioned, after making some change and run again. But when I rerun apply / plan, the error comes back.

My terraform does not run in different kubernetes clusters, but is indeed run in different containers / VMs. I run on my laptop as well as on travisCI builds. Just one week ago I split my terraform code to different repositories and have them run on travisCI as part of some CI/CD automation. Then this Error: unauthorize starts to happen.

This is what I've end up

provider "kubernetes" {
  # cannot downgrade from 1.10 to 1.9 by specifying `~> 1.9`, it has to be exact `1.9`; otherwise terraform will still upgrade to 1.10 even if `.terraform` directory is deleted
  version = "1.9"

  host = digitalocean_kubernetes_cluster.project_digitalocean_cluster.endpoint

  load_config_file = false

  token = digitalocean_kubernetes_cluster.project_digitalocean_cluster.kube_config[0].token

  cluster_ca_certificate = base64decode(
    digitalocean_kubernetes_cluster.project_digitalocean_cluster.kube_config[0].cluster_ca_certificate
  )
}

Since I'm also using a module that contains kubernetes provider, I downgraded the kubernetes provider to 1.9 in that module as well. Not sure if setting export KUBERNETES_SERVICE_HOST= in my case makes a difference, but I'd suggest set it up first along with the provider configs & versions till you can proceed w/o error. load_config_file = false also seems to matter as well, at least if using token.

fbdo added a commit to fbdo/gke-sql-terraform that referenced this issue Mar 25, 2020
@ghost ghost locked and limited conversation to collaborators Apr 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.