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

Refactor: Eliminate Global Variable to Enhance Testability #4997

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
91e910f
chore(3.14.0): Add the installation manifest for 3.14.0 version (#4995)
ispeakc0de Dec 17, 2024
e448062
Refactor: Eliminate Global Variable to Enhance Testability
andoriyaprashant Dec 17, 2024
36969a8
checks
andoriyaprashant Dec 17, 2024
b713310
chore: Adding Pokerbaazi as adopters (#4958)
S-ayanide Dec 20, 2024
e12cdef
Proposal: Load testing with locust (#4955)
kwx4957 Dec 20, 2024
a388ed8
chore(deps): Bump nanoid from 3.3.7 to 3.3.8 in /chaoscenter/web (#4992)
dependabot[bot] Dec 20, 2024
be791ec
chore(deps): Bump cross-spawn from 7.0.3 to 7.0.6 in /chaoscenter/web…
dependabot[bot] Dec 20, 2024
2615dac
chore: update base image because of the CVE-2024-3596 (#4964)
namkyu1999 Dec 20, 2024
500e1ff
JVM fault injection proposal (#4977)
bjoky Dec 20, 2024
188096a
Introducing LitmusChaos Guru on Gurubase.io (#4943)
kursataktas Dec 20, 2024
fef592c
chore(deps): Bump golang.org/x/crypto (#4985)
dependabot[bot] Dec 20, 2024
6ade2a7
Fix vulnerabilities in Graphql and Authentication server (#5002)
Saranya-jena Dec 20, 2024
57d07af
Add probe source part to kubernetes cmd properties of ChaosEngine man…
rogeriofbrito Dec 20, 2024
5840c35
user_jwt.go fixed
andoriyaprashant Dec 26, 2024
20f4d91
chore: Updating Governance policy and memebership roles (#5016)
S-ayanide Jan 9, 2025
200e174
chore: Fixing MAINTAINER.md formatting (#5017)
S-ayanide Jan 9, 2025
6ef4819
chore: Fixing ISSUE_TEMPLATE naming (#5018)
S-ayanide Jan 13, 2025
9cc779c
fix: return only message when no projects are found (#5011)
ayush3160 Jan 15, 2025
7e16aa7
Added 3.15 installation manifests and fixed vulnerabilities (#5025)
Saranya-jena Jan 15, 2025
a63eab8
refactor: update code syntax in backend server (#4944)
jemlog Jan 15, 2025
484a0ba
chore(3.15.0): Add the installation manifests for 3.15.0 version (#5027)
ispeakc0de Jan 15, 2025
f5cf97f
backend unit tests
andoriyaprashant Jan 18, 2025
56f0785
Merge branch 'master' into 1branch
andoriyaprashant Jan 18, 2025
6329562
imports
andoriyaprashant Jan 18, 2025
c783ef7
Merge branch '1branch' of https://github.com/andoriyaprashant/litmus …
andoriyaprashant Jan 18, 2025
21a3c8d
wrong type for method DeleteProbe and AddRemoteChaosHub
andoriyaprashant Jan 18, 2025
9efca1b
Imports Fix
andoriyaprashant Jan 18, 2025
ebf29ac
DeleteProbe and DeleteChaosHub wrong type
andoriyaprashant Jan 18, 2025
9911bd9
service and handler
andoriyaprashant Jan 18, 2025
8e69135
NewChaosProbeOperator
andoriyaprashant Jan 19, 2025
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: 2 additions & 0 deletions chaoscenter/authentication/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ss
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand Down
12 changes: 6 additions & 6 deletions chaoscenter/graphql/server/graph/environment.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func generateJWTTokenFromClaims(claims jwt.MapClaims) (string, error) {
}

func FuzzUserValidateJWT(f *testing.F) {
operator := &Operator{}
f.Fuzz(func(t *testing.T, data []byte) {
fuzzConsumer := fuzz.NewConsumer(data)
inputClaims := &jwt.MapClaims{}
Expand All @@ -72,7 +73,7 @@ func FuzzUserValidateJWT(f *testing.F) {
}

// Run the test with the generated JWT token
claims, err := UserValidateJWT(tokenString, "")
claims, err := operator.UserValidateJWT(tokenString, "")
if err != nil {
t.Errorf("Error encountered: %v", err)
}
Expand Down
16 changes: 13 additions & 3 deletions chaoscenter/graphql/server/pkg/authorization/user_jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@ import (
"github.com/golang-jwt/jwt"
)

type Operator struct {
authConfigOperator *authConfig.Operator
}

func NewAuthorizationOperator(mongodbOperator mongodb.MongoOperator) *Operator {
return &Operator{
authConfigOperator: authConfig.NewAuthConfigOperator(mongodbOperator),
}
}

// UserValidateJWT validates the cluster jwt
func UserValidateJWT(token string, salt string) (jwt.MapClaims, error) {
func (o *Operator) UserValidateJWT(token string, salt string) (jwt.MapClaims, error) {
tkn, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
if _, isValid := token.Method.(*jwt.SigningMethodHMAC); !isValid {
return nil, fmt.Errorf("invalid token %s", token.Header["alg"])
Expand All @@ -39,8 +49,8 @@ func UserValidateJWT(token string, salt string) (jwt.MapClaims, error) {
}

// GetUsername returns the username from the jwt token
func GetUsername(token string) (string, error) {
salt, err := authConfig.NewAuthConfigOperator(mongodb.Operator).GetAuthConfig(context.Background())
func (o *Operator) GetUsername(token string) (string, error) {
salt, err := o.authConfigOperator.GetAuthConfig(context.Background())
if err != nil {
return "", err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1350,7 +1350,7 @@ func (c *ChaosExperimentHandler) GetProbesInExperimentRun(ctx context.Context, p
}

for _, probeName := range _probe.ProbeNames {
singleProbe, err := dbSchemaProbe.NewChaosProbeOperator(c.mongodbOperator).GetProbeByName(ctx, probeName, projectID)
singleProbe, err := dbSchemaProbe.NewProbeOperator(c.mongodbOperator).GetProbeByName(ctx, probeName, projectID)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"reflect"
"testing"

"github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/authorization"
dbSchemaProbe "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/probe"

"github.com/gin-gonic/gin"
Expand All @@ -30,11 +31,12 @@ import (

var (
mongodbMockOperator = new(dbMocks.MongoOperator)
probeOperator = dbSchemaProbe.NewChaosProbeOperator(mongodbMockOperator)
probeOperator = dbSchemaProbe.NewProbeOperator(mongodbMockOperator)
infraOperator = dbChaosInfra.NewInfrastructureOperator(mongodbMockOperator)
chaosExperimentOperator = dbChaosExperiment.NewChaosExperimentOperator(mongodbMockOperator)
chaosExperimentRunOperator = dbChaosExperimentRun.NewChaosExperimentRunOperator(mongodbMockOperator)
probeService = probe.NewProbeService(probeOperator)
authConfigOperator = authorization.NewAuthorizationOperator(mongodbMockOperator)
probeService = probe.NewProbeService(probeOperator, authConfigOperator)
)

var chaosExperimentRunTestService = NewChaosExperimentService(chaosExperimentOperator, infraOperator, chaosExperimentRunOperator, probeService)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type ChaosExperimentRunHandler struct {
chaosExperimentRunOperator *dbChaosExperimentRun.Operator
probeService probe.Service
mongodbOperator mongodb.MongoOperator
authConfigOperator *authorization.Operator
}

// NewChaosExperimentRunHandler returns a new instance of ChaosWorkflowHandler
Expand All @@ -67,6 +68,7 @@ func NewChaosExperimentRunHandler(
chaosExperimentRunOperator *dbChaosExperimentRun.Operator,
probeService probe.Service,
mongodbOperator mongodb.MongoOperator,
authConfigOperator *authorization.Operator,
) *ChaosExperimentRunHandler {
return &ChaosExperimentRunHandler{
chaosExperimentRunService: chaosExperimentRunService,
Expand All @@ -76,6 +78,7 @@ func NewChaosExperimentRunHandler(
chaosExperimentRunOperator: chaosExperimentRunOperator,
probeService: probeService,
mongodbOperator: mongodbOperator,
authConfigOperator: authConfigOperator,
}
}

Expand Down Expand Up @@ -799,7 +802,7 @@ func (c *ChaosExperimentRunHandler) RunChaosWorkFlow(ctx context.Context, projec
)

tkn := ctx.Value(authorization.AuthKey).(string)
username, err := authorization.GetUsername(tkn)
username, err := c.authConfigOperator.GetUsername(tkn)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1003,7 +1006,7 @@ func (c *ChaosExperimentRunHandler) RunCronExperiment(ctx context.Context, proje
}

tkn := ctx.Value(authorization.AuthKey).(string)
username, err := authorization.GetUsername(tkn)
username, err := c.authConfigOperator.GetUsername(tkn)
if err != nil {
return err
}
Expand Down
18 changes: 14 additions & 4 deletions chaoscenter/graphql/server/pkg/chaos_infrastructure/cluster_jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,22 @@ import (
"github.com/golang-jwt/jwt"
)

type Operator struct {
authConfigOperator *authConfig.Operator
}

func NewChaosInfrastructureOperator(mongodbOperator mongodb.MongoOperator) *Operator {
return &Operator{
authConfigOperator: authConfig.NewAuthConfigOperator(mongodbOperator),
}
}

// InfraCreateJWT generates jwt used in chaos_infra registration
func InfraCreateJWT(id string) (string, error) {
func (o *Operator) InfraCreateJWT(id string) (string, error) {
claims := jwt.MapClaims{}
claims["chaos_infra_id"] = id
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
config, err := authConfig.NewAuthConfigOperator(mongodb.Operator).GetAuthConfig(context.Background())
config, err := o.authConfigOperator.GetAuthConfig(context.Background())
if err != nil {
return "", err
}
Expand All @@ -29,12 +39,12 @@ func InfraCreateJWT(id string) (string, error) {
}

// InfraValidateJWT validates the chaos_infra jwt
func InfraValidateJWT(token string) (string, error) {
func (o *Operator) InfraValidateJWT(token string) (string, error) {
tkn, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
}
config, err := authConfig.NewAuthConfigOperator(mongodb.Operator).GetAuthConfig(context.Background())
config, err := o.authConfigOperator.GetAuthConfig(context.Background())
if err != nil {
return "", err
}
Expand Down
30 changes: 20 additions & 10 deletions chaoscenter/graphql/server/pkg/chaos_infrastructure/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,17 @@ type Service interface {
}

type infraService struct {
infraOperator *dbChaosInfra.Operator
envOperator *dbEnvironments.Operator
infraOperator *dbChaosInfra.Operator
envOperator *dbEnvironments.Operator
authConfigOperator *authorization.Operator
}

// NewChaosInfrastructureService returns a new instance of Service
func NewChaosInfrastructureService(infraOperator *dbChaosInfra.Operator, envOperator *dbEnvironments.Operator) Service {
func NewChaosInfrastructureService(infraOperator *dbChaosInfra.Operator, envOperator *dbEnvironments.Operator, authConfigOperator *authorization.Operator) Service {
return &infraService{
infraOperator: infraOperator,
envOperator: envOperator,
infraOperator: infraOperator,
envOperator: envOperator,
authConfigOperator: authConfigOperator,
}
}

Expand Down Expand Up @@ -100,12 +102,16 @@ func (in *infraService) RegisterInfra(c context.Context, projectID string, input
)

tkn := c.Value(authorization.AuthKey).(string)
username, err := authorization.GetUsername(tkn)
username, err := in.authConfigOperator.GetUsername(tkn)
if err != nil {
return nil, err
}

token, err := InfraCreateJWT(infraID)
var mongodbOperator mongodb.MongoOperator

operator := NewChaosInfrastructureOperator(mongodbOperator)

token, err := operator.InfraCreateJWT(infraID)
if err != nil {
return &model.RegisterInfraResponse{}, err
}
Expand Down Expand Up @@ -221,7 +227,7 @@ func (in *infraService) RegisterInfra(c context.Context, projectID string, input
// DeleteInfra takes infraIDs and r parameters, deletes the infras from the database and sends a request to the subscriber for clean-up
func (in *infraService) DeleteInfra(ctx context.Context, projectID string, infraId string, r store.StateData) (string, error) {
tkn := ctx.Value(authorization.AuthKey).(string)
username, err := authorization.GetUsername(tkn)
username, err := in.authConfigOperator.GetUsername(tkn)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -306,7 +312,7 @@ func (in *infraService) DeleteInfra(ctx context.Context, projectID string, infra
func (in *infraService) GetInfra(ctx context.Context, projectID string, infraID string) (*model.Infra, error) {

tkn := ctx.Value(authorization.AuthKey).(string)
username, err := authorization.GetUsername(tkn)
username, err := in.authConfigOperator.GetUsername(tkn)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -950,7 +956,11 @@ func updateVersionFormat(str string) (int, error) {

// QueryServerVersion is used to fetch the version of the server
func (in *infraService) QueryServerVersion(ctx context.Context) (*model.ServerVersionResponse, error) {
dbVersion, err := config.GetConfig(ctx, "version")
var mongodbOperator mongodb.MongoOperator

configOperator := config.NewConfigOperator(mongodbOperator)

dbVersion, err := configOperator.GetConfig(ctx, "version")
if err != nil {
return nil, err
}
Expand Down
18 changes: 10 additions & 8 deletions chaoscenter/graphql/server/pkg/chaoshub/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ type Service interface {
}

type chaosHubService struct {
chaosHubOperator *dbSchemaChaosHub.Operator
chaosHubOperator *dbSchemaChaosHub.Operator
authConfigOperator *authorization.Operator
}

// NewService returns a new instance of Service
func NewService(chaosHubOperator *dbSchemaChaosHub.Operator) Service {
func NewService(chaosHubOperator *dbSchemaChaosHub.Operator, authConfigOperator *authorization.Operator) Service {
return &chaosHubService{
chaosHubOperator: chaosHubOperator,
chaosHubOperator: chaosHubOperator,
authConfigOperator: authConfigOperator,
}
}

Expand All @@ -75,7 +77,7 @@ func (c *chaosHubService) AddChaosHub(ctx context.Context, chaosHub model.Create
}

tkn := ctx.Value(authorization.AuthKey).(string)
username, err := authorization.GetUsername(tkn)
username, err := c.authConfigOperator.GetUsername(tkn)
if err != nil {
log.Error("error getting username: ", err)
return nil, err
Expand Down Expand Up @@ -144,7 +146,7 @@ func (c *chaosHubService) AddRemoteChaosHub(ctx context.Context, chaosHub model.
currentTime := time.Now()

tkn := ctx.Value(authorization.AuthKey).(string)
username, err := authorization.GetUsername(tkn)
username, err := c.authConfigOperator.GetUsername(tkn)

if err != nil {
log.Error("error getting userID: ", err)
Expand Down Expand Up @@ -211,7 +213,7 @@ func (c *chaosHubService) SaveChaosHub(ctx context.Context, chaosHub model.Creat
// Initialize a UID for new Hub.
uuid := uuid.New()
tkn := ctx.Value(authorization.AuthKey).(string)
username, err := authorization.GetUsername(tkn)
username, err := c.authConfigOperator.GetUsername(tkn)

if err != nil {
log.Error("error getting userID: ", err)
Expand Down Expand Up @@ -367,7 +369,7 @@ func (c *chaosHubService) UpdateChaosHub(ctx context.Context, chaosHub model.Upd

time := time.Now().UnixMilli()
tkn := ctx.Value(authorization.AuthKey).(string)
username, err := authorization.GetUsername(tkn)
username, err := c.authConfigOperator.GetUsername(tkn)

query := bson.D{{"hub_id", chaosHub.ID}, {"is_removed", false}}
update := bson.D{
Expand Down Expand Up @@ -410,7 +412,7 @@ func (c *chaosHubService) UpdateChaosHub(ctx context.Context, chaosHub model.Upd

func (c *chaosHubService) DeleteChaosHub(ctx context.Context, hubID string, projectID string) (bool, error) {
tkn := ctx.Value(authorization.AuthKey).(string)
username, err := authorization.GetUsername(tkn)
username, err := c.authConfigOperator.GetUsername(tkn)
if err != nil {
return false, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (c *Operator) UpdateChaosHub(ctx context.Context, query bson.D, update bson

// GetAggregateChaosHubs takes a mongo pipeline to retrieve the project details from the database
func (c *Operator) GetAggregateChaosHubs(ctx context.Context, pipeline mongo.Pipeline) (*mongo.Cursor, error) {
results, err := mongodb.Operator.Aggregate(ctx, mongodb.ChaosHubCollection, pipeline)
results, err := c.operator.Aggregate(ctx, mongodb.ChaosHubCollection, pipeline)
if err != nil {
return nil, fmt.Errorf("error on getting the chaos hubs : %v", err)
}
Expand Down
Loading
Loading