Skip to content

Commit a1c8b1d

Browse files
Fixes #43 - Upgrade Go SDK to spec 0.7 (#43)
* Upgrade to spec 0.7: SpecVersion, Secrets, Constants, Annotations and DataInputSchema Signed-off-by: Ricardo Zanini <zanini@redhat.com> * Fix formatting Signed-off-by: Ricardo Zanini <zanini@redhat.com> * FunctionTypes, Errors, SubFlowRef, Auth, Timeouts Signed-off-by: Ricardo Zanini <zanini@redhat.com> * Auth, Timeouts, Review states, functions Signed-off-by: Ricardo Zanini <zanini@redhat.com> * Adding basic validation test for every workflow Signed-off-by: Ricardo Zanini <zanini@redhat.com> * Update README Signed-off-by: Ricardo Zanini <zanini@redhat.com>
1 parent 4362a26 commit a1c8b1d

33 files changed

+1399
-223
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ Current status of features implemented in the SDK is listed in the table below:
1717
| Latest Releases | Conformance to spec version |
1818
| :---: | :---: |
1919
| [v1.0.0](https://github.com/serverlessworkflow/sdk-go/releases/tag/v1.0.0) | [v0.5](https://github.com/serverlessworkflow/specification/tree/0.5.x) |
20-
| [v2.0.1](https://github.com/serverlessworkflow/sdk-go/releases/tag/v2.0.0) | [v0.6](https://github.com/serverlessworkflow/specification/tree/0.6.x) |
20+
| [v2.0.1](https://github.com/serverlessworkflow/sdk-go/releases/tag/v2.0.1) | [v0.6](https://github.com/serverlessworkflow/specification/tree/0.6.x) |
21+
| [v2.1.0](https://github.com/serverlessworkflow/sdk-go/releases/tag/v2.1.0) | [v0.7](https://github.com/serverlessworkflow/specification/tree/0.7.x) |
2122

2223
## How to use
2324

model/auth.go

Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
// Copyright 2021 The Serverless Workflow Specification Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package model
16+
17+
import (
18+
"encoding/json"
19+
"fmt"
20+
)
21+
22+
// AuthType ...
23+
type AuthType string
24+
25+
const (
26+
// AuthTypeBasic ...
27+
AuthTypeBasic AuthType = "basic"
28+
// AuthTypeBearer ...
29+
AuthTypeBearer AuthType = "bearer"
30+
// AuthTypeOAuth2 ...
31+
AuthTypeOAuth2 AuthType = "oauth2"
32+
)
33+
34+
// GrantType ...
35+
type GrantType string
36+
37+
const (
38+
// GrantTypePassword ...
39+
GrantTypePassword GrantType = "password"
40+
// GrantTypeClientCredentials ...
41+
GrantTypeClientCredentials GrantType = "clientCredentials"
42+
// GrantTypeTokenExchange ...
43+
GrantTypeTokenExchange GrantType = "tokenExchange"
44+
)
45+
46+
// authTypesMapping map to support JSON unmarshalling when guessing the auth scheme
47+
var authTypesMapping = map[AuthType]AuthProperties{
48+
AuthTypeBasic: &BasicAuthProperties{},
49+
AuthTypeBearer: &BearerAuthProperties{},
50+
AuthTypeOAuth2: &OAuth2AuthProperties{},
51+
}
52+
53+
// Auth ...
54+
type Auth struct {
55+
// Name Unique auth definition name
56+
Name string `json:"name" validate:"required"`
57+
// Scheme Defines the auth type
58+
Scheme AuthType `json:"scheme,omitempty" validate:"omitempty,min=1"`
59+
// Properties ...
60+
Properties AuthProperties `json:"properties" validate:"required"`
61+
}
62+
63+
// UnmarshalJSON ...
64+
func (a *Auth) UnmarshalJSON(data []byte) error {
65+
auth := make(map[string]json.RawMessage)
66+
if err := json.Unmarshal(data, &auth); err != nil {
67+
// it's a file
68+
file, err := unmarshalFile(data)
69+
if err != nil {
70+
return err
71+
}
72+
// call us recursively
73+
if err := json.Unmarshal(file, &a); err != nil {
74+
return err
75+
}
76+
return nil
77+
}
78+
if err := unmarshalKey("scheme", auth, &a.Scheme); err != nil {
79+
return err
80+
}
81+
if err := unmarshalKey("name", auth, &a.Name); err != nil {
82+
return err
83+
}
84+
85+
if len(a.Scheme) == 0 {
86+
a.Scheme = AuthTypeBasic
87+
}
88+
if _, ok := authTypesMapping[a.Scheme]; !ok {
89+
return fmt.Errorf("authentication scheme %s not supported", authTypesMapping["type"])
90+
}
91+
// we take the type we want to unmarshal based on the scheme
92+
authProperties := authTypesMapping[a.Scheme]
93+
if err := unmarshalKey("properties", auth, authProperties); err != nil {
94+
return err
95+
}
96+
return nil
97+
}
98+
99+
// AuthProperties ...
100+
type AuthProperties interface {
101+
// GetMetadata ...
102+
GetMetadata() *Metadata
103+
// GetSecret ...
104+
GetSecret() string
105+
}
106+
107+
// BaseAuthProperties ...
108+
type BaseAuthProperties struct {
109+
Common
110+
// Secret Expression referencing a workflow secret that contains all needed auth info
111+
Secret string `json:"secret,omitempty"`
112+
}
113+
114+
// UnmarshalJSON ...
115+
func (b *BaseAuthProperties) UnmarshalJSON(data []byte) error {
116+
properties := make(map[string]json.RawMessage)
117+
if err := json.Unmarshal(data, &properties); err != nil {
118+
b.Secret, err = unmarshalString(data)
119+
if err != nil {
120+
return err
121+
}
122+
return nil
123+
}
124+
if err := unmarshalKey("metadata", properties, &b.Metadata); err != nil {
125+
return err
126+
}
127+
if err := unmarshalKey("secret", properties, &b.Secret); err != nil {
128+
return err
129+
}
130+
return nil
131+
}
132+
133+
// GetMetadata ...
134+
func (b *BaseAuthProperties) GetMetadata() *Metadata {
135+
return &b.Metadata
136+
}
137+
138+
// GetSecret ...
139+
func (b *BaseAuthProperties) GetSecret() string {
140+
return b.Secret
141+
}
142+
143+
// BasicAuthProperties Basic Auth Info
144+
type BasicAuthProperties struct {
145+
BaseAuthProperties
146+
// Username String or a workflow expression. Contains the username
147+
Username string `json:"username" validate:"required"`
148+
// Password String or a workflow expression. Contains the user password
149+
Password string `json:"password" validate:"required"`
150+
}
151+
152+
// UnmarshalJSON ...
153+
func (b *BasicAuthProperties) UnmarshalJSON(data []byte) error {
154+
properties := make(map[string]json.RawMessage)
155+
if err := json.Unmarshal(data, &properties); err != nil {
156+
err = json.Unmarshal(data, &b.BaseAuthProperties)
157+
if err != nil {
158+
return err
159+
}
160+
return nil
161+
}
162+
if err := unmarshalKey("username", properties, &b.Username); err != nil {
163+
return err
164+
}
165+
if err := unmarshalKey("password", properties, &b.Password); err != nil {
166+
return err
167+
}
168+
if err := unmarshalKey("metadata", properties, &b.Metadata); err != nil {
169+
return err
170+
}
171+
return nil
172+
}
173+
174+
// BearerAuthProperties Bearer auth information
175+
type BearerAuthProperties struct {
176+
BaseAuthProperties
177+
// Token String or a workflow expression. Contains the token
178+
Token string `json:"token" validate:"required"`
179+
}
180+
181+
// UnmarshalJSON ...
182+
func (b *BearerAuthProperties) UnmarshalJSON(data []byte) error {
183+
properties := make(map[string]json.RawMessage)
184+
if err := json.Unmarshal(data, &properties); err != nil {
185+
err = json.Unmarshal(data, &b.BaseAuthProperties)
186+
if err != nil {
187+
return err
188+
}
189+
return nil
190+
}
191+
if err := unmarshalKey("token", properties, &b.Token); err != nil {
192+
return err
193+
}
194+
if err := unmarshalKey("metadata", properties, &b.Metadata); err != nil {
195+
return err
196+
}
197+
return nil
198+
}
199+
200+
// OAuth2AuthProperties OAuth2 information
201+
type OAuth2AuthProperties struct {
202+
BaseAuthProperties
203+
// Authority String or a workflow expression. Contains the authority information
204+
Authority string `json:"authority,omitempty" validate:"omitempty,min=1"`
205+
// GrantType Defines the grant type
206+
GrantType GrantType `json:"grantType" validate:"required"`
207+
// ClientID String or a workflow expression. Contains the client identifier
208+
ClientID string `json:"clientId" validate:"required"`
209+
// ClientSecret Workflow secret or a workflow expression. Contains the client secret
210+
ClientSecret string `json:"clientSecret,omitempty" validate:"omitempty,min=1"`
211+
// Scopes Array containing strings or workflow expressions. Contains the OAuth2 scopes
212+
Scopes []string `json:"scopes,omitempty" validate:"omitempty,min=1"`
213+
// Username String or a workflow expression. Contains the username. Used only if grantType is 'resourceOwner'
214+
Username string `json:"username,omitempty" validate:"omitempty,min=1"`
215+
// Password String or a workflow expression. Contains the user password. Used only if grantType is 'resourceOwner'
216+
Password string `json:"password,omitempty" validate:"omitempty,min=1"`
217+
// Audiences Array containing strings or workflow expressions. Contains the OAuth2 audiences
218+
Audiences []string `json:"audiences,omitempty" validate:"omitempty,min=1"`
219+
// SubjectToken String or a workflow expression. Contains the subject token
220+
SubjectToken string `json:"subjectToken,omitempty" validate:"omitempty,min=1"`
221+
// RequestedSubject String or a workflow expression. Contains the requested subject
222+
RequestedSubject string `json:"requestedSubject,omitempty" validate:"omitempty,min=1"`
223+
// RequestedIssuer String or a workflow expression. Contains the requested issuer
224+
RequestedIssuer string `json:"requestedIssuer,omitempty" validate:"omitempty,min=1"`
225+
}
226+
227+
// TODO: use reflection to unmarshal the keys and think on a generic approach to handle them
228+
229+
// UnmarshalJSON ...
230+
func (b *OAuth2AuthProperties) UnmarshalJSON(data []byte) error {
231+
properties := make(map[string]json.RawMessage)
232+
if err := json.Unmarshal(data, &properties); err != nil {
233+
err = json.Unmarshal(data, &b.BaseAuthProperties)
234+
if err != nil {
235+
return err
236+
}
237+
return nil
238+
}
239+
if err := unmarshalKey("authority", properties, &b.Authority); err != nil {
240+
return err
241+
}
242+
if err := unmarshalKey("grantType", properties, &b.GrantType); err != nil {
243+
return err
244+
}
245+
if err := unmarshalKey("clientId", properties, &b.ClientID); err != nil {
246+
return err
247+
}
248+
if err := unmarshalKey("clientSecret", properties, &b.ClientSecret); err != nil {
249+
return err
250+
}
251+
if err := unmarshalKey("scopes", properties, &b.Scopes); err != nil {
252+
return err
253+
}
254+
if err := unmarshalKey("username", properties, &b.Username); err != nil {
255+
return err
256+
}
257+
if err := unmarshalKey("password", properties, &b.Password); err != nil {
258+
return err
259+
}
260+
if err := unmarshalKey("audiences", properties, &b.Audiences); err != nil {
261+
return err
262+
}
263+
if err := unmarshalKey("subjectToken", properties, &b.SubjectToken); err != nil {
264+
return err
265+
}
266+
if err := unmarshalKey("requestedSubject", properties, &b.RequestedSubject); err != nil {
267+
return err
268+
}
269+
if err := unmarshalKey("requestedIssuer", properties, &b.RequestedIssuer); err != nil {
270+
return err
271+
}
272+
if err := unmarshalKey("metadata", properties, &b.Metadata); err != nil {
273+
return err
274+
}
275+
return nil
276+
}

model/event.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ type Event struct {
5555
Type string `json:"type" validate:"required"`
5656
// Defines the CloudEvent as either 'consumed' or 'produced' by the workflow. Default is 'consumed'
5757
Kind EventKind `json:"kind,omitempty"`
58+
// If `true`, only the Event payload is accessible to consuming Workflow states. If `false`, both event payload and context attributes should be accessible"
59+
DataOnly bool `json:"dataOnly,omitempty"`
5860
// CloudEvent correlation definitions
5961
Correlation []Correlation `json:"correlation,omitempty" validate:"omitempty,dive"`
6062
}
@@ -79,11 +81,3 @@ type EventRef struct {
7981
// Add additional extension context attributes to the produced event
8082
ContextAttributes map[string]interface{} `json:"contextAttributes,omitempty"`
8183
}
82-
83-
// SubFlowRef ...
84-
type SubFlowRef struct {
85-
// Sub-workflow unique id
86-
WorkflowID string `json:"workflowId" validate:"required"`
87-
// Sub-workflow version
88-
Version string `json:"version,omitempty"`
89-
}

model/function.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ const (
2525
FunctionTypeExpression FunctionType = "expression"
2626
// FunctionTypeGraphQL ...
2727
FunctionTypeGraphQL FunctionType = "graphql"
28+
// FunctionTypeAsyncAPI ...
29+
FunctionTypeAsyncAPI FunctionType = "asyncapi"
30+
// FunctionTypeOData ...
31+
FunctionTypeOData FunctionType = "odata"
2832
)
2933

3034
// FunctionType ...
@@ -39,6 +43,8 @@ type Function struct {
3943
Operation string `json:"operation" validate:"required"`
4044
// Defines the function type. Is either `rest`, `rpc`, `expression` or `graphql`. Default is `rest`
4145
Type FunctionType `json:"type,omitempty"`
46+
// References an auth definition name to be used to access to resource defined in the operation parameter
47+
AuthRef string `json:"authRef,omitempty" validate:"omitempty,min=1"`
4248
}
4349

4450
// FunctionRef ...

model/retry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type Retry struct {
3232
// Numeric value, if specified the delay between retries is multiplied by this value.
3333
Multiplier floatstr.Float32OrString `json:"multiplier,omitempty" validate:"omitempty,min=0"`
3434
// Maximum number of retry attempts.
35-
MaxAttempts intstr.IntOrString `json:"maxAttempts" validate:"required,min=0"`
35+
MaxAttempts intstr.IntOrString `json:"maxAttempts" validate:"required"`
3636
// If float type, maximum amount of random time added or subtracted from the delay between each retry relative to total delay (between 0 and 1). If string type, absolute maximum amount of random time added or subtracted from the delay between each retry (ISO 8601 duration format)
3737
Jitter floatstr.Float32OrString `json:"jitter,omitempty" validate:"omitempty,min=0,max=1"`
3838
}

0 commit comments

Comments
 (0)