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

TLS Auth - Support for encrypted private key #2488

Conversation

Gabrielopesantos
Copy link
Contributor

@Gabrielopesantos Gabrielopesantos commented Apr 7, 2022

Related issue: #2435 (Also presents the problem)

Proposal:

  • Allow users to pass an password field which is used to decrypt the private key; If the key is not protected/encrypted, password will be ignored.

@CLAassistant
Copy link

CLAassistant commented Apr 7, 2022

CLA assistant check
All committers have signed the CLA.

@Gabrielopesantos Gabrielopesantos force-pushed the feat/support-for-encrypted-keys-in-tlsauth branch from de624f1 to 2ea9573 Compare April 8, 2022 21:26
@Gabrielopesantos Gabrielopesantos marked this pull request as ready for review April 8, 2022 21:27
@github-actions github-actions bot requested review from na-- and oleiade April 8, 2022 21:27
@na-- na-- requested review from mstoykov and codebien and removed request for na-- April 11, 2022 08:03
lib/options.go Outdated
return nil, fmt.Errorf("failed to decode PEM key")
}
blockType := block.Type
if encrypted := x509.IsEncryptedPEMBlock(block); encrypted {
Copy link
Collaborator

Choose a reason for hiding this comment

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

If we have password, shouldn't we always try to decrypt it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we always try decrypt, in cases were a non encrypted key is provided, since it does not a DEK header, x509.DecryptPEMBlock (https://pkg.go.dev/crypto/x509#DecryptPEMBlock) will return an error. In my opinion a good approach would be just add the warning saying that a password is not required if the key is not encrypted, as suggested below.

If both of you you agree with this, if possible, tell me how can I add a warning.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess that you can't log here 🤔 . But on the other hand I would argue it should be an outright error if there is a password, and it isn't encrypted. If you have a password and not DEK header ... you maybe shouldn't have password ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I understand your point. I just thought it would confuse some users the first time they use the feature. Made the change.

Copy link
Collaborator

Choose a reason for hiding this comment

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

confuse some users

I would argue it will be even more confusing if you add an option and nothing changes - if we at least then error out differently IMO it's better.

lib/options.go Show resolved Hide resolved
Copy link
Member

@oleiade oleiade left a comment

Choose a reason for hiding this comment

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

I'm in line with @mstoykov comments.
Plus, a few nitpicks that would have made the code easier to read from my perspective 👍🏻

Waiting on comments resolution and the branch to be green to approve 🟢

lib/options.go Show resolved Hide resolved
lib/options.go Show resolved Hide resolved
lib/options.go Outdated Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Apr 13, 2022

Codecov Report

Merging #2488 (7f9bcc9) into master (42f2e60) will increase coverage by 2.72%.
The diff coverage is 67.37%.

@@            Coverage Diff             @@
##           master    #2488      +/-   ##
==========================================
+ Coverage   72.71%   75.44%   +2.72%     
==========================================
  Files         184      200      +16     
  Lines       14571    15877    +1306     
==========================================
+ Hits        10596    11978    +1382     
+ Misses       3333     3164     -169     
- Partials      642      735      +93     
Flag Coverage Δ
ubuntu 75.33% <67.37%> (+2.68%) ⬆️
windows 75.07% <67.14%> (+2.70%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
api/common/context.go 100.00% <ø> (ø)
api/v1/client/client.go 0.00% <0.00%> (ø)
api/v1/client/metrics.go 0.00% <0.00%> (ø)
api/v1/client/status.go 0.00% <0.00%> (ø)
api/v1/group.go 54.05% <ø> (-35.01%) ⬇️
api/v1/metric.go 77.27% <ø> (-22.73%) ⬇️
api/v1/routes.go 55.31% <ø> (ø)
api/v1/status.go 100.00% <ø> (ø)
cmd/login_cloud.go 19.76% <0.00%> (+19.76%) ⬆️
cmd/login_influxdb.go 11.66% <0.00%> (+11.66%) ⬆️
... and 244 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 59a8883...7f9bcc9. Read the comment docs.

@mstoykov
Copy link
Collaborator

It seems like the tests aren't passing after the last changes @Gabrielopesantos, can you take a look 🙇 .

I will try to test them by hand by the end of the week and hopefully we can have this merge next week 🤞

@Gabrielopesantos
Copy link
Contributor Author

Ok,updated the tests and also added a new tests covering the case where a user defines a password with a non encrypted.

The CI seems to still be failing, maybe adding // nolint: staticcheck solves the problem?

@mstoykov
Copy link
Collaborator

Yeah - you need will need to nolint this, preferably with an additional comment on why we are still using it.

mstoykov
mstoykov previously approved these changes Apr 20, 2022
Copy link
Collaborator

@mstoykov mstoykov left a comment

Choose a reason for hiding this comment

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

Thanks a lot for working on this 🙇

LGTM - I have tried it out locally, and I don't see any problems, although I can't really make requests with it as I don't have any server that uses client certificates handy :(. That shouldn't matter though as that is handled by the stdlib - we just need to give it the certificate.

I have left one change on a comment, but I am not even certain it is all that much better than before, so we might leave it.

lib/options.go Outdated Show resolved Hide resolved
Co-authored-by: Mihail Stoykov <MStoykov@users.noreply.github.com>
Copy link
Member

@oleiade oleiade left a comment

Choose a reason for hiding this comment

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

🎉 👍🏻

@mstoykov mstoykov merged commit 49b0b8c into grafana:master Apr 21, 2022
@na-- na-- added this to the v0.38.0 milestone Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants