Skip to content

Commit

Permalink
feat: add backend
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamadch91 committed Nov 19, 2024
0 parents commit 29bf259
Show file tree
Hide file tree
Showing 30 changed files with 3,178 additions and 0 deletions.
30 changes: 30 additions & 0 deletions backend/DockerFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Stage 1: Build stage
FROM golang:1.23-alpine AS build

# Set the working directory
WORKDIR /app

# Copy and download dependencies
COPY go.mod go.sum ./
RUN go mod download

# Copy the source code
COPY . .

# Build the Go application
RUN go build -o schema .

# Stage 2: Final stage
FROM alpine:edge

# Set the working directory
WORKDIR /app

# Copy the binary from the build stage
COPY --from=build /app/schema .

# Set the timezone and install CA certificates
RUN apk --no-cache add ca-certificates tzdata

# Set the entrypoint command
ENTRYPOINT ["/app/schema"]
43 changes: 43 additions & 0 deletions backend/api/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package api

import (
"github.com/labstack/echo/v4"
"github.com/opengovern/schema/config"
"github.com/opengovern/schema/service"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
)

type API struct {
cfg config.SchemaConfig
tracer trace.Tracer
logger *zap.Logger
informationService *service.SchemaService
}

func New(cfg config.SchemaConfig, logger *zap.Logger, informationService *service.SchemaService) API {
return API{
cfg: cfg,
informationService: informationService,
tracer: otel.GetTracerProvider().Tracer("information.http.sources"),
logger: logger.Named("information-api"),
}
}

func (s API) Register(e *echo.Echo) {
// g := e.Group("/api/v1/schema")
}



func bindValidate(ctx echo.Context, i any) error {
if err := ctx.Bind(i); err != nil {
return err
}
if err := ctx.Validate(i); err != nil {
return err
}

return nil
}
43 changes: 43 additions & 0 deletions backend/api/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package api

import (
"strings"

"github.com/opengovern/schema/db/models"
)



const (
ComplianceResultSeverityNone db.ComplianceResultSeverity = "none"
ComplianceResultSeverityLow db.ComplianceResultSeverity = "low"
ComplianceResultSeverityMedium db.ComplianceResultSeverity = "medium"
ComplianceResultSeverityHigh db.ComplianceResultSeverity = "high"
ComplianceResultSeverityCritical db.ComplianceResultSeverity = "critical"
)


var complianceResultSeveritiesSeverities = []db.ComplianceResultSeverity{
ComplianceResultSeverityNone,
ComplianceResultSeverityLow,
ComplianceResultSeverityMedium,
ComplianceResultSeverityHigh,
ComplianceResultSeverityCritical,
}
func ParseComplianceResultSeverity(s string) db.ComplianceResultSeverity {
s = strings.ToLower(s)
for _, sev := range complianceResultSeveritiesSeverities {
if s == strings.ToLower(sev.String()) {
return sev
}
}
return ""
}

func ParseComplianceResultSeverities(list []string) []db.ComplianceResultSeverity {
result := make([]db.ComplianceResultSeverity, 0, len(list))
for _, s := range list {
result = append(result, ParseComplianceResultSeverity(s))
}
return result
}
8 changes: 8 additions & 0 deletions backend/config/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package config

import "github.com/opengovern/og-util/pkg/koanf"

type SchemaConfig struct {
Postgres koanf.Postgres `json:"postgres,omitempty" koanf:"postgres"`
Http koanf.HttpServer `json:"http,omitempty" koanf:"http"`
}
10 changes: 10 additions & 0 deletions backend/db/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package db

import (

"gorm.io/gorm"
)

type Database struct {
Orm *gorm.DB
}
43 changes: 43 additions & 0 deletions backend/db/models/controls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package db

import (
"time"

"github.com/lib/pq"
"github.com/opengovern/og-util/pkg/model"

)




type ControlTagsResult struct {
Key string
UniqueValues pq.StringArray `gorm:"type:text[]"`
}

type Control struct {
ID string `gorm:"primaryKey"`
Title string
Description string
Tags []ControlTag `gorm:"foreignKey:ControlID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
tagsMap map[string][]string `gorm:"-:all"`
IntegrationType pq.StringArray `gorm:"type:text[]"`
DocumentURI string
Enabled bool
QueryID *string
Query *Query `gorm:"foreignKey:QueryID;references:ID;constraint:OnDelete:SET NULL"`
Benchmarks []Benchmark `gorm:"many2many:benchmark_controls;"`
Severity ComplianceResultSeverity
ManualVerification bool
Managed bool
CreatedAt time.Time
UpdatedAt time.Time
}



type ControlTag struct {
model.Tag
ControlID string `gorm:"primaryKey"`
}
84 changes: 84 additions & 0 deletions backend/db/models/frameworks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package db

import (

"time"

"github.com/jackc/pgtype"


"github.com/lib/pq"
"github.com/opengovern/og-util/pkg/model"

"gorm.io/gorm"
)



type BenchmarkAssignment struct {
gorm.Model
BenchmarkId string `gorm:"index:idx_benchmark_source; index:idx_benchmark_rc; not null"`
IntegrationID *string `gorm:"index:idx_benchmark_source"`
ResourceCollection *string `gorm:"index:idx_benchmark_rc"`
AssignedAt time.Time
}

type BenchmarkAssignmentsCount struct {
BenchmarkId string
Count int
}

type BenchmarkMetadata struct {
IsRoot bool
Controls []string
PrimaryTables []string
ListOfTables []string
BenchmarkPath string
}

type Benchmark struct {
ID string `gorm:"primarykey"`
Title string
DisplayCode string
IntegrationType pq.StringArray `gorm:"type:text[]"`
Description string
LogoURI string
Category string
DocumentURI string
Enabled bool
AutoAssign bool
TracksDriftEvents bool
Metadata pgtype.JSONB

Tags []BenchmarkTag `gorm:"foreignKey:BenchmarkID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
tagsMap map[string][]string `gorm:"-:all"`

Children []Benchmark `gorm:"many2many:benchmark_children;"`
Controls []Control `gorm:"many2many:benchmark_controls;"`
CreatedAt time.Time
UpdatedAt time.Time
}

type BenchmarkChild struct {
BenchmarkID string
ChildID string
}

type BenchmarkTag struct {
model.Tag
BenchmarkID string `gorm:"primaryKey"`
}
type BenchmarkTagsResult struct {
Key string
UniqueValues pq.StringArray `gorm:"type:text[]"`
}

type BenchmarkControls struct {
BenchmarkID string
ControlID string
}

type ComplianceResultSeverity string
func (s ComplianceResultSeverity) String() string {
return string(s)
}
30 changes: 30 additions & 0 deletions backend/db/models/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package db

import (

"time"

"github.com/lib/pq"

)


type Query struct {
ID string `gorm:"primaryKey"`
QueryToExecute string
IntegrationType pq.StringArray `gorm:"type:text[]"`
PrimaryTable *string
ListOfTables pq.StringArray `gorm:"type:text[]"`
Engine string
Controls []Control `gorm:"foreignKey:QueryID"`
Parameters []QueryParameter `gorm:"foreignKey:QueryID"`
Global bool
CreatedAt time.Time
UpdatedAt time.Time
}

type QueryParameter struct {
QueryID string `gorm:"primaryKey"`
Key string `gorm:"primaryKey"`
Required bool `gorm:"not null"`
}
42 changes: 42 additions & 0 deletions backend/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
services:
schema-backend:
image: ghcr.io/opengovernance-io/schema-service:v0.0.9
container_name: schema-service
ports:
- "8080:8080"
environment:
- SCHEMA_HTTP__ADDRESS=0.0.0.0:8080
- SCHEMA_POSTGRES__HOST=postgres
- SCHEMA_POSTGRES__PORT=5432
- SCHEMA_POSTGRES__DB=schema
- SCHEMA_POSTGRES__USERNAME=schema
- SCHEMA_POSTGRES__PASSWORD=${PG_PASSWORD}
- SCHEMA_POSTGRES_SSL_MODE=disable
depends_on:
- postgres
networks:
- kaytu-network
restart: always
postgres:
image: postgres:14
container_name: postgres
volumes:
- pgdata:/var/lib/postgresql/data
environment:
- POSTGRES_USER=schema
- POSTGRES_PASSWORD=${PG_PASSWORD}
- POSTGRES_DB=schema
networks:
- kaytu-network
restart: always
volumes:
pgdata:
driver: local
driver_opts:
type: none
device: "/home/ec2-user/schema-service/pgdata"
o: bind

networks:
kaytu-network:
driver: bridge
Loading

0 comments on commit 29bf259

Please sign in to comment.