Skip to content

WIP OCPBUGS-44842: Bump library-go to cert-annotations-not-before-not-after branch #1768

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,5 @@ require (
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)

replace github.com/openshift/library-go => github.com/vrutkovs/library-go v0.0.0-20241122091102-4d0a3e9ced24
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,6 @@ github.com/openshift/build-machinery-go v0.0.0-20240613134303-8359781da660 h1:F0
github.com/openshift/build-machinery-go v0.0.0-20240613134303-8359781da660/go.mod h1:8jcm8UPtg2mCAsxfqKil1xrmRMI3a+XU2TZ9fF8A7TE=
github.com/openshift/client-go v0.0.0-20241001162912-da6d55e4611f h1:FRc0bVNWprihWS0GqQWzb3dY4dkCwpOP3mDw5NwSoR4=
github.com/openshift/client-go v0.0.0-20241001162912-da6d55e4611f/go.mod h1:KiZi2mJRH1TOJ3FtBDYS6YvUL30s/iIXaGSUrSa36mo=
github.com/openshift/library-go v0.0.0-20241119162247-f466fdd82330 h1:nK16PhWNPIjnv7Vux1jBlzEPgA9q/o/CwqsbRpQJrpY=
github.com/openshift/library-go v0.0.0-20241119162247-f466fdd82330/go.mod h1:9B1MYPoLtP9tqjWxcbUNVpwxy68zOH/3EIP6c31dAM0=
github.com/orisano/pixelmatch v0.0.0-20220722002657-fb0b55479cde/go.mod h1:nZgzbfBr3hhjoZnS66nKrHmduYNpc34ny7RK4z5/HM0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand Down Expand Up @@ -215,6 +213,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tmc/grpc-websocket-proxy v0.0.0-20220101234140-673ab2c3ae75 h1:6fotK7otjonDflCTK0BCfls4SPy3NcCVb5dqqmbRknE=
github.com/tmc/grpc-websocket-proxy v0.0.0-20220101234140-673ab2c3ae75/go.mod h1:KO6IkyS8Y3j8OdNO85qEYBsRPuteD+YciPomcXdrMnk=
github.com/vrutkovs/library-go v0.0.0-20241122091102-4d0a3e9ced24 h1:OMTPL8M+Wba2xfon/rctp0SDp8JdIJwSKv8ponsKgp8=
github.com/vrutkovs/library-go v0.0.0-20241122091102-4d0a3e9ced24/go.mod h1:9B1MYPoLtP9tqjWxcbUNVpwxy68zOH/3EIP6c31dAM0=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 h1:eY9dn8+vbi4tKz5Qo6v2eYzo7kUS51QINcR5jNpbZS8=
Expand Down
22 changes: 22 additions & 0 deletions pkg/operator/nodekubeconfigcontroller/nodekubeconfigcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package nodekubeconfigcontroller

import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"fmt"
"strings"
"time"
Expand All @@ -14,6 +17,7 @@ import (
"github.com/openshift/cluster-kube-apiserver-operator/bindata"
"github.com/openshift/cluster-kube-apiserver-operator/pkg/operator/operatorclient"
"github.com/openshift/library-go/pkg/controller/factory"
"github.com/openshift/library-go/pkg/operator/certrotation"
"github.com/openshift/library-go/pkg/operator/events"
"github.com/openshift/library-go/pkg/operator/resource/resourceapply"
"github.com/openshift/library-go/pkg/operator/resource/resourceread"
Expand Down Expand Up @@ -112,6 +116,22 @@ func ensureNodeKubeconfigs(ctx context.Context, client coreclientv1.CoreV1Interf
return fmt.Errorf("system:admin client private key missing from secret %s/node-system-admin-client", operatorclient.OperatorNamespace)
}

// Ensure secret key matches the certificate
_, err = tls.X509KeyPair(systemAdminClientCert, systemAdminClientKey)
if err != nil {
return fmt.Errorf("system:admin client private key doesn't match the certificate from secret %s/node-system-admin-client", operatorclient.OperatorNamespace)
}
// extract not-before/not-after timestamps valid x509 certificate
var block *pem.Block
block, _ = pem.Decode(systemAdminClientCert)
if block == nil || block.Type != "CERTIFICATE" || len(block.Headers) != 0 {
return fmt.Errorf("invalid first block found for certificate from secret %s/node-system-admin-client", operatorclient.OperatorNamespace)
}
parsedCert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return fmt.Errorf("failed to parse the certificate from secret %s/node-system-admin-client", operatorclient.OperatorNamespace)
}

servingCABundleCM, err := configmapLister.ConfigMaps(operatorclient.TargetNamespace).Get("kube-apiserver-server-ca")
if err != nil {
return err
Expand Down Expand Up @@ -152,6 +172,8 @@ func ensureNodeKubeconfigs(ctx context.Context, client coreclientv1.CoreV1Interf
requiredSecret.Annotations = map[string]string{}
}
requiredSecret.Annotations[annotations.OpenShiftComponent] = "kube-apiserver"
requiredSecret.Annotations[certrotation.CertificateNotBeforeAnnotation] = parsedCert.NotBefore.Format(time.RFC3339)
requiredSecret.Annotations[certrotation.CertificateNotAfterAnnotation] = parsedCert.NotAfter.Format(time.RFC3339)

_, _, err = resourceapply.ApplySecret(ctx, client, recorder, requiredSecret)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package nodekubeconfigcontroller

import (
"context"
"encoding/base64"
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/openshift/api/annotations"
configv1 "github.com/openshift/api/config/v1"
configlistersv1 "github.com/openshift/client-go/config/listers/config/v1"
"github.com/openshift/library-go/pkg/operator/certrotation"
"github.com/openshift/library-go/pkg/operator/events"
corev1 "k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
Expand Down Expand Up @@ -85,7 +88,34 @@ func (l *secretLister) Get(name string) (*corev1.Secret, error) {
return l.client.CoreV1().Secrets(l.namespace).Get(context.Background(), name, metav1.GetOptions{})
}

const privateKey = `
-----BEGIN PRIVATE KEY-----
MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEArvkpSCWaStPfbYr4
cCJyv8pXWnJ4K22emSrYDNcp7Dm6qjtN/lsVNuGDyWyR4cUaJYXkaD2OrZiXDzzk
BZlS3QIDAQABAkA9BZhoGPUec5XQVk8ejGUIjkC4woM2YhyVvmNq1v8/6q6V+uPw
yDEfBMapuLVY+QhyVELXFOCHA5iKxrlFHZThAiEA1XA5mlbHtrJqEZ7yI5m6+Szj
7YVzSkdSgfDZ//heAh8CIQDR3VbN9QmJRIM1yhIkP9BoWSxvXdH6QMXdC2X7Tkwj
gwIgcpbSxjLK/CIjYhx0oXpacIaSRCX+dKV//XVChPNh/T8CIQCSFscXZez2fhfs
eLb6PuXfzbuN5ryFvVM/VXDvaIi96wIgcHjUpONghaoA51XejMAxWanDiwAgRV5H
XNdFkBi4q7o=
-----END PRIVATE KEY-----`
const publicKey = `-----BEGIN CERTIFICATE-----
MIIBfzCCASmgAwIBAgIUEEUHu1PzqJCGQ63vxVokwBxGPYwwDQYJKoZIhvcNAQEL
BQAwFDESMBAGA1UEAwwJbG9jYWxob3N0MB4XDTI0MTEyNjA4NTA0NloXDTM0MTEy
NDA4NTA0NlowFDESMBAGA1UEAwwJbG9jYWxob3N0MFwwDQYJKoZIhvcNAQEBBQAD
SwAwSAJBAK75KUglmkrT322K+HAicr/KV1pyeCttnpkq2AzXKew5uqo7Tf5bFTbh
g8lskeHFGiWF5Gg9jq2Ylw885AWZUt0CAwEAAaNTMFEwHQYDVR0OBBYEFJna5Io+
idLKO73zypGl2itp92JUMB8GA1UdIwQYMBaAFJna5Io+idLKO73zypGl2itp92JU
MA8GA1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQELBQADQQB71tlkWNFDvMRxtz+a
NYMU1thAVfVFciNXPS07tUduFSwVvYORUxx2w+5JfUdKu69hLpBFVPqvHQjPoQgc
vUBI
-----END CERTIFICATE-----`
const certNotBefore = "2024-11-26T08:50:46Z"
const certNotAfter = "2034-11-24T08:50:46Z"

func TestEnsureNodeKubeconfigs(t *testing.T) {
publicKeyBase64 := base64.StdEncoding.EncodeToString([]byte(publicKey))
privateKeyBase64 := base64.StdEncoding.EncodeToString([]byte(privateKey))
tt := []struct {
name string
existingObjects []runtime.Object
Expand All @@ -111,8 +141,8 @@ func TestEnsureNodeKubeconfigs(t *testing.T) {
Name: "node-system-admin-client",
},
Data: map[string][]byte{
"tls.crt": []byte("system:admin certificate"),
"tls.key": []byte("system:admin key"),
"tls.crt": []byte(publicKey),
"tls.key": []byte(privateKey),
},
},
},
Expand Down Expand Up @@ -143,11 +173,13 @@ func TestEnsureNodeKubeconfigs(t *testing.T) {
Namespace: "openshift-kube-apiserver",
Name: "node-kubeconfigs",
Annotations: map[string]string{
annotations.OpenShiftComponent: "kube-apiserver",
annotations.OpenShiftComponent: "kube-apiserver",
certrotation.CertificateNotBeforeAnnotation: certNotBefore,
certrotation.CertificateNotAfterAnnotation: certNotAfter,
},
},
Data: map[string][]byte{
"localhost.kubeconfig": []byte(`apiVersion: v1
"localhost.kubeconfig": []byte(fmt.Sprintf(`apiVersion: v1
kind: Config
clusters:
- cluster:
Expand All @@ -163,10 +195,10 @@ current-context: system:admin
users:
- name: system:admin
user:
client-certificate-data: c3lzdGVtOmFkbWluIGNlcnRpZmljYXRl
client-key-data: c3lzdGVtOmFkbWluIGtleQ==
`),
"localhost-recovery.kubeconfig": []byte(`apiVersion: v1
client-certificate-data: %s
client-key-data: %s
`, publicKeyBase64, privateKeyBase64)),
"localhost-recovery.kubeconfig": []byte(fmt.Sprintf(`apiVersion: v1
kind: Config
clusters:
- cluster:
Expand All @@ -183,10 +215,10 @@ current-context: system:admin
users:
- name: system:admin
user:
client-certificate-data: c3lzdGVtOmFkbWluIGNlcnRpZmljYXRl
client-key-data: c3lzdGVtOmFkbWluIGtleQ==
`),
"lb-ext.kubeconfig": []byte(`apiVersion: v1
client-certificate-data: %s
client-key-data: %s
`, publicKeyBase64, privateKeyBase64)),
"lb-ext.kubeconfig": []byte(fmt.Sprintf(`apiVersion: v1
kind: Config
clusters:
- cluster:
Expand All @@ -202,10 +234,10 @@ current-context: system:admin
users:
- name: system:admin
user:
client-certificate-data: c3lzdGVtOmFkbWluIGNlcnRpZmljYXRl
client-key-data: c3lzdGVtOmFkbWluIGtleQ==
`),
"lb-int.kubeconfig": []byte(`apiVersion: v1
client-certificate-data: %s
client-key-data: %s
`, publicKeyBase64, privateKeyBase64)),
"lb-int.kubeconfig": []byte(fmt.Sprintf(`apiVersion: v1
kind: Config
clusters:
- cluster:
Expand All @@ -221,9 +253,9 @@ current-context: system:admin
users:
- name: system:admin
user:
client-certificate-data: c3lzdGVtOmFkbWluIGNlcnRpZmljYXRl
client-key-data: c3lzdGVtOmFkbWluIGtleQ==
`),
client-certificate-data: %s
client-key-data: %s
`, publicKeyBase64, privateKeyBase64)),
},
},
},
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ github.com/openshift/client-go/security/informers/externalversions/internalinter
github.com/openshift/client-go/security/informers/externalversions/security
github.com/openshift/client-go/security/informers/externalversions/security/v1
github.com/openshift/client-go/security/listers/security/v1
# github.com/openshift/library-go v0.0.0-20241119162247-f466fdd82330
# github.com/openshift/library-go v0.0.0-20241119162247-f466fdd82330 => github.com/vrutkovs/library-go v0.0.0-20241122091102-4d0a3e9ced24
## explicit; go 1.22.0
github.com/openshift/library-go/pkg/apiserver/jsonpatch
github.com/openshift/library-go/pkg/assets
Expand Down Expand Up @@ -1542,3 +1542,4 @@ sigs.k8s.io/structured-merge-diff/v4/value
## explicit; go 1.12
sigs.k8s.io/yaml
sigs.k8s.io/yaml/goyaml.v2
# github.com/openshift/library-go => github.com/vrutkovs/library-go v0.0.0-20241122091102-4d0a3e9ced24