Skip to content

Commit

Permalink
Merge pull request #4 from B-urb/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
B-urb authored Oct 30, 2023
2 parents fd68e9c + 0185eef commit e74e315
Show file tree
Hide file tree
Showing 33 changed files with 756 additions and 171 deletions.
37 changes: 34 additions & 3 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
run: go get ./...
- name: Build
working-directory: backend
run: go build -o build/kubevoyage ./cmd/kubevoyage
run: GOOS=linux GOARCH=amd64 go build -o build/kubevoyage ./cmd/kubevoyage
#- name: Test with the Go CLI
# run: go test
- name: Archive production artifacts
Expand Down Expand Up @@ -86,7 +86,7 @@ jobs:
push: true
tags: ${{ secrets.DOCKERHUB_USERNAME }}/kubevoyage:${{ env.BRANCH_NAME }}
release:
needs: build-docker
needs: helm-release
runs-on: ubuntu-latest
permissions:
contents: write
Expand All @@ -108,4 +108,35 @@ jobs:
- name: Release
env:
GITHUB_TOKEN: ${{ secrets.PAT }}
run: npx semantic-release --debug
run: npx semantic-release --debug
helm-release:
needs: build-docker
# depending on default permission settings for your org (contents being read-only or read-write for workloads), you will have to add permissions
# see: https://docs.github.com/en/actions/security-guides/automatic-token-authentication#modifying-the-permissions-for-the-github_token
permissions:
contents: write
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Configure Git
run: |
git config user.name "$GITHUB_ACTOR"
git config user.email "[email protected]"
- name: Install Helm
uses: azure/setup-helm@v3
env:
GITHUB_TOKEN: ${{ secrets.PAT }}

- name: Run chart-releaser
uses: helm/[email protected]
with:
charts_dir: deploy/
env:
registryImage: ${{ secrets.DOCKERHUB_USERNAME }}/kubevoyage
imageTag: ${{ github.head_ref || github.ref_name }}
CR_TOKEN: ${{ secrets.PAT }}
7 changes: 5 additions & 2 deletions .github/workflows/release-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
run: go get ./...
- name: Build
working-directory: backend
run: go build -o build/kubevoyage ./cmd/kubevoyage
run: GOOS=linux GOARCH=amd64 go build -o build/kubevoyage ./cmd/kubevoyage
#- name: Test with the Go CLI
# run: go test
- name: Archive production artifacts
Expand Down Expand Up @@ -79,7 +79,8 @@ jobs:
push: true
tags: ${{ secrets.DOCKERHUB_USERNAME }}/kubevoyage:${{ env.BRANCH_NAME }}

release:
helm-release:
needs: build-docker
# depending on default permission settings for your org (contents being read-only or read-write for workloads), you will have to add permissions
# see: https://docs.github.com/en/actions/security-guides/automatic-token-authentication#modifying-the-permissions-for-the-github_token
permissions:
Expand All @@ -98,6 +99,8 @@ jobs:
- name: Install Helm
uses: azure/setup-helm@v3
env:
GITHUB_TOKEN: ${{ secrets.PAT }}

- name: Run chart-releaser
uses: helm/[email protected]
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
run: go get ./...
- name: Build
working-directory: backend
run: go build -o build/kubevoyage ./cmd/kubevoyage
run: GOOS=linux GOARCH=amd64 go build -o build/kubevoyage ./cmd/kubevoyage
#- name: Test with the Go CLI
# run: go test
- name: Archive production artifacts
Expand Down Expand Up @@ -78,7 +78,7 @@ jobs:
context: .
push: true
tags: ${{ secrets.DOCKERHUB_USERNAME }}/kubevoyage:${{ env.BRANCH_NAME }}
release:
helm-release:
needs: build-docker
# depending on default permission settings for your org (contents being read-only or read-write for workloads), you will have to add permissions
# see: https://docs.github.com/en/actions/security-guides/automatic-token-authentication#modifying-the-permissions-for-the-github_token
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea
*.db
build
build

deploy/charts/values-local.yaml
13 changes: 9 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ FROM golang:1.21
LABEL authors="bjornurban"
EXPOSE 8080:8080

RUN mkdir /kubevoyage
RUN mkdir /kubevoyage/bin
COPY frontend/public /kubevoyage/public
COPY backend/build /kubevoyage/bin
WORKDIR /kubevoyage

# Copy frontend and backend files
COPY frontend/public ./public
COPY backend/build ./bin

# Ensure the binary has executable permissions
RUN chmod +x ./bin/kubevoyage

ENTRYPOINT ["./bin/kubevoyage"]
7 changes: 4 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Embarking on a secure journey in Kubernetes.

`KubeVoyage` is a Kubernetes authentication proxy designed to streamline user access to various sites. Built with a Svelte frontend, a Go backend, and an SQL database, it offers a robust solution for managing user access in a Kubernetes environment.

![KubeVoyage Logo](path_to_logo.png) <!-- If you have a logo, replace 'path_to_logo.png' with its path -->
![KubeVoyage Logo](frontend/public/Kubevoyage.png) <!-- If you have a logo, replace 'path_to_logo.png' with its path -->

## Features

Expand Down Expand Up @@ -74,7 +74,8 @@ Visit `http://localhost:8080` in your browser.
Use the provided Helm chart to deploy `KubeVoyage` to your Kubernetes cluster:

```bash
helm install kubevoyage ./path_to_helm_chart
helm repo add github-burban https://B-urb.github.io/KubeVoyage/
helm install kubevoyage github-burban/kubevoyage
```

## Testing
Expand All @@ -92,4 +93,4 @@ Pull requests are welcome! For major changes, please open an issue first to disc

## License

This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details.
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
160 changes: 93 additions & 67 deletions backend/cmd/kubevoyage/main.go
Original file line number Diff line number Diff line change
@@ -1,109 +1,135 @@
package main

import (
"fmt"
"github.com/B-Urb/KubeVoyage/internal/app"
"github.com/B-Urb/KubeVoyage/internal/handlers"
"github.com/B-Urb/KubeVoyage/internal/models"
"github.com/rs/cors"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"log"
"net/http"
"os"
"path/filepath"
"time"
// or "gorm.io/driver/postgres" for PostgreSQL
)

var db *gorm.DB

var frontendPathLocal = "./public" //"../frontend/public"

type loggingResponseWriter struct {
http.ResponseWriter
statusCode int
length int
}

func (lrw *loggingResponseWriter) Write(b []byte) (int, error) {
if lrw.statusCode == 0 {
// Default status code is 200 OK.
lrw.statusCode = http.StatusOK
}
size, err := lrw.ResponseWriter.Write(b)
lrw.length += size
return size, err
}

func (lrw *loggingResponseWriter) WriteHeader(statusCode int) {
lrw.statusCode = statusCode
lrw.ResponseWriter.WriteHeader(statusCode)
}
func logMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
lrw := &loggingResponseWriter{ResponseWriter: w}
start := time.Now()

next.ServeHTTP(lrw, r)

duration := time.Since(start)
log.Printf(
"Method: %s, Path: %s, RemoteAddr: %s, Duration: %s, StatusCode: %d, ResponseSize: %d bytes\n",
r.Method,
r.URL.Path,
r.RemoteAddr,
duration,
lrw.statusCode,
lrw.length,
)
})
}
func main() {
var err error
db, err = gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
app, err := application.NewApp() // Assuming NewApp is in the same package
if err != nil {
panic("failed to connect database")
log.Fatalf(err.Error())
}

handler := handlers.NewHandler(app.DB)
err = app.Init()
if err != nil {
log.Fatalf(err.Error())
}
mux := http.NewServeMux()

// Migrate the schema
db.AutoMigrate(&models.User{}, &models.Site{}, &models.UserSite{})
//generateTestData()
mux := setupServer(handler)

log.Println("Starting server on :8080")
log.Fatal(http.ListenAndServe(":8080", mux))
}
func setupServer(handle *handlers.Handler) http.Handler {
mux := http.NewServeMux()

handler := cors.Default().Handler(mux)

// Serve static files
fs := http.FileServer(http.Dir("../frontend/public/")) // Adjust the path based on your directory structure
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fs := http.FileServer(http.Dir(frontendPathLocal)) // Adjust the path based on your directory structure
mux.Handle("/", logMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Check if it's an API route first
if isAPIRoute(r.URL.Path) {
// Handle API routes separately
return
}

path := "../frontend/public" + r.URL.Path
log.Println(path)
_, err := os.Stat(path)
path := frontendPathLocal + r.URL.Path
absolutePath, err := filepath.Abs(path)
if err != nil {
fmt.Println("Error getting absolute path:", err)
return
}
fmt.Println("Absolute Path:", absolutePath)
_, err = os.Stat(path)

// If the file exists, serve it
if !os.IsNotExist(err) {
fs.ServeHTTP(w, r)
return
} else {
log.Println(err)
}

// Otherwise, serve index.html
http.ServeFile(w, r, "../frontend/public/index.html")
})
http.ServeFile(w, r, frontendPathLocal+"/index.html")
})))

mux.HandleFunc("/api/requests", func(w http.ResponseWriter, r *http.Request) {
mux.Handle("/api/requests", logMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handlers.HandleRequests(w, r, db)
})
mux.HandleFunc("/api/register", func(w http.ResponseWriter, r *http.Request) {
handlers.HandleRegister(w, r, db)
})
mux.HandleFunc("/api/login", func(w http.ResponseWriter, r *http.Request) {
handlers.HandleLogin(w, r, db)
})
mux.HandleFunc("/api/authenticate", func(w http.ResponseWriter, r *http.Request) {
handlers.HandleAuthenticate(w, r, db)
})
mux.HandleFunc("/api/request", func(w http.ResponseWriter, r *http.Request) {
handlers.HandleRequestSite(w, r, db)
})
// Start the server on port 8081
log.Println("Starting server on :8080")

log.Fatal(http.ListenAndServe(":8080", handler))
})))
mux.Handle("/api/register", logMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handle.HandleRegister(w, r)
})))
mux.Handle("/api/login", logMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handle.HandleLogin(w, r)
})))
mux.Handle("/api/authenticate", logMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handle.HandleAuthenticate(w, r)
})))
mux.Handle("/api/redirect", logMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handle.HandleRedirect(w, r)
})))
mux.Handle("/api/request", logMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handle.HandleRequestSite(w, r, db)
})))

// ... setup your routes and start your server
return handler
}

func isAPIRoute(path string) bool {
return len(path) >= 4 && path[0:4] == "/api"
}
func generateTestData() {
// Insert test data for Users
users := []models.User{
{Email: "[email protected]", Password: "password1", Role: "admin"},
{Email: "[email protected]", Password: "password2", Role: "user"},
{Email: "[email protected]", Password: "password3", Role: "user"},
}
for _, user := range users {
db.Create(&user)
}

// Insert test data for Sites
sites := []models.Site{
{URL: "https://site1.com"},
{URL: "https://site2.com"},
{URL: "https://site3.com"},
}
for _, site := range sites {
db.Create(&site)
}

// Insert test data for UserSite
userSites := []models.UserSite{
{UserID: 1, SiteID: 1, State: "authorized"},
{UserID: 2, SiteID: 2, State: "requested"},
{UserID: 3, SiteID: 3, State: "authorized"},
}
for _, userSite := range userSites {
db.Create(&userSite)
}
}
11 changes: 10 additions & 1 deletion backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,28 @@ module github.com/B-Urb/KubeVoyage
go 1.21

require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/golang-jwt/jwt/v5 v5.0.0
github.com/rs/cors v1.10.0
github.com/stretchr/testify v1.8.4
golang.org/x/crypto v0.13.0
gorm.io/driver/mysql v1.5.1
gorm.io/driver/postgres v1.5.2
gorm.io/driver/sqlite v1.5.3
gorm.io/gorm v1.25.4
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.3.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
golang.org/x/text v0.13.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit e74e315

Please sign in to comment.