| 1 |
package oauth2 //nolint:testpackage |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/http" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config" |
| 8 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/idtoken" |
| 9 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/oauth2/types" |
| 10 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/state" |
| 11 |
"github.com/stretchr/testify/require" |
| 12 |
) |
| 13 |
|
| 14 |
func BenchmarkGetAuthorizeParams(b *testing.B) { |
| 15 |
for _, tc := range []struct { |
| 16 |
name string |
| 17 |
input string |
| 18 |
}{ |
| 19 |
{ |
| 20 |
name: "single", |
| 21 |
input: "prompt=login", |
| 22 |
}, |
| 23 |
{ |
| 24 |
name: "multiple", |
| 25 |
input: "prompt=login&audience=vpn&kc_idp_hint=google", |
| 26 |
}, |
| 27 |
} { |
| 28 |
b.Run(tc.name, func(b *testing.B) { |
| 29 |
var params any |
| 30 |
|
| 31 |
b.ReportAllocs() |
| 32 |
b.ResetTimer() |
| 33 |
|
| 34 |
for b.Loop() { |
| 35 |
parsedParams, err := getAuthorizeParams(tc.input) |
| 36 |
if err != nil { |
| 37 |
b.Fatal(err) |
| 38 |
} |
| 39 |
|
| 40 |
params = parsedParams |
| 41 |
} |
| 42 |
|
| 43 |
_ = params |
| 44 |
}) |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
func BenchmarkCheckClientIPAddr(b *testing.B) { |
| 49 |
session := state.State{IPAddr: "127.0.0.1"} |
| 50 |
|
| 51 |
b.Run("direct", func(b *testing.B) { |
| 52 |
conf := config.Defaults |
| 53 |
req := &http.Request{RemoteAddr: "127.0.0.1:12345", Header: make(http.Header)} |
| 54 |
|
| 55 |
b.ReportAllocs() |
| 56 |
b.ResetTimer() |
| 57 |
|
| 58 |
for b.Loop() { |
| 59 |
if err := checkClientIPAddr(req, &conf, session); err != nil { |
| 60 |
b.Fatal(err) |
| 61 |
} |
| 62 |
} |
| 63 |
}) |
| 64 |
|
| 65 |
b.Run("proxy-headers", func(b *testing.B) { |
| 66 |
conf := config.Defaults |
| 67 |
conf.HTTP.EnableProxyHeaders = true |
| 68 |
conf.HTTP.TrustedProxies = append(conf.HTTP.TrustedProxies, "10.0.0.1/32") |
| 69 |
|
| 70 |
req := &http.Request{RemoteAddr: "10.0.0.1:12345", Header: http.Header{"X-Forwarded-For": []string{"127.0.0.1, 10.0.0.1"}}} |
| 71 |
|
| 72 |
b.ReportAllocs() |
| 73 |
b.ResetTimer() |
| 74 |
|
| 75 |
for b.Loop() { |
| 76 |
if err := checkClientIPAddr(req, &conf, session); err != nil { |
| 77 |
b.Fatal(err) |
| 78 |
} |
| 79 |
} |
| 80 |
}) |
| 81 |
} |
| 82 |
|
| 83 |
func BenchmarkCheckIdentityCEL(b *testing.B) { |
| 84 |
for _, tc := range []struct { |
| 85 |
name string |
| 86 |
expression string |
| 87 |
state state.State |
| 88 |
token *idtoken.IDToken |
| 89 |
}{ |
| 90 |
{ |
| 91 |
name: "equality", |
| 92 |
expression: "openvpn.commonName == user.username", |
| 93 |
state: state.State{ |
| 94 |
Client: state.ClientIdentifier{CommonName: "test-client"}, |
| 95 |
IPAddr: "127.0.0.1", |
| 96 |
}, |
| 97 |
token: &idtoken.IDToken{ |
| 98 |
IDTokenClaims: &idtoken.Claims{Claims: map[string]any{"preferred_username": "test-client"}}, |
| 99 |
}, |
| 100 |
}, |
| 101 |
{ |
| 102 |
name: "lower-ascii", |
| 103 |
expression: "openvpn.commonName.lowerAscii() == user.username.lowerAscii()", |
| 104 |
state: state.State{ |
| 105 |
Client: state.ClientIdentifier{CommonName: "Test-Client"}, |
| 106 |
IPAddr: "127.0.0.1", |
| 107 |
}, |
| 108 |
token: &idtoken.IDToken{ |
| 109 |
IDTokenClaims: &idtoken.Claims{Claims: map[string]any{"preferred_username": "test-client"}}, |
| 110 |
}, |
| 111 |
}, |
| 112 |
} { |
| 113 |
b.Run(tc.name, func(b *testing.B) { |
| 114 |
conf := config.Defaults |
| 115 |
conf.OAuth2.Validate.Expression = tc.expression |
| 116 |
|
| 117 |
client := &Client{conf: &conf} |
| 118 |
require.NoError(b, client.initializeCELValidation()) |
| 119 |
|
| 120 |
b.ReportAllocs() |
| 121 |
b.ResetTimer() |
| 122 |
|
| 123 |
for b.Loop() { |
| 124 |
if err := client.CheckIdentityCEL( |
| 125 |
CELAuthModeInteractive, |
| 126 |
tc.state, |
| 127 |
tc.token, |
| 128 |
types.UserInfo{Username: "test-client"}, |
| 129 |
); err != nil { |
| 130 |
b.Fatal(err) |
| 131 |
} |
| 132 |
} |
| 133 |
}) |
| 134 |
} |
| 135 |
} |