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

Make x509 altnames and extkeyusage optional #177

Closed
wants to merge 1 commit into from

Conversation

treydock
Copy link
Contributor

The defaults of empty arrays are generating invalid openssl configs

Example of what's generated:

[ v3_req ]
extendedKeyUsage  =
subjectAltName    = @alt_names

This throws all sorts of errors:

[root@rocky-8 /]# /opt/puppetlabs/puppet/bin/openssl req -new -key /etc/xdmod/simplesamlphp/cert/xdmod.key -config /etc/xdmod/simplesamlphp/cert/xdmod.cnf -out /etc/xdmod/simplesamlphp/cert/xdmod.csr
Error Loading request extension section v3_req
46912522284800:error:2206D06C:X509 V3 routines:X509V3_parse_list:invalid null name:crypto/x509v3/v3_utl.c:386:
46912522284800:error:22097069:X509 V3 routines:do_ext_nconf:invalid extension string:crypto/x509v3/v3_conf.c:93:name=extendedKeyUsage,section=
46912522284800:error:22098080:X509 V3 routines:X509V3_EXT_nconf:error in extension:crypto/x509v3/v3_conf.c:47:name=extendedKeyUsage, value=

@treydock treydock force-pushed the optional-x509-params branch from 1e962ea to 8ebf7cf Compare March 20, 2024 17:20
@treydock treydock requested a review from bastelfreak March 20, 2024 17:32
Array $altnames = [],
Array $extkeyusage = [],
Optional[Array] $altnames = undef,
Optional[Array] $extkeyusage = undef,
Optional[String] $email = undef,
Copy link
Contributor

@rtib rtib Mar 22, 2024

Choose a reason for hiding this comment

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

This is not solving the problem. It still allows to set an empty array to those parameters which will lead to the same result.

@@ -159,7 +159,7 @@
$_csr = pick($csr, "${_csr_dir}/${name}.csr")
$_key = pick($key, "${_key_dir}/${name}.key")

if !empty($altnames+$extkeyusage) {
if $altnames or $extkeyusage {
Copy link
Contributor

@rtib rtib Mar 22, 2024

Choose a reason for hiding this comment

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

What's about $req_ext = !empty($altnames) or !empty($extkeyusage) instead of that if-else-spaghetti

Copy link
Contributor

@zilchms zilchms Mar 22, 2024

Choose a reason for hiding this comment

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

well, you have to detect if we actually have values in extkexusage and altnames in order to set the req_ext. You cant set it always as that would error out when generating a cert, when both arrays are empty (and not set in the config, assuming its fixed). I am working on memory here though, when i last worked on it. I could be wrong.

If we only allow non-empty arrays and undef the 'if $altnames or $extkeyusage' works just fine, no need for the empty check

Copy link
Contributor

Choose a reason for hiding this comment

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

First: My suggesting is essentially to use

$boolean_variable = (expression)

over something like

if (expression) {
  $boolean_variable = true
} else {
  $boolean_variable = false
}

Second, the v3_req section of the config depends on a lot more parameters. The $req_ext is not passed to the template at all, it is referenced only at a single point, to pass it to x509_cert. Thus, you can drop this variable and replace the expression into the x509_cert to be passed to its req_ext parameter.

@rtib
Copy link
Contributor

rtib commented Mar 22, 2024

Sorry for the fly by comment, but you may have a look on 50317c2 which is fixing the config template too, I hope.

@treydock treydock force-pushed the optional-x509-params branch from 8ebf7cf to e01db1f Compare March 22, 2024 15:10
@treydock
Copy link
Contributor Author

@rtib What about the updated change to use Optional[Array[String[1]]] ? That way it's either Undef or a non-empty Array so no need to check if empty as Puppet will validate the parameters first.

The defaults of empty arrays are generating invalid openssl configs
@treydock treydock force-pushed the optional-x509-params branch from e01db1f to c6481cb Compare March 22, 2024 15:12
@rtib
Copy link
Contributor

rtib commented Mar 22, 2024

The semantics of the empty() function is covering all you need. It is built-in with Puppet, so there is no additional dependency.

Though notions like Optional[Array[String[1]]] seem pretty popular these days, IMO, they are kinda crabbed. This particular one still allow undef which you need to check for later and it is deprecated to use empty() for that. It seems more simple and easier to understand to have an Array or Array[String] with an empty array as default.

@rtib
Copy link
Contributor

rtib commented Mar 22, 2024

The root cause of the problem is lying in the template used to generate the config, not in type signature of the parameters. This is part of what rendered release 3.0.0 unusable (see #178). I'd prefer to not change the module API.

@zilchms
Copy link
Contributor

zilchms commented Mar 22, 2024

I agree partly with both: we have to fix the template to only set the values (and corresponding keys) in the config, if they are present. Checking for undef is easy enough in a template, default boolean behaviour will take care of that (even without empty).
I also agree with using Optional[Array[String[1]]]. That way only non empty Arrays with non-empty Strings can be passed in. The undef for the default value we can deal with easily in the config template.

@rtib
Copy link
Contributor

rtib commented Mar 22, 2024

... using Optional[Array[String[1]]]. That way only non empty Arrays with non-empty Strings can be passed in. The undef for the default value we can deal with easily in the config template.

Look at it:
Optional[Array[String[1]]] $altnames = undef,
or
Array $altnames = [],

Now imagine you are new to this module, probably new to Puppet, you want to use the module trying to understand its parameters. Which one you'd prefer?

@rtib
Copy link
Contributor

rtib commented Mar 22, 2024

From the software engineering point of view, the real difference between

if $somearray {

and

unless $somearray.empty {

is that the first one is using a hidden, implicit behaviour of the language, while the semantics of the later is fully explicit.

@treydock
Copy link
Contributor Author

I look at data types as a way to represent what's valid. If an empty array is not valid, it should not be allowed as a parameter irregardless of how it might look to someone new to Puppet. Data types should validate the inputs, if empty array is not a valid input, it shouldn't be allowed. Similar for empty strings. If empty strings are not a valid input, data types need to prevent them.

I'd imagine a new Puppet user would rather be prevented from invalid inputs or unexpected behavior rather than having an easier time looking at the code. If they go to look at the code cause they passed empty array and it broke X509 cert creation, it will be a lot harder to debug (as I recently found out) than if they are prevented from that issue to begin with.

@treydock
Copy link
Contributor Author

But the real issue is the template, so fixing that via 50317c2 might be better or be good to do at same time.

@rtib
Copy link
Contributor

rtib commented Mar 22, 2024

I look at data types as a way to represent what's valid.

Which is completely fine. However, there are two perspectives: one is the public API of the module, intended to be understood and used by others; and two, all the internal variables defined as they are used.

On the public API, a parameter called altnames, intended to hold a list of subjectAltName entries might be of type Array. There is nothing wrong having this array empty, even by default. There is no reason to tie the empty case to a special value, i.e. undef.

@treydock
Copy link
Contributor Author

Correct, empty array is a valid default in these cases. @rtib Do you want to open PR with template fixes or do you want me to handle with this PR?

@rtib
Copy link
Contributor

rtib commented Mar 22, 2024

@rtib Do you want to open PR with template fixes or do you want me to handle with this PR?

I think this PR is the right place for that. But, we need to test that thoroughly, that commit is just a first take.

@rtib
Copy link
Contributor

rtib commented Mar 25, 2024

I found some time to look deeper into this and did some refactoring:

@zilchms
Copy link
Contributor

zilchms commented Mar 25, 2024

Those look pretty good, would you like to provide them in a PR? :)

@rtib
Copy link
Contributor

rtib commented Mar 26, 2024

Opened #179

@zilchms
Copy link
Contributor

zilchms commented Apr 30, 2024

I think with the merge of #179 and #180, this is no longer needed? @treydock,. can you check that?

@treydock treydock closed this Apr 30, 2024
@treydock treydock deleted the optional-x509-params branch April 30, 2024 17:57
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.

3 participants