Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tn/add api to web #898

Merged
merged 4 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions backend/bin/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ func main() {
)
})

r.Route("/api", func(r chi.Router) {
server.API(r,
db, contentStore, logger,
)
})

logger.Log("msg", "starting Web server", "port", config.Port())
serveErr := http.ListenAndServe(":"+config.Port(), r)
logging.Fatal(logger, "msg", "server shutting down", "err", serveErr)
Expand Down
56 changes: 1 addition & 55 deletions backend/server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/ashirt-ops/ashirt-server/backend"
"github.com/ashirt-ops/ashirt-server/backend/contentstore"
"github.com/ashirt-ops/ashirt-server/backend/database"
"github.com/ashirt-ops/ashirt-server/backend/dtos"
"github.com/ashirt-ops/ashirt-server/backend/logging"
"github.com/ashirt-ops/ashirt-server/backend/server/middleware"
"github.com/ashirt-ops/ashirt-server/backend/services"
Expand All @@ -25,44 +24,12 @@ func API(r chi.Router, db *database.Connection, contentStore contentstore.Store,
r.Group(func(r chi.Router) {
r.Use(middleware.AuthenticateAppAndInjectCtx(db))
r.Use(middleware.LogRequests(logger))
bindSharedRoutes(r, db, contentStore)
bindAPIRoutes(r, db, contentStore)
})
}

func bindAPIRoutes(r chi.Router, db *database.Connection, contentStore contentstore.Store) {
route(r, "GET", "/operations", jsonHandler(func(r *http.Request) (interface{}, error) {
return services.ListOperations(r.Context(), db)
}))

route(r, "GET", "/checkconnection", jsonHandler(func(r *http.Request) (interface{}, error) {
return dtos.CheckConnection{Ok: true}, nil
}))

route(r, "POST", "/operations", jsonHandler(func(r *http.Request) (interface{}, error) {
dr := dissectJSONRequest(r)
i := services.CreateOperationInput{
Slug: dr.FromBody("slug").Required().AsString(),
Name: dr.FromBody("name").Required().AsString(),
OwnerID: middleware.UserID(r.Context()),
}
if dr.Error != nil {
return nil, dr.Error
}
return services.CreateOperation(r.Context(), db, i)
}))

route(r, "GET", "/operations/{operation_slug}/evidence/{evidence_uuid}", jsonHandler(func(r *http.Request) (interface{}, error) {
dr := dissectJSONRequest(r)
i := services.ReadEvidenceInput{
EvidenceUUID: dr.FromURL("evidence_uuid").Required().AsString(),
OperationSlug: dr.FromURL("operation_slug").Required().AsString(),
}
if dr.Error != nil {
return nil, dr.Error
}
return services.ReadEvidence(r.Context(), db, contentStore, i)
}))

route(r, "GET", "/operations/{operation_slug}/evidence/{evidence_uuid}/{type:media|preview}", mediaHandler(func(r *http.Request) (io.Reader, error) {
dr := dissectNoBodyRequest(r)
i := services.ReadEvidenceInput{
Expand Down Expand Up @@ -139,25 +106,4 @@ func bindAPIRoutes(r chi.Router, db *database.Connection, contentStore contentst
}
return nil, services.UpsertEvidenceMetadata(r.Context(), db, i)
}))

route(r, "GET", "/operations/{operation_slug}/tags", jsonHandler(func(r *http.Request) (interface{}, error) {
dr := dissectJSONRequest(r)
i := services.ListTagsForOperationInput{
OperationSlug: dr.FromURL("operation_slug").Required().AsString(),
}
return services.ListTagsForOperation(r.Context(), db, i)
}))

route(r, "POST", "/operations/{operation_slug}/tags", jsonHandler(func(r *http.Request) (interface{}, error) {
dr := dissectJSONRequest(r)
i := services.CreateTagInput{
Name: dr.FromBody("name").Required().AsString(),
ColorName: dr.FromBody("colorName").AsString(),
OperationSlug: dr.FromURL("operation_slug").Required().AsString(),
}
if dr.Error != nil {
return nil, dr.Error
}
return services.CreateTag(r.Context(), db, i)
}))
}
59 changes: 59 additions & 0 deletions backend/server/shared_routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2023, Yahoo Inc.
// Licensed under the terms of the MIT. See LICENSE file in project root for terms.

package server

import (
"net/http"

"github.com/ashirt-ops/ashirt-server/backend/contentstore"
"github.com/ashirt-ops/ashirt-server/backend/database"
"github.com/ashirt-ops/ashirt-server/backend/dtos"
"github.com/ashirt-ops/ashirt-server/backend/server/middleware"
"github.com/ashirt-ops/ashirt-server/backend/services"
"github.com/go-chi/chi/v5"
)

func bindSharedRoutes(r chi.Router, db *database.Connection, contentStore contentstore.Store) {
route(r, "GET", "/checkconnection", jsonHandler(func(r *http.Request) (interface{}, error) {
TylerNoblett marked this conversation as resolved.
Show resolved Hide resolved
return dtos.CheckConnection{Ok: true}, nil
}))

route(r, "GET", "/operations", jsonHandler(func(r *http.Request) (interface{}, error) {
return services.ListOperations(r.Context(), db)
}))

route(r, "POST", "/operations", jsonHandler(func(r *http.Request) (interface{}, error) {
dr := dissectJSONRequest(r)
i := services.CreateOperationInput{
Slug: dr.FromBody("slug").Required().AsString(),
Name: dr.FromBody("name").Required().AsString(),
OwnerID: middleware.UserID(r.Context()),
}
if dr.Error != nil {
return nil, dr.Error
}
return services.CreateOperation(r.Context(), db, i)
}))

route(r, "GET", "/operations/{operation_slug}/tags", jsonHandler(func(r *http.Request) (interface{}, error) {
dr := dissectJSONRequest(r)
i := services.ListTagsForOperationInput{
OperationSlug: dr.FromURL("operation_slug").Required().AsString(),
}
return services.ListTagsForOperation(r.Context(), db, i)
}))

route(r, "POST", "/operations/{operation_slug}/tags", jsonHandler(func(r *http.Request) (interface{}, error) {
dr := dissectJSONRequest(r)
i := services.CreateTagInput{
Name: dr.FromBody("name").Required().AsString(),
ColorName: dr.FromBody("colorName").AsString(),
OperationSlug: dr.FromURL("operation_slug").Required().AsString(),
}
if dr.Error != nil {
return nil, dr.Error
}
return services.CreateTag(r.Context(), db, i)
}))
}
39 changes: 1 addition & 38 deletions backend/server/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func Web(r chi.Router, db *database.Connection, contentStore contentstore.Store,
}
}

bindSharedRoutes(r, db, contentStore)
bindWebRoutes(r, db, contentStore, sessionStore, &authsWithOutRecovery)
})
}
Expand Down Expand Up @@ -278,27 +279,10 @@ func bindWebRoutes(r chi.Router, db *database.Connection, contentStore contentst
return nil, services.DeleteAuthSchemeUsers(r.Context(), db, schemeCode)
}))

route(r, "GET", "/operations", jsonHandler(func(r *http.Request) (interface{}, error) {
return services.ListOperations(r.Context(), db)
}))

route(r, "GET", "/admin/operations", jsonHandler(func(r *http.Request) (interface{}, error) {
return services.ListOperationsForAdmin(r.Context(), db)
}))

route(r, "POST", "/operations", jsonHandler(func(r *http.Request) (interface{}, error) {
dr := dissectJSONRequest(r)
i := services.CreateOperationInput{
Slug: dr.FromBody("slug").Required().AsString(),
Name: dr.FromBody("name").Required().AsString(),
OwnerID: middleware.UserID(r.Context()),
}
if dr.Error != nil {
return nil, dr.Error
}
return services.CreateOperation(r.Context(), db, i)
}))

route(r, "DELETE", "/operations/{operation_slug}", jsonHandler(func(r *http.Request) (interface{}, error) {
dr := dissectJSONRequest(r)
operationSlug := dr.FromURL("operation_slug").Required().AsString()
Expand Down Expand Up @@ -744,27 +728,6 @@ func bindWebRoutes(r chi.Router, db *database.Connection, contentStore contentst
return nil, services.DeleteQuery(r.Context(), db, i)
}))

route(r, "GET", "/operations/{operation_slug}/tags", jsonHandler(func(r *http.Request) (interface{}, error) {
dr := dissectJSONRequest(r)
i := services.ListTagsForOperationInput{
OperationSlug: dr.FromURL("operation_slug").Required().AsString(),
}
return services.ListTagsForOperation(r.Context(), db, i)
}))

route(r, "POST", "/operations/{operation_slug}/tags", jsonHandler(func(r *http.Request) (interface{}, error) {
dr := dissectJSONRequest(r)
i := services.CreateTagInput{
Name: dr.FromBody("name").Required().AsString(),
ColorName: dr.FromBody("colorName").AsString(),
OperationSlug: dr.FromURL("operation_slug").Required().AsString(),
}
if dr.Error != nil {
return nil, dr.Error
}
return services.CreateTag(r.Context(), db, i)
}))

route(r, "PUT", "/operations/{operation_slug}/tags/{tag_id}", jsonHandler(func(r *http.Request) (interface{}, error) {
dr := dissectJSONRequest(r)
i := services.UpdateTagInput{
Expand Down
19 changes: 2 additions & 17 deletions docker-compose-prod.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version: "3"
services:
ashirt-private-service:
ashirt-service:
build:
context: .
dockerfile: Dockerfile.prod.web
Expand All @@ -23,21 +23,6 @@ services:
AUTH_SERVICES: ashirt
DB_URI: dev-user:dev-user-password@tcp(db:3306)/dev-db


ashirt-public-service:
build:
context: .
dockerfile: Dockerfile.prod.api
ports:
- 8001:8000
restart: on-failure
environment:
APP_IMGSTORE_BUCKET_NAME: ""
APP_IMGSTORE_REGION: ""
APP_PORT: 8000
DB_URI: dev-user:dev-user-password@tcp(db:3306)/dev-db


frontend:
build:
context: .
Expand All @@ -46,7 +31,7 @@ services:
- 8080:8080
environment:
NGINX_PORT: 8080
WEB_URL: http://ashirt-private-service:8000
WEB_URL: http://ashirt-service:8000

db:
image: mysql:8.0
Expand Down
4 changes: 4 additions & 0 deletions frontend/default.conf.template
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ server {
proxy_pass ${WEB_URL};
}

location /api {
proxy_pass ${WEB_URL};
}

location /assets {
root /usr/share/nginx/html;
try_files $uri $uri/;
Expand Down
1 change: 1 addition & 0 deletions frontend/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ module.exports = (env, argv) => ({
historyApiFallback: true,
proxy: {
'/web': {target: process.env.WEB_BACKEND_ORIGIN},
'/api': {target: process.env.WEB_BACKEND_ORIGIN},
},
devMiddleware: {
publicPath: "/assets/"
Expand Down