-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
x-pack/filebeat/input/httpjson: add support for pem encoded keys
This adds a new Okta auth field, jwk_pem, that allows users to specify a PEM-encoded private key for authentication. Also refactor the JSON-based code to simplify and add minimal testing.
- Loading branch information
Showing
6 changed files
with
191 additions
and
57 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,88 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package httpjson | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/lestrrat-go/jwx/v2/jwt" | ||
"golang.org/x/oauth2" | ||
) | ||
|
||
func TestGenerateOktaJWT(t *testing.T) { | ||
// jwt is a JWT obtained from the Okta integration. | ||
const jwtText = `{ "d": "Cmhokw2MnZfX6da36nnsnQ7IPX9vE6se8_D1NgyL9j9rarYpexhlp45hswcAIFNgWA03NV848Gc0e84AW6wMbyD2E8LPI0Bd8lhdmzRE6L4or2Rxqqjk2Pr2aqGnqs4A0uTijAA7MfPF1zFFdR3EOVx499fEeTiM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RSA", "e": "AQAB", "kid": "koeFQjkyiav_3Qwr3aRinCqCD2LaEHOjFnje7XlkbdI", "n": "xloTY8bAuI5AEo8JursCd7w0LmELCae7JOFaVo9njGrG8tRNqgIdjPyoGY_ABwKkmjcCMLGMA29llFDbry8rB4LTWai-h_jX4_uUUnl52mLX-lO6merL5HEPZF438Ql9Hrxs5yGzT8n865-E_3uwYSBrhTjvlZJeXYUeVHfKo8pJSSsw3RZEjBW4Tt0eFmCZnFErtTyk3oUPaYVP-8YLLAenhUDV4Lm1dC4dxqUj0Oh6XrWgIb-eYHGolMY9g9xbgyd4ir39RodA_1DOjzHWpNfCM-J5ZOtfpuKCAe5__u7L8FT0m56XOxcDoVVsz1J1VNrACWAGbhDWNjyHfL5E2Q" }` | ||
cnf := &oauth2.Config{ | ||
ClientID: "0oaajljpeokFZLyKU5d7", | ||
Scopes: []string{"okta.logs.read"}, | ||
} | ||
got, err := generateOktaJWT([]byte(jwtText), cnf) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
tok, err := jwt.Parse([]byte(got), jwt.WithVerify(false)) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
if tok.Issuer() != cnf.ClientID { | ||
t.Errorf("unexpected issuer: got:%s want:%s", tok.Issuer(), cnf.ClientID) | ||
} | ||
if tok.Subject() != cnf.ClientID { | ||
t.Errorf("unexpected subject: got:%s want:%s", tok.Subject(), cnf.ClientID) | ||
} | ||
} | ||
|
||
func TestGenerateOktaJWTPEM(t *testing.T) { | ||
// jwtText is generated by https://mkjwk.org/ using the instructions at | ||
// https://developer.okta.com/docs/guides/dpop/nonoktaresourceserver/main/#create-the-json-web-token | ||
const jwtText = ` | ||
-----BEGIN PRIVATE KEY----- | ||
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCOuef3HMRhohVT | ||
5kSoAJgV+atpDjkwTwkOq+ImnbBlv75GaApG90w8VpjXjhqN/1KJmwfyrKiqui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Y6Kr2Clfg+26iAPFU | ||
MiWqUUInGGBuamqm5g6jI5sM28gQWeTsvC4IRXyes1Eq+uCHSQax15J/Y+3SSgNT | ||
9kjUYYkvWMwoRcPobRYWSZze7XkP2L8hFJ7EGvAaZGqAWxzgliS9HtnhAoGAONZ/ | ||
UqMw7Zoac/Ga5mhSwrj7ZvXxP6Gqzjofj+eKqrOlB5yMhIX6LJATfH6iq7cAMxxm | ||
Fu/G4Ll4oB3o5wACtI3wldV/MDtYfJBtoCTjBqPsfNOsZ9hMvBATlsc2qwzKjsAb | ||
tFhzTevoOYpSD75EcSS/G8Ec2iN9bagatBnpl00CgYBVqAOFZelNfP7dj//lpk8y | ||
EUAw7ABOq0S9wkpFWTXIVPoBQUipm3iAUqGNPmvr/9ShdZC9xeu5AwKram4caMWJ | ||
ExRhcDP1hFM6CdmSkIYEgBKvN9N0O4Lx1ba34gk74Hm65KXxokjJHOC0plO7c7ok | ||
LNV/bIgMHOMoxiGrwyjAhg== | ||
-----END PRIVATE KEY----- | ||
` | ||
cnf := &oauth2.Config{ | ||
ClientID: "0oaajljpeokFZLyKU5d7", | ||
Scopes: []string{"okta.logs.read"}, | ||
} | ||
got, err := generateOktaJWTPEM(jwtText, cnf) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
tok, err := jwt.Parse([]byte(got), jwt.WithVerify(false)) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
if tok.Issuer() != cnf.ClientID { | ||
t.Errorf("unexpected issuer: got:%s want:%s", tok.Issuer(), cnf.ClientID) | ||
} | ||
if tok.Subject() != cnf.ClientID { | ||
t.Errorf("unexpected subject: got:%s want:%s", tok.Subject(), cnf.ClientID) | ||
} | ||
} |
This file contains 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