From a73e6625e25747bf8b7d937f6284bac5ca08b351 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 16 Jun 2024 13:06:48 +0200 Subject: [PATCH] chore: Update TLS configuration in main.go to use certificate and key files --- .github/workflows/deploy-golang-develop.yml | 13 ++- .github/workflows/deploy-golang-prod.yml | 13 ++- .github/workflows/lint-test-build-golang.yml | 23 ++++ occupi-backend/.env.gpg | Bin 0 -> 559 bytes occupi-backend/cert.pem.gpg | Bin 0 -> 1587 bytes occupi-backend/cmd/occupi-backend/main.go | 2 +- occupi-backend/key.pem.gpg | Bin 0 -> 2587 bytes occupi-backend/tests/handlers_test.go | 116 +++++++++++++------ 8 files changed, 128 insertions(+), 39 deletions(-) create mode 100644 occupi-backend/.env.gpg create mode 100644 occupi-backend/cert.pem.gpg create mode 100644 occupi-backend/key.pem.gpg diff --git a/.github/workflows/deploy-golang-develop.yml b/.github/workflows/deploy-golang-develop.yml index 152484f1..91e977ad 100644 --- a/.github/workflows/deploy-golang-develop.yml +++ b/.github/workflows/deploy-golang-develop.yml @@ -54,6 +54,18 @@ jobs: uses: actions/setup-go@v5 with: go-version: '1.21' # Specify the Go version you are using + + - name: Decrypt env variables + run: | + echo "${{ secrets.GPG_PASSPHRASE }}" | gpg --quiet --batch --yes --decrypt --passphrase-fd 0 .env.gpg > .env + + - name: Decrypt key file + run: | + echo "${{ secrets.GPG_PASSPHRASE }}" | gpg --quiet --batch --yes --decrypt --passphrase-fd 0 key.pem.gpg > key.pem + + - name: Decrypt cert file + run: | + echo "${{ secrets.GPG_PASSPHRASE }}" | gpg --quiet --batch --yes --decrypt --passphrase-fd 0 cert.pem.gpg > cert.pem - name: Run tests run: | @@ -114,7 +126,6 @@ jobs: file: occupi-backend/Dockerfile.dev platforms: linux/amd64,linux/arm64 push: true - no-cache: true tags: ${{ secrets.DOCKER_USERNAME }}/occupi-backend:latest-develop deploy: diff --git a/.github/workflows/deploy-golang-prod.yml b/.github/workflows/deploy-golang-prod.yml index 578d3c91..d130de75 100644 --- a/.github/workflows/deploy-golang-prod.yml +++ b/.github/workflows/deploy-golang-prod.yml @@ -47,6 +47,18 @@ jobs: with: go-version: '1.21' # Specify the Go version you are using + - name: Decrypt env variables + run: | + echo "${{ secrets.GPG_PASSPHRASE }}" | gpg --quiet --batch --yes --decrypt --passphrase-fd 0 .env.gpg > .env + + - name: Decrypt key file + run: | + echo "${{ secrets.GPG_PASSPHRASE }}" | gpg --quiet --batch --yes --decrypt --passphrase-fd 0 key.pem.gpg > key.pem + + - name: Decrypt cert file + run: | + echo "${{ secrets.GPG_PASSPHRASE }}" | gpg --quiet --batch --yes --decrypt --passphrase-fd 0 cert.pem.gpg > cert.pem + - name: Run tests run: | go test -v -coverpkg=github.com/COS301-SE-2024/occupi/occupi-backend/pkg/utils,github.com/COS301-SE-2024/occupi/occupi-backend/pkg/handlers ./tests/... -coverprofile=coverage.out @@ -106,7 +118,6 @@ jobs: file: occupi-backend/Dockerfile.prod platforms: linux/amd64,linux/arm64 push: true - no-cache: true tags: ${{ secrets.DOCKER_USERNAME }}/occupi-backend:latest deploy: diff --git a/.github/workflows/lint-test-build-golang.yml b/.github/workflows/lint-test-build-golang.yml index 14594537..67fe6784 100644 --- a/.github/workflows/lint-test-build-golang.yml +++ b/.github/workflows/lint-test-build-golang.yml @@ -11,6 +11,17 @@ on: "occupi-backend/tests/**", ".github/workflows/lint-test-build-golang.yml" ] + + push: + branches: ["feat/actions/adding-env-variables-for-backend"] + paths: [ + "occupi-backend/cmd/**", + "occupi-backend/configs/**", + "occupi-backend/pkg/**", + "occupi-backend/.golangci.yml", + "occupi-backend/tests/**", + ".github/workflows/lint-test-build-golang.yml" + ] workflow_dispatch: @@ -52,6 +63,18 @@ jobs: uses: actions/setup-go@v5 with: go-version: '1.21' # Specify the Go version you are using + + - name: Decrypt env variables + run: | + echo "${{ secrets.GPG_PASSPHRASE }}" | gpg --quiet --batch --yes --decrypt --passphrase-fd 0 .env.gpg > .env + + - name: Decrypt key file + run: | + echo "${{ secrets.GPG_PASSPHRASE }}" | gpg --quiet --batch --yes --decrypt --passphrase-fd 0 key.pem.gpg > key.pem + + - name: Decrypt cert file + run: | + echo "${{ secrets.GPG_PASSPHRASE }}" | gpg --quiet --batch --yes --decrypt --passphrase-fd 0 cert.pem.gpg > cert.pem - name: Run tests run: | diff --git a/occupi-backend/.env.gpg b/occupi-backend/.env.gpg new file mode 100644 index 0000000000000000000000000000000000000000..7dd85a2cbd8d777ea2bcd62de879a529caa6d3b4 GIT binary patch literal 559 zcmV+~0?_@84Fm}T0<01ZJCC(HkN?u?0qAaZg*jjQm|xb2*OjQxRMy+xh#UI^jLksh zzd(UzXotpNeOWcI{t?dl*Z6O@_CyQE2q$JLrzle$fFL>Ynn8+{1{Hjlj?KnQHk{RP zz5~(MLYwyLOv$It+*8^gZ~E}!*{+)dfJdWUTW|)eUk&Qtk)qXA=QPyk0<^SK$AuUw zOh;&_!Shu`L-BE=k_#{+=Syez)2smJx8nIPyiVWDs?Cb;f-YK*7i%d1?3ucufdk1xcOUG7 zViM`BXJ26=+n;*BLaNhF{Ul9-?MhI9m*z_5IrcF<5c6LMewUK&I-0kG8|`p-yHHHt zlUTxH86@5#@9)2?r=mg?!;%OX$+*s%egfP;Y!)sJu;%HDW{sd-unsVQOfe3c^Xao) zKmqc|E!o098@uoee#5Fkwt)Y-MY0w=-&$-MxnokTtw83120}5Ak}$mdL;rb@y2mbI zOX5++9}hfCc}7=5^Hsb-5>NLlUGbOsy`P7rH9^=Syjs1ef)LP-FdE7zw{Jg()Bpqv xM9Z>b(kpO}lk~bTKfB2Q9cI|U^M`h6N7;Tm1CQVzZriMK0Ep$i%*b@>LYWl$9^3!` literal 0 HcmV?d00001 diff --git a/occupi-backend/cert.pem.gpg b/occupi-backend/cert.pem.gpg new file mode 100644 index 0000000000000000000000000000000000000000..368c1b8325132264772b8e6e40a8cd76e9310320 GIT binary patch literal 1587 zcmV-32F&@44Fm}T0x5GDhaF7kRR7ZI0m6*nLy{#dOqdq{62qe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kBbY?p$dXA`z|I+agjL+A?-Jzk$u~&26PDj6QKaWTlbg&`@fq>-czfL=Nd lIv}ujV#>|-lNsHTKEExxVU|+l$fEwZYDha}gxuu%)?asj6g&U` literal 0 HcmV?d00001 diff --git a/occupi-backend/cmd/occupi-backend/main.go b/occupi-backend/cmd/occupi-backend/main.go index 7e4ddff9..a154db55 100644 --- a/occupi-backend/cmd/occupi-backend/main.go +++ b/occupi-backend/cmd/occupi-backend/main.go @@ -61,7 +61,7 @@ func main() { logrus.Infof("Server running with key file: %s", keyFile) // Listening on the port with TLS - if err := ginRouter.Run(":"+configs.GetPort()); err != nil { + if err := ginRouter.RunTLS(":"+configs.GetPort(), certFile, keyFile); err != nil { logrus.Fatal("Failed to run server: ", err) } } diff --git a/occupi-backend/key.pem.gpg b/occupi-backend/key.pem.gpg new file mode 100644 index 0000000000000000000000000000000000000000..60947e2de938118d5bc9f2528ca3ac6d0174397b GIT binary patch literal 2587 zcmV+$3gq>S4Fm}T0s`$gn-}4J=l{~{0YoE~)@Z=1PBfhou9rLq5+XKmT>;*l%HQgJ zGCTsBD3&j#fCC`}FXaU!B3YhhSGwyHMjs=R2Dpi)l?e#;&`$AZ6pifz+Vigwa|G6R z@}sV-E|bg1_89s%-}71gvSd5?MUKOuuOa6az(OHz1df{Txg<+erf&+8FYC7;FDz62 z%DCq@=98Xvz{Mh{@qyaa9drExe9!VlOy^k=x^n-mpODWw!8?O~H-J_vky|DO#Xi`i(5YK%Tvo=M5nE91a>L)Ux!m8U^*#WBw&FJ^AX@#q|RqT)pTma zu!-a2(?Bi8?u4(eucZa&h_!xnSEfY0BD6 zuFzpcglOnx+%a;Nc-vms@wN|JO1cEZeN7`f|)JudvAcx9Fawt`Ntlxymtsf*` z^m7$A`EL&T+uGNZl>$jWC5ct>Xc@|i64xo%=t4=;#Qjr0dUcK4wVn6WW{1nH*QESQ zOirnnXNN}m<5(W52zYabH+jxUYAJ_VA=brVb%$BTdC~S2dUMXQHeMqAKo6hjWYlWW z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=9HDV*l3e ziICBF8U0b*#1%n^@IIEC@hcrfO`Ie8`c27~Y_mw3Gd`gCZUmBunG`#*C(7y$tjYy7 ze&^cvQIdA$;%kV-4UNhM@6Wn9QXq?(wT8@@lIXU@@EXWBCUB6oB#lTY={2jO)1#T0 z-1=7EJyYee#~OT8@2S=3ij{kVPbSNl>|ghC*R&5JmB>a4U{KYn{HJ?c!)SG|uk;e4 zWzYwp8lP2+wNXTsZos)vn_j(8ot;pE7pb^}vCqXojL*9k6Is6(QhTx~ON4dSKD|;T z-Elzu`tAR{L>LGu8ShkKOlQ_79XF42yB$uvfYQ^E>v0zQkkB;Np!A!V7&C*yS5*MB zj%>+6o1rEvS%G281p9z`H!4+S`=t56#Xb0K>5LFh^o1$^jFI5s4Z^;OIwl#l1;3VR z9P~6rjh@=`3oh}R-_<5ne((;g2hhE5uOJZo6%v;!_oA)_D3jq1TDvb|+f5KRjXW~M zg*l@wYw0pCU^a4xlXL~GYnH9#*aq@@$dszDlqaNj^7~#Kx**3)?7hiZZBX$61-@ek z@mS{ooMQAgQF;lP+3~i3c+X%<;ZTlzH@Bkuu2`G}IuGD`UpbEWh9XhX#U{877h`Wy zv@|qy)VTnz%O>*c+GVb8$e;a7+xmRYy&$+5h{*A0gSnHlNi}4EsWy&u!s>rtzG3=Z zNrN!na11A|c%ocAS;cxXPK1lisYt>yRnlbxXylx2NL{W0Zb7^JtpgbTT82i>##{Jt x?kt^)Hzc|!1dfuU3no9o|MDXzd1$g&CR_8&KZACC@EW|VA_%CH6miJY@eMEX9K8Sl literal 0 HcmV?d00001 diff --git a/occupi-backend/tests/handlers_test.go b/occupi-backend/tests/handlers_test.go index bd280595..6423e529 100644 --- a/occupi-backend/tests/handlers_test.go +++ b/occupi-backend/tests/handlers_test.go @@ -2,17 +2,23 @@ package tests import ( "encoding/json" + "fmt" "net/http" "net/http/httptest" "sync" "testing" "time" + "github.com/joho/godotenv" "github.com/stretchr/testify/assert" "github.com/gin-gonic/gin" + "github.com/COS301-SE-2024/occupi/occupi-backend/configs" + "github.com/COS301-SE-2024/occupi/occupi-backend/pkg/database" "github.com/COS301-SE-2024/occupi/occupi-backend/pkg/middleware" + "github.com/COS301-SE-2024/occupi/occupi-backend/pkg/router" + "github.com/COS301-SE-2024/occupi/occupi-backend/pkg/utils" // "github.com/joho/godotenv" // "github.com/stretchr/testify/assert" // "github.com/stretchr/testify/mock" @@ -141,13 +147,28 @@ func TestVerifyOTP_EmailNotRegistered(t *testing.T) { }*/ func TestPingRoute(t *testing.T) { - // Create a new Gin router - r := gin.Default() + // Load environment variables from .env file + if err := godotenv.Load("../.env"); err != nil { + t.Fatal(fmt.Printf("Error loading .env file with error as %s", err)) + } - // Register the route - r.GET("/ping", func(c *gin.Context) { - c.JSON(http.StatusOK, gin.H{"message": "pong -> I am alive and kicking"}) - }) + // setup logger to log all server interactions + utils.SetupLogger() + + // connect to the database + db := database.ConnectToDatabase() + + // set gin run mode + gin.SetMode(configs.GetGinRunMode()) + + // Create a Gin router + ginRouter := gin.Default() + + // adding rate limiting middleware + middleware.AttachRateLimitMiddleware(ginRouter) + + // Register routes + router.OccupiRouter(ginRouter, db) // Create a request to pass to the handler req, err := http.NewRequest("GET", "/ping", nil) @@ -159,7 +180,7 @@ func TestPingRoute(t *testing.T) { rr := httptest.NewRecorder() // Serve the request - r.ServeHTTP(rr, req) + ginRouter.ServeHTTP(rr, req) // Check the status code is what we expect. if status := rr.Code; status != http.StatusOK { @@ -183,18 +204,30 @@ func TestPingRoute(t *testing.T) { } func TestRateLimit(t *testing.T) { - // Create a new Gin router - router := gin.Default() + // Load environment variables from .env file + if err := godotenv.Load("../.env"); err != nil { + t.Fatal(fmt.Printf("Error loading .env file with error as %s", err)) + } - // attach rate limit middleware - middleware.AttachRateLimitMiddleware(router) + // setup logger to log all server interactions + utils.SetupLogger() - // Register the route - router.GET("/ping", func(c *gin.Context) { - c.JSON(http.StatusOK, gin.H{"message": "pong -> I am alive and kicking"}) - }) + // connect to the database + db := database.ConnectToDatabase() - server := httptest.NewServer(router) + // set gin run mode + gin.SetMode(configs.GetGinRunMode()) + + // Create a Gin router + ginRouter := gin.Default() + + // adding rate limiting middleware + middleware.AttachRateLimitMiddleware(ginRouter) + + // Register routes + router.OccupiRouter(ginRouter, db) + + server := httptest.NewServer(ginRouter) defer server.Close() var wg sync.WaitGroup @@ -230,18 +263,30 @@ func TestRateLimit(t *testing.T) { } func TestRateLimitWithMultipleIPs(t *testing.T) { - // Create a new Gin router - router := gin.Default() + // Load environment variables from .env file + if err := godotenv.Load("../.env"); err != nil { + t.Fatal(fmt.Printf("Error loading .env file with error as %s", err)) + } - // attach rate limit middleware - middleware.AttachRateLimitMiddleware(router) + // setup logger to log all server interactions + utils.SetupLogger() - // Register the route - router.GET("/ping", func(c *gin.Context) { - c.JSON(http.StatusOK, gin.H{"message": "pong -> I am alive and kicking"}) - }) + // connect to the database + db := database.ConnectToDatabase() - server := httptest.NewServer(router) + // set gin run mode + gin.SetMode(configs.GetGinRunMode()) + + // Create a Gin router + ginRouter := gin.Default() + + // adding rate limiting middleware + middleware.AttachRateLimitMiddleware(ginRouter) + + // Register routes + router.OccupiRouter(ginRouter, db) + + server := httptest.NewServer(ginRouter) defer server.Close() var wg sync.WaitGroup @@ -320,26 +365,26 @@ func TestRateLimitWithMultipleIPs(t *testing.T) { assert.Equal(t, rateLimitedCountIP2, 0, "There should be no requests from IP2 that are rate limited") } -/* func TestGetResource(t *testing.T) { // Load environment variables from .env file if err := godotenv.Load("../.env"); err != nil { t.Fatal("Error loading .env file: ", err) } - // Connect to the database + // setup logger to log all server interactions + utils.SetupLogger() + + // connect to the database db := database.ConnectToDatabase() + // set gin run mode + gin.SetMode(configs.GetGinRunMode()) + // Create a Gin router r := gin.Default() - // create a new valid session for management of shared variables - appsession := models.New(nil, db) - // Register the route - r.GET("/api/resource", func(c *gin.Context) { - handlers.FetchResource(c, appsession) - }) + router.OccupiRouter(r, db) // Create a request to pass to the handler req, err := http.NewRequest("GET", "/api/resource", nil) @@ -354,8 +399,7 @@ func TestGetResource(t *testing.T) { r.ServeHTTP(rr, req) // Check the status code is what we expect. - if status := rr.Code; status != http.StatusOK { - t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK) + if status := rr.Code; status != http.StatusNotFound { + t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusNotFound) } } -*/