Skip to content

Commit db660c6

Browse files
committed
Made workflow backup overrides get set if only one field is set
1 parent 1c9b885 commit db660c6

File tree

3 files changed

+153
-9
lines changed

3 files changed

+153
-9
lines changed

cloudSync.go

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -795,18 +795,69 @@ func CheckCreatorSelfPermission(ctx context.Context, requestUser, creatorUser Us
795795

796796
// Uploads updates for a workflow to a specific file on git
797797
func SetGitWorkflow(ctx context.Context, workflow Workflow, org *Org) error {
798-
if workflow.BackupConfig.UploadRepo != "" && workflow.BackupConfig.UploadBranch != "" && workflow.BackupConfig.UploadUsername != "" && workflow.BackupConfig.UploadToken != "" {
798+
if workflow.BackupConfig.UploadRepo != "" || workflow.BackupConfig.UploadBranch != "" || workflow.BackupConfig.UploadUsername != "" || workflow.BackupConfig.UploadToken != "" {
799799
//log.Printf("\n\n\n[DEBUG] Using workflow backup config for org %s (%s)\n\n\n", org.Name, org.Id)
800800

801801
org.Defaults.WorkflowUploadRepo = workflow.BackupConfig.UploadRepo
802802
org.Defaults.WorkflowUploadBranch = workflow.BackupConfig.UploadBranch
803803
org.Defaults.WorkflowUploadUsername = workflow.BackupConfig.UploadUsername
804804
org.Defaults.WorkflowUploadToken = workflow.BackupConfig.UploadToken
805+
806+
// FIXME: Decrypt here
807+
if workflow.BackupConfig.TokensEncrypted {
808+
log.Printf("\n\n[DEBUG] Should realtime decrypt token for org %s (%s)\n\n", org.Name, org.Id)
809+
org.Defaults.TokensEncrypted = true
810+
} else {
811+
org.Defaults.TokensEncrypted = false
812+
}
805813
}
806814

815+
if org.Defaults.TokensEncrypted == true {
816+
log.Printf("\n\n[DEBUG] Decrypting token for org %s (%s)\n\n", org.Name, org.Id)
817+
818+
parsedKey := fmt.Sprintf("%s_upload_token", org.Id)
819+
newValue, err := HandleKeyDecryption([]byte(org.Defaults.WorkflowUploadToken), parsedKey)
820+
if err != nil {
821+
log.Printf("[ERROR] Failed decrypting token for org %s (%s): %s", org.Name, org.Id, err)
822+
} else {
823+
org.Defaults.WorkflowUploadToken = string(newValue)
824+
}
807825

808-
if org.Defaults.WorkflowUploadRepo == "" || org.Defaults.WorkflowUploadBranch == "" || org.Defaults.WorkflowUploadUsername == "" || org.Defaults.WorkflowUploadToken == "" {
809-
//log.Printf("[DEBUG] No workflow upload repo for org %s (%s)", org.Name, org.Id)
826+
parsedKey = fmt.Sprintf("%s_upload_username", org.Id)
827+
newValue, err = HandleKeyDecryption([]byte(org.Defaults.WorkflowUploadUsername), parsedKey)
828+
if err != nil {
829+
log.Printf("[ERROR] Failed decrypting username for org %s (%s): %s", org.Name, org.Id, err)
830+
} else {
831+
org.Defaults.WorkflowUploadUsername = string(newValue)
832+
}
833+
834+
parsedKey = fmt.Sprintf("%s_upload_repo", org.Id)
835+
newValue, err = HandleKeyDecryption([]byte(org.Defaults.WorkflowUploadRepo), parsedKey)
836+
if err != nil {
837+
log.Printf("[ERROR] Failed decrypting repo for org %s (%s): %s", org.Name, org.Id, err)
838+
} else {
839+
org.Defaults.WorkflowUploadRepo = string(newValue)
840+
}
841+
842+
parsedKey = fmt.Sprintf("%s_upload_branch", org.Id)
843+
newValue, err = HandleKeyDecryption([]byte(org.Defaults.WorkflowUploadBranch), parsedKey)
844+
if err != nil {
845+
log.Printf("[ERROR] Failed decrypting branch for org %s (%s): %s", org.Name, org.Id, err)
846+
} else {
847+
org.Defaults.WorkflowUploadBranch = string(newValue)
848+
}
849+
850+
log.Printf("[DEBUG] Decrypted token for org %s (%s): %s", org.Name, org.Id, newValue)
851+
}
852+
853+
if len(org.Defaults.WorkflowUploadBranch) == 0 {
854+
org.Defaults.WorkflowUploadBranch = "master"
855+
}
856+
857+
858+
if org.Defaults.WorkflowUploadRepo == "" || org.Defaults.WorkflowUploadToken == "" {
859+
log.Printf("[DEBUG] Missing Repo/Token during Workflow backup upload for org %s (%s)", org.Name, org.Id)
860+
//return errors.New("Missing repo or token")
810861
return nil
811862
}
812863

@@ -844,12 +895,13 @@ func SetGitWorkflow(ctx context.Context, workflow Workflow, org *Org) error {
844895
commitMessage := fmt.Sprintf("User '%s' updated workflow '%s' with status '%s'", workflow.UpdatedBy, workflow.Name, workflow.Status)
845896
location := fmt.Sprintf("https://%s:%s@%s.git", org.Defaults.WorkflowUploadUsername, org.Defaults.WorkflowUploadToken, org.Defaults.WorkflowUploadRepo)
846897

847-
log.Printf("[DEBUG] Uploading workflow %s to repo: %s", workflow.ID, strings.Replace(location, org.Defaults.WorkflowUploadToken, "SCRAMBLED", -1))
898+
log.Printf("[DEBUG] Uploading workflow %s to repo: %s", workflow.ID, strings.Replace(location, org.Defaults.WorkflowUploadToken, "****", -1))
848899

849900
fs := memfs.New()
850901
if len(workflow.Status) == 0 {
851902
workflow.Status = "test"
852903
}
904+
853905
//filePath := fmt.Sprintf("/%s/%s.json", workflow.Status, workflow.ID)
854906
filePath := fmt.Sprintf("%s/%s/%s_%s.json", workflow.ExecutingOrg.Id, workflow.Status, strings.ReplaceAll(workflow.Name, " ", "-"), workflow.ID)
855907

@@ -861,7 +913,7 @@ func SetGitWorkflow(ctx context.Context, workflow Workflow, org *Org) error {
861913
// Initialize a new Git repository in memory
862914
w := &git.Worktree{}
863915
if err != nil {
864-
log.Printf("[ERROR] Error cloning repo: %s", err)
916+
log.Printf("[ERROR] Error cloning repo (workflow backup): %s", err)
865917
return err
866918
}
867919

@@ -895,7 +947,7 @@ func SetGitWorkflow(ctx context.Context, workflow Workflow, org *Org) error {
895947
}
896948

897949
// Commit the changes
898-
commit, err := w.Commit(commitMessage, &git.CommitOptions{
950+
_, err = w.Commit(commitMessage, &git.CommitOptions{
899951
Author: &object.Signature{
900952
Name: org.Defaults.WorkflowUploadUsername,
901953
Email: "",
@@ -907,8 +959,7 @@ func SetGitWorkflow(ctx context.Context, workflow Workflow, org *Org) error {
907959
return err
908960
}
909961

910-
// Print the commit hash
911-
log.Printf("[DEBUG] Commit Hash: %s", commit)
962+
//log.Printf("[DEBUG] Commit Hash: %s", commit)
912963

913964
// Push the changes to a remote repository (replace URL with your repository URL)
914965
// fmt.Sprintf("refs/heads/%s:refs/heads/%s", org.Defaults.WorkflowUploadBranch, org.Defaults.WorkflowUploadBranch)},
@@ -919,7 +970,7 @@ func SetGitWorkflow(ctx context.Context, workflow Workflow, org *Org) error {
919970
RemoteURL: location,
920971
})
921972
if err != nil {
922-
log.Printf("[ERROR] Pushing changes: %v (2)", err)
973+
log.Printf("[ERROR] Change Push issue: %v (2)", err)
923974
return err
924975
}
925976

shared.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,8 @@ func HandleGetOrg(resp http.ResponseWriter, request *http.Request) {
926926

927927
// Update active org for user to this one?
928928
// This makes it possible to walk around in the UI for the org
929+
930+
/*
929931
if user.ActiveOrg.Id != org.Id {
930932
log.Printf("[AUDIT] User %s (%s) is admin and has access to org %s. Updating active org to this one.", user.Username, user.Id, org.Id)
931933
user.ActiveOrg.Id = org.Id
@@ -941,6 +943,7 @@ func HandleGetOrg(resp http.ResponseWriter, request *http.Request) {
941943
DeleteCache(ctx, fmt.Sprintf("user_%s", user.Username))
942944
DeleteCache(ctx, fmt.Sprintf("user_%s", user.Id))
943945
}
946+
*/
944947

945948
} else {
946949
userFound := false
@@ -6710,6 +6713,7 @@ func diffWorkflows(oldWorkflow Workflow, newWorkflow Workflow, update bool) {
67106713
Id: childWorkflow.ExecutingOrg.Id,
67116714
Name: childWorkflow.ExecutingOrg.Name,
67126715
}
6716+
67136717
SetGitWorkflow(ctx, childWorkflow, &passedOrg)
67146718
}
67156719

@@ -8488,6 +8492,54 @@ func SaveWorkflow(resp http.ResponseWriter, request *http.Request) {
84888492
workflow.SuborgDistribution = []string{}
84898493
}
84908494

8495+
// Encrypt git backup info
8496+
if !workflow.BackupConfig.TokensEncrypted {
8497+
if len(workflow.BackupConfig.UploadRepo) > 0 {
8498+
parsedKey := fmt.Sprintf("%s_upload_repo", workflow.OrgId)
8499+
encryptedToken, err := handleKeyEncryption([]byte(workflow.BackupConfig.UploadRepo), parsedKey)
8500+
if err != nil {
8501+
log.Printf("[ERROR] Failed encrypting token for %s (%s): %s", workflow.Name, workflow.ID, err)
8502+
} else {
8503+
workflow.BackupConfig.UploadRepo = string(encryptedToken)
8504+
workflow.BackupConfig.TokensEncrypted = true
8505+
}
8506+
}
8507+
8508+
if len(workflow.BackupConfig.UploadBranch) > 0 {
8509+
parsedKey := fmt.Sprintf("%s_upload_branch", workflow.OrgId)
8510+
encryptedToken, err := handleKeyEncryption([]byte(workflow.BackupConfig.UploadBranch), parsedKey)
8511+
if err != nil {
8512+
log.Printf("[ERROR] Failed encrypting token for %s (%s): %s", workflow.Name, workflow.ID, err)
8513+
} else {
8514+
workflow.BackupConfig.UploadBranch = string(encryptedToken)
8515+
workflow.BackupConfig.TokensEncrypted = true
8516+
}
8517+
}
8518+
8519+
if len(workflow.BackupConfig.UploadUsername) > 0 {
8520+
parsedKey := fmt.Sprintf("%s_upload_username", workflow.OrgId)
8521+
encryptedToken, err := handleKeyEncryption([]byte(workflow.BackupConfig.UploadUsername), parsedKey)
8522+
if err != nil {
8523+
log.Printf("[ERROR] Failed encrypting token for %s (%s): %s", workflow.Name, workflow.ID, err)
8524+
} else {
8525+
workflow.BackupConfig.UploadUsername = string(encryptedToken)
8526+
workflow.BackupConfig.TokensEncrypted = true
8527+
}
8528+
}
8529+
8530+
if len(workflow.BackupConfig.UploadToken) > 0 {
8531+
parsedKey := fmt.Sprintf("%s_upload_token", workflow.OrgId)
8532+
encryptedToken, err := handleKeyEncryption([]byte(workflow.BackupConfig.UploadToken), parsedKey)
8533+
if err != nil {
8534+
log.Printf("[ERROR] Failed encrypting token for %s (%s): %s", workflow.Name, workflow.ID, err)
8535+
} else {
8536+
workflow.BackupConfig.UploadToken = string(encryptedToken)
8537+
workflow.BackupConfig.TokensEncrypted = true
8538+
}
8539+
}
8540+
}
8541+
8542+
84918543
err = SetWorkflow(ctx, workflow, workflow.ID)
84928544
if err != nil {
84938545
log.Printf("[ERROR] Failed saving workflow to database: %s", err)
@@ -8533,6 +8585,7 @@ func SaveWorkflow(resp http.ResponseWriter, request *http.Request) {
85338585
Name: user.ActiveOrg.Name,
85348586
}
85358587

8588+
85368589
SetWorkflowRevision(ctx, workflow)
85378590
err = SetGitWorkflow(ctx, workflow, org)
85388591
if err != nil {
@@ -9841,7 +9894,9 @@ func GetSpecificWorkflow(resp http.ResponseWriter, request *http.Request) {
98419894
}
98429895
}
98439896

9897+
98449898
if workflow.Public {
9899+
workflow.BackupConfig = BackupConfig{}
98459900
workflow.ExecutingOrg = OrgMini{}
98469901
workflow.Org = []OrgMini{}
98479902
workflow.OrgId = ""
@@ -9852,6 +9907,40 @@ func GetSpecificWorkflow(resp http.ResponseWriter, request *http.Request) {
98529907
}
98539908
}
98549909

9910+
if workflow.BackupConfig.TokensEncrypted {
9911+
parsedKey := fmt.Sprintf("%s_upload_token", workflow.OrgId)
9912+
newValue, err := HandleKeyDecryption([]byte(workflow.BackupConfig.UploadToken), parsedKey)
9913+
if err != nil {
9914+
log.Printf("[ERROR] Failed decrypting token for workflow %s (%s): %s", workflow.Name, workflow.ID, err)
9915+
} else {
9916+
workflow.BackupConfig.UploadToken = string(newValue)
9917+
}
9918+
9919+
parsedKey = fmt.Sprintf("%s_upload_username", workflow.OrgId)
9920+
newValue, err = HandleKeyDecryption([]byte(workflow.BackupConfig.UploadUsername), parsedKey)
9921+
if err != nil {
9922+
log.Printf("[ERROR] Failed decrypting username for workflow %s (%s): %s", workflow.Name, workflow.ID, err)
9923+
} else {
9924+
workflow.BackupConfig.UploadUsername = string(newValue)
9925+
}
9926+
9927+
parsedKey = fmt.Sprintf("%s_upload_repo", workflow.OrgId)
9928+
newValue, err = HandleKeyDecryption([]byte(workflow.BackupConfig.UploadRepo), parsedKey)
9929+
if err != nil {
9930+
log.Printf("[ERROR] Failed decrypting repo for workflow %s (%s): %s", workflow.Name, workflow.ID, err)
9931+
} else {
9932+
workflow.BackupConfig.UploadRepo = string(newValue)
9933+
}
9934+
9935+
parsedKey = fmt.Sprintf("%s_upload_branch", workflow.OrgId)
9936+
newValue, err = HandleKeyDecryption([]byte(workflow.BackupConfig.UploadBranch), parsedKey)
9937+
if err != nil {
9938+
log.Printf("[ERROR] Failed decrypting branch for org %s (%s): %s", workflow.Name, workflow.ID, err)
9939+
} else {
9940+
workflow.BackupConfig.UploadBranch = string(newValue)
9941+
}
9942+
}
9943+
98559944
log.Printf("[INFO] Got new version of workflow %s (%s) for org %s and user %s (%s). Actions: %d, Triggers: %d", workflow.Name, workflow.ID, user.ActiveOrg.Id, user.Username, user.Id, len(workflow.Actions), len(workflow.Triggers))
98569945

98579946
body, err := json.Marshal(workflow)

structs.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,8 @@ type Defaults struct {
957957
WorkflowUploadUsername string `json:"workflow_upload_username" datastore:"workflow_upload_username"`
958958
WorkflowUploadToken string `json:"workflow_upload_token" datastore:"workflow_upload_token"`
959959

960+
TokensEncrypted bool `json:"tokens_encrypted" datastore:"tokens_encrypted"`
961+
960962
NewsletterDisabled bool `json:"newsletter" datastore:"newsletter_disabled"`
961963
WeeklyRecommendationsDisabled bool `json:"weekly_recommendations" datastore:"weekly_recommendations_disabled"`
962964

@@ -1329,6 +1331,8 @@ type BackupConfig struct {
13291331
UploadBranch string `json:"upload_branch" datastore:"upload_branch"`
13301332
UploadUsername string `json:"upload_username" datastore:"upload_username"`
13311333
UploadToken string `json:"upload_token" datastore:"upload_token"`
1334+
1335+
TokensEncrypted bool `json:"tokens_encrypted" datastore:"tokens_encrypted"`
13321336
}
13331337

13341338
type Category struct {

0 commit comments

Comments
 (0)