Skip to content

Commit 3433ba2

Browse files
authored
Fixes unsupported AST kind *ast.InterfaceType on the custon AuthProperties type (#143)
Signed-off-by: Spolti <filippespolti@gmail.com>
1 parent 9c2ef9c commit 3433ba2

File tree

5 files changed

+101
-108
lines changed

5 files changed

+101
-108
lines changed

hack/deepcopy-gen.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ if [ "${GENS}" = "all" ] || grep -qw "deepcopy" <<<"${GENS}"; then
4141
echo "Generating deepcopy funcs"
4242
export GO111MODULE=on
4343
# for debug purposes, increase the log level by updating the -v flag to higher numbers, e.g. -v 4
44-
"${GOPATH}/bin/deepcopy-gen" -v 2 \
44+
"${GOPATH}/bin/deepcopy-gen" -v 1 \
4545
--input-dirs ./model -O zz_generated.deepcopy \
4646
--go-header-file "${SCRIPT_ROOT}/hack/boilerplate.txt" \
4747
"$@"

model/auth.go

Lines changed: 69 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package model
1717
import (
1818
"encoding/json"
1919
"fmt"
20+
"strings"
2021
)
2122

2223
// AuthType ...
@@ -43,18 +44,6 @@ const (
4344
GrantTypeTokenExchange GrantType = "tokenExchange"
4445
)
4546

46-
func getAuthProperties(authType AuthType) (AuthProperties, bool) {
47-
switch authType {
48-
case AuthTypeBasic:
49-
return &BasicAuthProperties{}, true
50-
case AuthTypeBearer:
51-
return &BearerAuthProperties{}, true
52-
case AuthTypeOAuth2:
53-
return &OAuth2AuthProperties{}, true
54-
}
55-
return nil, false
56-
}
57-
5847
// Auth ...
5948
type Auth struct {
6049
// Name Unique auth definition name
@@ -90,74 +79,73 @@ func (a *Auth) UnmarshalJSON(data []byte) error {
9079
if len(a.Scheme) == 0 {
9180
a.Scheme = AuthTypeBasic
9281
}
93-
authProperties, ok := getAuthProperties(a.Scheme)
94-
if !ok {
95-
return fmt.Errorf("authentication scheme %s not supported", a.Scheme)
96-
}
9782

98-
// we take the type we want to unmarshal based on the scheme
99-
if err := unmarshalKey("properties", auth, authProperties); err != nil {
100-
return err
101-
}
83+
switch a.Scheme {
84+
case AuthTypeBasic:
85+
authProperties := &BasicAuthProperties{}
10286

103-
a.Properties = authProperties
104-
return nil
105-
}
87+
if err := unmarshalKey("properties", auth, authProperties); err != nil {
88+
return err
89+
}
90+
a.Properties.Basic = authProperties
10691

107-
// AuthProperties ...
108-
type AuthProperties interface {
109-
// GetMetadata ...
110-
GetMetadata() *Metadata
111-
// GetSecret ...
112-
GetSecret() string
113-
// DeepCopyAuthProperties fixes in.Properties.DeepCopyAuthProperties undefined (type AuthProperties has no
114-
// field or method DeepCopyAuthProperties)
115-
DeepCopyAuthProperties() AuthProperties
116-
}
92+
return nil
11793

118-
// BaseAuthProperties ...
119-
type BaseAuthProperties struct {
120-
Common `json:",inline"`
121-
// Secret Expression referencing a workflow secret that contains all needed auth info
122-
Secret string `json:"secret,omitempty"`
123-
}
94+
case AuthTypeBearer:
95+
authProperties := &BearerAuthProperties{}
96+
if err := unmarshalKey("properties", auth, authProperties); err != nil {
97+
return err
98+
}
99+
a.Properties.Bearer = authProperties
100+
return nil
124101

125-
// UnmarshalJSON ...
126-
func (b *BaseAuthProperties) UnmarshalJSON(data []byte) error {
127-
properties := make(map[string]json.RawMessage)
128-
if err := json.Unmarshal(data, &properties); err != nil {
129-
b.Secret, err = unmarshalString(data)
130-
if err != nil {
102+
case AuthTypeOAuth2:
103+
authProperties := &OAuth2AuthProperties{}
104+
if err := unmarshalKey("properties", auth, authProperties); err != nil {
131105
return err
132106
}
107+
a.Properties.OAuth2 = authProperties
133108
return nil
134-
}
135-
if err := unmarshalKey("metadata", properties, &b.Metadata); err != nil {
136-
return err
137-
}
138-
if err := unmarshalKey("secret", properties, &b.Secret); err != nil {
139-
return err
140-
}
141-
return nil
142-
}
143109

144-
// GetMetadata ...
145-
func (b *BaseAuthProperties) GetMetadata() *Metadata {
146-
return &b.Metadata
110+
default:
111+
return fmt.Errorf("failed to parse auth properties")
112+
}
147113
}
148114

149-
// GetSecret ...
150-
func (b *BaseAuthProperties) GetSecret() string {
151-
return b.Secret
115+
func (a *Auth) MarshalJSON() ([]byte, error) {
116+
custom, err := json.Marshal(&struct {
117+
Name string `json:"name" validate:"required"`
118+
Scheme AuthType `json:"scheme,omitempty" validate:"omitempty,min=1"`
119+
Properties AuthProperties `json:"properties" validate:"required"`
120+
}{
121+
Name: a.Name,
122+
Scheme: a.Scheme,
123+
Properties: a.Properties,
124+
})
125+
if err != nil {
126+
fmt.Println(err)
127+
}
128+
st := strings.Replace(string(custom), "null,", "", 1)
129+
st = strings.Replace(st, "\"Basic\":", "", 1)
130+
st = strings.Replace(st, "\"Oauth2\":", "", 1)
131+
st = strings.Replace(st, "\"Bearer\":", "", 1)
132+
st = strings.Replace(st, "{{", "{", 1)
133+
st = strings.TrimSuffix(st, "}")
134+
return []byte(st), nil
152135
}
153136

154-
func (b *BasicAuthProperties) DeepCopyAuthProperties() AuthProperties {
155-
return b
137+
// AuthProperties ...
138+
type AuthProperties struct {
139+
Basic *BasicAuthProperties `json:",omitempty"`
140+
Bearer *BearerAuthProperties `json:",omitempty"`
141+
OAuth2 *OAuth2AuthProperties `json:",omitempty"`
156142
}
157143

158144
// BasicAuthProperties Basic Auth Info
159145
type BasicAuthProperties struct {
160-
BaseAuthProperties `json:",inline"`
146+
Common `json:",inline"`
147+
// Secret Expression referencing a workflow secret that contains all needed auth info
148+
Secret string `json:"secret,omitempty"`
161149
// Username String or a workflow expression. Contains the username
162150
Username string `json:"username" validate:"required"`
163151
// Password String or a workflow expression. Contains the user password
@@ -168,11 +156,7 @@ type BasicAuthProperties struct {
168156
func (b *BasicAuthProperties) UnmarshalJSON(data []byte) error {
169157
properties := make(map[string]json.RawMessage)
170158
if err := json.Unmarshal(data, &properties); err != nil {
171-
err = json.Unmarshal(data, &b.BaseAuthProperties)
172-
if err != nil {
173-
return err
174-
}
175-
return nil
159+
return err
176160
}
177161
if err := unmarshalKey("username", properties, &b.Username); err != nil {
178162
return err
@@ -183,42 +167,44 @@ func (b *BasicAuthProperties) UnmarshalJSON(data []byte) error {
183167
if err := unmarshalKey("metadata", properties, &b.Metadata); err != nil {
184168
return err
185169
}
170+
if err := unmarshalKey("secret", properties, &b.Secret); err != nil {
171+
return err
172+
}
186173
return nil
187174
}
188175

189176
// BearerAuthProperties Bearer auth information
190177
type BearerAuthProperties struct {
191-
BaseAuthProperties `json:",inline"`
178+
Common `json:",inline"`
179+
// Secret Expression referencing a workflow secret that contains all needed auth info
180+
Secret string `json:"secret,omitempty"`
192181
// Token String or a workflow expression. Contains the token
193182
Token string `json:"token" validate:"required"`
194183
}
195184

196-
func (b *BearerAuthProperties) DeepCopyAuthProperties() AuthProperties {
197-
return b
198-
}
199-
200185
// UnmarshalJSON ...
201186
func (b *BearerAuthProperties) UnmarshalJSON(data []byte) error {
202187
properties := make(map[string]json.RawMessage)
203188
if err := json.Unmarshal(data, &properties); err != nil {
204-
err = json.Unmarshal(data, &b.BaseAuthProperties)
205-
if err != nil {
206-
return err
207-
}
208-
return nil
189+
return err
209190
}
210191
if err := unmarshalKey("token", properties, &b.Token); err != nil {
211192
return err
212193
}
213194
if err := unmarshalKey("metadata", properties, &b.Metadata); err != nil {
214195
return err
215196
}
197+
if err := unmarshalKey("secret", properties, &b.Secret); err != nil {
198+
return err
199+
}
216200
return nil
217201
}
218202

219203
// OAuth2AuthProperties OAuth2 information
220204
type OAuth2AuthProperties struct {
221-
BaseAuthProperties `json:",inline"`
205+
Common `json:",inline"`
206+
// Secret Expression referencing a workflow secret that contains all needed auth info
207+
Secret string `json:"secret,omitempty"`
222208
// Authority String or a workflow expression. Contains the authority information
223209
Authority string `json:"authority,omitempty" validate:"omitempty,min=1"`
224210
// GrantType Defines the grant type
@@ -243,21 +229,13 @@ type OAuth2AuthProperties struct {
243229
RequestedIssuer string `json:"requestedIssuer,omitempty" validate:"omitempty,min=1"`
244230
}
245231

246-
func (b *OAuth2AuthProperties) DeepCopyAuthProperties() AuthProperties {
247-
return b
248-
}
249-
250232
// TODO: use reflection to unmarshal the keys and think on a generic approach to handle them
251233

252234
// UnmarshalJSON ...
253235
func (b *OAuth2AuthProperties) UnmarshalJSON(data []byte) error {
254236
properties := make(map[string]json.RawMessage)
255237
if err := json.Unmarshal(data, &properties); err != nil {
256-
err = json.Unmarshal(data, &b.BaseAuthProperties)
257-
if err != nil {
258-
return err
259-
}
260-
return nil
238+
return err
261239
}
262240
if err := unmarshalKey("authority", properties, &b.Authority); err != nil {
263241
return err
@@ -295,5 +273,8 @@ func (b *OAuth2AuthProperties) UnmarshalJSON(data []byte) error {
295273
if err := unmarshalKey("metadata", properties, &b.Metadata); err != nil {
296274
return err
297275
}
276+
if err := unmarshalKey("secret", properties, &b.Secret); err != nil {
277+
return err
278+
}
298279
return nil
299280
}

model/auth_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ func TestUnmarshalJSONMultipleAuthProperties(t *testing.T) {
4646
err = json.Unmarshal([]byte(a2JSON), &a2)
4747
assert.NoError(t, err)
4848

49-
a1Properties := a1.Properties.(*BearerAuthProperties)
50-
a2Properties := a2.Properties.(*BearerAuthProperties)
49+
a1Properties := a1.Properties.Bearer
50+
a2Properties := a2.Properties.Bearer
5151

5252
assert.Equal(t, "token1", a1Properties.Token)
5353
assert.Equal(t, "token2", a2Properties.Token)
@@ -79,8 +79,8 @@ func TestUnmarshalJSONMultipleAuthProperties(t *testing.T) {
7979
err = json.Unmarshal([]byte(a2JSON), &a2)
8080
assert.NoError(t, err)
8181

82-
a1Properties := a1.Properties.(*OAuth2AuthProperties)
83-
a2Properties := a2.Properties.(*OAuth2AuthProperties)
82+
a1Properties := a1.Properties.OAuth2
83+
a2Properties := a2.Properties.OAuth2
8484

8585
assert.Equal(t, "secret1", a1Properties.ClientSecret)
8686
assert.Equal(t, "secret2", a2Properties.ClientSecret)

model/zz_generated.deepcopy.go

Lines changed: 23 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

parser/parser_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func TestFromFile(t *testing.T) {
181181
assert.Equal(t, len(auth), 1)
182182
assert.Equal(t, "testAuth", auth[0].Name)
183183
assert.Equal(t, model.AuthTypeBearer, auth[0].Scheme)
184-
bearerProperties := auth[0].Properties.(*model.BearerAuthProperties).Token
184+
bearerProperties := auth[0].Properties.Bearer.Token
185185
assert.Equal(t, "test_token", bearerProperties)
186186
},
187187
}, {
@@ -204,16 +204,16 @@ func TestFromFile(t *testing.T) {
204204
assert.Equal(t, len(auth), 2)
205205
assert.Equal(t, "testAuth", auth[0].Name)
206206
assert.Equal(t, model.AuthTypeBearer, auth[0].Scheme)
207-
bearerProperties := auth[0].Properties.(*model.BearerAuthProperties).Token
207+
bearerProperties := auth[0].Properties.Bearer.Token
208208
assert.Equal(t, "test_token", bearerProperties)
209209
assert.Equal(t, "testAuth2", auth[1].Name)
210210
assert.Equal(t, model.AuthTypeBasic, auth[1].Scheme)
211-
basicProperties := auth[1].Properties.(*model.BasicAuthProperties)
211+
basicProperties := auth[1].Properties.Basic
212212
assert.Equal(t, "test_user", basicProperties.Username)
213213
assert.Equal(t, "test_pwd", basicProperties.Password)
214214
// metadata
215215
assert.Equal(t, model.Metadata{"metadata1": model.FromString("metadata1"), "metadata2": model.FromString("metadata2")}, w.Metadata)
216-
assert.Equal(t, &model.Metadata{"auth1": model.FromString("auth1"), "auth2": model.FromString("auth2")}, auth[0].Properties.GetMetadata())
216+
assert.Equal(t, model.Metadata{"auth1": model.FromString("auth1"), "auth2": model.FromString("auth2")}, auth[0].Properties.Bearer.Metadata)
217217
},
218218
}, {
219219
"./testdata/workflows/applicationrequest.rp.json", func(t *testing.T, w *model.Workflow) {

0 commit comments

Comments
 (0)