Skip to content

Add TLS package and helper methods to runtime#102

Merged
hiddeco merged 1 commit intomainfrom
feature/certs
Jul 9, 2021
Merged

Add TLS package and helper methods to runtime#102
hiddeco merged 1 commit intomainfrom
feature/certs

Conversation

@phillebaba
Copy link
Copy Markdown
Member

This change is meant to standardize how certificates are read from secrets by the controllers. This logic is currently shared by notification-controller and image-reflector-controller, there might be more.

The original logic is taken from image-reflector-controller.
https://github.com/fluxcd/image-reflector-controller/blob/de3de64adddf8bb00276e12aa0436968bc94ff39/controllers/imagerepository_controller.go#L278-L309

I did however make a change to the behavior so that the functions will return an error if the secret does not contain a ca or cert. Or when the client cert is set but not client key and vice versa. My opinion is that this behavior is more logical than the previous one.

@phillebaba
Copy link
Copy Markdown
Member Author

@squaremo do you have any opinion about the change in behavior compared to image-reflector-controller?

@squaremo
Copy link
Copy Markdown
Member

@squaremo do you have any opinion about the change in behavior compared to image-reflector-controller?

Seems reasonable to me. I can't think of a situation in which you'd want a client cert without the key, or vice versa.

Comment thread runtime/cert/cert.go Outdated

clientCert, clientCertOk := certSecret.Data[ClientCert]
clientKey, clientKeyOk := certSecret.Data[ClientKey]
if clientCertOk && !clientKeyOk {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would usually put mutually exclusive conditions using the same variables like these in a switch, because it looks a bit like a logic table:

switch {
  case clientCertOk && !clientKeyOk:
    // ...
  case !clientCertOk && clientKeyOk:
    // ...
  case clientCertOk && clientKeyOk:
    // ...
}

Modulo the error message, all you care about is that they are both present or neither, so you could also do this:

if clientKeyOk != clientCertOk {
  return nil, fmt.Errorf("found one of %s or %s, and expected both or neither", ClientCert, ClientKey)
}
if clientKeyOk && clientCertOk {
  // ...
}

If you don't think either of these an improvement, your formulation is totally fine -- just fine-tuning :-)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

You are right and I think the second option is smart.

Copy link
Copy Markdown
Member

@hiddeco hiddeco left a comment

Choose a reason for hiding this comment

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

I think this util can also be used in the source-controller for e.g. the Helm repository indexes.

Comment thread runtime/cert/cert.go Outdated
@phillebaba phillebaba requested a review from hiddeco June 17, 2021 11:16
@@ -0,0 +1,51 @@
/*
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please add _test to this file, we don't want these certs in flux binary or the controllers.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It may be even better to simply use the fixtures from https://cs.opensource.google/go/x/crypto/+/master:ssh/testdata/keys.go

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ok I will remove the certs from this PR.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@hiddeco I had a look at the testdata and I am a bit confused about how that can be used to replace the test certificate currently in the PR.

@hiddeco
Copy link
Copy Markdown
Member

hiddeco commented Jul 7, 2021

@phillebaba this PR is still waiting on an update. Couple of additional comments:

  • Due to all the efforts in Add conditions and patch helpers, and docs to runtime #101, the introduction of the package requires a doc.go file to outline the package
  • Comments are now up to 120 characters in length
  • Given it only deals with TLS, I would maybe also call the package tls and not certs (this would match crypto/tls)

@hiddeco hiddeco added the area/runtime Controller runtime related issues and pull requests label Jul 8, 2021
Signed-off-by: Philip Laine <philip.laine@gmail.com>
Co-authored-by: Hidde Beydals <hello@hidde.co>
Signed-off-by: Hidde Beydals <hello@hidde.co>
@hiddeco hiddeco changed the title Add certificate helper methods and example certificate Add TLS package and helper methods to runtime Jul 9, 2021
@hiddeco hiddeco merged commit 7a2365c into main Jul 9, 2021
@hiddeco hiddeco deleted the feature/certs branch July 9, 2021 09:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/runtime Controller runtime related issues and pull requests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants