Skip to content

Commit

Permalink
x-pack/filebeat/input/{cel,httpjson}: fix PEM key validation
Browse files Browse the repository at this point in the history
Previously the validation was attempting to parse the PEM text as a key
and was also attempting to parse the data as the wrong kind of key.
  • Loading branch information
efd6 committed Mar 19, 2024
1 parent 51974d9 commit 97b2444
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ fields added to events containing the Beats version. {pull}37553[37553]
- Fix duplicated addition of regexp extension in CEL input. {pull}38181[38181]
- Fix the incorrect values generated by the uri_parts processor. {pull}38216[38216]
- Fix HTTPJSON handling of empty object bodies in POST requests. {issue}33961[33961] {pull}38290[38290]
- Fix PEM key validation for CEL and HTTPJSON inputs. {pull}[]

*Heartbeat*

Expand Down
11 changes: 10 additions & 1 deletion x-pack/filebeat/input/cel/config_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
package cel

import (
"bytes"
"context"
"crypto/x509"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"io/fs"
Expand Down Expand Up @@ -341,7 +343,14 @@ func (o *oAuth2Config) validateOktaProvider() error {
}
// jwk_pem
if o.OktaJWKPEM != "" {
_, err := x509.ParsePKCS1PrivateKey([]byte(o.OktaJWKPEM))
blk, rest := pem.Decode([]byte(o.OktaJWKPEM))
if rest := bytes.TrimSpace(rest); len(rest) != 0 {
return fmt.Errorf("PEM text has trailing data: %s", rest)
}
_, err := x509.ParsePKCS8PrivateKey(blk.Bytes)
if err != nil {
return fmt.Errorf("okta validation error: %w", err)
}
return err
}
// jwk_file
Expand Down
13 changes: 11 additions & 2 deletions x-pack/filebeat/input/httpjson/config_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
package httpjson

import (
"bytes"
"context"
"crypto/x509"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"io/fs"
Expand Down Expand Up @@ -309,8 +311,15 @@ func (o *oAuth2Config) validateOktaProvider() error {
}
// jwk_pem
if o.OktaJWKPEM != "" {
_, err := x509.ParsePKCS1PrivateKey([]byte(o.OktaJWKPEM))
return err
blk, rest := pem.Decode([]byte(o.OktaJWKPEM))
if rest := bytes.TrimSpace(rest); len(rest) != 0 {
return fmt.Errorf("PEM text has trailing data: %s", rest)
}
_, err := x509.ParsePKCS8PrivateKey(blk.Bytes)
if err != nil {
return fmt.Errorf("okta validation error: %w", err)
}
return nil
}
// jwk_file
if o.OktaJWKFile != "" {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/filebeat/input/httpjson/config_okta_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func signJWT(cnf *oauth2.Config, key any) (string, error) {
Expiration(now.Add(time.Hour)).
Build()
if err != nil {
return "", err
return "", fmt.Errorf("failed to create token: %w", err)
}
signedToken, err := jwt.Sign(tok, jwt.WithKey(jwa.RS256, key))
if err != nil {
Expand Down

0 comments on commit 97b2444

Please sign in to comment.