diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index fa8946a4009..b36af186eda 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -43,7 +43,7 @@ jobs: # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@v1 + uses: github/codeql-action/init@v3 with: languages: ${{ matrix.language }} # If you wish to specify custom queries, you can do so here or in a config file. @@ -54,7 +54,7 @@ jobs: # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). # If this step fails, then you should remove it and run the build manually (see below) - name: Autobuild - uses: github/codeql-action/autobuild@v1 + uses: github/codeql-action/autobuild@v3 # ℹī¸ Command-line programs to run using the OS shell. # 📚 https://git.io/JvXDl @@ -68,4 +68,4 @@ jobs: # make release - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v1 + uses: github/codeql-action/analyze@v3 diff --git a/chaoscenter/authentication/api/handlers/rest/user_handlers_test.go b/chaoscenter/authentication/api/handlers/rest/user_handlers_test.go index e06e4b29645..157b101c3a3 100644 --- a/chaoscenter/authentication/api/handlers/rest/user_handlers_test.go +++ b/chaoscenter/authentication/api/handlers/rest/user_handlers_test.go @@ -4,7 +4,7 @@ import ( "bytes" "encoding/json" "errors" - "io/ioutil" + "io" "log" "net/http" "net/http/httptest" @@ -26,7 +26,7 @@ import ( // TestMain is the entry point for testing func TestMain(m *testing.M) { gin.SetMode(gin.TestMode) - log.SetOutput(ioutil.Discard) + log.SetOutput(io.Discard) os.Exit(m.Run()) } @@ -516,7 +516,7 @@ func TestResetPassword(t *testing.T) { c := GetTestGinContext(w) c.Request.Method = http.MethodPost bodyBytes, _ := json.Marshal(tt.inputBody) - c.Request.Body = ioutil.NopCloser(bytes.NewReader([]byte(bodyBytes))) + c.Request.Body = io.NopCloser(bytes.NewReader(bodyBytes)) c.Set("role", tt.mockRole) c.Set("uid", tt.mockUID) c.Set("username", tt.mockUsername) @@ -592,7 +592,7 @@ func TestUpdateUserState(t *testing.T) { c := GetTestGinContext(w) c.Request.Method = http.MethodPost bodyBytes, _ := json.Marshal(tc.inputBody) - c.Request.Body = ioutil.NopCloser(bytes.NewReader([]byte(bodyBytes))) + c.Request.Body = io.NopCloser(bytes.NewReader([]byte(bodyBytes))) c.Set("role", tc.mockRole) c.Set("uid", tc.mockUID) c.Set("username", tc.mockUsername) diff --git a/chaoscenter/graphql/server/pkg/chaos_infrastructure/infra_utils.go b/chaoscenter/graphql/server/pkg/chaos_infrastructure/infra_utils.go index d28acc65ac9..0a780186311 100644 --- a/chaoscenter/graphql/server/pkg/chaos_infrastructure/infra_utils.go +++ b/chaoscenter/graphql/server/pkg/chaos_infrastructure/infra_utils.go @@ -2,6 +2,8 @@ package chaos_infrastructure import ( "fmt" + "os" + "strings" "github.com/ghodss/yaml" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" @@ -9,12 +11,8 @@ import ( dbChaosInfra "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_infrastructure" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/k8s" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/utils" - "github.com/sirupsen/logrus" + log "github.com/sirupsen/logrus" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - - "io/ioutil" - "os" - "strings" ) type SubscriberConfigurations struct { @@ -65,7 +63,7 @@ func GetK8sInfraYaml(infra dbChaosInfra.ChaosInfra) ([]byte, error) { } else if infra.InfraScope == NamespaceScope { respData, err = ManifestParser(infra, "manifests/namespace", &config) } else { - logrus.Error("INFRA_SCOPE env is empty!") + log.Error("INFRA_SCOPE env is empty!") } if err != nil { return nil, err @@ -129,7 +127,11 @@ func ManifestParser(infra dbChaosInfra.ChaosInfra, rootPath string, config *Subs return nil, fmt.Errorf("failed to open the file %v", err) } - defer file.Close() + defer func() { + if err := file.Close(); err != nil { + log.Warnf("failed to close file: %v", err) + } + }() list, err := file.Readdirnames(0) // 0 to read all files and folders if err != nil { @@ -174,7 +176,7 @@ func ManifestParser(infra dbChaosInfra.ChaosInfra, rootPath string, config *Subs } for _, fileName := range list { - fileContent, err := ioutil.ReadFile(rootPath + "/" + fileName) + fileContent, err := os.ReadFile(rootPath + "/" + fileName) if err != nil { return nil, fmt.Errorf("failed to read the file %v", err) } diff --git a/chaoscenter/graphql/server/pkg/chaoshub/handler/handler.go b/chaoscenter/graphql/server/pkg/chaoshub/handler/handler.go index a9af8cf6f97..63e027ce58d 100644 --- a/chaoscenter/graphql/server/pkg/chaoshub/handler/handler.go +++ b/chaoscenter/graphql/server/pkg/chaoshub/handler/handler.go @@ -5,9 +5,9 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net/http" "os" + "path" "path/filepath" "strconv" "strings" @@ -17,9 +17,7 @@ import ( chaoshubops "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaoshub/ops" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_hub" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/utils" - log "github.com/sirupsen/logrus" - "gopkg.in/yaml.v2" ) @@ -37,9 +35,9 @@ func GetChartsPath(chartsInput model.CloningInput, projectID string, isDefault b } // GetChartsData is used to get details of charts like experiments. -func GetChartsData(ChartsPath string) ([]*model.Chart, error) { +func GetChartsData(chartsPath string) ([]*model.Chart, error) { var allChartsDetails []ChaosChart - Charts, err := ioutil.ReadDir(ChartsPath) + Charts, err := os.ReadDir(path.Clean(chartsPath)) if err != nil { log.Error("file reading error", err) return nil, err @@ -48,7 +46,7 @@ func GetChartsData(ChartsPath string) ([]*model.Chart, error) { if chart.Name() == "icons" { continue } - chartDetails, _ := ReadExperimentFile(ChartsPath + chart.Name() + "/" + chart.Name() + ".chartserviceversion.yaml") + chartDetails, _ := ReadExperimentFile(chartsPath + chart.Name() + "/" + chart.Name() + ".chartserviceversion.yaml") allChartsDetails = append(allChartsDetails, chartDetails) } @@ -77,14 +75,17 @@ func GetExperimentData(experimentFilePath string) (*model.Chart, error) { return nil, err } var chartData *model.Chart - json.Unmarshal(e, &chartData) + err = json.Unmarshal(e, &chartData) + if err != nil { + return nil, err + } return chartData, nil } // ReadExperimentFile is used for reading experiment file from given path -func ReadExperimentFile(path string) (ChaosChart, error) { +func ReadExperimentFile(givenPath string) (ChaosChart, error) { var experiment ChaosChart - experimentFile, err := ioutil.ReadFile(path) + experimentFile, err := os.ReadFile(path.Clean(givenPath)) if err != nil { return experiment, fmt.Errorf("file path of the, err: %+v", err) } @@ -97,7 +98,7 @@ func ReadExperimentFile(path string) (ChaosChart, error) { // ReadExperimentYAMLFile is used for reading experiment/engine file from given path func ReadExperimentYAMLFile(path string) (string, error) { var s string - YAMLData, err := ioutil.ReadFile(path) + YAMLData, err := os.ReadFile(path) if err != nil { return s, fmt.Errorf("file path of the, err: %+v", err) } @@ -110,7 +111,7 @@ func ReadExperimentYAMLFile(path string) (string, error) { func ListPredefinedWorkflowDetails(name string, projectID string) ([]*model.PredefinedExperimentList, error) { experimentsPath := DefaultPath + projectID + "/" + name + "/workflows" var predefinedWorkflows []*model.PredefinedExperimentList - files, err := ioutil.ReadDir(experimentsPath) + files, err := os.ReadDir(experimentsPath) if err != nil { return nil, err } @@ -160,13 +161,17 @@ func DownloadRemoteHub(hubDetails model.CreateRemoteChaosHub, projectID string) return err } //create the destination directory where the hub will be downloaded - hubpath := dirPath + "/" + hubDetails.Name + ".zip" - destDir, err := os.Create(hubpath) + hubPath := dirPath + "/" + hubDetails.Name + ".zip" + destDir, err := os.Create(path.Clean(hubPath)) if err != nil { log.Error(err) return err } - defer destDir.Close() + defer func() { + if err := destDir.Close(); err != nil { + log.Warnf("failed to close dir: %v", err) + } + }() //download the zip file from the provided url download, err := http.Get(hubDetails.RepoURL) @@ -175,7 +180,11 @@ func DownloadRemoteHub(hubDetails model.CreateRemoteChaosHub, projectID string) return err } - defer download.Body.Close() + defer func() { + if err := download.Body.Close(); err != nil { + log.Warnf("failed to close body: %v", err) + } + }() if download.StatusCode != http.StatusOK { return fmt.Errorf("err: " + download.Status) @@ -189,14 +198,14 @@ func DownloadRemoteHub(hubDetails model.CreateRemoteChaosHub, projectID string) contentLength := download.Header.Get("content-length") length, err := strconv.Atoi(contentLength) if length > maxSize { - _ = os.Remove(hubpath) + _ = os.Remove(path.Clean(hubPath)) return fmt.Errorf("err: File size exceeded the threshold %d", length) } //validate the content-type contentType := download.Header.Get("content-type") if contentType != "application/zip" { - _ = os.Remove(hubpath) + _ = os.Remove(path.Clean(hubPath)) return fmt.Errorf("err: Invalid file type %s", contentType) } @@ -208,13 +217,13 @@ func DownloadRemoteHub(hubDetails model.CreateRemoteChaosHub, projectID string) } //unzip the ChaosHub to the default hub directory - err = UnzipRemoteHub(hubpath, hubDetails, projectID) + err = UnzipRemoteHub(hubPath, projectID) if err != nil { return err } //remove the redundant zip file - err = os.Remove(hubpath) + err = os.Remove(path.Clean(hubPath)) if err != nil { return err } @@ -222,16 +231,24 @@ func DownloadRemoteHub(hubDetails model.CreateRemoteChaosHub, projectID string) } // UnzipRemoteHub is used to unzip the zip file -func UnzipRemoteHub(zipPath string, hubDetails model.CreateRemoteChaosHub, projectID string) error { +func UnzipRemoteHub(zipPath string, projectID string) error { extractPath := DefaultPath + projectID zipReader, err := zip.OpenReader(zipPath) if err != nil { log.Error(err) return err } - defer zipReader.Close() + defer func() { + if err := zipReader.Close(); err != nil { + log.Warnf("failed to close reader: %v", err) + } + }() + for _, file := range zipReader.File { - CopyZipItems(file, extractPath, file.Name) + err := CopyZipItems(file, extractPath, file.Name) + if err != nil { + return err + } } return nil } @@ -260,9 +277,18 @@ func CopyZipItems(file *zip.File, extractPath string, chartsPath string) error { if err != nil { log.Error(err) } - fileCopy.Close() + defer func() { + if err := fileCopy.Close(); err != nil { + log.Warnf("failed to close file: %v", err) + } + }() + } - fileReader.Close() + defer func() { + if err := fileReader.Close(); err != nil { + log.Warnf("failed to close file: %v", err) + } + }() return nil } diff --git a/chaoscenter/graphql/server/pkg/chaoshub/handler/handler_test.go b/chaoscenter/graphql/server/pkg/chaoshub/handler/handler_test.go index 54ad7b63a0a..ffcd2106ac3 100644 --- a/chaoscenter/graphql/server/pkg/chaoshub/handler/handler_test.go +++ b/chaoscenter/graphql/server/pkg/chaoshub/handler/handler_test.go @@ -1,18 +1,16 @@ package handler_test import ( - "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" - "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaoshub/handler" - chaosHubOps "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaoshub/ops" - "github.com/litmuschaos/litmus/chaoscenter/graphql/server/utils" - - "io/ioutil" + "io" "os" "testing" "github.com/gin-gonic/gin" "github.com/google/uuid" - + "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" + "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaoshub/handler" + chaosHubOps "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaoshub/ops" + "github.com/litmuschaos/litmus/chaoscenter/graphql/server/utils" log "github.com/sirupsen/logrus" "github.com/stretchr/testify/assert" ) @@ -20,7 +18,7 @@ import ( // TestMain is the entry point for testing func TestMain(m *testing.M) { gin.SetMode(gin.TestMode) - log.SetOutput(ioutil.Discard) + log.SetOutput(io.Discard) os.Exit(m.Run()) } diff --git a/chaoscenter/graphql/server/pkg/chaoshub/ops/gitops_test.go b/chaoscenter/graphql/server/pkg/chaoshub/ops/gitops_test.go index b4e34a85f75..3494fb17970 100644 --- a/chaoscenter/graphql/server/pkg/chaoshub/ops/gitops_test.go +++ b/chaoscenter/graphql/server/pkg/chaoshub/ops/gitops_test.go @@ -2,7 +2,7 @@ package chaoshubops_test import ( "fmt" - "io/ioutil" + "io" "os" "testing" "time" @@ -25,7 +25,7 @@ var ( // TestMain is the entry point for testing func TestMain(m *testing.M) { gin.SetMode(gin.TestMode) - log.SetOutput(ioutil.Discard) + log.SetOutput(io.Discard) os.Exit(m.Run()) } diff --git a/chaoscenter/graphql/server/pkg/chaoshub/service.go b/chaoscenter/graphql/server/pkg/chaoshub/service.go index 278b14dec41..5d0a78255e9 100644 --- a/chaoscenter/graphql/server/pkg/chaoshub/service.go +++ b/chaoscenter/graphql/server/pkg/chaoshub/service.go @@ -4,11 +4,13 @@ import ( "context" "errors" "fmt" - "io/ioutil" "os" + "path" "strconv" "time" + "github.com/google/uuid" + "github.com/jinzhu/copier" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/authorization" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaoshub/handler" @@ -16,13 +18,9 @@ import ( "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb" dbSchemaChaosHub "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_hub" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/utils" - "go.mongodb.org/mongo-driver/mongo" - - "github.com/google/uuid" - "github.com/jinzhu/copier" - log "github.com/sirupsen/logrus" "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo" ) const ( @@ -480,22 +478,22 @@ func (c *chaosHubService) GetChaosFault(ctx context.Context, request model.Exper } //Get fault chartserviceversion.yaml data - csvPath := basePath + "/" + request.ExperimentName + ".chartserviceversion.yaml" - csvYaml, err := ioutil.ReadFile(csvPath) + csvPath := path.Clean(basePath + "/" + request.ExperimentName + ".chartserviceversion.yaml") + csvYaml, err := os.ReadFile(csvPath) if err != nil { csvYaml = []byte("") } //Get engine.yaml data - enginePath := basePath + "/" + "engine.yaml" - engineYaml, err := ioutil.ReadFile(enginePath) + enginePath := path.Clean(basePath + "/" + "engine.yaml") + engineYaml, err := os.ReadFile(enginePath) if err != nil { engineYaml = []byte("") } //Get fault.yaml data - faultPath := basePath + "/" + "fault.yaml" - faultYaml, err := ioutil.ReadFile(faultPath) + faultPath := path.Clean(basePath + "/" + "fault.yaml") + faultYaml, err := os.ReadFile(faultPath) if err != nil { faultYaml = []byte("") } @@ -729,8 +727,9 @@ func (c *chaosHubService) ListPredefinedExperiments(ctx context.Context, hubID s } else { hubPath = DefaultPath + projectID + "/" + hub.Name + "/experiments/" } + hubPath = path.Clean(hubPath) var predefinedWorkflows []*model.PredefinedExperimentList - files, err := ioutil.ReadDir(hubPath) + files, err := os.ReadDir(hubPath) if err != nil { return nil, err } @@ -803,24 +802,24 @@ func (c *chaosHubService) getPredefinedExperimentDetails(experimentsPath string, var ( csvManifest = "" workflowManifest = "" - path = experimentsPath + experiment + "/" + experiment + ".chartserviceversion.yaml" + predefinedPath = experimentsPath + experiment + "/" + experiment + ".chartserviceversion.yaml" isExist = true preDefinedWorkflow = &model.PredefinedExperimentList{} ) - _, err := os.Stat(path) + _, err := os.Stat(predefinedPath) if err != nil { isExist = false } if isExist { - yamlData, err := ioutil.ReadFile(experimentsPath + experiment + "/" + experiment + ".chartserviceversion.yaml") + yamlData, err := os.ReadFile(path.Clean(experimentsPath + experiment + "/" + experiment + ".chartserviceversion.yaml")) if err != nil { csvManifest = "" } csvManifest = string(yamlData) - yamlData, err = ioutil.ReadFile(experimentsPath + experiment + "/" + "experiment.yaml") + yamlData, err = os.ReadFile(path.Clean(experimentsPath + experiment + "/" + "experiment.yaml")) if err != nil { workflowManifest = "" } diff --git a/chaoscenter/graphql/server/pkg/gitops/gitops.go b/chaoscenter/graphql/server/pkg/gitops/gitops.go index 864297c4780..a37ac869dd3 100644 --- a/chaoscenter/graphql/server/pkg/gitops/gitops.go +++ b/chaoscenter/graphql/server/pkg/gitops/gitops.go @@ -6,16 +6,12 @@ import ( "errors" "fmt" "io" - "io/ioutil" "os" + "path" "strconv" "strings" "time" - "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" - "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/authorization" - "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/gitops" - "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" @@ -23,6 +19,9 @@ import ( "github.com/go-git/go-git/v5/plumbing/transport/http" "github.com/go-git/go-git/v5/plumbing/transport/ssh" "github.com/golang-jwt/jwt" + "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" + "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/authorization" + "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/gitops" log "github.com/sirupsen/logrus" ssh2 "golang.org/x/crypto/ssh" ) @@ -113,7 +112,7 @@ func (c GitConfig) setupGitRepo(user GitUser) error { gitInfo := map[string]string{"projectID": c.ProjectID, "revision": "1"} if exists { - data, err := ioutil.ReadFile(projectPath + "/.info") + data, err := os.ReadFile(path.Clean(projectPath + "/.info")) if err != nil { return errors.New("can't read existing git info file " + err.Error()) } @@ -137,7 +136,7 @@ func (c GitConfig) setupGitRepo(user GitUser) error { if err != nil { return err } - err = ioutil.WriteFile(projectPath+"/.info", data, 0644) + err = os.WriteFile(path.Clean(projectPath+"/.info"), data, 0644) if err != nil { return err } diff --git a/chaoscenter/graphql/server/pkg/gitops/service.go b/chaoscenter/graphql/server/pkg/gitops/service.go index 9fbb662fecd..0f8cd1e108c 100644 --- a/chaoscenter/graphql/server/pkg/gitops/service.go +++ b/chaoscenter/graphql/server/pkg/gitops/service.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "io/ioutil" "os" "path/filepath" "strconv" @@ -12,19 +11,18 @@ import ( "sync" "time" - chaosExperimentOps "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaos_experiment/ops" - "github.com/ghodss/yaml" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" - chaos_infra "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaos_infrastructure" - data_store "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/data-store" + chaosExperimentOps "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaos_experiment/ops" + chaosInfra "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaos_infrastructure" + dataStore "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/data-store" store "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/data-store" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_experiment" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/chaos_infrastructure" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/database/mongodb/gitops" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/grpc" - "github.com/sirupsen/logrus" + log "github.com/sirupsen/logrus" "github.com/tidwall/gjson" "github.com/tidwall/sjson" "go.mongodb.org/mongo-driver/bson" @@ -68,7 +66,7 @@ func NewGitOpsService(gitOpsOperator *gitops.Operator, chaosExperimentService ch } } -// GitOpsNotificationHandler sends experiment run request(single run experiment only) to agent on gitops notification +// GitOpsNotificationHandler sends experiment run request(single run experiment only) to agent on GitOps notification func (g *gitOpsService) GitOpsNotificationHandler(ctx context.Context, infra chaos_infrastructure.ChaosInfra, experimentID string) (string, error) { gitLock.Lock(infra.ProjectID, nil) defer gitLock.Unlock(infra.ProjectID, nil) @@ -77,7 +75,7 @@ func (g *gitOpsService) GitOpsNotificationHandler(ctx context.Context, infra cha return "", errors.New("Cannot get Git Config from DB : " + err.Error()) } if config == nil { - return "Gitops Disabled", nil + return "GitOps Disabled", nil } query := bson.D{{"infra_id", infra.InfraID}, {"experiment_id", experimentID}, {"is_removed", false}} experiments, err := g.chaosExperimentOps.GetExperiments(query) @@ -99,7 +97,7 @@ func (g *gitOpsService) GitOpsNotificationHandler(ctx context.Context, infra cha } username := "git-ops" - chaos_infra.SendExperimentToSubscriber(experiments[0].ProjectID, &model.ChaosExperimentRequest{ + chaosInfra.SendExperimentToSubscriber(experiments[0].ProjectID, &model.ChaosExperimentRequest{ ExperimentManifest: experiments[0].Revision[len(experiments[0].Revision)-1].ExperimentManifest, InfraID: experiments[0].InfraID, }, &username, nil, "create", store.Store) @@ -107,7 +105,7 @@ func (g *gitOpsService) GitOpsNotificationHandler(ctx context.Context, infra cha return "Request Acknowledged for experimentID: " + experimentID, nil } -// EnableGitOpsHandler enables gitops for a particular project +// EnableGitOpsHandler enables GitOps for a particular project func (g *gitOpsService) EnableGitOpsHandler(ctx context.Context, projectID string, config model.GitConfig) (bool, error) { gitLock.Lock(projectID, nil) defer gitLock.Unlock(projectID, nil) @@ -117,14 +115,18 @@ func (g *gitOpsService) EnableGitOpsHandler(ctx context.Context, projectID strin var conn *grpc2.ClientConn client, conn := grpc.GetAuthGRPCSvcClient(conn) - defer conn.Close() + defer func() { + if err := conn.Close(); err != nil { + log.Warnf("failed to close connection: %v", err) + } + }() _, err := grpc.GetProjectById(client, projectID) if err != nil { return false, errors.New("Failed to setup GitOps : " + err.Error()) } - logrus.Info("Enabling Gitops") + logrus.Info("Enabling GitOps") gitDB := gitops.GetGitConfigDB(projectID, config) commit, err := SetupGitOps(GitUserFromContext(ctx), GetGitOpsConfig(gitDB)) @@ -141,12 +143,12 @@ func (g *gitOpsService) EnableGitOpsHandler(ctx context.Context, projectID strin return true, nil } -// DisableGitOpsHandler disables gitops for a specific project +// DisableGitOpsHandler disables GitOps for a specific project func (g *gitOpsService) DisableGitOpsHandler(ctx context.Context, projectID string) (bool, error) { gitLock.Lock(projectID, nil) defer gitLock.Unlock(projectID, nil) - logrus.Info("Disabling Gitops") + logrus.Info("Disabling GitOps") err := g.gitOpsOperator.DeleteGitConfig(ctx, projectID) if err != nil { return false, errors.New("Failed to delete git config from DB : " + err.Error()) @@ -160,7 +162,7 @@ func (g *gitOpsService) DisableGitOpsHandler(ctx context.Context, projectID stri return true, nil } -// UpdateGitOpsDetailsHandler updates an exiting gitops config for a project +// UpdateGitOpsDetailsHandler updates an exiting GitOps config for a project func (g *gitOpsService) UpdateGitOpsDetailsHandler(ctx context.Context, projectID string, config model.GitConfig) (bool, error) { gitLock.Lock(projectID, nil) defer gitLock.Unlock(projectID, nil) @@ -176,7 +178,7 @@ func (g *gitOpsService) UpdateGitOpsDetailsHandler(ctx context.Context, projectI return false, errors.New("GitOps Disabled ") } - logrus.Info("Enabling Gitops") + logrus.Info("Enabling GitOps") gitDB := gitops.GetGitConfigDB(projectID, config) gitConfig := GetGitOpsConfig(gitDB) @@ -205,7 +207,7 @@ func (g *gitOpsService) UpdateGitOpsDetailsHandler(ctx context.Context, projectI return true, nil } -// GetGitOpsDetails returns the current gitops config for the requested project +// GetGitOpsDetails returns the current GitOps config for the requested project func (g *gitOpsService) GetGitOpsDetails(ctx context.Context, projectID string) (*model.GitConfigResponse, error) { gitLock.Lock(projectID, nil) defer gitLock.Unlock(projectID, nil) @@ -269,7 +271,7 @@ func (g *gitOpsService) UpsertExperimentToGit(ctx context.Context, projectID str return errors.New("Cannot convert manifest to yaml : " + err.Error()) } - err = ioutil.WriteFile(experimentPath, data, 0644) + err = os.WriteFile(experimentPath, data, 0644) if err != nil { return errors.New("Cannot write experiment to git : " + err.Error()) } @@ -468,7 +470,7 @@ func (g *gitOpsService) SyncDBToGit(ctx context.Context, config GitConfig) error continue } // read changes [new additions/updates] - data, err := ioutil.ReadFile(config.LocalPath + "/" + file) + data, err := os.ReadFile(config.LocalPath + "/" + file) if err != nil { logrus.Error("Error reading data from git file : " + file + " | " + err.Error()) continue @@ -573,7 +575,7 @@ func (g *gitOpsService) createExperiment(ctx context.Context, data, file string, return false, errors.New("Cannot convert manifest to yaml : " + err.Error()) } - err = ioutil.WriteFile(experimentPath, yamlData, 0644) + err = os.WriteFile(experimentPath, yamlData, 0644) if err != nil { return false, errors.New("Cannot write experiment to git : " + err.Error()) } @@ -624,10 +626,10 @@ func (g *gitOpsService) updateExperiment(ctx context.Context, data, wfID, file s if err != nil { return err } - return g.chaosExperimentService.ProcessExperimentUpdate(input, "git-ops", wfType, revID, updateRevision, config.ProjectID, data_store.Store) + return g.chaosExperimentService.ProcessExperimentUpdate(input, "git-ops", wfType, revID, updateRevision, config.ProjectID, dataStore.Store) } -// deleteExperiment helps in deleting a experiment from DB during the SyncDBToGit operation +// deleteExperiment helps in deleting an experiment from DB during the SyncDBToGit operation func (g *gitOpsService) deleteExperiment(file string, config GitConfig) error { _, fileName := filepath.Split(file) fileName = strings.Replace(fileName, ".yaml", "", -1) @@ -638,5 +640,5 @@ func (g *gitOpsService) deleteExperiment(file string, config GitConfig) error { return err } - return g.chaosExperimentService.ProcessExperimentDelete(query, experiment, "git-ops", data_store.Store) + return g.chaosExperimentService.ProcessExperimentDelete(query, experiment, "git-ops", dataStore.Store) } diff --git a/chaoscenter/subscriber/go.sum b/chaoscenter/subscriber/go.sum index f2df63f6892..82c972e25a5 100644 --- a/chaoscenter/subscriber/go.sum +++ b/chaoscenter/subscriber/go.sum @@ -234,6 +234,7 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8= diff --git a/chaoscenter/subscriber/pkg/graphql/operations.go b/chaoscenter/subscriber/pkg/graphql/operations.go index 510bdf546af..b2ea6372454 100644 --- a/chaoscenter/subscriber/pkg/graphql/operations.go +++ b/chaoscenter/subscriber/pkg/graphql/operations.go @@ -3,10 +3,12 @@ package graphql import ( "bytes" "encoding/json" - "io/ioutil" + "io" "net/http" "strconv" "strings" + + log "github.com/sirupsen/logrus" ) func (gql *subscriberGql) SendRequest(server string, payload []byte) (string, error) { @@ -20,8 +22,13 @@ func (gql *subscriberGql) SendRequest(server string, payload []byte) (string, er return "", err } - body, err := ioutil.ReadAll(resp.Body) - resp.Body.Close() + body, err := io.ReadAll(resp.Body) + defer func() { + if err := resp.Body.Close(); err != nil { + log.Warnf("failed to close body: %v", err) + } + }() + if err != nil { return "", err }