Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

PLATFORMINFRA-218: always recreate csr #15

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion agent/registration/csr.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (

"github.com/go-logr/logr"
certv1 "k8s.io/api/certificates/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
clientset "k8s.io/client-go/kubernetes"
restclient "k8s.io/client-go/rest"
Expand Down Expand Up @@ -118,9 +120,18 @@ func (bcsr *ByohCSR) RequestBYOHClientCert(hostname string) (string, types.UID,
}
certTimeToExpire := bcsr.expiryDuration
bcsr.logger.Info("certTimeToExpire", "duration", certTimeToExpire)

csrName := fmt.Sprintf(ByohCSRNameFormat, hostname)
err = bcsr.bootstrapClient.CertificatesV1().CertificateSigningRequests().Delete(
context.TODO(), csrName, metav1.DeleteOptions{})

if err != nil && !errors.IsNotFound(err) {
return "", "", err
}

reqName, reqUID, err := csr.RequestCertificate(bcsr.bootstrapClient,
csrData,
fmt.Sprintf(ByohCSRNameFormat, hostname),
csrName,
certv1.KubeAPIServerClientSignerName,
&certTimeToExpire,
[]certv1.KeyUsage{certv1.UsageClientAuth},
Expand Down
18 changes: 15 additions & 3 deletions agent/registration/csr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ kovW9X7Ook/tTW0HyX6D6HRciA==

Expect(os.Remove(registration.TmpPrivateKey)).ShouldNot(HaveOccurred())
})
It("should fail creating CSR if the private key got changed", func() {
It("should create CSR if the private key got changed", func() {
byohCSR, err := builder.CertificateSigningRequest(
fmt.Sprintf(registration.ByohCSRNameFormat, hostName),
fmt.Sprintf(registration.ByohCSRCNFormat, hostName),
Expand All @@ -160,8 +160,20 @@ kovW9X7Ook/tTW0HyX6D6HRciA==
CSRRegistrar, err := registration.NewByohCSR(cfg, klogr.New(), certExpiryDuration)
Expect(err).ShouldNot(HaveOccurred())
_, _, err = CSRRegistrar.RequestBYOHClientCert(hostName)
Expect(err).Should(HaveOccurred())
Expect(err.Error()).Should(ContainSubstring("retrieved csr is not compatible"))
Expect(err).NotTo(HaveOccurred())
ByohCSR, err := k8sClientSet.CertificatesV1().CertificateSigningRequests().Get(ctx, fmt.Sprintf(registration.ByohCSRNameFormat, hostName), metav1.GetOptions{})
Expect(err).ShouldNot(HaveOccurred())
// Validate k8s CSR resource
Expect(ByohCSR.Spec.SignerName).Should(Equal(certv1.KubeAPIServerClientSignerName))
Expect(ByohCSR.Spec.Usages).Should(Equal([]certv1.KeyUsage{certv1.UsageClientAuth}))
Expect(*ByohCSR.Spec.ExpirationSeconds).Should(Equal(int32((time.Hour * 24).Seconds())))
// Validate Certificate Request
pemData, _ := pem.Decode(ByohCSR.Spec.Request)
Expect(pemData).ToNot(Equal(nil))
csr, err := x509.ParseCertificateRequest(pemData.Bytes)
Expect(err).ToNot(HaveOccurred())
Expect(csr.Subject.CommonName).To(Equal(fmt.Sprintf(registration.ByohCSRCNFormat, hostName)))
Expect(csr.Subject.Organization[0]).To(Equal("byoh:hosts"))

Expect(os.Remove(registration.TmpPrivateKey)).ShouldNot(HaveOccurred())
})
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/BYOHDockerFile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG BASE_IMAGE=ubuntu:20.04
ARG BASE_IMAGE=ubuntu:22.04
FROM $BASE_IMAGE as build
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it absolutely neccesary?

Copy link
Collaborator Author

@bullet1337 bullet1337 Apr 5, 2024

Choose a reason for hiding this comment

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

yep, it must be the same as local where is binary was built. otherwise there is a problem with libc version

ARG TARGETARCH

Expand Down
Loading