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(inputs.x509): Multiple sources with non-overlapping DNS entries. #11613

Merged
merged 3 commits into from
Aug 5, 2022

Conversation

srebhan
Copy link
Member

@srebhan srebhan commented Aug 3, 2022

resolves #11623

This PR fixes an issue for https, tcp and smtp certificate validation when using multiple sources with non-overlapping DNS entries in the certificates. That is if the DNS name of the first source is not valid in, at least one, other certificates.

The error results from a stateful behavior of the serverName() function, evaluating the c.tlsCfg.ServerName entry and returning this with priority in combination with setting exactly this field (c.tlsCfg.ServerName) when getting the certificate via getCert(). After getting the first source (e.g. server-A.org), the, previously empty, c.tlsCfg.ServerName field is set to server-A.org in getCert(). While the https and tcp variants of the function is emptying the field (leading to issues where the name was set by the user), the smtp variant leaves the field as is. Due to the fact that getCert() also uses serverName() to get the property, any further call to getCert() will not be able to overwrite the setting as c.tlsCfg.ServerName is now set (to server-A.org) and this is returned with priority. Therefore, from now on (after the first call to getCert()), serverName() will always return the name of the first entry in sources!
As the server name is also used to verify the certificate in Gather(), the verification of all certificates runs against the name of the first entry in sources, which will fail in case that name is not in the certificates of any of the other sources!

The PR removes the state in c.tlsCfg by cloning the configuration for certificate retrieval. This way, only user-set names are returned by serverName() and thus the verification works.

@srebhan srebhan marked this pull request as draft August 3, 2022 16:41
Copy link
Contributor

@MyaLongmire MyaLongmire left a comment

Choose a reason for hiding this comment

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

Looks good to me. Thanks for doing this @srebhan

edit: just saw this is a draft, sorry 😰

@srebhan srebhan changed the title debug(inputs.x509): Add debugging for invalid certs. fix(inputs.x509): Multiple sources with non-overlapping DNS entries. Aug 4, 2022
@srebhan srebhan added fix pr to fix corresponding bug plugin/input 1. Request for new input plugins 2. Issues/PRs that are related to input plugins labels Aug 4, 2022
@srebhan srebhan requested a review from MyaLongmire August 4, 2022 10:53
@srebhan srebhan marked this pull request as ready for review August 4, 2022 10:53
@chrism417
Copy link

Tested this on my end and it fixed my issue with 5 mail servers in one source file. Thanks so much @srebhan

image

Copy link
Contributor

@MyaLongmire MyaLongmire left a comment

Choose a reason for hiding this comment

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

The linter is complaining var-naming: var downloadTlsCfg should be downloadTLSCfg. Once that is renamed it looks good to me.

Comment on lines +369 to +377
c.Log.Debugf("Invalid certificate at index %2d!", i)
c.Log.Debugf(" cert DNS names: %v", cert.DNSNames)
c.Log.Debugf(" cert IP addresses: %v", cert.IPAddresses)
c.Log.Debugf(" opts.DNSName: %v", opts.DNSName)
c.Log.Debugf(" verify options: %v", opts)
c.Log.Debugf(" verify error: %v", err)
c.Log.Debugf(" location: %v", location)
c.Log.Debugf(" tlsCfg.ServerName: %v", c.tlsCfg.ServerName)
c.Log.Debugf(" ServerName: %v", c.ServerName)
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like this invalid cert info could be more useful as metric fields instead of console debug messages. I think if we log it like this, it might not be clear to the user what cert is invalid. They might think the invalid cert was found when any telegraf plugin connected to a service and not know it is from x509_cert.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think we should expose these internal information as fields. There already is a verification_code field which contain the info. I just added this to be able to quickly look into "hey, telegraf says the cert is invalid but it is valid" issues. Should I remove the info then?

Copy link
Contributor

Choose a reason for hiding this comment

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

On second thought, for a user to see these they'll have to turn on debug logging and the messages will start with "[inputs.x509_cert]" so they shouldn't confuse anyone.

When debugging it will be useful to have opts available.

@srebhan
Copy link
Member Author

srebhan commented Aug 5, 2022

@MyaLongmire

The linter is complaining var-naming: var downloadTlsCfg should be downloadTLSCfg. Once that is renamed it looks good to me.

fixed.

@srebhan srebhan requested a review from MyaLongmire August 5, 2022 12:29
@srebhan
Copy link
Member Author

srebhan commented Aug 5, 2022

@MyaLongmire can you maybe add your approval again so it is obvious that you are OK with the latest changes!?

@reimda reimda merged commit 7bd9c91 into influxdata:master Aug 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix pr to fix corresponding bug plugin/input 1. Request for new input plugins 2. Issues/PRs that are related to input plugins
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[inputs.x509_cert] Using wrong server name for verification
4 participants