Skip to content
This repository has been archived by the owner on Jan 24, 2025. It is now read-only.

Commit

Permalink
Add verification service, service factory, and cloud provider control…
Browse files Browse the repository at this point in the history
…lers

Implement certificate generation and PDF generator
  • Loading branch information
austinsonger committed Dec 26, 2023
1 parent 1ad26aa commit 3ff4020
Show file tree
Hide file tree
Showing 13 changed files with 301 additions and 45 deletions.
37 changes: 25 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
FROM golang:1.19.4-alpine3.17 as builder

RUN apk update && apk add git make bash
# Start from the official Golang image to build the binary.
FROM golang:alpine as builder

# Enable Go modules.
ENV GO111MODULE=on

# Set the working directory outside $GOPATH to enable the support for modules.
WORKDIR /app

COPY . ./
# Copy go mod and sum files to download dependencies.
COPY go.mod go.sum ./

# Download all dependencies.
RUN go mod download

# Do dep installs outside, due to private git modules
# RUN make dep
# Copy the source from the current directory to the working directory inside the container.
COPY . .

RUN make build
# Build the Go app.
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o main .

# Start a new stage from scratch for a smaller final image.
FROM alpine:latest

WORKDIR /app
RUN apk --no-cache add ca-certificates

WORKDIR /root/

COPY --from=builder /app/main /app/
COPY --from=builder /app/public /app/public
COPY --from=builder /app/dist /app/dist
# Copy the pre-built binary file from the previous stage.
COPY --from=builder /app/main .

EXPOSE 4001
# Command to run the executable.
CMD ["./main"]

CMD [ "/app/main" ]
# Expose port 8080 to the outside world.
EXPOSE 8080
32 changes: 32 additions & 0 deletions api/handlers/certificate_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package handlers

import (
"net/http"

"github.com/labstack/echo/v4"
// Import your service packages
)

// CertificateController handles certificate related requests
type CertificateController struct {
// fields for services
}

// NewCertificateController creates a new instance
func NewCertificateController() *CertificateController {
return &CertificateController{
// Initialize fields
}
}

// GenerateCertificate handles requests to generate a destruction certificate
func (ctrl *CertificateController) GenerateCertificate(c echo.Context) error {
// certificate generation logic
return c.JSON(http.StatusOK, certificate)
}

// GetCertificate retrieves a specific destruction certificate
func (ctrl *CertificateController) GetCertificate(c echo.Context) error {
// logic to retrieve a certificate
return c.JSON(http.StatusOK, certificate)
}
32 changes: 32 additions & 0 deletions api/handlers/cloud_providers_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package handlers

import (
"net/http"

"github.com/labstack/echo/v4"
// Import your service packages
)

// CloudProvidersController handles requests related to cloud providers
type CloudProvidersController struct {
// fields like service instances
}

// NewCloudProvidersController creates a new controller instance
func NewCloudProvidersController() *CloudProvidersController {
return &CloudProvidersController{
// fields
}
}

// ListProviders handles GET requests to list cloud providers
func (ctrl *CloudProvidersController) ListProviders(c echo.Context) error {
// logic to list cloud providers
return c.JSON(http.StatusOK, providers)
}

// InitiateDestruction handles POST requests to initiate data destruction
func (ctrl *CloudProvidersController) InitiateDestruction(c echo.Context) error {
// logic to start data destruction
return c.JSON(http.StatusOK, response)
}
26 changes: 26 additions & 0 deletions api/handlers/verification_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package handlers

import (
"net/http"

"github.com/labstack/echo/v4"
// service packages
)

// VerificationController handles requests related to verification of data destruction
type VerificationController struct {
// fields for services
}

// NewVerificationController creates a new controller instance
func NewVerificationController() *VerificationController {
return &VerificationController{
// Initialize fields
}
}

// VerifyDestruction handles requests to verify data destruction
func (ctrl *VerificationController) VerifyDestruction(c echo.Context) error {
// verification logic
return c.JSON(http.StatusOK, verificationResult)
}
21 changes: 21 additions & 0 deletions api/services/aws_verification_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package services

// Import AWS SDK

// AWSVerificationService is an implementation of VerificationService for AWS
type AWSVerificationService struct {
// Add necessary fields, like AWS client
}

func NewAWSVerificationService( /* params */ ) *AWSVerificationService {
return &AWSVerificationService{
// Initialize fields
}
}

// VerifyDestruction checks if data has been completely destroyed on AWS
func (svc *AWSVerificationService) VerifyDestruction(cloudProvider, identifier string) (bool, error) {
// Implement logic to verify destruction on AWS
// For example, check if an S3 object still exists
return false, nil
}
21 changes: 21 additions & 0 deletions api/services/azure_verification_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package services

// Import Azure SDK

// AzureVerificationService is an implementation of VerificationService for Azure
type AzureVerificationService struct {
// necessary fields, like Azure client
}

func NewAzureVerificationService( /* params */ ) *AzureVerificationService {
return &AzureVerificationService{
// Initialize fields
}
}

// VerifyDestruction checks if data has been completely destroyed on Azure
func (svc *AzureVerificationService) VerifyDestruction(cloudProvider, identifier string) (bool, error) {
// logic to verify destruction on Azure

return false, nil
}
21 changes: 21 additions & 0 deletions api/services/gcp_verification_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package services

// Import GCP SDK

// GCPVerificationService is an implementation of VerificationService for GCP
type GCPVerificationService struct {
// necessary fields, like GCP client
}

func NewGCPVerificationService( /* params */ ) *GCPVerificationService {
return &GCPVerificationService{
// Initialize fields
}
}

// VerifyDestruction checks if data has been completely destroyed on GCP
func (svc *GCPVerificationService) VerifyDestruction(cloudProvider, identifier string) (bool, error) {
// logic to verify destruction on GCP

return false, nil
}
17 changes: 17 additions & 0 deletions api/services/service_factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package services

import "fmt"

// GetVerificationService returns the appropriate verification service for a given cloud provider
func GetVerificationService(cloudProvider string) (VerificationService, error) {
switch cloudProvider {
case "aws":
return NewAWSVerificationService( /* params */ ), nil
case "gcp":
return NewGCPVerificationService( /* params */ ), nil
case "azure":
return NewAzureVerificationService( /* params */ ), nil
default:
return nil, fmt.Errorf("unsupported cloud provider: %s", cloudProvider)
}
}
6 changes: 6 additions & 0 deletions api/services/verification_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package services

// VerificationService defines the interface for a service that verifies data destruction
type VerificationService interface {
VerifyDestruction(cloudProvider, identifier string) (bool, error)
}
38 changes: 38 additions & 0 deletions certificates/generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package certificates

import (
"bytes"
"time"
// Import packages for PDF or document generation
)

// DestructionCertificate contains the details for the data destruction certificate
type DestructionCertificate struct {
CloudProvider string
Service string
DataType string
DestroyedAt time.Time
AdditionalInfo map[string]string
}

// CertificateGenerator defines the interface for certificate generation
type CertificateGenerator interface {
Generate(certificate DestructionCertificate) ([]byte, error)
}

// PDFGenerator implements CertificateGenerator for PDF certificates
type PDFGenerator struct {
}

// NewPDFGenerator creates a new instance of PDFGenerator
func NewPDFGenerator() *PDFGenerator {
return &PDFGenerator{}
}

// Generate creates a PDF certificate from the given DestructionCertificate details
func (g *PDFGenerator) Generate(certificate DestructionCertificate) ([]byte, error) {
var buf bytes.Buffer
// Implement PDF generation logic, populating the buffer with the PDF data

return buf.Bytes(), nil
}
26 changes: 17 additions & 9 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
version: '3.7'

version: '3.8'

services:
postgres:
image: postgres:13-alpine
container_name: gotes-mx-pg
restart: always
environment:
- POSTGRES_HOST_AUTH_METHOD=trust
app:
build: .
ports:
- '5757:5432'
- "8080:8080"
volumes:
- .:/app
- /app/node_modules
environment:
- GO_ENV=production
command: ./main
frontend:
image: nginx:alpine
volumes:
- './postgres/data:/var/lib/postgresql/data'
- ./dist:/usr/share/nginx/html:ro
ports:
- "80:80"
57 changes: 33 additions & 24 deletions utils/errs/errs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,55 @@ import (
"strings"
)

// Wrap is wrapper that includes error trace in errors
func Wrap(any interface{}, a ...interface{}) error {
if any != nil {
err := error(nil)
// StackTraceError includes the original error and its stack trace
type StackTraceError struct {
Err error
StackTrace string
}

switch any := any.(type) {
case string:
err = fmt.Errorf(any, a...)
case error:
err = fmt.Errorf(any.Error(), a...)
default:
err = fmt.Errorf("%v", err)
}
// Error implements the error interface for StackTraceError
func (e StackTraceError) Error() string {
return fmt.Sprintf("%s: %v", e.StackTrace, e.Err)
}

_, fn, line, _ := runtime.Caller(1)
// Wrap enriches the given error with a stack trace. It can take either an error or a string.
func Wrap(any interface{}, a ...interface{}) error {
if any == nil {
return nil
}

return fmt.Errorf("%s:%d %v", fn, line, err)
var err error
switch v := any.(type) {
case string:
err = fmt.Errorf(v, a...)
case error:
err = v
default:
err = fmt.Errorf("%v", v)
}

return nil
_, fn, line, _ := runtime.Caller(1)
stackTrace := fmt.Sprintf("%s:%d", fn, line)

return StackTraceError{Err: err, StackTrace: stackTrace}
}

// Errorf is wrapper to create new error along with error trace
func Errorf(format string, a ...any) error {
// Errorf creates a new error with a stack trace.
func Errorf(format string, a ...interface{}) error {
err := fmt.Errorf(format, a...)
_, fn, line, _ := runtime.Caller(1)

return fmt.Errorf("%s:%d %v", fn, line, err)
return StackTraceError{Err: err, StackTrace: fmt.Sprintf("%s:%d", fn, line)}
}

// ParseError is a method to parse error code and error message from a single error trace.
// Please make sure that the error have following format:
// <<ErrorCode>>: <<ErrorMessage>>
// Example:
// PaymentUnauthorized: payment cannot be authorized
// ParseError extracts the error code and message, assuming a specific format.
func ParseError(err error) (code, message string) {
splits := strings.Split(err.Error(), ":")
splits := strings.SplitN(err.Error(), ": ", 2)
if len(splits) != 2 {
return "GeneralError", err.Error()
}

return strings.TrimSpace(splits[0]), strings.TrimSpace(splits[1])
}

// Attach additional helper functions or custom error types as needed.
Loading

0 comments on commit 3ff4020

Please sign in to comment.