Skip to content
This repository has been archived by the owner on Apr 17, 2024. It is now read-only.

KSD-389: add support for JWT Auth token #36

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion cmd/migrations/migrations/20240212181900_initial.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ create table
create table
public.customers (
id uuid not null default gen_random_uuid (),
name character varying not null,
company character varying not null,
email character varying not null,
created_at timestamp with time zone null default now(),
updated_at timestamp with time zone null default now(),
Expand Down
4 changes: 4 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"github.com/koor-tech/genesis/gateway/middleware"
"github.com/koor-tech/genesis/internal/auth"
"log/slog"
"os"
"time"
Expand Down Expand Up @@ -61,6 +63,8 @@ func getFxBaseOpts() []fx.Option {
rabbitmq.NewClient,
handler.NewCluster,
cluster.NewService,
auth.NewService,
middleware.NewMiddleware,

// Add any dependencies here so they can just be injected
),
Expand Down
1 change: 1 addition & 0 deletions config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ notifications:
cloudProvider:
hetzner:
token: ""
secret: "abcdefghijklmnopqrstuvwxyz12345"
31 changes: 19 additions & 12 deletions gateway/handler/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package handler
import (
"context"
"errors"
"fmt"
"net/http"

"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/koor-tech/genesis/gateway/request"
"github.com/koor-tech/genesis/internal/cluster"
"github.com/koor-tech/genesis/pkg/models"
"go.uber.org/fx"
Expand All @@ -33,19 +33,22 @@ func NewCluster(p Params) *Cluster {
}
}

func (h *Cluster) CreateCluster(c *gin.Context) {
var createClusterRequest request.CreateClusterRequest
if err := c.ShouldBindJSON(&createClusterRequest); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": ErrorBadRequest})
return
func (h *Cluster) getCustomer(c *gin.Context) *models.Customer {
//debug
if claims, exists := c.Get("claims"); exists {
claims := claims.(*models.AuthJwtClaims)
fmt.Println("-----------------------")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should remove the print statements before the PR is merged or at least changing to log statements that they are in line with the other log outputs.

fmt.Printf("got Claims: %+v\n", claims)
fmt.Println("-----------------------")
return claims.Customer
}
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
return nil
}

customer := models.Customer{
ID: uuid.New(),
Name: createClusterRequest.ClientName,
Email: createClusterRequest.ClientEmail,
}
koorCluster, err := h.clusterSvc.BuildCluster(c, &customer, uuid.MustParse("80be226b-8355-4dea-b41a-6e17ea37559a"))
func (h *Cluster) CreateCluster(c *gin.Context) {
customer := h.getCustomer(c)
koorCluster, err := h.clusterSvc.BuildCluster(c, customer, uuid.MustParse("80be226b-8355-4dea-b41a-6e17ea37559a"))
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err})
return
Expand All @@ -56,6 +59,8 @@ func (h *Cluster) CreateCluster(c *gin.Context) {

func (h *Cluster) GetCluster(c *gin.Context) {
clusterID := uuid.MustParse(c.Param("id"))
// add validation to allow only get cluster status from its cluster
_ = h.getCustomer(c)

koorCluster, err := h.clusterSvc.GetCluster(context.Background(), clusterID)
if err != nil {
Expand All @@ -71,6 +76,8 @@ func (h *Cluster) GetCluster(c *gin.Context) {
}

func (h *Cluster) DeleteCluster(c *gin.Context) {
// add validation to allow only get cluster status from its cluster
_ = h.getCustomer(c)
clusterID := uuid.MustParse(c.Param("id"))

if err := h.clusterSvc.DeleteCluster(context.Background(), clusterID); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions gateway/handler/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ func mapCluster(koorCluster *models.Cluster) *schemas.Cluster {
ID: koorCluster.ID,
Phase: int(koorCluster.ClusterState.Phase),
Customer: schemas.Customer{
ID: koorCluster.Customer.ID,
Name: koorCluster.Customer.Name,
Email: koorCluster.Customer.Email,
ID: koorCluster.Customer.ID,
Company: koorCluster.Customer.Company,
Email: koorCluster.Customer.Email,
},
Provider: schemas.Provider{
ID: koorCluster.Provider.ID,
Expand Down
9 changes: 5 additions & 4 deletions gateway/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gateway

import (
"context"
"github.com/koor-tech/genesis/gateway/middleware"
"log/slog"
"net"
"net/http"
Expand All @@ -23,6 +24,7 @@ type Params struct {

Shutdowner fx.Shutdowner
ClusterHandler *handler.Cluster // Use annotations for easily adding new handlers on the go instead of "just one"
AuthMiddleware *middleware.AuthMiddleware
}

func New(p Params) (*gin.Engine, error) {
Expand All @@ -35,7 +37,7 @@ func New(p Params) (*gin.Engine, error) {
r.Use(sloggin.New(p.Logger))
r.Use(gin.Recovery())

RegisterRoutes(r, p.ClusterHandler)
RegisterRoutes(r, p.ClusterHandler, p.AuthMiddleware)

// Create HTTP Server for graceful shutdown handling
srv := &http.Server{
Expand Down Expand Up @@ -69,10 +71,9 @@ func New(p Params) (*gin.Engine, error) {
return r, nil
}

func RegisterRoutes(r *gin.Engine, clusterHandler *handler.Cluster) *gin.Engine {
func RegisterRoutes(r *gin.Engine, clusterHandler *handler.Cluster, authMiddleware *middleware.AuthMiddleware) *gin.Engine {
r.GET("/status", handler.GetStatus)

v1 := r.Group("/api/v1")
v1 := r.Group("/api/v1").Use(authMiddleware.Validate)
{
v1.POST("/clusters", clusterHandler.CreateCluster)
v1.GET("/clusters/:id", clusterHandler.GetCluster)
Expand Down
6 changes: 3 additions & 3 deletions gateway/schemas/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package schemas
import "github.com/google/uuid"

type Customer struct {
ID uuid.UUID `json:"id"`
Email string `json:"email"`
Name string `json:"name"`
ID uuid.UUID `json:"id"`
Email string `json:"email"`
Company string `json:"company"`
}

type Provider struct {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/Masterminds/squirrel v1.5.4
github.com/creasty/defaults v1.7.0
github.com/gin-gonic/gin v1.9.1
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/uuid v1.6.0
github.com/hashicorp/terraform-exec v0.20.0
github.com/hetznercloud/hcloud-go/v2 v2.7.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down
13 changes: 3 additions & 10 deletions internal/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,6 @@ func (s *Service) getCustomerDir(customer *models.Customer) string {

func (s *Service) BuildCluster(ctx context.Context, customer *models.Customer, providerID uuid.UUID) (*models.Cluster, error) {
s.logger.Info("BuildCluster", "customer", customer.ID)

customer, err := s.customerRepository.Save(ctx, customer)
if err != nil {
s.logger.Error("unable to save client", "err", err)
return nil, err
}

provider, err := s.providerRepository.QueryByID(ctx, providerID)
if err != nil {
s.logger.Error("unable to get provider", "err", err)
Expand Down Expand Up @@ -282,13 +275,13 @@ func (s *Service) ResumeCluster(ctx context.Context, clusterID uuid.UUID) error
s.logger.Error("unable to save the state of the cluster ", "err ", err, "clusterID", cluster.ID)
}

servers, err := s.cloudProvider.GetServerByLabels(ctx, cluster.Customer.Name)
servers, err := s.cloudProvider.GetServerByLabels(ctx, cluster.Customer.Company)
if err != nil {
s.logger.Error("unable to get server error:", "err", err)
return err
}
s.logger.Info("attaching volumes")
err = s.cloudProvider.AttacheVolumesToServers(ctx, cluster.Customer.Name, servers)
err = s.cloudProvider.AttacheVolumesToServers(ctx, cluster.Customer.Company, servers)
if err != nil {
s.logger.Error("unable to get server error:", "err", err)
return err
Expand All @@ -307,7 +300,7 @@ func (s *Service) ResumeCluster(ctx context.Context, clusterID uuid.UUID) error
}
//
s.logger.Info("installing rook-ceph")
kubeConfigName := fmt.Sprintf("koor-client-%s-kubeconfig", cluster.Customer.Name)
kubeConfigName := fmt.Sprintf("koor-client-%s-kubeconfig", cluster.Customer.Company)
clusterState.Phase = models.ClusterPhaseInstallCephInit
err = s.clusterStateRepository.Update(ctx, clusterState)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Config struct {
RabbitMQ RabbitMQ `yaml:"rabbitmq"`
Notifications Notifications `yaml:"notifications"`
CloudProvider CloudProvider `yaml:"cloudProvider"`
Secret string `default:"abc" yaml:"secret"`
}

type Directories struct {
Expand Down
6 changes: 3 additions & 3 deletions pkg/models/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package models
import "github.com/google/uuid"

type Customer struct {
ID uuid.UUID `db:"id"`
Name string `db:"name"`
Email string `db:"email"`
ID uuid.UUID `db:"id"`
Company string `db:"company"`
Email string `db:"email"`
}
1 change: 1 addition & 0 deletions pkg/models/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import "errors"

var (
ErrClusterNotFound = errors.New("cluster not found")
ErrCustomerNotFound = errors.New("customer not found")
ErrClusterStateNotFound = errors.New("cluster state not found")
)
2 changes: 1 addition & 1 deletion pkg/models/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type TerraformConfig struct {

func NewTerraformConfig(cluster *Cluster, dst string) *TerraformConfig {
return &TerraformConfig{
ClusterName: "koor-client-" + cluster.Customer.Name,
ClusterName: "koor-client-" + cluster.Customer.Company,
SshPublicKeyFile: dst + "/id_ed25519.pub",
ControlPlaneVmCount: 1,
InitialMachineDeploymentReplicas: 4,
Expand Down
2 changes: 1 addition & 1 deletion pkg/repositories/postgres/cluster/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (r *ClusterRepository) QueryByID(ctx context.Context, clusterID uuid.UUID)
`c.customer_id`,
`c.kube_config`,
`customers.id as "customers.id"`,
`customers.name as "customers.name"`,
`customers.company as "customers.company"`,
`customers.email as "customers.email"`,
`c.provider_id`,
`p.id as "providers.id"`,
Expand Down
10 changes: 5 additions & 5 deletions pkg/repositories/postgres/customers/customers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"database/sql"
"errors"

sq "github.com/Masterminds/squirrel"
"github.com/google/uuid"
"github.com/koor-tech/genesis/pkg/database"
Expand All @@ -24,8 +23,8 @@ func NewCustomersRepository(db *database.DB) *CustomersRepository {
func (r *CustomersRepository) Save(ctx context.Context, customer *models.Customer) (*models.Customer, error) {
sqlStmt, args, _ := sq.StatementBuilder.PlaceholderFormat(sq.Dollar).
Insert(`customers`).
Columns(`id`, `name`, `email`).
Values(customer.ID, customer.Name, customer.Email).
Columns(`id`, `company`, `email`).
Values(customer.ID, customer.Company, customer.Email).
ToSql()

_, err := r.db.Conn.ExecContext(ctx, sqlStmt, args...)
Expand All @@ -39,15 +38,16 @@ func (r *CustomersRepository) QueryByID(ctx context.Context, ID uuid.UUID) (*mod
var builder = sq.StatementBuilder.PlaceholderFormat(sq.Dollar).
Select(
`c.id`,
`cs.name`,
`c.company`,
`c.email`,
).
From(`customers c`)
var c models.Customer

sqlStmt, args, _ := builder.Where(`c.id = $1`, ID).ToSql()
if err := r.db.Conn.GetContext(ctx, &c, sqlStmt, args...); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, models.ErrClusterNotFound
return nil, models.ErrCustomerNotFound
}
return nil, err
}
Expand Down