Skip to content

Commit 0c641dc

Browse files
author
Jayesh Choudhary
authored
Merge pull request #519 from spaceuptech/v0.13.1
V0.13.1
2 parents 8592366 + 2e21ca5 commit 0c641dc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1011
-203
lines changed

.gitignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ test
88
*.exe~
99
*.dll
1010
*.so
11-
*.dylib
1211
*.zip
13-
12+
*.dylib
1413
# Test binary, build with `go test -c`
1514
*.test
1615

config/config.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type Project struct {
1818
// Admin stores the admin credentials
1919
type Admin struct {
2020
Secret string `json:"secret" yaml:"secret"`
21-
Operation OperationConfig `json:"operatiop"`
21+
Operation OperationConfig `json:"operation"`
2222
Users []AdminUser `json:"users" yaml:"users"`
2323
}
2424

@@ -114,8 +114,9 @@ type Service struct {
114114

115115
// Endpoint holds the config of a endpoint
116116
type Endpoint struct {
117-
Path string `json:"path" yaml:"path"`
118-
Rule *Rule `json:"rule" yaml:"rule"`
117+
Method string `json:"method" yaml:"method"`
118+
Path string `json:"path" yaml:"path"`
119+
Rule *Rule `json:"rule" yaml:"rule"`
119120
}
120121

121122
// FileStore holds the config for the file store module

main.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ var essentialFlags = []cli.Flag{
2222
EnvVar: "NODE_ID",
2323
},
2424
cli.StringFlag{
25-
Name: "config",
26-
Value: "config.yaml",
27-
Usage: "Load space cloud config from `FILE`",
25+
Name: "config",
26+
Value: "config.yaml",
27+
Usage: "Load space cloud config from `FILE`",
28+
EnvVar: "CONFIG",
2829
},
2930
cli.StringFlag{
3031
Name: "ssl-cert",

modules/auth/auth.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/dgrijalva/jwt-go"
88

99
"github.com/spaceuptech/space-cloud/config"
10-
"github.com/spaceuptech/space-cloud/modules/auth/schema"
10+
"github.com/spaceuptech/space-cloud/modules/schema"
1111
"github.com/spaceuptech/space-cloud/modules/crud"
1212

1313
"github.com/spaceuptech/space-cloud/modules/functions"

modules/auth/handle_crud.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package auth
22

33
import (
4+
"context"
45
"net/http"
56

67
"github.com/spaceuptech/space-cloud/config"
@@ -9,7 +10,7 @@ import (
910
)
1011

1112
// IsCreateOpAuthorised checks if the crud operation is authorised
12-
func (m *Module) IsCreateOpAuthorised(project, dbType, col, token string, req *model.CreateRequest) (int, error) {
13+
func (m *Module) IsCreateOpAuthorised(ctx context.Context, project, dbType, col, token string, req *model.CreateRequest) (int, error) {
1314
m.RLock()
1415
defer m.RUnlock()
1516

@@ -32,7 +33,7 @@ func (m *Module) IsCreateOpAuthorised(project, dbType, col, token string, req *m
3233

3334
for _, row := range rows {
3435
args["doc"] = row
35-
err := m.matchRule(project, rule, map[string]interface{}{"args": args}, auth)
36+
err := m.matchRule(ctx, project, rule, map[string]interface{}{"args": args}, auth)
3637
if err != nil {
3738
return http.StatusForbidden, err
3839
}
@@ -46,7 +47,7 @@ func (m *Module) IsCreateOpAuthorised(project, dbType, col, token string, req *m
4647
}
4748

4849
// IsReadOpAuthorised checks if the crud operation is authorised
49-
func (m *Module) IsReadOpAuthorised(project, dbType, col, token string, req *model.ReadRequest) (int, error) {
50+
func (m *Module) IsReadOpAuthorised(ctx context.Context, project, dbType, col, token string, req *model.ReadRequest) (int, error) {
5051
m.RLock()
5152
defer m.RUnlock()
5253

@@ -56,7 +57,7 @@ func (m *Module) IsReadOpAuthorised(project, dbType, col, token string, req *mod
5657
}
5758

5859
args := map[string]interface{}{"op": req.Operation, "auth": auth, "find": req.Find, "token": token}
59-
err = m.matchRule(project, rule, map[string]interface{}{"args": args}, auth)
60+
err = m.matchRule(ctx, project, rule, map[string]interface{}{"args": args}, auth)
6061
if err != nil {
6162
return http.StatusForbidden, err
6263
}
@@ -65,7 +66,7 @@ func (m *Module) IsReadOpAuthorised(project, dbType, col, token string, req *mod
6566
}
6667

6768
// IsUpdateOpAuthorised checks if the crud operation is authorised
68-
func (m *Module) IsUpdateOpAuthorised(project, dbType, col, token string, req *model.UpdateRequest) (int, error) {
69+
func (m *Module) IsUpdateOpAuthorised(ctx context.Context, project, dbType, col, token string, req *model.UpdateRequest) (int, error) {
6970
m.RLock()
7071
defer m.RUnlock()
7172

@@ -75,7 +76,7 @@ func (m *Module) IsUpdateOpAuthorised(project, dbType, col, token string, req *m
7576
}
7677

7778
args := map[string]interface{}{"op": req.Operation, "auth": auth, "find": req.Find, "update": req.Update, "token": token}
78-
err = m.matchRule(project, rule, map[string]interface{}{"args": args}, auth)
79+
err = m.matchRule(ctx, project, rule, map[string]interface{}{"args": args}, auth)
7980
if err != nil {
8081
return http.StatusForbidden, err
8182
}
@@ -88,7 +89,7 @@ func (m *Module) IsUpdateOpAuthorised(project, dbType, col, token string, req *m
8889
}
8990

9091
// IsDeleteOpAuthorised checks if the crud operation is authorised
91-
func (m *Module) IsDeleteOpAuthorised(project, dbType, col, token string, req *model.DeleteRequest) (int, error) {
92+
func (m *Module) IsDeleteOpAuthorised(ctx context.Context, project, dbType, col, token string, req *model.DeleteRequest) (int, error) {
9293
m.RLock()
9394
defer m.RUnlock()
9495

@@ -98,7 +99,7 @@ func (m *Module) IsDeleteOpAuthorised(project, dbType, col, token string, req *m
9899
}
99100

100101
args := map[string]interface{}{"op": req.Operation, "auth": auth, "find": req.Find, "token": token}
101-
err = m.matchRule(project, rule, map[string]interface{}{"args": args}, auth)
102+
err = m.matchRule(ctx, project, rule, map[string]interface{}{"args": args}, auth)
102103
if err != nil {
103104
return http.StatusForbidden, err
104105
}
@@ -107,7 +108,7 @@ func (m *Module) IsDeleteOpAuthorised(project, dbType, col, token string, req *m
107108
}
108109

109110
// IsAggregateOpAuthorised checks if the crud operation is authorised
110-
func (m *Module) IsAggregateOpAuthorised(project, dbType, col, token string, req *model.AggregateRequest) (int, error) {
111+
func (m *Module) IsAggregateOpAuthorised(ctx context.Context, project, dbType, col, token string, req *model.AggregateRequest) (int, error) {
111112
m.RLock()
112113
defer m.RUnlock()
113114

@@ -117,7 +118,7 @@ func (m *Module) IsAggregateOpAuthorised(project, dbType, col, token string, req
117118
}
118119

119120
args := map[string]interface{}{"op": req.Operation, "auth": auth, "pipeline": req.Pipeline, "token": token}
120-
err = m.matchRule(project, rule, map[string]interface{}{"args": args}, auth)
121+
err = m.matchRule(ctx, project, rule, map[string]interface{}{"args": args}, auth)
121122
if err != nil {
122123
return http.StatusForbidden, err
123124
}

modules/auth/handle_file.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package auth
22

33
import (
4+
"context"
45
"errors"
56
"os"
67
"strings"
@@ -10,7 +11,7 @@ import (
1011
)
1112

1213
// IsFileOpAuthorised checks if the caller is authorized to make the request
13-
func (m *Module) IsFileOpAuthorised(project, token, path string, op utils.FileOpType, args map[string]interface{}) error {
14+
func (m *Module) IsFileOpAuthorised(ctx context.Context, project, token, path string, op utils.FileOpType, args map[string]interface{}) error {
1415
m.RLock()
1516
defer m.RUnlock()
1617

@@ -38,7 +39,7 @@ func (m *Module) IsFileOpAuthorised(project, token, path string, op utils.FileOp
3839
args["token"] = token
3940

4041
// Match the rule
41-
return m.matchRule(project, rule, map[string]interface{}{"args": args}, auth)
42+
return m.matchRule(ctx, project, rule, map[string]interface{}{"args": args}, auth)
4243
}
4344

4445
func (m *Module) getFileRule(path string) (map[string]interface{}, *config.FileRule, error) {

modules/auth/handle_functions.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package auth
22

33
import (
4+
"context"
45
"errors"
56

67
"github.com/spaceuptech/space-cloud/config"
78
)
89

910
// IsFuncCallAuthorised checks if the func call is authorised
10-
func (m *Module) IsFuncCallAuthorised(project, service, function, token string, params interface{}) (TokenClaims, error) {
11+
func (m *Module) IsFuncCallAuthorised(ctx context.Context, project, service, function, token string, params interface{}) (TokenClaims, error) {
1112
m.RLock()
1213
defer m.RUnlock()
1314

@@ -27,7 +28,7 @@ func (m *Module) IsFuncCallAuthorised(project, service, function, token string,
2728
return nil, err
2829
}
2930

30-
if err = m.matchRule(project, rule, map[string]interface{}{
31+
if err = m.matchRule(ctx, project, rule, map[string]interface{}{
3132
"args": map[string]interface{}{"auth": auth, "params": params, "token": token},
3233
}, auth); err != nil {
3334
return nil, err

modules/auth/match.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/spaceuptech/space-cloud/modules/crud"
1313
)
1414

15-
func (m *Module) matchRule(project string, rule *config.Rule, args map[string]interface{}, auth map[string]interface{}) error {
15+
func (m *Module) matchRule(ctx context.Context, project string, rule *config.Rule, args map[string]interface{}, auth map[string]interface{}) error {
1616
if project != m.project {
1717
return errors.New("invalid project details provided")
1818
}
@@ -41,17 +41,17 @@ func (m *Module) matchRule(project string, rule *config.Rule, args map[string]in
4141
return matchOr(rule, args)
4242

4343
case "webhook":
44-
return matchFunc(rule, m.makeHttpRequest, args)
44+
return matchFunc(ctx, rule, m.makeHttpRequest, args)
4545

4646
case "query":
47-
return matchQuery(project, rule, m.crud, args)
47+
return matchQuery(ctx, project, rule, m.crud, args)
4848

4949
default:
5050
return ErrIncorrectMatch
5151
}
5252
}
5353

54-
func matchFunc(rule *config.Rule, MakeHttpRequest utils.MakeHttpRequest, args map[string]interface{}) error {
54+
func matchFunc(ctx context.Context, rule *config.Rule, MakeHttpRequest utils.MakeHttpRequest, args map[string]interface{}) error {
5555
obj := args["args"].(map[string]interface{})
5656
token := obj["token"].(string)
5757
delete(obj, "token")
@@ -63,15 +63,15 @@ func matchFunc(rule *config.Rule, MakeHttpRequest utils.MakeHttpRequest, args ma
6363
return MakeHttpRequest(ctx, "POST", rule.Url, token, obj, &result)
6464
}
6565

66-
func matchQuery(project string, rule *config.Rule, crud *crud.Module, args map[string]interface{}) error {
66+
func matchQuery(ctx context.Context, project string, rule *config.Rule, crud *crud.Module, args map[string]interface{}) error {
6767
// Adjust the find object to load any variables referenced from state
6868
rule.Find = utils.Adjust(rule.Find, args).(map[string]interface{})
6969

7070
// Create a new read request
7171
req := &model.ReadRequest{Find: rule.Find, Operation: utils.One}
7272

7373
// Execute the read request
74-
_, err := crud.Read(context.TODO(), rule.DB, project, rule.Col, req)
74+
_, err := crud.Read(ctx, rule.DB, project, rule.Col, req)
7575
return err
7676
}
7777

modules/crud/mgo/mongo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (m *Mongo) IsClientSafe() error {
5555
}
5656

5757
func (m *Mongo) connect() error {
58-
timeOut := 5 * time.Second
58+
timeOut := 3 * time.Second
5959

6060
ctx, cancel := context.WithTimeout(context.Background(), timeOut)
6161
defer cancel()

modules/crud/sql/collections.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (s *SQL) GetCollections(ctx context.Context, project string) ([]utils.Datab
2020
}
2121

2222
sqlString = strings.Replace(sqlString, "\"", "", -1)
23-
rows, err := s.client.Queryx(sqlString, args...)
23+
rows, err := s.client.QueryxContext(ctx, sqlString, args...)
2424
if err != nil {
2525
return nil, err
2626
}

modules/crud/sql/sql.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (s *SQL) IsClientSafe() error {
9090
}
9191

9292
func (s *SQL) connect() error {
93-
timeOut := 5 * time.Second
93+
timeOut := 3 * time.Second
9494

9595
ctx, cancel := context.WithTimeout(context.Background(), timeOut)
9696
defer cancel()

modules/eventing/handle_intents.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ import (
1313
)
1414

1515
func (m *Module) processIntents(t *time.Time) {
16-
m.lock.RLock()
17-
defer m.lock.RUnlock()
1816

1917
// Return if module is not enabled
20-
if !m.config.Enabled {
18+
if !m.IsEnabled() {
2119
return
2220
}
21+
m.lock.RLock()
22+
project := m.project
23+
dbType, col := m.config.DBType, m.config.Col
24+
m.lock.RUnlock()
2325

2426
// Create a context with 5 second timeout
2527
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
@@ -35,9 +37,7 @@ func (m *Module) processIntents(t *time.Time) {
3537
},
3638
}}
3739

38-
dbType, col := m.config.DBType, m.config.Col
39-
40-
results, err := m.crud.Read(ctx, dbType, m.project, col, &readRequest)
40+
results, err := m.crud.Read(ctx, dbType, project, col, &readRequest)
4141
if err != nil {
4242
log.Println("Eventing intent routine error:", err)
4343
return
@@ -62,6 +62,9 @@ func (m *Module) processIntents(t *time.Time) {
6262
}
6363

6464
func (m *Module) processIntent(eventDoc *model.EventDocument) {
65+
m.lock.RLock()
66+
defer m.lock.RUnlock()
67+
6568
// Create a context with 5 second timeout
6669
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
6770
defer cancel()

modules/eventing/handle_staged.go

+11-8
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ import (
1313
)
1414

1515
func (m *Module) processStagedEvents(t *time.Time) {
16-
m.lock.RLock()
17-
defer m.lock.RUnlock()
1816

1917
// Return if module is not enabled
20-
if !m.config.Enabled {
18+
if !m.IsEnabled() {
2119
return
2220
}
21+
m.lock.RLock()
22+
project := m.project
23+
dbType, col := m.config.DBType, m.config.Col
24+
m.lock.RUnlock()
2325

2426
// Create a context with 5 second timeout
2527
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
@@ -35,9 +37,7 @@ func (m *Module) processStagedEvents(t *time.Time) {
3537
},
3638
}}
3739

38-
dbType, col := m.config.DBType, m.config.Col
39-
40-
results, err := m.crud.Read(ctx, dbType, m.project, col, &readRequest)
40+
results, err := m.crud.Read(ctx, dbType, project, col, &readRequest)
4141
if err != nil {
4242
log.Println("Eventing stage routine error:", err)
4343
return
@@ -58,6 +58,9 @@ func (m *Module) processStagedEvents(t *time.Time) {
5858
}
5959

6060
func (m *Module) processStagedEvent(eventDoc *model.EventDocument) {
61+
m.lock.RLock()
62+
defer m.lock.RUnlock()
63+
6164
// Create a context with 5 second timeout
6265
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
6366
defer cancel()
@@ -112,7 +115,7 @@ func (m *Module) processStagedEvent(eventDoc *model.EventDocument) {
112115
}
113116
}
114117

115-
m.crud.InternalUpdate(ctx, m.config.DBType, m.project, m.config.Col, m.generateProcessedEventRequest(eventDoc.ID))
118+
m.crud.InternalUpdate(ctxLocal, m.config.DBType, m.project, m.config.Col, m.generateProcessedEventRequest(eventDoc.ID))
116119
return
117120
}
118121

@@ -129,7 +132,7 @@ func (m *Module) processStagedEvent(eventDoc *model.EventDocument) {
129132
time.Sleep(5 * time.Second)
130133
}
131134

132-
if err := m.crud.InternalUpdate(context.TODO(), m.config.DBType, m.project, m.config.Col, m.generateFailedEventRequest(eventDoc.ID, "Max retires limit reached")); err != nil {
135+
if err := m.crud.InternalUpdate(ctx, m.config.DBType, m.project, m.config.Col, m.generateFailedEventRequest(eventDoc.ID, "Max retires limit reached")); err != nil {
133136
log.Println("Eventing staged event handler could not update event doc:", err)
134137
}
135138
}

0 commit comments

Comments
 (0)