Skip to content

Commit b7b52d2

Browse files
authored
Merge pull request #135 from LalitDeore/distribution
Suborg distribution for datastore
2 parents 1908a96 + c0843f0 commit b7b52d2

File tree

4 files changed

+126
-11
lines changed

4 files changed

+126
-11
lines changed

db-connector.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12600,6 +12600,22 @@ func GetAllCacheKeys(ctx context.Context, orgId string, max int, inputcursor str
1260012600
}
1260112601
}
1260212602

12603+
foundOrg, err := GetOrg(ctx, orgId)
12604+
if err == nil && len(foundOrg.ChildOrgs) == 0 && len(foundOrg.CreatorOrg) > 0 && foundOrg.CreatorOrg != orgId {
12605+
parentOrg, err := GetOrg(ctx, foundOrg.CreatorOrg)
12606+
if err == nil {
12607+
parentOrgCache, _, err := GetAllCacheKeys(ctx, parentOrg.Id, max, inputcursor)
12608+
if err == nil {
12609+
for _, parentCache := range parentOrgCache {
12610+
if !ArrayContains(parentCache.SuborgDistribution, orgId) {
12611+
continue
12612+
}
12613+
cacheKeys = append(cacheKeys, parentCache)
12614+
}
12615+
}
12616+
}
12617+
}
12618+
1260312619
return cacheKeys, cursor, nil
1260412620
}
1260512621

files.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2117,7 +2117,7 @@ func HandleSetFileConfig(resp http.ResponseWriter, request *http.Request) {
21172117
return
21182118
}
21192119

2120-
if user.Role != "admin" {
2120+
if user.ActiveOrg.Role != "admin" {
21212121
log.Printf("User (%s) isn't admin during file edit config", user.Username)
21222122
resp.WriteHeader(401)
21232123
resp.Write([]byte(`{"success": false, "reason": "only admin can edit file config"}`))

shared.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17548,6 +17548,105 @@ func HandleListCacheKeys(resp http.ResponseWriter, request *http.Request) {
1754817548
resp.Write(b)
1754917549
}
1755017550

17551+
func HandleCacheConfig(resp http.ResponseWriter, request *http.Request) {
17552+
cors := HandleCors(resp, request)
17553+
if cors {
17554+
return
17555+
}
17556+
17557+
user, err := HandleApiAuthentication(resp, request)
17558+
if err != nil {
17559+
log.Printf("[DEBUG] Api authentication failed in cache config: %s", err)
17560+
resp.WriteHeader(401)
17561+
resp.Write([]byte(`{"success": false, "reason": "Failed authentication"}`))
17562+
return
17563+
}
17564+
17565+
if user.ActiveOrg.Role != "admin" {
17566+
log.Printf("[AUDIT] User %s (%s) tried to list cache keys without admin role", user.Username, user.Id)
17567+
resp.WriteHeader(401)
17568+
resp.Write([]byte(`{"success": false, "reason": "Only admins can distribute cache to sub-orgs"}`))
17569+
return
17570+
}
17571+
17572+
var orgId string
17573+
location := strings.Split(request.URL.String(), "/")
17574+
if location[1] == "api" {
17575+
if len(location) <= 4 {
17576+
log.Printf("Path too short: %d", len(location))
17577+
resp.WriteHeader(401)
17578+
resp.Write([]byte(`{"success": false}`))
17579+
return
17580+
}
17581+
17582+
orgId = location[4]
17583+
}
17584+
17585+
if len(orgId) == 0 {
17586+
log.Printf("[ERROR] Missing org id in cache config")
17587+
resp.WriteHeader(401)
17588+
resp.Write([]byte(`{"success": false, "reason": "Missing org id"}`))
17589+
return
17590+
}
17591+
17592+
type cacheConfig struct {
17593+
Key string `json:"key"`
17594+
Action string `json:"action"`
17595+
SelectedSuborg []string `json:"selected_suborgs"`
17596+
}
17597+
17598+
var config cacheConfig
17599+
17600+
body, err := ioutil.ReadAll(request.Body)
17601+
if err != nil {
17602+
log.Printf("Error with body read: %s", err)
17603+
resp.WriteHeader(401)
17604+
resp.Write([]byte(`{"success": false}`))
17605+
return
17606+
}
17607+
17608+
err = json.Unmarshal(body, &config)
17609+
if err != nil {
17610+
log.Printf("[WARNING] Failed unmarshalling in cache config: %s", err)
17611+
resp.WriteHeader(401)
17612+
resp.Write([]byte(`{"success": false}`))
17613+
return
17614+
}
17615+
17616+
ctx := GetContext(request)
17617+
17618+
cacheId := fmt.Sprintf("%s_%s", orgId, config.Key)
17619+
cache, err := GetCacheKey(ctx, cacheId)
17620+
if err != nil {
17621+
log.Printf("[WARNING] Failed getting cache key '%s' for org %s (config)", config.Key, orgId)
17622+
resp.WriteHeader(400)
17623+
resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "Failed to get key. Does it exist?", "extra": "%s"}`, cache.Key)))
17624+
return
17625+
}
17626+
17627+
if config.Action == "suborg_distribute" {
17628+
17629+
if len(config.SelectedSuborg) == 0 {
17630+
cache.SuborgDistribution = []string{}
17631+
} else {
17632+
cache.SuborgDistribution = config.SelectedSuborg
17633+
}
17634+
17635+
err = SetCacheKey(ctx, *cache)
17636+
if err != nil {
17637+
log.Printf("[WARNING] Failed setting cache key '%s' for org %s (config)", config.Key, orgId)
17638+
resp.WriteHeader(400)
17639+
resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "Failed to set key. Does it exist?", "extra": "%s"}`, cache.Key)))
17640+
return
17641+
}
17642+
}
17643+
17644+
log.Printf("[INFO] Successfully updated cache key '%s' for org %s", config.Key, orgId)
17645+
17646+
resp.WriteHeader(200)
17647+
resp.Write([]byte(`{"success": true, "reason" : "Cache updated successfully!"}`))
17648+
}
17649+
1755117650
func HandleDeleteCacheKey(resp http.ResponseWriter, request *http.Request) {
1755217651
cors := HandleCors(resp, request)
1755317652
if cors {

structs.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -986,10 +986,10 @@ type CacheKeyData struct {
986986
Created int64 `json:"created" datastore:"Created"`
987987
Edited int64 `json:"edited" datastore:"Edited"`
988988

989-
FormattedKey string `json:"formatted_key,omitempty" datastore:"FormattedKey"`
990-
PublicAuthorization string `json:"public_authorization,omitempty" datastore:"PublicAuthorization"` // Used for public authorization
989+
FormattedKey string `json:"formatted_key,omitempty" datastore:"FormattedKey"`
990+
PublicAuthorization string `json:"public_authorization,omitempty" datastore:"PublicAuthorization"` // Used for public authorization
991+
SuborgDistribution []string `json:"suborg_distribution" datastore:"suborg_distribution"`
991992
}
992-
993993
type SyncConfig struct {
994994
Interval int64 `json:"interval" datastore:"interval"`
995995
Apikey string `json:"api_key" datastore:"api_key"`
@@ -1183,7 +1183,7 @@ type Action struct {
11831183

11841184
ParentControlled bool `json:"parent_controlled" datastore:"parent_controlled"` // If the parent workflow node exists, and shouldn't be editable by child workflow
11851185

1186-
ParameterLocks []ParameterLock `json:"parameter_locks" datastore:"parameter_locks"`
1186+
ParameterLocks []ParameterLock `json:"parameter_locks" datastore:"parameter_locks"`
11871187
}
11881188

11891189
// Added environment for location to execute
@@ -1209,12 +1209,12 @@ type Trigger struct {
12091209
X float64 `json:"x" datastore:"x"`
12101210
Y float64 `json:"y" datastore:"y"`
12111211
} `json:"position"`
1212-
Priority int `json:"priority" datastore:"priority"`
1213-
SourceWorkflow string `json:"source_workflow" yaml:"source_workflow" datastore:"source_workflow"`
1214-
ExecutionDelay int64 `json:"execution_delay" yaml:"execution_delay" datastore:"execution_delay"`
1215-
AppAssociation WorkflowApp `json:"app_association" yaml:"app_association" datastore:"app_association"`
1216-
ParentControlled bool `json:"parent_controlled" datastore:"parent_controlled"` // If the parent workflow node exists, and shouldn't be editable by child workflow
1217-
1212+
Priority int `json:"priority" datastore:"priority"`
1213+
SourceWorkflow string `json:"source_workflow" yaml:"source_workflow" datastore:"source_workflow"`
1214+
ExecutionDelay int64 `json:"execution_delay" yaml:"execution_delay" datastore:"execution_delay"`
1215+
AppAssociation WorkflowApp `json:"app_association" yaml:"app_association" datastore:"app_association"`
1216+
ParentControlled bool `json:"parent_controlled" datastore:"parent_controlled"` // If the parent workflow node exists, and shouldn't be editable by child workflow
1217+
12181218
// TODO: make this a predictable field
12191219
// generated from current ID + workflow ID + orgid as seed
12201220
ReplacementForTrigger string `json:"replacement_for_trigger" datastore:"replacement_for_trigger"` // If this trigger is a replacement for another trigger

0 commit comments

Comments
 (0)