From 774cf3474083efb15ab364c4660cd0c3dbe44eb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wilson=20J=C3=BAnior?= Date: Thu, 8 Aug 2024 14:17:17 -0300 Subject: [PATCH] cmd: add name field using cert-manager to allow multiples instances --- api/v1alpha1/rpaasinstance_types.go | 4 +++ cmd/plugin/rpaasv2/cmd/certificates.go | 9 +++-- cmd/plugin/rpaasv2/cmd/certificates_test.go | 37 +++++++++++++++++++++ pkg/rpaas/client/types/types.go | 3 ++ pkg/web/extra_files.go | 3 -- 5 files changed, 51 insertions(+), 5 deletions(-) diff --git a/api/v1alpha1/rpaasinstance_types.go b/api/v1alpha1/rpaasinstance_types.go index 6615afb46..f80cbecb9 100644 --- a/api/v1alpha1/rpaasinstance_types.go +++ b/api/v1alpha1/rpaasinstance_types.go @@ -153,6 +153,10 @@ type DynamicCertificates struct { } type CertManager struct { + // Name is recently introduced to allow multiple certificates in the same instance. + // +optional + Name string `json:"name"` + // Issuer refers either to Issuer or ClusterIssuer resource. // // NOTE: when there's no Issuer on this name, it tries using ClusterIssuer instead. diff --git a/cmd/plugin/rpaasv2/cmd/certificates.go b/cmd/plugin/rpaasv2/cmd/certificates.go index e16c5adce..78fad99b9 100644 --- a/cmd/plugin/rpaasv2/cmd/certificates.go +++ b/cmd/plugin/rpaasv2/cmd/certificates.go @@ -49,7 +49,6 @@ func NewCmdUpdateCertitifcate() *cli.Command { &cli.StringFlag{ Name: "name", Usage: "an identifier for the current certificate and key", - Value: "default", }, &cli.PathFlag{ Name: "certificate", @@ -104,9 +103,14 @@ func runUpdateCertificate(c *cli.Context) error { return err } + name := c.String("name") + if name == "" { + name = "default" + } + args := rpaasclient.UpdateCertificateArgs{ Instance: c.String("instance"), - Name: c.String("name"), + Name: name, Certificate: string(certificate), Key: string(key), } @@ -131,6 +135,7 @@ func updateCertManagerCertificate(c *cli.Context, client rpaasclient.Client) (bo err := client.UpdateCertManager(c.Context, rpaasclient.UpdateCertManagerArgs{ Instance: c.String("instance"), CertManager: clientTypes.CertManager{ + Name: c.String("name"), Issuer: c.String("issuer"), DNSNames: c.StringSlice("dns"), IPAddresses: c.StringSlice("ip"), diff --git a/cmd/plugin/rpaasv2/cmd/certificates_test.go b/cmd/plugin/rpaasv2/cmd/certificates_test.go index 718f064bc..61b9890b6 100644 --- a/cmd/plugin/rpaasv2/cmd/certificates_test.go +++ b/cmd/plugin/rpaasv2/cmd/certificates_test.go @@ -93,6 +93,24 @@ EKTcWGekdmdDPsHloRNtsiCa697B2O9IFA== expected: "certificate \"my-instance.example.com\" updated in my-instance\n", }, + { + name: "when UpdateCertificate with default name", + args: []string{"./rpaasv2", "certificates", "update", "-i", "my-instance", "--cert", certFile.Name(), "--key", keyFile.Name()}, + client: &fake.FakeClient{ + FakeUpdateCertificate: func(args rpaasclient.UpdateCertificateArgs) error { + expected := rpaasclient.UpdateCertificateArgs{ + Instance: "my-instance", + Name: "default", + Certificate: certPem, + Key: keyPem, + } + assert.Equal(t, expected, args) + return nil + }, + }, + expected: "certificate \"default\" updated in my-instance\n", + }, + { name: "enabling cert-manager integration", args: []string{"./rpaasv2", "certificates", "add", "-i", "my-instance", "--cert-manager", "--issuer", "lets-encrypt", "--dns", "my-instance.example.com", "--dns", "foo.example.com", "--ip", "169.196.100.100", "--ip", "2001:db8:dead:beef::"}, @@ -112,6 +130,25 @@ EKTcWGekdmdDPsHloRNtsiCa697B2O9IFA== expected: "cert manager certificate was updated\n", }, + { + name: "enabling cert-manager integration with name definition", + args: []string{"./rpaasv2", "certificates", "add", "-i", "my-instance", "--cert-manager", "--name", "cert01", "--issuer", "lets-encrypt", "--dns", "my-instance.example.com"}, + client: &fake.FakeClient{ + FakeUpdateCertManager: func(args rpaasclient.UpdateCertManagerArgs) error { + assert.Equal(t, rpaasclient.UpdateCertManagerArgs{ + Instance: "my-instance", + CertManager: types.CertManager{ + Name: "cert01", + Issuer: "lets-encrypt", + DNSNames: []string{"my-instance.example.com"}, + }, + }, args) + return nil + }, + }, + expected: "cert manager certificate was updated\n", + }, + { name: "passing DNS names without cert manager flag", args: []string{"./rpaasv2", "certificates", "add", "-i", "my-instance", "--dns", "my-instance.example.com"}, diff --git a/pkg/rpaas/client/types/types.go b/pkg/rpaas/client/types/types.go index e1e011245..234661cf8 100644 --- a/pkg/rpaas/client/types/types.go +++ b/pkg/rpaas/client/types/types.go @@ -162,6 +162,9 @@ type CertManager struct { Issuer string `json:"issuer"` DNSNames []string `json:"dnsNames,omitempty"` IPAddresses []string `json:"ipAddresses,omitempty"` + + // Name is used to multiple certificates in the same instance, and also to take over the manual certificates + Name string `json:"name,omitempty"` } type Metadata struct { diff --git a/pkg/web/extra_files.go b/pkg/web/extra_files.go index 626569ca9..63bfc6387 100644 --- a/pkg/web/extra_files.go +++ b/pkg/web/extra_files.go @@ -126,9 +126,6 @@ func deleteExtraFiles(c echo.Context) error { } var files []string err = c.Bind(&files) - if err != nil { - return err - } if err != nil { return &echo.HTTPError{ Code: http.StatusBadRequest,