From 516a7a054111a2259fdc0f4fb9f77eb32f2921d4 Mon Sep 17 00:00:00 2001 From: Nageshbansal <76246968+Nageshbansal@users.noreply.github.com> Date: Fri, 1 Mar 2024 16:06:32 +0530 Subject: [PATCH 1/3] Adds docs for toleratios in source cmdProbe (#4442) Signed-off-by: nagesh bansal --- .../chaos-resources/probes/cmdProbe.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/mkdocs/docs/experiments/concepts/chaos-resources/probes/cmdProbe.md b/mkdocs/docs/experiments/concepts/chaos-resources/probes/cmdProbe.md index 1b0e4cb89fe..b7b2e9733fe 100644 --- a/mkdocs/docs/experiments/concepts/chaos-resources/probes/cmdProbe.md +++ b/mkdocs/docs/experiments/concepts/chaos-resources/probes/cmdProbe.md @@ -614,6 +614,29 @@ In source mode, the command execution is carried out from within a new pod whose + + + + + + + + + + + + + + + + + + + + + +
Field.tolerations
DescriptionFlag to hold the tolerations for the probe pod
TypeOptional
Range(type: []corev1.Tolerations
NotesThe .tolerations Flag to hold the Tolerations for the probe pod
+ From 5449439d8ca5bf7d2977dc47ebbf86965665f1de Mon Sep 17 00:00:00 2001 From: Kanhaiya yadav <93936630+kanhaiya04@users.noreply.github.com> Date: Mon, 4 Mar 2024 18:00:26 +0530 Subject: [PATCH 2/3] pagination is now working for environment (#4470) Signed-off-by: kanhaiya04 --- chaoscenter/web/src/controllers/Environments/Environment.tsx | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/chaoscenter/web/src/controllers/Environments/Environment.tsx b/chaoscenter/web/src/controllers/Environments/Environment.tsx index 1bf5d2517ca..2abb02dfd08 100644 --- a/chaoscenter/web/src/controllers/Environments/Environment.tsx +++ b/chaoscenter/web/src/controllers/Environments/Environment.tsx @@ -15,7 +15,7 @@ const EnvironmentController: React.FC = () => { // State for pagination const page = parseInt(searchParams.get('page') ?? '0'); - const limit = parseInt(searchParams.get('limit') ?? '15'); + const limit = parseInt(searchParams.get('limit') ?? '5'); const setPage = (newPage: number): void => updateSearchParams({ page: newPage.toString() }); const setLimit = (newLimit: number): void => updateSearchParams({ limit: newLimit.toString() }); @@ -27,7 +27,7 @@ const EnvironmentController: React.FC = () => { } = listEnvironment({ projectID: scope.projectID, environmentIDs: [], - pagination: { page, limit: 5 }, + pagination: { page, limit }, options: { onError: err => showError(err.message), fetchPolicy: 'cache-first' @@ -57,7 +57,6 @@ const EnvironmentController: React.FC = () => { const environments = envData?.listEnvironments.environments; const totalEnvironments = envData?.listEnvironments.totalNoOfEnvironments; - const environmentTableData: EnvironmentDetailsTableProps | undefined = environments && { content: generateEnvironmentTableContent(environments), pagination: { From da8ae756998bfb388972d9536d223d57a6e5ce47 Mon Sep 17 00:00:00 2001 From: Namkyu Park <53862866+namkyu1999@users.noreply.github.com> Date: Tue, 5 Mar 2024 16:05:21 +0900 Subject: [PATCH 3/3] Replace deprecated package (#4475) * fix: replace deprecated library in chaoscenter/graphql Signed-off-by: namkyu1999 * fix: replace deprecated library in authentication server Signed-off-by: namkyu1999 * fix: replace deprecated library in subscriber Signed-off-by: namkyu1999 * fix: resolve security issue Signed-off-by: namkyu1999 * fix: update codeql version Signed-off-by: namkyu1999 --------- Signed-off-by: namkyu1999 --- .github/workflows/codeql-analysis.yml | 6 +- .../api/handlers/rest/user_handlers_test.go | 8 +- .../pkg/chaos_infrastructure/infra_utils.go | 18 +++-- .../server/pkg/chaoshub/handler/handler.go | 74 +++++++++++++------ .../pkg/chaoshub/handler/handler_test.go | 14 ++-- .../server/pkg/chaoshub/ops/gitops_test.go | 4 +- .../graphql/server/pkg/chaoshub/service.go | 33 ++++----- .../graphql/server/pkg/gitops/gitops.go | 13 ++-- .../graphql/server/pkg/gitops/service.go | 48 ++++++------ chaoscenter/subscriber/go.sum | 1 + .../subscriber/pkg/graphql/operations.go | 13 +++- 11 files changed, 133 insertions(+), 99 deletions(-) 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 }
Field