Skip to content

Commit

Permalink
Merge pull request #190 from onattech/DA-202_Connect_Editor_To_Backend
Browse files Browse the repository at this point in the history
DA-202: Connect code editor to backend
  • Loading branch information
saul-data authored Mar 19, 2022
2 parents b854fa3 + 1711612 commit a5b2e53
Show file tree
Hide file tree
Showing 17 changed files with 692 additions and 176 deletions.
280 changes: 255 additions & 25 deletions app/mainapp/graphql/private/generated.go

Large diffs are not rendered by default.

18 changes: 17 additions & 1 deletion app/mainapp/graphql/private/resolvers/code_editor.graphqls
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
scalar Upload

type CodeFolders {
folderID: String!
parentID: String!
Expand Down Expand Up @@ -33,5 +35,19 @@ extend type Mutation {
+ **Route**: Private
+ **Permissions**: admin_platform, platform_environment, specific_pipeline[write]
"""
updateFilesNode(input:[FilesNodeInput]!): String!
updateFilesNode(input:FilesNodeInput): String!

"""
Upload a node file.
+ **Route**: Private
+ **Permissions**: admin_platform, platform_environment, specific_pipeline[write]
"""
uploadFileNode(environmentID: String!, nodeID: String!, pipelineID: String!, file:Upload!): String!

"""
Run script.
+ **Route**: Private
+ **Permissions**: admin_platform, platform_environment, specific_pipeline[write]
"""
codeEditorRun(environmentID: String!, nodeID: String!, pipelineID: String!, path:String!): String!
}
104 changes: 80 additions & 24 deletions app/mainapp/graphql/private/resolvers/code_editor.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,31 @@ package privateresolvers
// will be copied through when generating and any unknown code will be moved to the end.

import (
"bufio"
"context"
permissions "dataplane/mainapp/auth_permissions"
"dataplane/mainapp/database"
"dataplane/mainapp/database/models"
privategraphql "dataplane/mainapp/graphql/private"
"dataplane/mainapp/logging"
"errors"
"log"
"os"

"github.com/99designs/gqlgen/graphql"
"gorm.io/gorm/clause"
)

func (r *mutationResolver) UpdateFilesNode(ctx context.Context, input []*privategraphql.FilesNodeInput) (string, error) {
func (r *mutationResolver) UpdateFilesNode(ctx context.Context, input *privategraphql.FilesNodeInput) (string, error) {
currentUser := ctx.Value("currentUser").(string)
platformID := ctx.Value("platformID").(string)

// ----- Permissions
perms := []models.Permissions{
{Subject: "user", SubjectID: currentUser, Resource: "admin_platform", ResourceID: platformID, Access: "write", EnvironmentID: "d_platform"},
{Subject: "user", SubjectID: currentUser, Resource: "platform_environment", ResourceID: platformID, Access: "write", EnvironmentID: input[0].EnvironmentID},
{Subject: "user", SubjectID: currentUser, Resource: "environment_edit_all_pipelines", ResourceID: platformID, Access: "write", EnvironmentID: input[0].EnvironmentID},
{Subject: "user", SubjectID: currentUser, Resource: "specific_pipeline", ResourceID: input[0].PipelineID, Access: "write", EnvironmentID: input[0].EnvironmentID},
{Subject: "user", SubjectID: currentUser, Resource: "platform_environment", ResourceID: platformID, Access: "write", EnvironmentID: input.EnvironmentID},
{Subject: "user", SubjectID: currentUser, Resource: "environment_edit_all_pipelines", ResourceID: platformID, Access: "write", EnvironmentID: input.EnvironmentID},
{Subject: "user", SubjectID: currentUser, Resource: "specific_pipeline", ResourceID: input.PipelineID, Access: "write", EnvironmentID: input.EnvironmentID},
}

permOutcome, _, _, _ := permissions.MultiplePermissionChecks(perms)
Expand All @@ -36,32 +39,85 @@ func (r *mutationResolver) UpdateFilesNode(ctx context.Context, input []*private

// ----- Add node files to database

for _, p := range input {

f := &models.CodeFolders{
EnvironmentID: p.EnvironmentID,
PipelineID: p.PipelineID,
NodeID: p.NodeID,
FolderID: p.FolderID,
ParentID: p.ParentID,
FolderName: p.FolderName,
FType: p.FType,
Level: "node",
Active: p.Active,
}
f := &models.CodeFolders{
EnvironmentID: input.EnvironmentID,
PipelineID: input.PipelineID,
NodeID: input.NodeID,
FolderID: input.FolderID,
ParentID: input.ParentID,
FolderName: input.FolderName,
FType: input.FType,
Level: "node",
Active: input.Active,
}

err := database.DBConn.Clauses(clause.OnConflict{UpdateAll: true}).Where("folder_id = ?", p.FolderID).
Create(&f).Error
err := database.DBConn.Clauses(clause.OnConflict{UpdateAll: true}).Where("folder_id = ?", input.FolderID).
Create(&f).Error

if err != nil {
if os.Getenv("debug") == "true" {
logging.PrintSecretsRedact(err)
}
return "", errors.New("update files node database error.")
if err != nil {
if os.Getenv("debug") == "true" {
logging.PrintSecretsRedact(err)
}
return "", errors.New("update files node database error.")
}

return "Success", nil
}

func (r *mutationResolver) UploadFileNode(ctx context.Context, environmentID string, nodeID string, pipelineID string, file graphql.Upload) (string, error) {
currentUser := ctx.Value("currentUser").(string)
platformID := ctx.Value("platformID").(string)

// ----- Permissions
perms := []models.Permissions{
{Subject: "user", SubjectID: currentUser, Resource: "admin_platform", ResourceID: platformID, Access: "write", EnvironmentID: "d_platform"},
{Subject: "user", SubjectID: currentUser, Resource: "platform_environment", ResourceID: platformID, Access: "write", EnvironmentID: environmentID},
{Subject: "user", SubjectID: currentUser, Resource: "environment_edit_all_pipelines", ResourceID: platformID, Access: "write", EnvironmentID: environmentID},
{Subject: "user", SubjectID: currentUser, Resource: "specific_pipeline", ResourceID: pipelineID, Access: "write", EnvironmentID: environmentID},
}

permOutcome, _, _, _ := permissions.MultiplePermissionChecks(perms)

if permOutcome == "denied" {
return "", errors.New("Requires permissions.")
}

// Save to code-files
save, err := os.Create("../../code-files/" + file.Filename)
if err != nil {
log.Fatal(err)
}

writer := bufio.NewWriter(save)

p := make([]byte, file.Size)
file.File.Read(p)
writer.Write(p)
writer.Flush()

return "Success", nil
}

func (r *mutationResolver) CodeEditorRun(ctx context.Context, environmentID string, nodeID string, pipelineID string, path string) (string, error) {
currentUser := ctx.Value("currentUser").(string)
platformID := ctx.Value("platformID").(string)

// ----- Permissions
perms := []models.Permissions{
{Subject: "user", SubjectID: currentUser, Resource: "admin_platform", ResourceID: platformID, Access: "write", EnvironmentID: "d_platform"},
{Subject: "user", SubjectID: currentUser, Resource: "platform_environment", ResourceID: platformID, Access: "write", EnvironmentID: environmentID},
{Subject: "user", SubjectID: currentUser, Resource: "environment_edit_all_pipelines", ResourceID: platformID, Access: "write", EnvironmentID: environmentID},
{Subject: "user", SubjectID: currentUser, Resource: "specific_pipeline", ResourceID: pipelineID, Access: "write", EnvironmentID: environmentID},
}

permOutcome, _, _, _ := permissions.MultiplePermissionChecks(perms)

if permOutcome == "denied" {
return "", errors.New("Requires permissions.")
}

log.Println("Path: ", path)

return "Success", nil
}

Expand Down
13 changes: 13 additions & 0 deletions app/mainapp/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,19 @@ func Setup(port string) *fiber.App {
worker.RoomUpdates(c, environment, subject, id)
}))

// Download code files
app.Get("/app/private/code-files/:filename", func(c *fiber.Ctx) error {
filename := string(c.Params("filename"))
dat, err := os.ReadFile("../../code-files/" + filename)
if err != nil {
if os.Getenv("debug") == "true" {
logging.PrintSecretsRedact(err)
}
return err
}
return c.SendString(string(dat))
})

// Check healthz
app.Get("/healthz", func(c *fiber.Ctx) error {
return c.SendString("Hello 👋! Healthy 🍏")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const BashNode = (props) => {

<Box mt={0}>
<MoreInfoMenu iconHorizontal iconColor="#0073C6" iconColorDark="#0073C6" iconSize={19} noPadding>
{isEditorPage ? <ProcessTypeEditorModeItem /> : <ProcessTypeNodeItem nodeId={props.id} />}
{isEditorPage ? <ProcessTypeEditorModeItem /> : <ProcessTypeNodeItem nodeId={props.id} workerGroup={props.data.workerGroup} />}
</MoreInfoMenu>
</Box>
</Grid>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const PythonNode = (props) => {

<Box mt={0}>
<MoreInfoMenu iconHorizontal iconColor="#0073C6" iconColorDark="#0073C6" iconSize={19} noPadding>
{isEditorPage ? <ProcessTypeEditorModeItem /> : <ProcessTypeNodeItem nodeId={props.id} />}
{isEditorPage ? <ProcessTypeEditorModeItem /> : <ProcessTypeNodeItem nodeId={props.id} workerGroup={props.data.workerGroup} />}
</MoreInfoMenu>
</Box>
</Grid>
Expand Down
Loading

0 comments on commit a5b2e53

Please sign in to comment.