Skip to content

Commit 91e1d5b

Browse files
committed
fix(authx): JSON unmarshalling for Dynamic auth type
Correcting the `UnmarshalJSON` method to properly unmarshal JSON, particularlyaddressing the population of the embedded `Secret` field. This was achieved by using a type alias to avoid recursive calls and rely on default unmarshalling behavior. Signed-off-by: Dwi Siswanto <[email protected]>
1 parent a4859df commit 91e1d5b

File tree

2 files changed

+132
-6
lines changed

2 files changed

+132
-6
lines changed

pkg/authprovider/authx/dynamic.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,15 @@ func (d *Dynamic) GetDomainAndDomainRegex() ([]string, []string) {
5252
}
5353

5454
func (d *Dynamic) UnmarshalJSON(data []byte) error {
55-
if err := json.Unmarshal(data, &d); err != nil {
56-
return err
57-
}
58-
var s Secret
59-
if err := json.Unmarshal(data, &s); err != nil {
55+
// Use an alias type (auxiliary) to avoid a recursive call in this method.
56+
type Alias Dynamic
57+
58+
// If d.Secret was nil, json.Unmarshal will allocate a new Secret object
59+
// and populate it from the top level JSON fields.
60+
if err := json.Unmarshal(data, (*Alias)(d)); err != nil {
6061
return err
6162
}
62-
d.Secret = &s
63+
6364
return nil
6465
}
6566

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package authx
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestDynamicUnmarshalJSON(t *testing.T) {
10+
t.Run("basic-unmarshal", func(t *testing.T) {
11+
data := []byte(`{
12+
"template": "test-template.yaml",
13+
"variables": [
14+
{
15+
"key": "username",
16+
"value": "testuser"
17+
}
18+
],
19+
"secrets": [
20+
{
21+
"type": "BasicAuth",
22+
"domains": ["example.com"],
23+
"username": "user1",
24+
"password": "pass1"
25+
}
26+
],
27+
"type": "BasicAuth",
28+
"domains": ["test.com"],
29+
"username": "testuser",
30+
"password": "testpass"
31+
}`)
32+
33+
var d Dynamic
34+
err := d.UnmarshalJSON(data)
35+
require.NoError(t, err)
36+
37+
// Secret
38+
require.NotNil(t, d.Secret)
39+
require.Equal(t, "BasicAuth", d.Secret.Type)
40+
require.Equal(t, []string{"test.com"}, d.Secret.Domains)
41+
require.Equal(t, "testuser", d.Secret.Username)
42+
require.Equal(t, "testpass", d.Secret.Password)
43+
44+
// Dynamic fields
45+
require.Equal(t, "test-template.yaml", d.TemplatePath)
46+
require.Len(t, d.Variables, 1)
47+
require.Equal(t, "username", d.Variables[0].Key)
48+
require.Equal(t, "testuser", d.Variables[0].Value)
49+
require.Len(t, d.Secrets, 1)
50+
require.Equal(t, "BasicAuth", d.Secrets[0].Type)
51+
require.Equal(t, []string{"example.com"}, d.Secrets[0].Domains)
52+
require.Equal(t, "user1", d.Secrets[0].Username)
53+
require.Equal(t, "pass1", d.Secrets[0].Password)
54+
})
55+
56+
t.Run("complex-unmarshal", func(t *testing.T) {
57+
data := []byte(`{
58+
"template": "test-template.yaml",
59+
"variables": [
60+
{
61+
"key": "token",
62+
"value": "Bearer xyz"
63+
}
64+
],
65+
"secrets": [
66+
{
67+
"type": "CookiesAuth",
68+
"domains": ["example.com"],
69+
"cookies": [
70+
{
71+
"key": "session",
72+
"value": "abc123"
73+
}
74+
]
75+
}
76+
],
77+
"type": "HeadersAuth",
78+
"domains": ["api.test.com"],
79+
"headers": [
80+
{
81+
"key": "X-API-Key",
82+
"value": "secret-key"
83+
}
84+
]
85+
}`)
86+
87+
var d Dynamic
88+
err := d.UnmarshalJSON(data)
89+
require.NoError(t, err)
90+
91+
// Secret
92+
require.NotNil(t, d.Secret)
93+
require.Equal(t, "HeadersAuth", d.Secret.Type)
94+
require.Equal(t, []string{"api.test.com"}, d.Secret.Domains)
95+
require.Len(t, d.Secret.Headers, 1)
96+
require.Equal(t, "X-API-Key", d.Secret.Headers[0].Key)
97+
require.Equal(t, "secret-key", d.Secret.Headers[0].Value)
98+
99+
// Dynamic fields
100+
require.Equal(t, "test-template.yaml", d.TemplatePath)
101+
require.Len(t, d.Variables, 1)
102+
require.Equal(t, "token", d.Variables[0].Key)
103+
require.Equal(t, "Bearer xyz", d.Variables[0].Value)
104+
require.Len(t, d.Secrets, 1)
105+
require.Equal(t, "CookiesAuth", d.Secrets[0].Type)
106+
require.Equal(t, []string{"example.com"}, d.Secrets[0].Domains)
107+
require.Len(t, d.Secrets[0].Cookies, 1)
108+
require.Equal(t, "session", d.Secrets[0].Cookies[0].Key)
109+
require.Equal(t, "abc123", d.Secrets[0].Cookies[0].Value)
110+
})
111+
112+
t.Run("invalid-json", func(t *testing.T) {
113+
data := []byte(`{invalid json}`)
114+
var d Dynamic
115+
err := d.UnmarshalJSON(data)
116+
require.Error(t, err)
117+
})
118+
119+
t.Run("empty-json", func(t *testing.T) {
120+
data := []byte(`{}`)
121+
var d Dynamic
122+
err := d.UnmarshalJSON(data)
123+
require.NoError(t, err)
124+
})
125+
}

0 commit comments

Comments
 (0)