Skip to content

Commit 7d55062

Browse files
committed
fix: support more key formats
1 parent fd1d6f8 commit 7d55062

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

docs/usage/stateful-applications.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Where:
8181
* `$NAMESPACE_NAME` is the name of the namespace where the *PVC* lives in.
8282
* `$TARGET_NODE_NAME` is the name of the node where the *PVC* will be moved to.
8383

84-
Under the hood, the migration process leverages the Liqo cross-cluster network fabric and the [Restic project](https://restic.net/) to back up the original data in a temporary repository, and then restore it in a brand-new *PVC* forced to be created in the target cluster.
84+
Under the hood, the migration process leverages the Liqo cross-cluster network fabric and the [Restic project](https://github.com/restic/restic) to back up the original data in a temporary repository, and then restore it in a brand-new *PVC* forced to be created in the target cluster.
8585

8686
```{warning}
8787
*Liqo* and *liqoctl* **are not** backup tools. Make sure to properly back up important data before starting the migration process.

pkg/liqo-controller-manager/authentication/csr.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,24 @@ func checkCSR(csr, publicKey []byte, checkPublicKey bool, commonName, organizati
170170
return fmt.Errorf("invalid public key")
171171
}
172172

173-
// Marshal CSR public key to PKIX DER and compare with provided PKIX public key bytes
173+
// Parse the provided public key (supports PEM, PKIX DER, or raw Ed25519)
174+
parsedPubKey, err := parsePublicKey(publicKey)
175+
if err != nil {
176+
return fmt.Errorf("failed to parse provided public key: %w", err)
177+
}
178+
179+
// Marshal both keys to PKIX DER for comparison
174180
csrPubDER, err := x509.MarshalPKIXPublicKey(x509Csr.PublicKey)
175181
if err != nil {
176182
return fmt.Errorf("failed to marshal CSR public key: %w", err)
177183
}
178184

179-
if !bytes.Equal(csrPubDER, publicKey) {
185+
parsedPubDER, err := x509.MarshalPKIXPublicKey(parsedPubKey)
186+
if err != nil {
187+
return fmt.Errorf("failed to marshal provided public key: %w", err)
188+
}
189+
190+
if !bytes.Equal(csrPubDER, parsedPubDER) {
180191
return fmt.Errorf("invalid public key")
181192
}
182193
}

pkg/liqo-controller-manager/authentication/keys.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,37 @@ func SignNonce(priv crypto.PrivateKey, nonce []byte) ([]byte, error) {
109109
}
110110
}
111111

112-
// VerifyNonce verifies the signature of a nonce using the PKIX-encoded public key bytes of the cluster.
112+
// parsePublicKey parses a public key from various formats:
113+
// - PKIX DER-encoded bytes
114+
// - PEM-encoded public key
115+
// - Raw Ed25519 public key bytes (32 bytes)
116+
func parsePublicKey(pubKeyBytes []byte) (crypto.PublicKey, error) {
117+
// Try PEM decoding first
118+
if block, _ := pem.Decode(pubKeyBytes); block != nil {
119+
pubKeyBytes = block.Bytes
120+
}
121+
122+
// Try PKIX parsing
123+
if pub, err := x509.ParsePKIXPublicKey(pubKeyBytes); err == nil {
124+
return pub, nil
125+
}
126+
127+
// Try raw Ed25519 public key (32 bytes)
128+
if len(pubKeyBytes) == ed25519.PublicKeySize {
129+
return ed25519.PublicKey(pubKeyBytes), nil
130+
}
131+
132+
return nil, fmt.Errorf("unable to parse public key: unrecognized format (length: %d)", len(pubKeyBytes))
133+
}
134+
135+
// VerifyNonce verifies the signature of a nonce using the public key bytes of the cluster.
113136
// The public key can be Ed25519, RSA, or ECDSA.
114-
func VerifyNonce(pubKeyPKIX, nonce, signature []byte) (bool, error) {
115-
pub, err := x509.ParsePKIXPublicKey(pubKeyPKIX)
137+
// It accepts the public key in multiple formats:
138+
// - PKIX DER-encoded bytes
139+
// - PEM-encoded public key (will be decoded first)
140+
// - Raw Ed25519 public key bytes (32 bytes)
141+
func VerifyNonce(pubKeyBytes, nonce, signature []byte) (bool, error) {
142+
pub, err := parsePublicKey(pubKeyBytes)
116143
if err != nil {
117144
return false, fmt.Errorf("failed to parse public key: %w", err)
118145
}

0 commit comments

Comments
 (0)