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

Update Loadbalancer SSL Certificate #702

Closed
nightmare-rg opened this issue Feb 22, 2024 · 4 comments
Closed

Update Loadbalancer SSL Certificate #702

nightmare-rg opened this issue Feb 22, 2024 · 4 comments
Assignees
Labels

Comments

@nightmare-rg
Copy link

nightmare-rg commented Feb 22, 2024

TL;DR

I have a simple shell script for updating loadbalancer certificates with renewed lets encrypt certs. My script works with v1.36 and now I got an error.

Expected behavior

/tmp/hcloud version
hcloud 1.36.0
/tmp/hcloud load-balancer update-service staging-keycloak --http-certificates 1261648 --listen-port 443
500ms [==================================] 100.00%
Service 443 on Load Balancer 1550279 was updated

Observed behavior

/usr/local/bin/hcloud version
hcloud 1.42.0
/usr/local/bin/hcloud load-balancer update-service staging-keycloak --http-certificates 1261648 --listen-port 443
hcloud: invalid input in field 'health_check' (invalid_input)

Minimal working example

#!/bin/bash

# Set your Hetzner API token
export HCLOUD_TOKEN="XXXXXX"

# Set the name of your load balancers
LB_NAME=('staging-keycloak')

# Set the name of the certificate you want to replace
CERT_NAME="example-wildcart-cert-"$(date +"%Y%m%d-%H%M%S")

# Set the path to your new certificate and private key files
CERT_FILE="/etc/letsencrypt/live/example.de/fullchain.pem"
KEY_FILE="/etc/letsencrypt/live/example.de/privkey.pem"

# Upload the new certificate
CERT_ID=$(/usr/local/bin/hcloud certificate create --name $CERT_NAME --cert-file $CERT_FILE --key-file $KEY_FILE | grep -oP '(?<=Certificate )\d+')

echo $CERT_ID

for lb in "${LB_NAME[@]}";
do
    # Update the load balancer to use the new certificate
    echo "/usr/local/bin/hcloud load-balancer update-service $lb --http-certificates $CERT_ID --listen-port 443"
    /usr/local/bin/hcloud load-balancer update-service $lb --http-certificates $CERT_ID --listen-port 443
done

Log output

hcloud: invalid input in field 'health_check' (invalid_input)

Additional information

Bildschirmfoto 2024-02-22 um 09 12 55

@apricote apricote self-assigned this Feb 22, 2024
@apricote
Copy link
Member

I can reproduce this. Looks like we now sent (broken) empty options in the request.

Request from CLI 1.36.0:

--- Request:
POST /v1/load_balancers/1710364/actions/update_service HTTP/1.1
Host: api.hetzner.cloud
User-Agent: hcloud-cli/unknown hcloud-go/1.47.0
Content-Length: 80
Authorization: REDACTED
Content-Type: application/json
Accept-Encoding: gzip

{"listen_port":443,"destination_port":8080,"http":{},"health_check":{"http":{}}}

--- Response:
HTTP/2.0 201 Created

Request from CLI v1.42.0:

--- Request:
POST /v1/load_balancers/1710364/actions/update_service HTTP/1.1
Host: api.hetzner.cloud
User-Agent: hcloud-cli/1.42.0-dev hcloud-go/2.6.0
Content-Length: 118
Authorization: REDACTED
Content-Type: application/json
Accept-Encoding: gzip

{"listen_port":443,"destination_port":8080,"http":{"certificates":null},"health_check":{"http":{"status_codes":null}}}

--- Response:
HTTP/2.0 422 Unprocessable Entity

@apricote
Copy link
Member

By bisecting I found out that is is broken since commit 59d73f4, which bumped us to hcloud-go v2.5.0. Further bisecting hcloud-go it looks like this commit is at the root of it: hetznercloud/hcloud-go@6feda4d. This makes sense, we overhauled the way we convert our public Go Types and the API Schema types.

Will take a look into fixing this.

phm07 added a commit to hetznercloud/hcloud-go that referenced this issue Mar 5, 2024
In the conversion from LoadBalancerUpdateServiceOpts to schema.LoadBalancerActionUpdateServiceRequest, there are conversions from slices to slice pointers. Slice pointers exist in schemas to differentiate between absent and empty.

Goverter converts nil slices to &nil. This leads to the field being marshaled to `null` in JSON (even with the `omitempty` flag) instead of being not present (See hetznercloud/cli#702)

This PR fixes this issue by adding manual conversion methods
phm07 added a commit to hetznercloud/hcloud-go that referenced this issue Mar 5, 2024
In the conversion from LoadBalancerUpdateServiceOpts to
schema.LoadBalancerActionUpdateServiceRequest, there are conversions
from slices to slice pointers. Slice pointers exist in schemas to
differentiate between absent and empty.

Goverter converts nil slices to &nil. This leads to the field being
marshaled to `null` in JSON (even with the `omitempty` flag) instead of
being not present (See hetznercloud/cli#702)

This PR fixes this issue by adding manual conversion methods and adds
tests to account for this behavior.

---------

Co-authored-by: pauhull <[email protected]>
@phm07
Copy link
Contributor

phm07 commented Apr 2, 2024

Should be fixed with #720.

Can you please confirm that it works for you with

  • go install github.com/hetznercloud/cli/cmd/hcloud@6cea4cd
  • or go run github.com/hetznercloud/cli/cmd/hcloud@6cea4cd load-balancer update-service staging-keycloak --http-certificates 1261648 --listen-port 443

@nightmare-rg
Copy link
Author

@phm07 it works fine with newest go version. Thank you!

go run github.com/hetznercloud/cli/cmd/hcloud@6cea4cd load-balancer update-service staging-keycloak --http-certificates 1261648 --listen-port 443
1.1s [===================================] 100.00%
Service 443 on Load Balancer 1550279 was updated
root@keycloak-1:~#
go version
go version go1.22.2 linux/amd64

@apricote apricote closed this as completed Apr 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants