Replace deprecated package (#4559)

* upgrade: upgrade codeql version

Signed-off-by: namkyu1999 <lak9348@konkuk.ac.kr>

* feat: replace deprecated pkg in auth

Signed-off-by: namkyu1999 <lak9348@konkuk.ac.kr>

* feat: replace unused pkg to subscriber

Signed-off-by: namkyu1999 <lak9348@konkuk.ac.kr>

* fix: replace unused pkg to graphql

Signed-off-by: namkyu1999 <lak9348@konkuk.ac.kr>

* fix: resolve conflict

Signed-off-by: namkyu1999 <lak9348@konkuk.ac.kr>

---------

Signed-off-by: namkyu1999 <lak9348@konkuk.ac.kr>
Co-authored-by: Saranya Jena <saranya.jena@harness.io>
This commit is contained in:
Namkyu Park 2024-05-13 03:12:51 +09:00 committed by GitHub
parent 53c1165144
commit 87f06850ab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 160 additions and 202 deletions

View File

@ -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

View File

@ -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())
}
@ -519,7 +519,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([]byte(bodyBytes)))
c.Set("role", tt.mockRole)
c.Set("uid", tt.mockUID)
c.Set("username", tt.mockUsername)
@ -595,7 +595,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)

View File

@ -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,14 +127,19 @@ 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(file *os.File) {
err := file.Close()
if err != nil {
log.Errorf("failed to close the file %v", err)
}
}(file)
list, err := file.Readdirnames(0) // 0 to read all files and folders
if err != nil {
return nil, fmt.Errorf("failed to read the file %v", err)
}
var nodeselector string
var nodeSelector string
if infra.NodeSelector != nil {
selector := strings.Split(*infra.NodeSelector, ",")
selectorList := make(map[string]string)
@ -145,18 +148,17 @@ func ManifestParser(infra dbChaosInfra.ChaosInfra, rootPath string, config *Subs
selectorList[kv[0]] = kv[1]
}
nodeSelector := struct {
NodeSelector map[string]string `yaml:"nodeSelector" json:"nodeSelector"`
}{
NodeSelector: selectorList,
}
byt, err := yaml.Marshal(nodeSelector)
byt, err := yaml.Marshal(
struct {
NodeSelector map[string]string `yaml:"nodeSelector" json:"nodeSelector"`
}{
NodeSelector: selectorList,
})
if err != nil {
return nil, fmt.Errorf("failed to marshal the node selector %v", err)
}
nodeselector = string(utils.AddRootIndent(byt, 6))
nodeSelector = string(utils.AddRootIndent(byt, 6))
}
var tolerations string
@ -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)
}
@ -209,7 +211,7 @@ func ManifestParser(infra dbChaosInfra.ChaosInfra, rootPath string, config *Subs
}
if infra.NodeSelector != nil {
newContent = strings.Replace(newContent, "#{NODE_SELECTOR}", nodeselector, -1)
newContent = strings.Replace(newContent, "#{NODE_SELECTOR}", nodeSelector, -1)
}
generatedYAML = append(generatedYAML, newContent)
}
@ -250,7 +252,8 @@ func SendExperimentToSubscriber(projectID string, workflow *model.ChaosExperimen
var workflowObj unstructured.Unstructured
err := yaml.Unmarshal([]byte(workflow.ExperimentManifest), &workflowObj)
if err != nil {
fmt.Errorf("error while parsing experiment manifest %v", err)
log.Errorf("error while parsing experiment manifest %v", err)
return
}
SendRequestToSubscriber(SubscriberRequests{

View File

@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
@ -19,9 +18,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"
)
@ -39,9 +36,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(chartsPath)
if err != nil {
log.Error("file reading error", err)
return nil, err
@ -50,7 +47,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)
}
@ -79,14 +76,16 @@ func GetExperimentData(experimentFilePath string) (*model.Chart, error) {
return nil, err
}
var chartData *model.Chart
json.Unmarshal(e, &chartData)
if err = json.Unmarshal(e, &chartData); err != nil {
return nil, err
}
return chartData, nil
}
// ReadExperimentFile is used for reading experiment file from given path
func ReadExperimentFile(path string) (ChaosChart, error) {
var experiment ChaosChart
experimentFile, err := ioutil.ReadFile(path)
experimentFile, err := os.ReadFile(path)
if err != nil {
return experiment, fmt.Errorf("file path of the, err: %+v", err)
}
@ -99,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)
}
@ -112,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
}
@ -162,8 +161,8 @@ 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(hubPath)
if err != nil {
log.Error(err)
return err
@ -191,14 +190,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(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(hubPath)
return fmt.Errorf("err: Invalid file type %s", contentType)
}
@ -210,13 +209,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(hubPath)
if err != nil {
return err
}
@ -224,16 +223,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(zipReader *zip.ReadCloser) {
err := zipReader.Close()
if err != nil {
log.Error(err)
}
}(zipReader)
for _, file := range zipReader.File {
CopyZipItems(file, extractPath, file.Name)
err := CopyZipItems(file, extractPath, file.Name)
if err != nil {
return err
}
}
return nil
}
@ -365,7 +372,12 @@ func DefaultChaosHubIconHandler() gin.HandlerFunc {
}
}
defer img.Close()
defer func(img *os.File) {
err := img.Close()
if err != nil {
log.WithError(err).Error("error while closing the file")
}
}(img)
c.Writer.Header().Set("Content-Type", "image/png")
c.Writer.WriteHeader(responseStatusCode)

View File

@ -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())
}

View File

@ -2,18 +2,17 @@ package chaoshubops_test
import (
"fmt"
"io/ioutil"
"io"
"os"
"testing"
"time"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
chaosHubOps "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaoshub/ops"
"github.com/gin-gonic/gin"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/google/uuid"
"github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model"
chaosHubOps "github.com/litmuschaos/litmus/chaoscenter/graphql/server/pkg/chaoshub/ops"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
@ -25,7 +24,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())
}

View File

@ -4,11 +4,12 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"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 +17,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 (
@ -67,8 +64,8 @@ func NewService(chaosHubOperator *dbSchemaChaosHub.Operator) Service {
func (c *chaosHubService) AddChaosHub(ctx context.Context, chaosHub model.CreateChaosHubRequest, projectID string) (*model.ChaosHub, error) {
if IsExist, err := c.IsChaosHubAvailable(ctx, chaosHub.Name, projectID); err != nil {
return nil, err
} else if IsExist {
return nil, errors.New("Name Already exists")
} else if IsExist == true {
return nil, errors.New("name already exists")
}
currentTime := time.Now()
cloneHub := NewCloningInputFrom(chaosHub)
@ -123,7 +120,7 @@ func (c *chaosHubService) AddChaosHub(ctx context.Context, chaosHub model.Create
return nil, err
}
// Cloning the repository at a path from chaoshub link structure.
// Cloning the repository at a path from ChaosHub link structure.
if err := chaosHubOps.GitClone(cloneHub, projectID); err != nil {
log.Error(err)
}
@ -136,8 +133,8 @@ func (c *chaosHubService) AddRemoteChaosHub(ctx context.Context, chaosHub model.
if err != nil {
return nil, err
}
if IsExist {
return nil, errors.New("Name Already exists")
if IsExist == true {
return nil, errors.New("name already exists")
}
description := ""
if chaosHub.Description != nil {
@ -205,8 +202,8 @@ func (c *chaosHubService) SaveChaosHub(ctx context.Context, chaosHub model.Creat
if err != nil {
return nil, err
}
if IsExist {
return nil, errors.New("Name Already exists")
if IsExist == true {
return nil, errors.New("name already exists")
}
// Initialize a UID for new Hub.
@ -345,7 +342,7 @@ func (c *chaosHubService) UpdateChaosHub(ctx context.Context, chaosHub model.Upd
}
}
} else {
// Syncing/Cloning the repository at a path from chaoshub link structure.
// Syncing/Cloning the repository at a path from ChaosHub link structure.
if prevChaosHub.Name != chaosHub.Name || prevChaosHub.RepoURL != chaosHub.RepoURL || prevChaosHub.RepoBranch != chaosHub.RepoBranch || prevChaosHub.IsPrivate != chaosHub.IsPrivate || prevChaosHub.AuthType != chaosHub.AuthType.String() {
err = os.RemoveAll(clonePath)
if err != nil {
@ -481,21 +478,21 @@ 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)
csvYaml, err := os.ReadFile(csvPath)
if err != nil {
csvYaml = []byte("")
}
//Get engine.yaml data
enginePath := basePath + "/" + "engine.yaml"
engineYaml, err := ioutil.ReadFile(enginePath)
engineYaml, err := os.ReadFile(enginePath)
if err != nil {
engineYaml = []byte("")
}
//Get fault.yaml data
faultPath := basePath + "/" + "fault.yaml"
faultYaml, err := ioutil.ReadFile(faultPath)
faultYaml, err := os.ReadFile(faultPath)
if err != nil {
faultYaml = []byte("")
}
@ -507,7 +504,7 @@ func (c *chaosHubService) GetChaosFault(ctx context.Context, request model.Exper
}, nil
}
// ListChaosHubs returns the array of hubdetails with their current status.
// ListChaosHubs returns the array of hub details with their current status.
func (c *chaosHubService) ListChaosHubs(ctx context.Context, projectID string, request *model.ListChaosHubRequest) ([]*model.ChaosHubStatus, error) {
defaultHub := c.listDefaultHubs()
updatedDefaultHub := dbSchemaChaosHub.ChaosHub{
@ -730,7 +727,7 @@ func (c *chaosHubService) ListPredefinedExperiments(ctx context.Context, hubID s
hubPath = DefaultPath + projectID + "/" + hub.Name + "/experiments/"
}
var predefinedWorkflows []*model.PredefinedExperimentList
files, err := ioutil.ReadDir(hubPath)
files, err := os.ReadDir(hubPath)
if err != nil {
return nil, err
}
@ -813,14 +810,14 @@ func (c *chaosHubService) getPredefinedExperimentDetails(experimentsPath string,
}
if isExist {
yamlData, err := ioutil.ReadFile(experimentsPath + experiment + "/" + experiment + ".chartserviceversion.yaml")
yamlData, err := os.ReadFile(experimentsPath + experiment + "/" + experiment + ".chartserviceversion.yaml")
if err != nil {
csvManifest = ""
}
csvManifest = string(yamlData)
yamlData, err = ioutil.ReadFile(experimentsPath + experiment + "/" + "experiment.yaml")
yamlData, err = os.ReadFile(experimentsPath + experiment + "/" + "experiment.yaml")
if err != nil {
workflowManifest = ""
}
@ -837,64 +834,6 @@ func (c *chaosHubService) getPredefinedExperimentDetails(experimentsPath string,
return preDefinedWorkflow
}
// GetExperimentManifestDetails is used to send the ChaosEngine and ChaosExperiment YAMLs
//func (c *chaosHubService) GetExperimentManifestDetails(ctx context.Context, request model.ExperimentRequest, projectID string) (*model.ExperimentDetails, error) {
//
// engineType := model.FileTypeEngine
// experimentType := model.FileTypeExperiment
//
// engineData, err := c.GetYAMLData(model.ExperimentRequest{
// ChartName: request.ChartName,
// ExperimentName: request.ExperimentName,
// Name: request.Name,
// FileType: (*string)(&engineType),
// }, projectID)
// if err != nil {
// engineData = ""
// }
// experimentData, err := c.GetYAMLData(model.ExperimentRequest{
// ChartName: request.ChartName,
// ExperimentName: request.ExperimentName,
// Name: request.Name,
// FileType: (*string)(&experimentType),
// }, projectID)
// if err != nil {
// experimentData = ""
// }
// experimentDetails := &model.ExperimentDetails{
// EngineDetails: engineData,
// ExperimentDetails: experimentData,
// }
// return experimentDetails, nil
//}
//func (c *chaosHubService) ListPredefinedWorkflows(name string, projectID string) ([]*model.PredefinedWorkflowList, error) {
// workflowsList, err := handler.ListPredefinedWorkflowDetails(name, projectID)
// if err != nil {
// return nil, err
// }
// return workflowsList, nil
//}
// GetPredefinedExperimentYAMLData is responsible for sending the workflow.yaml for a given pre-defined workflow.
//func (c *chaosHubService) GetPredefinedExperimentYAMLData(request model.ExperimentRequest, projectID string) (string, error) {
// var YAMLPath string
// if request.FileType == nil {
// return "", errors.New("provide a valid filetype")
// }
// if strings.ToLower(*request.FileType) != "workflow" {
// return "", errors.New("invalid file type")
// }
// if strings.ToLower(request.ChartName) == "predefined" && strings.ToLower(*request.FileType) == "workflow" {
// YAMLPath = handler.GetPredefinedExperimentManifest(request, projectID)
// }
// YAMLData, err := handler.ReadExperimentYAMLFile(YAMLPath)
// if err != nil {
// return "", err
// }
// return YAMLData, nil
//}
// IsChaosHubAvailable is used for checking if hub already exist or not
func (c *chaosHubService) IsChaosHubAvailable(ctx context.Context, name string, projectID string) (bool, error) {
chaosHubs, err := c.chaosHubOperator.GetChaosHubByProjectID(ctx, projectID)

View File

@ -6,16 +6,11 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"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 +18,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"
)
@ -87,7 +85,7 @@ func GetGitOpsConfig(repoData gitops.GitConfigDB) GitConfig {
LatestCommit: repoData.LatestCommit,
UserName: repoData.UserName,
Password: repoData.Password,
AuthType: model.AuthType(repoData.AuthType),
AuthType: repoData.AuthType,
Token: repoData.Token,
SSHPrivateKey: repoData.SSHPrivateKey,
}
@ -95,7 +93,7 @@ func GetGitOpsConfig(repoData gitops.GitConfigDB) GitConfig {
return gitConfig
}
// setupGitRepo helps clones and sets up the repo for gitops
// setupGitRepo helps clones and sets up the repo for GitOps
func (c GitConfig) setupGitRepo(user GitUser) error {
projectPath := c.LocalPath + "/" + ProjectDataPath + "/" + c.ProjectID
@ -113,7 +111,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(projectPath + "/.info")
if err != nil {
return errors.New("can't read existing git info file " + err.Error())
}
@ -137,7 +135,7 @@ func (c GitConfig) setupGitRepo(user GitUser) error {
if err != nil {
return err
}
err = ioutil.WriteFile(projectPath+"/.info", data, 0644)
err = os.WriteFile(projectPath+"/.info", data, 0644)
if err != nil {
return err
}
@ -204,7 +202,7 @@ func (c GitConfig) getAuthMethod() (transport.AuthMethod, error) {
}
}
// UnsafeGitPull executes git pull after a hard reset when uncommited changes are present in repo. Not safe.
// UnsafeGitPull executes git pull after a hard reset when uncommitted changes are present in repo. Not safe.
func (c GitConfig) UnsafeGitPull() error {
cleanStatus, err := c.GitGetStatus()
if err != nil {
@ -282,7 +280,7 @@ func (c GitConfig) GitPull() error {
ReferenceName: plumbing.NewBranchReferenceName(c.Branch),
SingleBranch: true,
})
if err != nil && err != git.NoErrAlreadyUpToDate {
if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
return err
}
return nil
@ -325,7 +323,7 @@ func (c GitConfig) GitPush() error {
Auth: auth,
Progress: nil,
})
if err == git.NoErrAlreadyUpToDate {
if errors.Is(err, git.NoErrAlreadyUpToDate) {
return nil
}
return err
@ -445,7 +443,7 @@ func (c GitConfig) GetLatestCommitHash() (string, error) {
return commit.Hash.String(), nil
}
// SetupGitOps clones and sets up the repo for gitops and returns the LatestCommit
// SetupGitOps clones and sets up the repo for git ops and returns the LatestCommit
func SetupGitOps(user GitUser, gitConfig GitConfig) (string, error) {
err := gitConfig.setupGitRepo(user)
if err != nil {

View File

@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
@ -12,19 +11,17 @@ 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 +65,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,12 +74,12 @@ 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)
if err != nil {
logrus.Error("Could not get experiment :", err)
log.Error("Could not get experiment :", err)
return "could not get experiment", err
}
if len(experiments) == 0 {
@ -94,12 +91,12 @@ func (g *gitOpsService) GitOpsNotificationHandler(ctx context.Context, infra cha
}
experiments[0].Revision[len(experiments[0].Revision)-1].ExperimentManifest, err = sjson.Set(experiments[0].Revision[len(experiments[0].Revision)-1].ExperimentManifest, "metadata.name", experiments[0].Name+"-"+strconv.FormatInt(time.Now().UnixMilli(), 10))
if err != nil {
logrus.Error("Failed to updated experiment name :", err)
log.Error("Failed to updated experiment name :", err)
return "", errors.New("Failed to updated experiment name " + err.Error())
}
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 +104,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 +114,19 @@ func (g *gitOpsService) EnableGitOpsHandler(ctx context.Context, projectID strin
var conn *grpc2.ClientConn
client, conn := grpc.GetAuthGRPCSvcClient(conn)
defer conn.Close()
defer func(conn *grpc2.ClientConn) {
err := conn.Close()
if err != nil {
log.Error("Failed to close connection : ", err)
}
}(conn)
_, err := grpc.GetProjectById(client, projectID)
if err != nil {
return false, errors.New("Failed to setup GitOps : " + err.Error())
}
logrus.Info("Enabling Gitops")
log.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")
log.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")
log.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())
}
@ -296,7 +298,7 @@ func (g *gitOpsService) UpsertExperimentToGit(ctx context.Context, projectID str
// DeleteExperimentFromGit deletes experiment from git
func (g *gitOpsService) DeleteExperimentFromGit(ctx context.Context, projectID string, experiment *model.ChaosExperimentRequest) error {
logrus.Info("Deleting Experiment...")
log.Info("Deleting Experiment...")
gitLock.Lock(projectID, nil)
defer gitLock.Unlock(projectID, nil)
@ -323,7 +325,7 @@ func (g *gitOpsService) DeleteExperimentFromGit(ctx context.Context, projectID s
return errors.New("Cannot delete experiment from git : " + err.Error())
}
if !exists {
logrus.Error("File not found in git : ", gitConfig.LocalPath+"/"+experimentPath)
log.Error("File not found in git : ", gitConfig.LocalPath+"/"+experimentPath)
return nil
}
err = os.RemoveAll(gitConfig.LocalPath + "/" + experimentPath)
@ -333,13 +335,13 @@ func (g *gitOpsService) DeleteExperimentFromGit(ctx context.Context, projectID s
commit, err := gitConfig.GitCommit(GitUserFromContext(ctx), "Deleted Experiment : "+experiment.ExperimentName, &experimentPath)
if err != nil {
logrus.Error("Error", err)
log.Error("Error", err)
return errors.New("Cannot commit experiment[delete] to git : " + err.Error())
}
err = gitConfig.GitPush()
if err != nil {
logrus.Error("Error", err)
log.Error("Error", err)
return errors.New("Cannot push experiment[delete] to git : " + err.Error())
}
@ -370,7 +372,7 @@ func (g *gitOpsService) gitSyncHelper(config gitops.GitConfigDB, wg *sync.WaitGr
// get most recent data from db after acquiring lock
conf, err := g.gitOpsOperator.GetGitConfig(ctx, config.ProjectID)
if err != nil {
logrus.Error("Repo Sync ERROR: ", config.ProjectID, err.Error())
log.Error("Repo Sync ERROR: ", config.ProjectID, err.Error())
}
if conf == nil {
return
@ -380,7 +382,7 @@ func (g *gitOpsService) gitSyncHelper(config gitops.GitConfigDB, wg *sync.WaitGr
err = g.SyncDBToGit(nil, gitConfig)
if err != nil {
logrus.Error("Repo Sync ERROR: ", conf.ProjectID, err.Error())
log.Error("Repo Sync ERROR: ", conf.ProjectID, err.Error())
}
}
@ -391,17 +393,17 @@ func (g *gitOpsService) GitOpsSyncHandler(singleRun bool) {
for {
ctx, cancel := context.WithTimeout(backgroundContext, timeout)
logrus.Info("Running GitOps DB Sync...")
log.Info("Running GitOps DB Sync...")
configs, err := g.gitOpsOperator.GetAllGitConfig(ctx)
cancel()
if err != nil {
logrus.Error("Failed to get git configs from db : ", err) //condition
log.Error("Failed to get git configs from db : ", err) //condition
}
count := len(configs)
if count > 0 {
logrus.Info("Updating : ", configs) // condition
log.Info("Updating : ", configs) // condition
count = count - 1
for count >= 0 {
@ -418,7 +420,7 @@ func (g *gitOpsService) GitOpsSyncHandler(singleRun bool) {
wg.Wait()
}
logrus.Info("GitOps DB Sync Complete") //condition
log.Info("GitOps DB Sync Complete") //condition
}
if singleRun {
break
@ -448,7 +450,7 @@ func (g *gitOpsService) SyncDBToGit(ctx context.Context, config GitConfig) error
if latestCommit == config.LatestCommit {
return nil
}
logrus.Info(latestCommit, " ", config.LatestCommit, "File Changes: ", files)
log.Info(latestCommit, " ", config.LatestCommit, "File Changes: ", files)
newExperiments := false
for file := range files {
if !strings.HasSuffix(file, ".yaml") {
@ -462,20 +464,20 @@ func (g *gitOpsService) SyncDBToGit(ctx context.Context, config GitConfig) error
if !exists {
err = g.deleteExperiment(file, config)
if err != nil {
logrus.Error("Error while deleting experiment db entry : " + file + " | " + err.Error())
log.Error("Error while deleting experiment db entry : " + file + " | " + err.Error())
continue
}
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())
log.Error("Error reading data from git file : " + file + " | " + err.Error())
continue
}
data, err = yaml.YAMLToJSON(data)
if err != nil {
logrus.Error("Error unmarshalling data from git file : " + file + " | " + err.Error())
log.Error("Error unmarshalling data from git file : " + file + " | " + err.Error())
continue
}
wfID := gjson.Get(string(data), "metadata.labels.experiment_id").String()
@ -484,12 +486,12 @@ func (g *gitOpsService) SyncDBToGit(ctx context.Context, config GitConfig) error
continue
}
logrus.Info("WFID in changed File :", wfID)
log.Info("WFID in changed File :", wfID)
if wfID == "" {
logrus.Info("New Experiment pushed to git : " + file)
log.Info("New Experiment pushed to git : " + file)
flag, err := g.createExperiment(ctx, string(data), file, config)
if err != nil {
logrus.Error("Error while creating new experiment db entry : " + file + " | " + err.Error())
log.Error("Error while creating new experiment db entry : " + file + " | " + err.Error())
continue
}
if flag {
@ -498,7 +500,7 @@ func (g *gitOpsService) SyncDBToGit(ctx context.Context, config GitConfig) error
} else {
err = g.updateExperiment(ctx, string(data), wfID, file, config)
if err != nil {
logrus.Error("Error while updating experiment db entry : " + file + " | " + err.Error())
log.Error("Error while updating experiment db entry : " + file + " | " + err.Error())
continue
}
}
@ -539,7 +541,7 @@ func (g *gitOpsService) createExperiment(ctx context.Context, data, file string,
fileName = strings.Replace(fileName, ".yaml", "", -1)
wfName := gjson.Get(data, "metadata.name").String()
infraID := gjson.Get(data, "metadata.labels.infra_id").String()
logrus.Info("Experiment Details | wf_name: ", wfName, " infra_id: ", infraID)
log.Info("Experiment Details | wf_name: ", wfName, " infra_id: ", infraID)
if wfName == "" || infraID == "" {
return false, nil
}
@ -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())
}
@ -587,9 +589,9 @@ func (g *gitOpsService) updateExperiment(ctx context.Context, data, wfID, file s
fileName = strings.Replace(fileName, ".yaml", "", -1)
wfName := gjson.Get(data, "metadata.name").String()
infraID := gjson.Get(data, "metadata.labels.infra_id").String()
logrus.Info("Experiment Details | wf_name: ", wfName, " infra_id: ", infraID)
log.Info("Experiment Details | wf_name: ", wfName, " infra_id: ", infraID)
if wfName == "" || infraID == "" {
logrus.Error("Cannot Update experiment missing experiment name or infra id")
log.Error("Cannot Update experiment missing experiment name or infra id")
return nil
}
@ -603,7 +605,7 @@ func (g *gitOpsService) updateExperiment(ctx context.Context, data, wfID, file s
}
if infraID != experiment[0].InfraID {
logrus.Error("Cannot change infra id for existing experiment")
log.Error("Cannot change infra id for existing experiment")
return nil
}
@ -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 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)
}

View File

@ -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
}