From 6f945d1a6887884def9e07aa20fb876812890735 Mon Sep 17 00:00:00 2001 From: frikky Date: Mon, 18 Jan 2021 10:04:02 +0100 Subject: [PATCH] Fixed API authentication for files --- .env | 2 +- backend/go-app/files.go | 3 +- backend/go-app/main.go | 6 +-- backend/go-app/walkoff.go | 61 +++++++++++++++++++------- docker-compose.yml | 6 +-- frontend/Dockerfile | 7 +-- frontend/src/views/Admin.jsx | 37 +++++++++++----- frontend/src/views/AngularWorkflow.jsx | 11 ++++- 8 files changed, 95 insertions(+), 38 deletions(-) diff --git a/.env b/.env index 45389f1f..5fdb2e83 100644 --- a/.env +++ b/.env @@ -29,7 +29,7 @@ BACKEND_PORT=5001 FRONTEND_PORT=3001 FRONTEND_PORT_HTTPS=3443 OUTER_HOSTNAME=shuffle-backend -DB_LOCATION=./shuffle-database +DB_LOCATION=./shuffle-database-new # Proxy configurations. SHUFFLE_PASS_WORKER_PROXY must be FALSE to not pass the proxy information to sub-apps. # PS: It will skip proxy for diff --git a/backend/go-app/files.go b/backend/go-app/files.go index 7312af1f..0779a08c 100644 --- a/backend/go-app/files.go +++ b/backend/go-app/files.go @@ -701,7 +701,8 @@ func handleCreateFile(resp http.ResponseWriter, request *http.Request) { // Loads of validation below if len(curfile.Filename) == 0 || len(curfile.OrgId) == 0 || len(curfile.WorkflowId) == 0 { - log.Printf("[ERROR] Missing field during upload.") + log.Printf("[ERROR] Missing field during fileupload. Required: filename, org_id, workflow_id") + log.Printf("INPUT: %s", string(body)) resp.WriteHeader(401) resp.Write([]byte(fmt.Sprintf(`{"success": false, "reason": "Missing field. Required: filename, org_id, workflow_id"}`))) return diff --git a/backend/go-app/main.go b/backend/go-app/main.go index 6e304dd2..f19679e0 100644 --- a/backend/go-app/main.go +++ b/backend/go-app/main.go @@ -614,13 +614,13 @@ func handleApiAuthentication(resp http.ResponseWriter, request *http.Request) (U apikey := request.Header.Get("Authorization") if len(apikey) > 0 { if !strings.HasPrefix(apikey, "Bearer ") { - log.Printf("Apikey doesn't start with bearer") + log.Printf("[WARNING] Apikey doesn't start with bearer") return User{}, errors.New("No bearer token for authorization header") } apikeyCheck := strings.Split(apikey, " ") if len(apikeyCheck) != 2 { - log.Printf("Invalid format for apikey.") + log.Printf("[WARNING] Invalid format for apikey.") return User{}, errors.New("Invalid format for apikey") } @@ -643,7 +643,7 @@ func handleApiAuthentication(resp http.ResponseWriter, request *http.Request) (U if len(Userdata.Username) > 0 { return Userdata, nil } else { - return Userdata, errors.New(fmt.Sprintf("User is invalid - no username found")) + return Userdata, errors.New(fmt.Sprintf("[WARNING] User is invalid - no username found")) } } diff --git a/backend/go-app/walkoff.go b/backend/go-app/walkoff.go index 0c5e2fc4..b8804c39 100644 --- a/backend/go-app/walkoff.go +++ b/backend/go-app/walkoff.go @@ -909,8 +909,8 @@ func validateNewWorkerExecution(body []byte) error { return err } - log.Printf("LEN: %s", string(body)) - log.Printf("LEN: %d", len(string(body))) + //log.Printf("LEN: %s", string(body)) + //log.Printf("LEN: %d", len(string(body))) baseExecution, err := getWorkflowExecution(ctx, execution.ExecutionId) if err != nil { log.Printf("[ERROR] Failed getting execution (workflowqueue) %s: %s", execution.ExecutionId, err) @@ -1710,7 +1710,7 @@ func setNewWorkflow(resp http.ResponseWriter, request *http.Request) { user.ActiveOrg.Users = []User{} workflow.ExecutingOrg = user.ActiveOrg workflow.OrgId = user.ActiveOrg.Id - log.Printf("TRIGGERS: %d", len(workflow.Triggers)) + //log.Printf("TRIGGERS: %d", len(workflow.Triggers)) ctx := context.Background() //err = increaseStatisticsField(ctx, "total_workflows", workflow.ID, 1, workflow.OrgId) @@ -1744,7 +1744,7 @@ func setNewWorkflow(resp http.ResponseWriter, request *http.Request) { // Initialized without functions = adding a hello world node. if len(newActions) == 0 { - log.Printf("APPENDING NEW APP FOR NEW WORKFLOW") + //log.Printf("APPENDING NEW APP FOR NEW WORKFLOW") // Adds the Testing app if it's a new workflow workflowapps, err := getAllWorkflowApps(ctx) @@ -2188,8 +2188,10 @@ func saveWorkflow(resp http.ResponseWriter, request *http.Request) { workflowapps, apperr := getAllWorkflowApps(ctx) allAuths, err := getAllWorkflowAppAuth(ctx, user.ActiveOrg.Id) - if err == nil && len(allAuths) > 0 && len(workflowapps) > 0 && apperr == nil { + if err == nil && len(workflowapps) > 0 && apperr == nil { + log.Printf("Setting actions") actionFixing := []Action{} + appsAdded := []string{} for _, action := range newActions { setAuthentication := false if len(action.AuthenticationId) > 0 { @@ -2253,6 +2255,12 @@ func saveWorkflow(resp http.ResponseWriter, request *http.Request) { } } + for _, added := range appsAdded { + if outerapp.ID == added { + found = true + } + } + // FIXME: Add app auth if !found { timeNow := int64(time.Now().Unix()) @@ -2281,6 +2289,8 @@ func saveWorkflow(resp http.ResponseWriter, request *http.Request) { err = setWorkflowAppAuthDatastore(ctx, appAuth, appAuth.Id) if err != nil { log.Printf("Failed setting appauth for with name %s", appAuth.Label) + } else { + appsAdded = append(appsAdded, outerapp.ID) } } @@ -2299,14 +2309,16 @@ func saveWorkflow(resp http.ResponseWriter, request *http.Request) { } newActions = actionFixing + } else { + log.Printf("Err: %s - %s", err, apperr) + //workflowapps, apperr := getAllWorkflowApps(ctx) + //allAuths, err := getAllWorkflowAppAuth(ctx, user.ActiveOrg.Id) } - //workflow.PreviouslySaved = true + workflow.PreviouslySaved = true } - //PreviouslySaved bool `json:"first_save" datastore:"first_save"` workflow.Actions = newActions - newTriggers := []Trigger{} for _, trigger := range workflow.Triggers { log.Printf("Trigger %s: %s", trigger.TriggerType, trigger.Status) @@ -3421,15 +3433,19 @@ func handleExecution(id string, workflow Workflow, request *http.Request) (Workf return WorkflowExecution{}, "Failed building missing Docker images", err } - b, err := json.Marshal(workflowExecution) - if err == nil { - log.Printf("%s", string(b)) - log.Printf("LEN: %d", len(string(b))) - //workflowExecution.ExecutionOrg.SyncFeatures = Org{} - } + //b, err := json.Marshal(workflowExecution) + //if err == nil { + // log.Printf("%s", string(b)) + // log.Printf("LEN: %d", len(string(b))) + // //workflowExecution.ExecutionOrg.SyncFeatures = Org{} + //} - workflowExecution.Workflow.ExecutingOrg = Org{} - workflowExecution.Workflow.Org = []Org{} + workflowExecution.Workflow.ExecutingOrg = Org{ + Id: workflowExecution.Workflow.ExecutingOrg.Id, + } + workflowExecution.Workflow.Org = []Org{ + workflowExecution.Workflow.ExecutingOrg, + } //Org []Org `json:"org,omitempty" datastore:"org"` err = setWorkflowExecution(ctx, workflowExecution, true) if err != nil { @@ -4735,6 +4751,7 @@ func setAuthenticationConfig(resp http.ResponseWriter, request *http.Request) { } if config.Action == "assign_everywhere" { + log.Printf("Should set authentication config") q := datastore.NewQuery("workflow").Filter("org_id =", user.ActiveOrg.Id) q = q.Order("-edited").Limit(35) @@ -4750,14 +4767,21 @@ func setAuthenticationConfig(resp http.ResponseWriter, request *http.Request) { // FIXME: Add function to remove auth from other auth's actionCnt := 0 workflowCnt := 0 + authenticationUsage := []AuthenticationUsage{} for _, workflow := range workflows { newActions := []Action{} edited := false + usage := AuthenticationUsage{ + WorkflowId: workflow.ID, + Nodes: []string{}, + } + for _, action := range workflow.Actions { if action.AppName == auth.App.Name { //log.Printf("FOUND ACTION TO UPDATE: %#v", action) edited = true actionCnt += 1 + usage.Nodes = append(usage.Nodes, action.ID) } newActions = append(newActions, action) @@ -4765,6 +4789,8 @@ func setAuthenticationConfig(resp http.ResponseWriter, request *http.Request) { workflow.Actions = newActions if edited { + //auth.Usage = usage + authenticationUsage = append(authenticationUsage, usage) err = setWorkflow(ctx, workflow, workflow.ID) if err != nil { log.Printf("Failed setting (authupdate) workflow: %s", err) @@ -4775,9 +4801,12 @@ func setAuthenticationConfig(resp http.ResponseWriter, request *http.Request) { } } + //Usage []AuthenticationUsage `json:"usage" datastore:"usage"` + log.Printf("Found %d workflows, %d actions", workflowCnt, actionCnt) if actionCnt > 0 && workflowCnt > 0 { auth.WorkflowCount = int64(workflowCnt) auth.NodeCount = int64(actionCnt) + auth.Usage = authenticationUsage err = setWorkflowAppAuthDatastore(ctx, *auth, auth.Id) if err != nil { diff --git a/docker-compose.yml b/docker-compose.yml index c6632614..1ce245a3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,7 +1,7 @@ version: '3' services: frontend: - build: ./frontend + #build: ./frontend image: ghcr.io/frikky/shuffle-frontend:0.8.53 container_name: shuffle-frontend hostname: shuffle-frontend @@ -16,8 +16,8 @@ services: depends_on: - backend backend: - #build: ./backend - image: ghcr.io/frikky/shuffle-backend:0.8.53 + build: ./backend + image: ghcr.io/frikky/shuffle-backend:0.8.54 container_name: shuffle-backend hostname: ${BACKEND_HOSTNAME} # Here for debugging: diff --git a/frontend/Dockerfile b/frontend/Dockerfile index cc791b82..f48b048d 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -8,7 +8,8 @@ ENV PATH /usr/src/app/node_modules/.bin:$PATH COPY package.json /usr/src/app/package.json -RUN npm install --verbose +#RUN npm install --verbose +RUN yarn install # copy only required files to not trigger rebuilding every time COPY ./certs /usr/src/app/certs/ @@ -19,9 +20,9 @@ COPY ./*.json /usr/src/app/ # There were issues with the webpack installer from package.json RUN rm -rf /usr/src/app/node_modules/webpack -RUN npm install webpack@4.42.0 +#RUN yarn add webpack@4.42.0 -RUN npm run-script build +RUN yarn build # Production environment FROM nginx:latest diff --git a/frontend/src/views/Admin.jsx b/frontend/src/views/Admin.jsx index ae7ec058..bc94a537 100644 --- a/frontend/src/views/Admin.jsx +++ b/frontend/src/views/Admin.jsx @@ -301,6 +301,7 @@ const Admin = (props) => { } else { //alert.success("Successfully password!") setSelectedUserModalOpen(false) + getAppAuthentication() } }), ) @@ -762,6 +763,7 @@ const Admin = (props) => { .then((responseJson) => { if (responseJson.success) { //console.log(responseJson.data) + console.log(responseJson) setAuthentication(responseJson.data) } else { alert.error("Failed getting authentications") @@ -2033,7 +2035,7 @@ const Admin = (props) => {

App Authentication

Control the authentication options for individual apps. Actions can be destructive! - . Learn more +  Learn more
@@ -2087,7 +2089,7 @@ const Admin = (props) => { style={{minWidth: 150, maxWidth: 150}} /> { > - { - editAuthenticationConfig(data.id) - }} - > - - + {data.defined ? + + { + editAuthenticationConfig(data.id) + }} + > + + + + : + + { + }} + > + + + + } { deleteAuthentication(data) diff --git a/frontend/src/views/AngularWorkflow.jsx b/frontend/src/views/AngularWorkflow.jsx index a1dead44..d860d561 100644 --- a/frontend/src/views/AngularWorkflow.jsx +++ b/frontend/src/views/AngularWorkflow.jsx @@ -948,7 +948,16 @@ const AngularWorkflow = (props) => { }) .then((responseJson) => { if (responseJson.success) { - setAppAuthentication(responseJson.data) + var newauth = [] + for (var key in responseJson.data) { + if (responseJson.data[key].defined === false) { + continue + } + + newauth.push(responseJson.data[key]) + } + + setAppAuthentication(newauth) } else { alert.error("Failed getting authentications") }