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

Development #4

Merged
merged 49 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
0226b15
fix(infra): change action helm chart
Sep 30, 2023
f40ac54
Merge remote-tracking branch 'origin/development' into development
Sep 30, 2023
64ce427
fix(infra): change action helm chart
Sep 30, 2023
78e4c36
fix(infra): change action helm chart
Sep 30, 2023
ef9b4a5
fix(infra): change helm chart version
Sep 30, 2023
0455153
fix(infra): change helm chart version
Sep 30, 2023
a44991d
fix(infra): change helm chart version
Sep 30, 2023
147a569
fix(infra): change helm chart version
Sep 30, 2023
33b3572
fix(docs): update readme
Oct 1, 2023
b274497
fix(docs): update readme
Oct 1, 2023
04c3a9e
fix(infra): update helmchart
Oct 1, 2023
ccecf1a
fix(infra): update helmchart
Oct 1, 2023
e1d7506
fix(backend): refactor go modules
Oct 1, 2023
d7a0639
fix(infra): update actions
Oct 1, 2023
ddbac29
fix(backend): more refactoring in backend packages
Oct 1, 2023
6b3c04f
fix(backend): update jwt dependency
Oct 4, 2023
1ed43dd
fix(backend): update frontend routes
Oct 11, 2023
32bcbfe
fix(infra): update chart version
Oct 11, 2023
c850422
fix(backend): fix go dependencies
Oct 11, 2023
3ac6797
fix(infra): update env vars in helm chart
Oct 11, 2023
75e0a4c
fix(infra): update env vars in helm chart
Oct 11, 2023
4fa1559
fix(infra): update env vars in helm chart
Oct 11, 2023
65c171f
fix(backend): update public path
Oct 11, 2023
8527ed7
fix(backend): update authenticate endpoint to allow admin and add adm…
Oct 11, 2023
7b327c7
fix(backend): set default db name
Oct 11, 2023
22ce636
fix(infra): update chart values
Oct 11, 2023
10a9779
fix(backend): adding admin user correctly
Oct 11, 2023
6864213
fix(infra): helm secret
Oct 11, 2023
7682b87
fix(backend): remove function from db module
Oct 11, 2023
cad6428
fix(infra): update chart version
Oct 11, 2023
27e919e
fix(frontend): set correct request url
Oct 11, 2023
238b76f
fix(frontend): set correct page title
Oct 11, 2023
0f77e24
fix(backend): set correct redirect cookie
Oct 11, 2023
a521165
fix(backend): frontend path
Oct 11, 2023
c717684
fix(backend): add header logging for debug
Oct 16, 2023
e01b9b0
fix(backend): add correct source header for redirect url
Oct 16, 2023
4c6531f
fix(backend): add image of logo
Oct 16, 2023
062e9fc
fix(backend): add image of logo
Oct 16, 2023
3e895ea
fix(backend): improve extract domain function
Oct 16, 2023
b45493f
fix(backend): improve extract domain function
Oct 16, 2023
1d01878
fix(backend): update url parse logic
Oct 16, 2023
bf1cd0e
fix(backend): update url parse logic
Oct 16, 2023
dc98c5a
fix(backend): update cookie key names
Oct 16, 2023
4320420
fix(backend): update cookie key names
Oct 16, 2023
beb65df
fix(backend): test different setup
Oct 16, 2023
da1433e
fix(frontend): update frontend redirect logic
Oct 21, 2023
e4f761c
fix(backend): update redirect logic and url determination
Oct 21, 2023
b8d730e
fix(backend): update frontend dir
Oct 24, 2023
0185eef
fix(frontend): loading state on login
Oct 30, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading