-
Notifications
You must be signed in to change notification settings - Fork 244
OCPBUGS-33013: certsyncpod: Swap secret/cm directories atomically #2009
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
tchap
wants to merge
2
commits into
openshift:master
Choose a base branch
from
tchap:atomic-certsync
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
pkg/operator/staticpod/certsyncpod/certsync_controller_linux_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
//go:build linux | ||
|
||
package certsyncpod | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"crypto/ecdsa" | ||
"crypto/elliptic" | ||
"crypto/rand" | ||
"crypto/x509" | ||
"crypto/x509/pkix" | ||
"encoding/pem" | ||
"math/big" | ||
"os" | ||
"path/filepath" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
"k8s.io/apiserver/pkg/server/dynamiccertificates" | ||
|
||
"github.com/openshift/library-go/pkg/operator/staticpod/internal/atomicdir" | ||
) | ||
|
||
// TestDynamicCertificates makes sure the receiving side of certificate synchronization works as expected. | ||
// It reads and watches the certificates being synchronized in the same way as e.g. kube-apiserver, | ||
// the very same libraries are being used. | ||
func TestDynamicCertificates(t *testing.T) { | ||
const typeName = "secret" | ||
om := metav1.ObjectMeta{ | ||
Namespace: "openshift-kube-apiserver", | ||
Name: "s1", | ||
} | ||
|
||
// Generate all necessary keypairs. | ||
tlsCert, tlsKey := generateKeypair(t) | ||
tlsCertUpdated, tlsKeyUpdated := generateKeypair(t) | ||
|
||
// Write the keypair into a secret directory. | ||
secretDir := filepath.Join(t.TempDir(), "secrets", om.Name) | ||
certFile := filepath.Join(secretDir, "tls.crt") | ||
keyFile := filepath.Join(secretDir, "tls.key") | ||
|
||
if err := os.MkdirAll(secretDir, 0700); err != nil { | ||
t.Fatalf("Failed to create secret directory %q: %v", secretDir, err) | ||
} | ||
if err := os.WriteFile(certFile, tlsCert, 0600); err != nil { | ||
t.Fatalf("Failed to write TLS certificate into %q: %v", certFile, err) | ||
} | ||
if err := os.WriteFile(keyFile, tlsKey, 0600); err != nil { | ||
t.Fatalf("Failed to write TLS key into %q: %v", keyFile, err) | ||
} | ||
|
||
// Start the watcher. | ||
// This reads the keypair synchronously so the initial state is loaded here. | ||
dc, err := dynamiccertificates.NewDynamicServingContentFromFiles("localhost TLS", certFile, keyFile) | ||
if err != nil { | ||
t.Fatalf("Failed to init dynamic certificate: %v", err) | ||
} | ||
|
||
// Check the initial keypair is loaded. | ||
cert, key := dc.CurrentCertKeyContent() | ||
if !bytes.Equal(cert, tlsCert) || !bytes.Equal(key, tlsKey) { | ||
t.Fatal("Unexpected initial keypair loaded") | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
var wg sync.WaitGroup | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
dc.Run(ctx, 1) | ||
}() | ||
defer wg.Wait() | ||
defer cancel() | ||
|
||
// Poll until update detected. | ||
files := map[string][]byte{ | ||
"tls.crt": tlsCertUpdated, | ||
"tls.key": tlsKeyUpdated, | ||
} | ||
err = wait.PollUntilContextCancel(ctx, 250*time.Millisecond, true, func(ctx context.Context) (bool, error) { | ||
// Replace the secret directory. | ||
if err := atomicdir.Sync(secretDir, files, 0600); err != nil { | ||
t.Errorf("Failed to write files: %v", err) | ||
return false, err | ||
} | ||
|
||
// Check the loaded content matches. | ||
// This is most probably updated based on write in a previous Poll invocation. | ||
cert, key := dc.CurrentCertKeyContent() | ||
return bytes.Equal(cert, tlsCertUpdated) && bytes.Equal(key, tlsKeyUpdated), nil | ||
}) | ||
if err != nil { | ||
t.Fatalf("Failed to wait for dynamic certificate: %v", err) | ||
} | ||
} | ||
|
||
// generateKeypair returns (cert, key). | ||
func generateKeypair(t *testing.T) ([]byte, []byte) { | ||
t.Helper() | ||
|
||
privateKey, err := ecdsa.GenerateKey(elliptic.P224(), rand.Reader) | ||
if err != nil { | ||
t.Fatalf("Failed to generate TLS key: %v", err) | ||
} | ||
|
||
notBefore := time.Now() | ||
notAfter := notBefore.Add(1 * time.Hour) | ||
|
||
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128) | ||
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit) | ||
if err != nil { | ||
t.Fatalf("Failed to generate serial number for TLS keypair: %v", err) | ||
} | ||
|
||
template := x509.Certificate{ | ||
SerialNumber: serialNumber, | ||
Subject: pkix.Name{ | ||
Organization: []string{"Example Org"}, | ||
}, | ||
NotBefore: notBefore, | ||
NotAfter: notAfter, | ||
KeyUsage: x509.KeyUsageDigitalSignature, | ||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, | ||
BasicConstraintsValid: true, | ||
DNSNames: []string{"example.com"}, | ||
} | ||
|
||
publicKeyBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &privateKey.PublicKey, privateKey) | ||
if err != nil { | ||
t.Fatalf("Failed to create TLS certificate: %v", err) | ||
} | ||
|
||
var certOut bytes.Buffer | ||
if err := pem.Encode(&certOut, &pem.Block{Type: "CERTIFICATE", Bytes: publicKeyBytes}); err != nil { | ||
t.Fatalf("Failed to write certificate PEM: %v", err) | ||
} | ||
|
||
privateKeyBytes, err := x509.MarshalPKCS8PrivateKey(privateKey) | ||
if err != nil { | ||
t.Fatalf("Unable to marshal private key: %v", err) | ||
} | ||
|
||
var keyOut bytes.Buffer | ||
if err := pem.Encode(&keyOut, &pem.Block{Type: "PRIVATE KEY", Bytes: privateKeyBytes}); err != nil { | ||
t.Fatalf("Failed to write certificate PEM: %v", err) | ||
} | ||
|
||
return certOut.Bytes(), keyOut.Bytes() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.