Skip to content

feat: Add key_algorithm configuration to application template#113

Open
hawksight wants to merge 2 commits intomainfrom
certificate-options
Open

feat: Add key_algorithm configuration to application template#113
hawksight wants to merge 2 commits intomainfrom
certificate-options

Conversation

@hawksight
Copy link
Member

Partially fixes #33.

The key algorithm and relevant parameters were hard coded. This change allows you to specify options you would like in a succinct manner. It also defaults the values if unspecified in order to minimise configuration. key_reuse option also now defaults to false for more minimal configuration.

Example testing resource:

resource "tlspc_certificate_template" "provider_test" {
  name          = "provider-test-template"
  ca_type       = data.tlspc_ca_product.built_in.type
  ca_product_id = data.tlspc_ca_product.built_in.id
  # key_reuse     = true
  # key_algorithms = ["RSA_1024", "RSA_2048", "RSA_3072", "RSA_4096", "EC_P256", "EC_P384", "EC_P521", "EC_ED25519"]
  key_algorithms = ["RSA_3072", "RSA_4096", "EC_ED25519"]
}

Result:

Screenshot 2026-03-18 at 14 55 42

State:

# tlspc_certificate_template.provider_test:
resource "tlspc_certificate_template" "provider_test" {
    ca_product_id  = "47678210-f64b-11ee-a73e-d7aec1cfe39b"
    ca_type        = "BUILTIN"
    id             = "50086510-220e-11f1-9dc7-ab147436f184"
    key_algorithms = [
        "RSA_3072",
        "RSA_4096",
        "EC_ED25519",
    ]
    key_reuse      = false
    name           = "provider-test-template"
}

Partially fixes #33.

The key algoirthm and relevant parameters were hard coded.
This change allows you to specify options you would like in a succinct manner.
It also defaults the values if unspecified in order to minimise configuration.
key_reuse option also now defaults to false for more minimal configuration.

Signed-off-by: Peter Fiddes <peter.fiddes@jetstack.io>
@hawksight hawksight self-assigned this Mar 18, 2026
@hawksight hawksight added documentation Improvements or additions to documentation enhancement New feature or request go Pull requests that update Go code labels Mar 18, 2026
@hawksight hawksight marked this pull request as ready for review March 18, 2026 14:57
@hawksight hawksight requested a review from aidy as a code owner March 18, 2026 14:57
@snyk-io
Copy link

snyk-io bot commented Mar 18, 2026

Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues
Code Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@hawksight hawksight added this to the v0.6.0 milestone Mar 18, 2026
prts := strings.Split(v.ValueString(), "_")

// First check no unknown key types or malformed input.
if prts[0] != "RSA" && prts[0] != "EC" || len(prts) != 2 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Probably for later; but I think we could add a validator to the schema attribute.

Copy link
Collaborator

Choose a reason for hiding this comment

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

(which is better, because then we validate at plan time, rather than at apply time)

continue
}
if !slices.Contains(allowedKeyLengths, int32(length)) {
fmt.Printf("Unsupported key length: %d\n", length)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Generally, I've tried to avoid doing too much validation in terraform space.
If the platform gains support for a new key length, I don't think we should have to do another release of the provider just to add it.

That's why I've been phrasing things in the docs as "valid options include" rather than "this is the list of valid options"

Copy link
Member Author

Choose a reason for hiding this comment

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

Looking at the validator option here: https://developer.hashicorp.com/terraform/plugin/framework/validation#attribute-validation

I think that may well reduce the need to validate in code and cut it down. I'd be happy to do that as it would mean less maintenance.

However I don't feel like key options will change quickly or massively. I felt it was better to be explicit with the available options now, rather than cater to something that's unlikely to change. I'm sure there would be a big noise around adding say quantum key support?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I kinda reckon that the provider should validate the format, there's no point sending stuff we know is garbage, but the api should validate the value. I think there's a difference between being clear about what the available options are now, vs. only allowing those options without a further code change/provider release.

I had been more thinking about the user roles here, where I might expect that other roles might be added for more granular access - but even here, although I'd not expect the types (RSA, EC) to change, I might expect different key lengths or curves to be added.

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe take another look now, I've changed to use a validator, here've and example catch:

│ Error: Invalid Attribute Value Match
│
│   with tlspc_certificate_template.provider_test,
│   on main.tf line 98, in resource "tlspc_certificate_template" "provider_test":
│   98:   key_algorithms = ["RSA_3072", "RSA_4096", "EC_ED25519", "EC_4096"]
│
│ Attribute key_algorithms[3] value must be one of: ["RSA_1024" "RSA_2048" "RSA_3072" "RSA_4096" "EC_P256" "EC_P384" "EC_P521" "EC_ED25519"], got:
│ "EC_4096"
╵

Given it's a very small list that seemed easier to comprehend than a complicated regex.. which effectively does the same: Narrow input to the 8 available options.

I tried regex, but quickly found if I tried to be clever, I left a hole in the middle. eg.

  # tlspc_certificate_template.provider_test will be updated in-place
  ~ resource "tlspc_certificate_template" "provider_test" {
        id             = "8b7e0170-239b-11f1-aa82-bb1f9e36b693"
      ~ key_algorithms = [
            # (2 unchanged elements hidden)
            "EC_ED25519",
          + "EC_4096",
        ]
        name           = "provider-test-template"
        # (3 unchanged attributes hidden)
    }

Copy link
Collaborator

Choose a reason for hiding this comment

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

What error do we get back from the API if we do send an invalid key alg?

@hawksight hawksight requested a review from aidy March 19, 2026 14:14
Signed-off-by: Peter Fiddes <peter.fiddes@jetstack.io>
@hawksight hawksight force-pushed the certificate-options branch from bdfdedc to 0487ec1 Compare March 19, 2026 14:16
Default: listdefault.StaticValue(defaultKeyAlgorithms),
Validators: []validator.List{
listvalidator.ValueStringsAre(
stringvalidator.OneOf(allowedAlgorithms...),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Unless you need to use allowedAlgorithms in multiple places, I would just hardcode the list in here.
Because, as a global array variable, the list is otherwise mutable.

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 wasn't sure if it was worth having them on the data source as well.
I don't think it's needed for that so I will move it over.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation enhancement New feature or request go Pull requests that update Go code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement remaining fields for certificate_template resource

2 participants