diff --git a/chaoscenter/graphql/server/graph/environment.resolvers.go b/chaoscenter/graphql/server/graph/environment.resolvers.go index 7cbfdd03e9f..7894d0e6fe1 100644 --- a/chaoscenter/graphql/server/graph/environment.resolvers.go +++ b/chaoscenter/graphql/server/graph/environment.resolvers.go @@ -13,7 +13,7 @@ import ( ) // CreateEnvironment is the resolver for the createEnvironment field. -func (r *mutationResolver) CreateEnvironment(ctx context.Context, projectID string, request *model.CreateEnvironmentRequest) (*model.Environment, error) { +func (r *mutationResolver) CreateEnvironment(ctx context.Context, projectID string, request *model.CreateEnvironmentRequest, authConfigOperator *authorization.Operator) (*model.Environment, error) { logFields := logrus.Fields{ "projectId": projectID, } @@ -26,7 +26,7 @@ func (r *mutationResolver) CreateEnvironment(ctx context.Context, projectID stri } tkn := ctx.Value(authorization.AuthKey).(string) - username, err := authorization.GetUsername(tkn) + username, err := authConfigOperator.GetUsername(tkn) if err != nil { return nil, err } @@ -35,7 +35,7 @@ func (r *mutationResolver) CreateEnvironment(ctx context.Context, projectID stri } // UpdateEnvironment is the resolver for the updateEnvironment field. -func (r *mutationResolver) UpdateEnvironment(ctx context.Context, projectID string, request *model.UpdateEnvironmentRequest) (string, error) { +func (r *mutationResolver) UpdateEnvironment(ctx context.Context, projectID string, request *model.UpdateEnvironmentRequest, authOperator *authorization.Operator) (string, error) { logFields := logrus.Fields{ "projectId": projectID, "environmentId": request.EnvironmentID, @@ -49,7 +49,7 @@ func (r *mutationResolver) UpdateEnvironment(ctx context.Context, projectID stri } tkn := ctx.Value(authorization.AuthKey).(string) - username, err := authorization.GetUsername(tkn) + username, err := authOperator.GetUsername(tkn) if err != nil { return "", err } @@ -58,7 +58,7 @@ func (r *mutationResolver) UpdateEnvironment(ctx context.Context, projectID stri } // DeleteEnvironment is the resolver for the deleteEnvironment field. -func (r *mutationResolver) DeleteEnvironment(ctx context.Context, projectID string, environmentID string) (string, error) { +func (r *mutationResolver) DeleteEnvironment(ctx context.Context, projectID string, environmentID string, authOperator *authorization.Operator) (string, error) { logFields := logrus.Fields{ "projectId": projectID, "environmentId": environmentID, @@ -72,7 +72,7 @@ func (r *mutationResolver) DeleteEnvironment(ctx context.Context, projectID stri } tkn := ctx.Value(authorization.AuthKey).(string) - username, err := authorization.GetUsername(tkn) + username, err := authOperator.GetUsername(tkn) if err != nil { return "", err } @@ -81,7 +81,7 @@ func (r *mutationResolver) DeleteEnvironment(ctx context.Context, projectID stri } // GetEnvironment is the resolver for the getEnvironment field. -func (r *queryResolver) GetEnvironment(ctx context.Context, projectID string, environmentID string) (*model.Environment, error) { +func (r *queryResolver) GetEnvironment(ctx context.Context, projectID string, environmentID string, authOperator *authorization.Operator) (*model.Environment, error) { logFields := logrus.Fields{ "projectId": projectID, "environmentId": environmentID, @@ -97,7 +97,7 @@ func (r *queryResolver) GetEnvironment(ctx context.Context, projectID string, en } // ListEnvironments is the resolver for the listEnvironments field. -func (r *queryResolver) ListEnvironments(ctx context.Context, projectID string, request *model.ListEnvironmentRequest) (*model.ListEnvironmentResponse, error) { +func (r *queryResolver) ListEnvironments(ctx context.Context, projectID string, request *model.ListEnvironmentRequest, authOperator *authorization.Operator) (*model.ListEnvironmentResponse, error) { logFields := logrus.Fields{ "projectId": projectID, } diff --git a/chaoscenter/graphql/server/pkg/authorization/authorization_fuzz_test.go b/chaoscenter/graphql/server/pkg/authorization/authorization_fuzz_test.go index 2df965d780e..3f99b9a2110 100644 --- a/chaoscenter/graphql/server/pkg/authorization/authorization_fuzz_test.go +++ b/chaoscenter/graphql/server/pkg/authorization/authorization_fuzz_test.go @@ -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{} @@ -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) } diff --git a/chaoscenter/graphql/server/pkg/chaos_infrastructure/cluster_jwt.go b/chaoscenter/graphql/server/pkg/chaos_infrastructure/cluster_jwt.go index bb001e80c44..2a93e666dc3 100644 --- a/chaoscenter/graphql/server/pkg/chaos_infrastructure/cluster_jwt.go +++ b/chaoscenter/graphql/server/pkg/chaos_infrastructure/cluster_jwt.go @@ -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 } @@ -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 } diff --git a/chaoscenter/graphql/server/pkg/chaos_infrastructure/service.go b/chaoscenter/graphql/server/pkg/chaos_infrastructure/service.go index 8c9663d241f..177d1a4ef8f 100644 --- a/chaoscenter/graphql/server/pkg/chaos_infrastructure/service.go +++ b/chaoscenter/graphql/server/pkg/chaos_infrastructure/service.go @@ -59,13 +59,15 @@ type Service interface { type infraService struct { 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, + authConfigOperator: authConfigOperator, } } @@ -100,7 +102,7 @@ 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 } @@ -221,7 +223,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 } @@ -306,7 +308,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 } diff --git a/chaoscenter/graphql/server/pkg/chaoshub/service.go b/chaoscenter/graphql/server/pkg/chaoshub/service.go index 53f46cc975f..9bb8376752e 100644 --- a/chaoscenter/graphql/server/pkg/chaoshub/service.go +++ b/chaoscenter/graphql/server/pkg/chaoshub/service.go @@ -49,15 +49,18 @@ type Service interface { GetChaosHubStats(ctx context.Context, projectID string) (*model.GetChaosHubStatsResponse, error) } + 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 { - return &chaosHubService{ - chaosHubOperator: chaosHubOperator, - } +func NewService(chaosHubOperator *dbSchemaChaosHub.Operator, authConfigOperator *authorization.Operator) Service { + return &chaosHubService{ + chaosHubOperator: chaosHubOperator, + authConfigOperator: authConfigOperator, + } } // AddChaosHub is used for Adding a new ChaosHub @@ -75,7 +78,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 @@ -129,7 +132,7 @@ func (c *chaosHubService) AddChaosHub(ctx context.Context, chaosHub model.Create return newHub.GetOutputChaosHub(), nil } -func (c *chaosHubService) AddRemoteChaosHub(ctx context.Context, chaosHub model.CreateRemoteChaosHub, projectID string) (*model.ChaosHub, error) { +func (c *chaosHubService) AddRemoteChaosHub(ctx context.Context, chaosHub model.CreateRemoteChaosHub, projectID string, authConfigOperator *authorization.Operator) (*model.ChaosHub, error) { IsExist, err := c.IsChaosHubAvailable(ctx, chaosHub.Name, projectID) if err != nil { return nil, err @@ -144,7 +147,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) @@ -198,7 +201,7 @@ func (c *chaosHubService) AddRemoteChaosHub(ctx context.Context, chaosHub model. } // SaveChaosHub is used for Adding a new ChaosHub -func (c *chaosHubService) SaveChaosHub(ctx context.Context, chaosHub model.CreateChaosHubRequest, projectID string) (*model.ChaosHub, error) { +func (c *chaosHubService) SaveChaosHub(ctx context.Context, chaosHub model.CreateChaosHubRequest, projectID string, authConfigOperator *authorization.Operator) (*model.ChaosHub, error) { IsExist, err := c.IsChaosHubAvailable(ctx, chaosHub.Name, projectID) if err != nil { @@ -211,7 +214,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) @@ -310,7 +313,7 @@ func (c *chaosHubService) SyncChaosHub(ctx context.Context, hubID string, projec return "Successfully synced ChaosHub", nil } -func (c *chaosHubService) UpdateChaosHub(ctx context.Context, chaosHub model.UpdateChaosHubRequest, projectID string) (*model.ChaosHub, error) { +func (c *chaosHubService) UpdateChaosHub(ctx context.Context, chaosHub model.UpdateChaosHubRequest, projectID string, authConfigOperator *authorization.Operator) (*model.ChaosHub, error) { cloneHub := model.CloningInput{ RepoBranch: chaosHub.RepoBranch, @@ -367,7 +370,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{ @@ -408,9 +411,9 @@ func (c *chaosHubService) UpdateChaosHub(ctx context.Context, chaosHub model.Upd return &newChaosHub, nil } -func (c *chaosHubService) DeleteChaosHub(ctx context.Context, hubID string, projectID string) (bool, error) { +func (c *chaosHubService) DeleteChaosHub(ctx context.Context, hubID string, projectID string, authConfigOperator *authorization.Operator) (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 } diff --git a/chaoscenter/graphql/server/pkg/handlers/file_handler.go b/chaoscenter/graphql/server/pkg/handlers/file_handler.go index 8ccf6f178e5..49d1df27f02 100644 --- a/chaoscenter/graphql/server/pkg/handlers/file_handler.go +++ b/chaoscenter/graphql/server/pkg/handlers/file_handler.go @@ -19,45 +19,45 @@ import ( func FileHandler(mongodbOperator mongodb.MongoOperator) gin.HandlerFunc { return func(c *gin.Context) { token := strings.TrimSuffix(c.Param("key"), ".yaml") - - infraId, err := chaos_infrastructure.InfraValidateJWT(token) + chaosInfraOperator := chaos_infrastructure.NewChaosInfrastructureOperator(mongodbOperator) + infraId, err := chaosInfraOperator.InfraValidateJWT(token) if err != nil { - logrus.Error(err) + logrus.Error("Error validating JWT: ", err) utils.WriteHeaders(&c.Writer, 500) c.Writer.Write([]byte(err.Error())) } infra, err := dbChaosInfra.NewInfrastructureOperator(mongodbOperator).GetInfra(infraId) if err != nil { - logrus.Error(err) + logrus.Error("Error fetching infra details: ", err) utils.WriteHeaders(&c.Writer, 500) c.Writer.Write([]byte(err.Error())) } reqHeader, ok := c.Value("request-header").(http.Header) if !ok { - logrus.Error("unable to parse referer header") + logrus.Error("Unable to parse Referer header") utils.WriteHeaders(&c.Writer, 500) - c.Writer.Write([]byte("unable to parse referer header")) + c.Writer.Write([]byte("Unable to parse Referer header")) } referrer := reqHeader.Get("Referer") if referrer == "" { - logrus.Error("unable to parse referer header") + logrus.Error("Referer header is empty") utils.WriteHeaders(&c.Writer, 500) - c.Writer.Write([]byte("unable to parse referer header")) + c.Writer.Write([]byte("Referer header is empty")) } referrerURL, err := url.Parse(referrer) if err != nil { - logrus.Error(err) + logrus.Error("Error parsing Referer URL: ", err) utils.WriteHeaders(&c.Writer, 500) c.Writer.Write([]byte(err.Error())) } response, err := chaos_infrastructure.GetK8sInfraYaml(fmt.Sprintf("%s://%s", referrerURL.Scheme, referrerURL.Host), infra) if err != nil { - logrus.Error(err) + logrus.Error("Error generating Kubernetes infra YAML: ", err) utils.WriteHeaders(&c.Writer, 500) c.Writer.Write([]byte(err.Error())) } diff --git a/chaoscenter/graphql/server/pkg/probe/handler/handler.go b/chaoscenter/graphql/server/pkg/probe/handler/handler.go index c3dbf68204e..e0049763674 100644 --- a/chaoscenter/graphql/server/pkg/probe/handler/handler.go +++ b/chaoscenter/graphql/server/pkg/probe/handler/handler.go @@ -30,20 +30,21 @@ import ( ) type Service interface { - AddProbe(ctx context.Context, probe model.ProbeRequest, projectID string) (*model.Probe, error) - UpdateProbe(ctx context.Context, probe model.ProbeRequest, projectID string) (string, error) - ListProbes(ctx context.Context, probeNames []string, infrastructureType *model.InfrastructureType, filter *model.ProbeFilterInput, projectID string) ([]*model.Probe, error) - DeleteProbe(ctx context.Context, probeName, projectID string) (bool, error) - GetProbe(ctx context.Context, probeName, projectID string) (*model.Probe, error) - GetProbeReference(ctx context.Context, probeName, projectID string) (*model.GetProbeReferenceResponse, error) - GetProbeYAMLData(ctx context.Context, probe model.GetProbeYAMLRequest, projectID string) (string, error) - ValidateUniqueProbe(ctx context.Context, probeName, projectID string) (bool, error) - GenerateExperimentManifestWithProbes(manifest string, projectID string) (argoTypes.Workflow, error) - GenerateCronExperimentManifestWithProbes(manifest string, projectID string) (argoTypes.CronWorkflow, error) + AddProbe(ctx context.Context, probe model.ProbeRequest, projectID string) (*model.Probe, error) + UpdateProbe(ctx context.Context, probe model.ProbeRequest, projectID string) (string, error) + ListProbes(ctx context.Context, probeNames []string, infrastructureType *model.InfrastructureType, filter *model.ProbeFilterInput, projectID string) ([]*model.Probe, error) + DeleteProbe(ctx context.Context, probeName, projectID string) (bool, error) + GetProbe(ctx context.Context, probeName, projectID string) (*model.Probe, error) + GetProbeReference(ctx context.Context, probeName, projectID string) (*model.GetProbeReferenceResponse, error) + GetProbeYAMLData(ctx context.Context, probe model.GetProbeYAMLRequest, projectID string) (string, error) + ValidateUniqueProbe(ctx context.Context, probeName, projectID string) (bool, error) + GenerateExperimentManifestWithProbes(manifest string, projectID string) (argoTypes.Workflow, error) + GenerateCronExperimentManifestWithProbes(manifest string, projectID string) (argoTypes.CronWorkflow, error) } type probeService struct { probeOperator *dbSchemaProbe.Operator + authConfigOperator *authorization.Operator } type Operator struct { @@ -58,10 +59,11 @@ func NewProbeOperator(mongodbOperator mongodb.MongoOperator) *Operator { } // NewProbeService returns a new instance of probeService -func NewProbeService(probeOperator *dbSchemaProbe.Operator) Service { - return &probeService{ - probeOperator: probeOperator, - } +func NewProbeService(probeOperator *dbSchemaProbe.Operator, authConfigOperator *authorization.Operator) Service { + return &probeService{ + probeOperator: probeOperator, + authConfigOperator: authConfigOperator, + } } func Error(logFields logrus.Fields, message string) error { @@ -81,7 +83,7 @@ func (p *probeService) AddProbe(ctx context.Context, probe model.ProbeRequest, p return nil, errors.New("JWT token not found") } - username, err := authorization.GetUsername(tkn) + username, err := p.authConfigOperator.GetUsername(tkn) if err != nil { return nil, err } @@ -149,9 +151,9 @@ func (p *probeService) AddProbe(ctx context.Context, probe model.ProbeRequest, p } // UpdateProbe - Update a new Probe -func (p *probeService) UpdateProbe(ctx context.Context, request model.ProbeRequest, projectID string) (string, error) { +func (p *probeService) UpdateProbe(ctx context.Context, request model.ProbeRequest, projectID string, authOperator *authorization.Operator) (string, error) { tkn := ctx.Value(authorization.AuthKey).(string) - username, err := authorization.GetUsername(tkn) + username, err := p.authConfigOperator.GetUsername(tkn) if err != nil { return "", err } @@ -398,6 +400,8 @@ func (p *probeService) ListProbes(ctx context.Context, probeNames []string, infr } func GetProbeExecutionHistoryInExperimentRuns(projectID string, probeName string) ([]*model.ProbeRecentExecutions, error) { + var mongodbOperator mongodb.MongoOperator + var ( pipeline mongo.Pipeline expRuns []dbChaosExperimentRun.ChaosExperimentRun @@ -417,7 +421,7 @@ func GetProbeExecutionHistoryInExperimentRuns(projectID string, probeName string pipeline = append(pipeline, matchIdentifierStage) // Call aggregation on pipeline - experimentRunOperator := dbChaosExperimentRun.NewChaosExperimentRunOperator(NewProbeOperator.operator) + experimentRunOperator := dbChaosExperimentRun.NewChaosExperimentRunOperator(NewProbeOperator(mongodbOperator).operator) expRunCursor, err := experimentRunOperator.GetAggregateExperimentRuns(pipeline) if err != nil { return nil, errors.New("DB aggregate stage error: " + err.Error()) @@ -496,14 +500,14 @@ func GetProbeExecutionHistoryInExperimentRuns(projectID string, probeName string } // DeleteProbe - Deletes a single Probe -func (p *probeService) DeleteProbe(ctx context.Context, probeName, projectID string) (bool, error) { +func (p *probeService) DeleteProbe(ctx context.Context, probeName, projectID string, authOperator *authorization.Operator) (bool, error) { _, err := p.probeOperator.GetProbeByName(ctx, probeName, projectID) if err != nil { return false, err } tkn := ctx.Value(authorization.AuthKey).(string) - username, err := authorization.GetUsername(tkn) + username, err := authOperator.GetUsername(tkn) Time := time.Now().UnixMilli()