Skip to content

Commit

Permalink
Adding readiness routes to graphql-server and auth-server for readine…
Browse files Browse the repository at this point in the history
…ss probes (#3495)

* Adding probes

Signed-off-by: rajdas98 <[email protected]>

* fix auth server for readiness endpoint

Signed-off-by: rajdas98 <[email protected]>

* adding readiness route

Signed-off-by: rajdas98 <[email protected]>

* revertg

Signed-off-by: rajdas98 <[email protected]>

* revertg

Signed-off-by: rajdas98 <[email protected]>

* changes

Signed-off-by: rajdas98 <[email protected]>

* changes

Signed-off-by: rajdas98 <[email protected]>

* changes

Signed-off-by: rajdas98 <[email protected]>
  • Loading branch information
imrajdas authored Mar 14, 2022
1 parent 2056168 commit cf03d1e
Show file tree
Hide file tree
Showing 18 changed files with 342 additions and 108 deletions.
6 changes: 3 additions & 3 deletions USERS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
This is a list of users that are using & benefited by LitmusChaos. Please send a PR to this file (along with details [here](./adopters/users))
to add yourselves.
This is a list of users that are using & benefited by LitmusChaos. Please send a PR to this file (along with details [here](./adopters/users))
to add yourselves.


| User | Usecase | Details |
Expand All @@ -10,4 +10,4 @@ to add yourselves.
| [Bhaumik Shah](https://github.com/Bhaumik1802)|Use LitmusChaos for Kafka Resiliency on Dev/Staging|[My Story](adopters/users/Bhaumik_Shah.md)|
| [Jayadeep KM](https://github.com/kmjayadeep)|Ensure reliability of microservices|[My Story](adopters/users/Jayadeep_KM.md)|
| [Shantanu Deshpande](https://github.com/ishantanu)|Chaos Engineering Practice as SRE|[My Story](adopters/users/Shantanu_Deshpande.md)|
| [Omar Hanafi](https://github.com/oHanafi)|Performance Anomaly Detection in Cloud and Containerized Applications|Coming Soon!|
| [Omar Hanafi](https://github.com/oHanafi)|Performance Anomaly Detection in Cloud and Containerized Applications|Coming Soon!|
71 changes: 71 additions & 0 deletions litmus-portal/authentication/api/handlers/rest/misc_handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package rest

import (
"litmus/litmus-portal/authentication/pkg/entities"
"litmus/litmus-portal/authentication/pkg/services"

"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)

func contains(s []string, str string) bool {
for _, v := range s {
if v == str {
return true
}
}

return false
}

type ReadinessAPIStatus struct {
DataBase string `json:"database"`
Collections string `json:"collections"`
}

// Status will request users list and return, if successful,
// an http code 200
func Status(service services.ApplicationService) gin.HandlerFunc {
return func(c *gin.Context) {
_, err := service.GetUsers()
if err != nil {
log.Error(err)
c.JSON(500, entities.APIStatus{"down"})
return
}
c.JSON(200, entities.APIStatus{"up"})
}
}

func Readiness(service services.ApplicationService) gin.HandlerFunc {
return func(c *gin.Context) {
var (
db_flag = "up"
col_flag = "up"
)

dbs, err := service.ListDataBase()
if !contains(dbs, "auth") {
db_flag = "down"
}

if err != nil {
log.Error(err)
c.JSON(500, ReadinessAPIStatus{"down", "unknown"})
return
}

cols, err := service.ListCollection()
if !contains(cols, "project") || !contains(cols, "users") {
col_flag = "down"
}

if err != nil {
log.Error(err)
c.JSON(500, ReadinessAPIStatus{db_flag, "down"})
return
}

c.JSON(200, ReadinessAPIStatus{db_flag, col_flag})
}
}
14 changes: 0 additions & 14 deletions litmus-portal/authentication/api/handlers/rest/user_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,6 @@ import (
"golang.org/x/crypto/bcrypt"
)

// Status will request users list and return, if successful,
// an http code 200
func Status(service services.ApplicationService) gin.HandlerFunc {
return func(c *gin.Context) {
_, err := service.GetUsers()
if err != nil {
log.Error(err)
c.JSON(500, entities.APIStatus{"down"})
return
}
c.JSON(200, entities.APIStatus{"up"})
}
}

func CreateUser(service services.ApplicationService) gin.HandlerFunc {
return func(c *gin.Context) {
userRole := c.MustGet("role").(string)
Expand Down
10 changes: 8 additions & 2 deletions litmus-portal/authentication/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
grpcPresenter "litmus/litmus-portal/authentication/api/presenter/protos"
"litmus/litmus-portal/authentication/api/routes"
"litmus/litmus-portal/authentication/pkg/entities"
"litmus/litmus-portal/authentication/pkg/misc"
"litmus/litmus-portal/authentication/pkg/project"
"litmus/litmus-portal/authentication/pkg/services"
"litmus/litmus-portal/authentication/pkg/user"
Expand Down Expand Up @@ -53,11 +54,13 @@ func main() {

flag.Parse()

db, err := utils.DatabaseConnection()
client, err := utils.MongoConnection()
if err != nil {
log.Fatal("database connection error $s", err)
}

db := client.Database(utils.DBName)

// Creating User Collection
err = utils.CreateCollection(utils.UserCollection, db)
if err != nil {
Expand All @@ -81,7 +84,9 @@ func main() {
projectCollection := db.Collection(utils.ProjectCollection)
projectRepo := project.NewRepo(projectCollection)

applicationService := services.NewService(userRepo, projectRepo, db)
miscRepo := misc.NewRepo(db, client)

applicationService := services.NewService(userRepo, projectRepo, miscRepo, db)

validatedAdminSetup(applicationService)

Expand Down Expand Up @@ -145,6 +150,7 @@ func runRestServer(applicationService services.ApplicationService) {
if utils.DexEnabled {
routes.DexRouter(app, applicationService)
}
routes.MiscRouter(app, applicationService)
routes.UserRouter(app, applicationService)
routes.ProjectRouter(app, applicationService)

Expand Down
13 changes: 13 additions & 0 deletions litmus-portal/authentication/api/routes/misc_routers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package routes

import (
"litmus/litmus-portal/authentication/api/handlers/rest"
"litmus/litmus-portal/authentication/pkg/services"

"github.com/gin-gonic/gin"
)

func MiscRouter(router *gin.Engine, service services.ApplicationService) {
router.GET("/status", rest.Status(service))
router.GET("/readiness", rest.Readiness(service))
}
1 change: 0 additions & 1 deletion litmus-portal/authentication/api/routes/user_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

// UserRouter creates all the required routes for user authentications purposes.
func UserRouter(router *gin.Engine, service services.ApplicationService) {
router.GET("/status", rest.Status(service))
router.POST("/login", rest.LoginUser(service))
router.Use(middleware.JwtMiddleware())
router.POST("/update/password", rest.UpdatePassword(service))
Expand Down
46 changes: 46 additions & 0 deletions litmus-portal/authentication/pkg/misc/repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package misc

import (
"context"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)

//Repository holds the mongo database implementation of the Service
type Repository interface {
ListCollection() ([]string, error)
ListDataBase() ([]string, error)
}

type repository struct {
DataBase *mongo.Database
Client *mongo.Client
}

func (r repository) ListCollection() ([]string, error) {
var err error
cols, err := r.DataBase.ListCollectionNames(context.Background(), bson.D{})
if err != nil {
return nil, err
}

return cols, nil
}

func (r repository) ListDataBase() ([]string, error) {
var err error
dbs, err := r.Client.ListDatabaseNames(context.Background(), bson.D{})
if err != nil {
return nil, err
}

return dbs, nil
}

func NewRepo(db *mongo.Database, client *mongo.Client) Repository {
return &repository{
DataBase: db,
Client: client,
}
}
1 change: 1 addition & 0 deletions litmus-portal/authentication/pkg/project/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type repository struct {
// GetProject returns a project based on a query or filter value
func (r repository) GetProjectByProjectID(projectID string) (*entities.Project, error) {
var project = new(entities.Project)

findOneErr := r.Collection.FindOne(context.TODO(), bson.D{{"_id", projectID}}).Decode(&project)
if findOneErr != nil {
return nil, findOneErr
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package services

import (
"litmus/litmus-portal/authentication/pkg/misc"
"litmus/litmus-portal/authentication/pkg/project"
"litmus/litmus-portal/authentication/pkg/user"

Expand All @@ -11,19 +12,22 @@ type ApplicationService interface {
userService
projectService
transactionService
miscService
}

type applicationService struct {
userRepository user.Repository
projectRepository project.Repository
miscRepository misc.Repository
db *mongo.Database
}

// NewService creates a new instance of this service
func NewService(userRepo user.Repository, projectRepo project.Repository, db *mongo.Database) ApplicationService {
func NewService(userRepo user.Repository, projectRepo project.Repository, miscRepo misc.Repository, db *mongo.Database) ApplicationService {
return &applicationService{
userRepository: userRepo,
projectRepository: projectRepo,
db: db,
miscRepository: miscRepo,
}
}
15 changes: 15 additions & 0 deletions litmus-portal/authentication/pkg/services/misc_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package services

// Service creates a service for user authentication operations
type miscService interface {
ListCollection() ([]string, error)
ListDataBase() ([]string, error)
}

func (a applicationService) ListCollection() ([]string, error) {
return a.miscRepository.ListCollection()
}

func (a applicationService) ListDataBase() ([]string, error) {
return a.miscRepository.ListDataBase()
}
2 changes: 2 additions & 0 deletions litmus-portal/authentication/pkg/user/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type Repository interface {

type repository struct {
Collection *mongo.Collection
DataBase *mongo.Database
Client *mongo.Client
}

// LoginUser helps to Login the user via OAuth, if user does not exists, creates a new user
Expand Down
8 changes: 4 additions & 4 deletions litmus-portal/authentication/pkg/utils/mongo_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"go.mongodb.org/mongo-driver/mongo/options"
)

// DatabaseConnection creates a connection to the mongo database
func DatabaseConnection() (*mongo.Database, error) {
// MongoConnection creates a connection to the mongo
func MongoConnection() (*mongo.Client, error) {
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
mongoCredentials := options.Credential{
Username: DBUser,
Expand All @@ -22,8 +22,8 @@ func DatabaseConnection() (*mongo.Database, error) {
if err != nil {
return nil, err
}
db := client.Database(DBName)
return db, nil

return client, nil
}

// CreateIndex creates a unique index for the given field in the collectionName
Expand Down
11 changes: 0 additions & 11 deletions litmus-portal/graphql-server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -328,10 +328,8 @@ github.com/coreos/pkg v0.0.0-20180108230652-97fdf19511ea/go.mod h1:E3G3o1h8I7cfc
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/coreos/prometheus-operator v0.34.0/go.mod h1:Li6rMllG/hYIyXfMuvUwhyC+hqwJVHdsDdP21hypT1M=
github.com/coreos/rkt v1.30.0/go.mod h1:O634mlH6U7qk87poQifK6M2rsFNt+FyUTWNMnP1hF1U=
github.com/cpuguy83/go-md2man v1.0.10 h1:BSKMNlYxDvnunlTymqtgONjNnaRV1sTpcovwwjF22jk=
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.0 h1:EoUDS0afbrsXAZ9YQ9jdu/mZ2sXgT1/2yyNng4PGlyM=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down Expand Up @@ -1013,7 +1011,6 @@ github.com/markbates/pkger v0.17.1/go.mod h1:0JoVlrol20BSywW79rN3kdFFsE5xYM+rSCQ
github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kNSCBdG0=
github.com/marten-seemann/qtls v0.2.3/go.mod h1:xzjG7avBwGGbdZ8dTGxlBnLArsVKLvwmjgmPuiQEcYk=
github.com/martinlindhe/base36 v1.0.0/go.mod h1:+AtEs8xrBpCeYgSLoY/aJ6Wf37jtBuR0s35750M27+8=
github.com/matryer/moq v0.0.0-20200106131100-75d0ddfc0007 h1:reVOUXwnhsYv/8UqjvhrMOu5CNT9UapHFLbQ2JcXsmg=
github.com/matryer/moq v0.0.0-20200106131100-75d0ddfc0007/go.mod h1:9ELz6aaclSIGnZBoaSLZ3NAl1VTufbOrXBPvtcy6WiQ=
github.com/mattbaird/jsonpatch v0.0.0-20171005235357-81af80346b1a/go.mod h1:M1qoD/MqPgTZIk0EWKB38wE28ACRfVcn+cU08jyArI0=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
Expand Down Expand Up @@ -1314,9 +1311,7 @@ github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/rubenv/sql-migrate v0.0.0-20191025130928-9355dd04f4b3/go.mod h1:WS0rl9eEliYI8DPnr3TOwz4439pay+qNgzJoVya/DmY=
github.com/rubiojr/go-vhd v0.0.0-20200706105327-02e210299021/go.mod h1:DM5xW0nvfNNm2uytzsvhI3OnX8uzaRAg8UX/CnDqbto=
github.com/russross/blackfriday v0.0.0-20170610170232-067529f716f4/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
Expand All @@ -1338,7 +1333,6 @@ github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFR
github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk=
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041/go.mod h1:N5mDOmsrJOB+vfqUK+7DmDyjhSLIIBnXo9lvZJj3MWQ=
github.com/shurcooL/httpfs v0.0.0-20171119174359-809beceb2371/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/shurcooL/vfsgen v0.0.0-20180121065927-ffb13db8def0/go.mod h1:TrYk7fJVaAttu97ZZKrO9UbRa8izdowaMIZcxYMbVaw=
github.com/sirupsen/logrus v1.0.5/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc=
Expand Down Expand Up @@ -1445,9 +1439,7 @@ github.com/ultraware/funlen v0.0.1/go.mod h1:Dp4UiAus7Wdb9KUZsYWZEWiRzGuM2kXM1lP
github.com/ultraware/funlen v0.0.2/go.mod h1:Dp4UiAus7Wdb9KUZsYWZEWiRzGuM2kXM1lPbfaF6xhA=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli v1.22.2 h1:gsqYFH8bb9ekPA12kRo0hfjngWQjkJPlN9R0N78BoUo=
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli/v2 v2.1.1 h1:Qt8FeAtxE/vfdrLmR3rxR6JRE0RoVmbXu8+6kZtYU4k=
github.com/urfave/cli/v2 v2.1.1/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ=
github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
Expand All @@ -1459,7 +1451,6 @@ github.com/valyala/fasttemplate v1.1.0/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPU
github.com/valyala/gozstd v1.7.0/go.mod h1:y5Ew47GLlP37EkTB+B4s7r6A5rdaeB7ftbl9zoYiIPQ=
github.com/valyala/quicktemplate v1.1.1/go.mod h1:EH+4AkTd43SvgIbQHYu59/cJyxDoOVRUAfrukLPuGJ4=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
github.com/vektah/dataloaden v0.2.1-0.20190515034641-a19b9a6e7c9e h1:+w0Zm/9gaWpEAyDlU1eKOuk5twTjAjuevXqcJJw8hrg=
github.com/vektah/dataloaden v0.2.1-0.20190515034641-a19b9a6e7c9e/go.mod h1:/HUdMve7rvxZma+2ZELQeNh88+003LL7Pf/CZ089j8U=
github.com/vektah/gqlparser v1.1.2 h1:ZsyLGn7/7jDNI+y4SEhI4yAxRChlv15pUHMjijT+e68=
github.com/vektah/gqlparser v1.1.2/go.mod h1:1ycwN7Ij5njmMkPPAOaRFY4rET2Enx7IkVv3vaXspKw=
Expand Down Expand Up @@ -1649,7 +1640,6 @@ golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.1-0.20200828183125-ce943fd02449/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20170915142106-8351a756f30f/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180112015858-5ccada7d0a7b/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down Expand Up @@ -1979,7 +1969,6 @@ golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4f
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.6 h1:SIasE1FVIQOWz2GEAHFOmoW7xchJcqlucjSULTL0Ag4=
golang.org/x/tools v0.1.6/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
Loading

0 comments on commit cf03d1e

Please sign in to comment.