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

Fix kubernetes_service nil ptr dereference #9788

Merged
merged 2 commits into from
Apr 1, 2022
Merged

Conversation

lxea
Copy link
Contributor

@lxea lxea commented Jan 14, 2022

Based off of the stack trace in #9721 (havent successfuly reproduced the issue), it appears that the nil ptr dereference happens when creds.tlsConfig is nil.

#9721

@russjones
Copy link
Contributor

@codingllama @jimbishopp Can you guys take a look?

@codingllama
Copy link
Contributor

@codingllama @jimbishopp Can you guys take a look?

I have an open question above, but happy to approve if it's too much trouble.

@jimbishopp
Copy link
Contributor

jimbishopp commented Feb 9, 2022

@russjones I was waiting for Alan's question to be answered as well. I was also concerned that it could not be reproduced.

@jimbishopp
Copy link
Contributor

jimbishopp commented Feb 9, 2022

@lxea LMK if this is your understanding of this issue.

@codingllama This may answer the question.

It looks like the k8s lib is bypassing the TLS config under certain conditions resulting in the TLS config being set to nil.

k8s.io/client-go/transport/transport.go:60

 func TLSConfigFor(c *Config) (*tls.Config, error) {
	if !(c.HasCA() || c.HasCertAuth() || c.HasCertCallback() || c.TLS.Insecure || len(c.TLS.ServerName) > 0 || len(c.TLS.NextProtos) > 0) {
		return nil, nil
	}
...
}

We then panic while writing a debug log message:
kube/proxy/forwarder.go:1499

func (f *Forwarder) newClusterSessionLocal(ctx authContext) (*clusterSession, error) {
...
     f.log.Debugf("local Servername: %v", creds.tlsConfig.ServerName)

I don't know enough about our k8s implementation yet, but I assume by the way this is written that client auth is not required?

@lxea
Copy link
Contributor Author

lxea commented Feb 10, 2022

@lxea LMK if this is your understanding of this issue.
....

Yeah, this is my current understanding however I wasnt able to reproduce the error unfortunatly.

@russjones
Copy link
Contributor

@r0mant What do you think? Should we merge this without a repro?

@r0mant
Copy link
Collaborator

r0mant commented Feb 16, 2022

It may be fine to merge without the repro but I'm not sure the current fix checks for nil in the best place. Looking at the code, it seems like tlsConfig is returned as nil in the extractNewCreds function from rest.TLSConfigFor:

https://github.com/gravitational/teleport/blob/v7.3.2/lib/kube/proxy/auth.go#L159

I think that would be a more appropriate place to check that tlsConfig is not nil (in addition to checking err).

As for the repro, I think getting a kubeconfig from the user experiencing this issue will help. Looking at rest.TLSConfigFor implementation (it's a method from K8s client lib) it returns "nil, nil" in a few specific cases based on the kubeconfig as @jimbishopp mentioned above.

@lxea lxea force-pushed the lxea/kube_trace_fix branch 2 times, most recently from 0f36bfc to f466c65 Compare February 18, 2022 13:31
Copy link
Collaborator

@r0mant r0mant left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also please add test coverage.

lib/kube/proxy/auth.go Outdated Show resolved Hide resolved
@lxea lxea force-pushed the lxea/kube_trace_fix branch from f466c65 to a1f46a4 Compare March 14, 2022 11:10
Comment on lines 178 to 184
messageArgs := "c.HasCA, " +
"c.HasCertAuth, " +
"c.HasCertCallback, " +
"c.TLS.Insecure, " +
"len(c.TLS.ServerName) > 0, " +
"len(c.TLS.NextProtos) > 0 "
return nil, trace.BadParameter("failed to generate TLS config from kubeConfig. All of %s were false", messageArgs)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lxea I'm not sure this will help much in troubleshooting tbh. Can we dump the entire clientCfg in the log (or rather, it's TLS section)? But make sure to redact fields like CAData, CertData, KeyData.

@lxea lxea force-pushed the lxea/kube_trace_fix branch 3 times, most recently from 660bcb5 to c47908b Compare March 23, 2022 13:24
@r0mant
Copy link
Collaborator

r0mant commented Mar 23, 2022

@codingllama @jimbishopp Can one of you give this another look please? We couldn't get the customer to share their kubeconfig with us so far so we want to merge this so it fixes the panic and logs the config (with sensitive fields redacted) which would help in troubleshooting if it happens again.

@@ -174,6 +174,19 @@ func extractKubeCreds(ctx context.Context, cluster string, clientCfg *rest.Confi
if err != nil {
return nil, trace.Wrap(err, "failed to generate TLS config from kubeconfig: %v", err)
}
if tlsConfig == nil {
cc := rest.CopyConfig(clientCfg)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to echo the entire config? It seems a bit dangerous to do.

Maybe use rest.AnonymousClientConfig? Although I'd rather not echo everything if possible.

lib/kube/proxy/auth_test.go Outdated Show resolved Hide resolved
lib/kube/proxy/auth_test.go Outdated Show resolved Hide resolved
lib/kube/proxy/auth_test.go Outdated Show resolved Hide resolved
@lxea lxea force-pushed the lxea/kube_trace_fix branch from c47908b to be58bb3 Compare March 25, 2022 12:52
lib/kube/proxy/auth_test.go Outdated Show resolved Hide resolved
lib/kube/proxy/auth_test.go Outdated Show resolved Hide resolved
lib/kube/proxy/auth.go Outdated Show resolved Hide resolved
@lxea lxea force-pushed the lxea/kube_trace_fix branch 2 times, most recently from 7a45e40 to b9340a1 Compare March 30, 2022 09:15
@lxea lxea requested a review from codingllama March 30, 2022 09:15
lib/kube/proxy/auth_test.go Outdated Show resolved Hide resolved
lib/kube/proxy/auth_test.go Outdated Show resolved Hide resolved
lib/kube/proxy/auth_test.go Outdated Show resolved Hide resolved
lib/kube/proxy/auth_test.go Show resolved Hide resolved
@lxea lxea force-pushed the lxea/kube_trace_fix branch from 729233b to 5e9b25a Compare March 31, 2022 14:24
@lxea lxea enabled auto-merge (squash) March 31, 2022 14:25
This was referenced Mar 31, 2022
@lxea lxea merged commit 4a42cae into master Apr 1, 2022
@lxea lxea deleted the lxea/kube_trace_fix branch April 1, 2022 08:10
@lxea lxea mentioned this pull request Apr 21, 2022
tigrato added a commit that referenced this pull request May 16, 2022
tigrato added a commit that referenced this pull request May 17, 2022
tigrato added a commit that referenced this pull request May 24, 2022
tigrato added a commit that referenced this pull request May 24, 2022
* Revert "Avoid nil dereferencing when tlsConfig is nil. (#9788)"

This reverts commit 4a42cae.
tigrato added a commit that referenced this pull request May 24, 2022
tigrato added a commit that referenced this pull request May 24, 2022
tigrato added a commit that referenced this pull request May 25, 2022
tigrato added a commit that referenced this pull request May 25, 2022
tigrato added a commit that referenced this pull request May 26, 2022
r0mant pushed a commit that referenced this pull request May 26, 2022
tigrato added a commit that referenced this pull request Jun 1, 2022
tigrato added a commit that referenced this pull request Jun 1, 2022
@webvictim webvictim mentioned this pull request Jun 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants