Skip to content

Commit

Permalink
Merge pull request #291 from leanix/feature/HEL-2770
Browse files Browse the repository at this point in the history
add more admin logs
  • Loading branch information
daniellesarmiento authored Jun 16, 2023
2 parents a57b156 + 836a35b commit 2e9e687
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 31 deletions.
6 changes: 1 addition & 5 deletions pkg/iris/common/models/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import (
"time"
)

const (
typeAsK8sNamespace string = "leanix.vsm.item-discovered.kubernetes.namespace"
)

// struct to extend Log with RunId
type Log struct {
// Root *Logger
Expand Down Expand Up @@ -69,7 +65,7 @@ func NewAdminLogEvent(configurationId string, runId string, workspaceId string,
Type := fmt.Sprintf("leanix.vsm.item-logged.admin")
Scope := fmt.Sprintf("workspace/%s", workspaceId)
Source := fmt.Sprintf("kubernetes/%s#%s", configurationId, runId)
Time := fmt.Sprintf(time.Now().Format(time.RFC3339))
Time := fmt.Sprintf(time.Now().Format(time.RFC3339Nano))
DataContentType := fmt.Sprintf("application/json")
DataSchema := fmt.Sprintf("/vsm-iris/schemas/feedback-items/v1")

Expand Down
74 changes: 48 additions & 26 deletions pkg/iris/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,15 @@ func NewScanner(kind string, uri string, runId string, token string, workspaceId
}

const (
IN_PROGRESS string = "IN_PROGRESS"
FAILED string = "FAILED"
SUCCESSFUL string = "SUCCESSFUL"
SUCCESSFUL_WARNING string = "SUCCESSFUL_WARNING"
ERROR string = "ERROR"
WARNING string = "WARNING"
INFO string = "INFO"
WORKLOAD string = "WORKLOAD"
IN_PROGRESS string = "IN_PROGRESS"
FAILED string = "FAILED"
SUCCESSFUL string = "SUCCESSFUL"
ERROR string = "ERROR"
INFO string = "INFO"
WORKLOAD string = "WORKLOAD"
)

const StatusErrorFormat = "Scan failed while posting status. RunId: [%s], with reason: '%v'"
const StatusErrorFormat = "Scan failed while posting status. RunId: '%s', with reason: '%v'"

func (s *scanner) Scan(getKubernetesAPI kubernetes.GetKubernetesAPI, config *rest.Config, configurationName string) error {
configuration, err := s.configService.GetConfiguration(configurationName)
Expand All @@ -70,7 +68,7 @@ func (s *scanner) Scan(getKubernetesAPI kubernetes.GetKubernetesAPI, config *res
return err
}

logger.Infof("Scan started for RunId: [%s]", s.runId)
logger.Infof("Scan started for Run Id: '%s'", s.runId)
logger.Infof("Configuration used: %s", configuration)

err = s.ShareStatus(kubernetesConfig.ID, IN_PROGRESS, "Started Kubernetes Scan")
Expand All @@ -79,20 +77,29 @@ func (s *scanner) Scan(getKubernetesAPI kubernetes.GetKubernetesAPI, config *res
return err
}

feedbackErr := s.ShareAdminLogs(kubernetesConfig.ID, INFO, fmt.Sprintf("Scan started with Run Id: '%v'.", s.runId))
if feedbackErr != nil {
return feedbackErr
}

kubernetesAPI, err := getKubernetesAPI(config)
if err != nil {
return s.LogAndShareError("Scan failed while getting Kubernetes API. RunId: [%s], with reason: '%v'", ERROR, err, kubernetesConfig.ID)
return s.LogAndShareError("Scan failed while getting Kubernetes API. Run Id: '%s', with reason: '%v'", ERROR, err, kubernetesConfig.ID)
}

logger.Info("Retrieved kubernetes config successfully")
err = s.ShareAdminLogs(kubernetesConfig.ID, INFO, "Retrieved kubernetes config successfully")
if err != nil {
logger.Errorf(StatusErrorFormat, s.runId, err)
return err
}

if kubernetesConfig.DiscoveryMode == WORKLOAD {
logger.Info("Scanning of Workloads is enabled")
logger.Info("Workload scanning enabled.")
err = s.ShareAdminLogs(kubernetesConfig.ID, INFO, fmt.Sprintf("Workload scanning enabled for the configuration '%v'.", configurationName))
if err != nil {
logger.Errorf(StatusErrorFormat, s.runId, err)
return err
}
return s.ScanWorkloads(kubernetesAPI, kubernetesConfig)
}

Expand All @@ -109,31 +116,40 @@ func (s *scanner) ScanNamespaces(kubernetesConfig models.KubernetesConfig, kuber

nodes, err := kubernetesAPI.Nodes()
if err != nil {
return s.LogAndShareError("Scan failed while retrieving k8s cluster nodes. RunId: [%s], with reason: '%v'", ERROR, err, kubernetesConfig.ID)
return s.LogAndShareError("Scan failed while retrieving k8s cluster nodes. Run Id: '%s', with reason: '%v'", ERROR, err, kubernetesConfig.ID)
}

clusterDTO, err := mapper.MapCluster(kubernetesConfig.Cluster, nodes)
if err != nil {
return s.LogAndShareError("Scan failed while aggregating cluster information. RunId: [%s], with reason: '%v'", ERROR, err, kubernetesConfig.ID)
return s.LogAndShareError("Scan failed while aggregating cluster information. Run Id: '%s', with reason: '%v'", ERROR, err, kubernetesConfig.ID)
}
feedbackErr := s.ShareAdminLogs(kubernetesConfig.ID, INFO, fmt.Sprintf("Namespace scanning enabled for the cluster '%v'.", clusterDTO.Name))
if feedbackErr != nil {
return feedbackErr
}

// Aggregate cluster information for the event
namespaces, err := kubernetesAPI.Namespaces(kubernetesConfig.BlackListedNamespaces)
if err != nil {
return s.LogAndShareError("Scan failed while retrieving k8s namespaces. RunId: [%s], with reason: '%v'", ERROR, err, kubernetesConfig.ID)
return s.LogAndShareError("Scan failed while retrieving Kubernetes namespaces. Run Id: '%s', with reason: '%v'", ERROR, err, kubernetesConfig.ID)
}
//Fetch old scan results
ecstDiscoveredData, err := s.ProcessNamespace(kubernetesAPI, mapper, namespaces.Items, clusterDTO)
if err != nil {
return s.LogAndShareError("Scan failed while retrieving k8s deployments. RunId: [%s], with reason: '%v'", ERROR, err, kubernetesConfig.ID)
return s.LogAndShareError("Scan failed while retrieving k8s deployments. Run Id: '%s', with reason: '%v'", ERROR, err, kubernetesConfig.ID)
}

err = s.eventProducer.ProcessResults(ecstDiscoveredData, oldResults, kubernetesConfig.ID)
if err != nil {
return s.LogAndShareError("Scan failed while posting ECST results. RunId: [%s], with reason: '%v'", ERROR, err, kubernetesConfig.ID)
return s.LogAndShareError("Scan failed while posting ECST results. Run Id: '%s', with reason: '%v'", ERROR, err, kubernetesConfig.ID)
}

logger.Infof("Scan Finished for RunId: [%s]", s.runId)
feedbackErr = s.ShareAdminLogs(kubernetesConfig.ID, INFO, fmt.Sprintf("Found and processed %v unblacklisted namespaces from the cluster '%v'.", len(namespaces.Items), clusterDTO.Name))
if feedbackErr != nil {
return feedbackErr
}

logger.Infof("Scan Finished for RunId: '%s'", s.runId)
err = s.ShareStatus(kubernetesConfig.ID, SUCCESSFUL, "Successfully Scanned")
if err != nil {
logger.Errorf(StatusErrorFormat, s.runId, err)
Expand All @@ -147,27 +163,33 @@ func (s *scanner) ScanWorkloads(kubernetesAPI *kubernetes.API, kubernetesConfig

nodes, err := kubernetesAPI.Nodes()
if err != nil {
return s.LogAndShareError("Scan failed while retrieving k8s cluster nodes. RunId: [%s], with reason: '%v'", ERROR, err, kubernetesConfig.ID)
return s.LogAndShareError("Scan failed while retrieving k8s cluster nodes. Run Id: '%s', with reason: '%v'", ERROR, err, kubernetesConfig.ID)
}

clusterInfo, err := mapper.MapCluster(kubernetesConfig.Cluster, nodes)
if err != nil {
return s.LogAndShareError("Scan failed while aggregating cluster information. RunId: [%s], with reason: '%v'", ERROR, err, kubernetesConfig.ID)
return s.LogAndShareError("Scan failed while aggregating cluster information. Run Id: '%s', with reason: '%v'", ERROR, err, kubernetesConfig.ID)
}

discoveredWorkloads, err := s.ProcessWorkloads(mapper, clusterInfo)
if err != nil {
return s.LogAndShareError("Scan failed while retrieving k8s workload. RunId: [%s], with reason: '%v'", ERROR, err, kubernetesConfig.ID)
return s.LogAndShareError("Scan failed while retrieving k8s workload. Run Id: '%s', with reason: '%v'", ERROR, err, kubernetesConfig.ID)
}
oldResults, err := s.configService.GetScanResults(kubernetesConfig.ID)
if err != nil {
return err
}
err = s.workloadEventProducer.ProcessWorkloads(discoveredWorkloads, oldResults, kubernetesConfig.ID)
if err != nil {
return s.LogAndShareError("Scan failed while posting ECST results. RunId: [%s], with reason: '%v'", ERROR, err, kubernetesConfig.ID)
return s.LogAndShareError("Scan failed while posting ECST results. Run Id: '%s', with reason: '%v'", ERROR, err, kubernetesConfig.ID)
}
logger.Infof("Scan Finished for RunId: [%s]", s.runId)

feedbackErr := s.ShareAdminLogs(kubernetesConfig.ID, INFO, fmt.Sprintf("Found and processed %v workloads from the cluster '%v'.", len(discoveredWorkloads), clusterInfo.Name))
if feedbackErr != nil {
return feedbackErr
}

logger.Infof("Scan Finished for Run Id: '%s'", s.runId)
err = s.ShareStatus(kubernetesConfig.ID, SUCCESSFUL, "Successfully Scanned")
if err != nil {
logger.Errorf(StatusErrorFormat, s.runId, err)
Expand Down Expand Up @@ -243,7 +265,7 @@ func (s *scanner) ShareStatus(configid string, status string, message string) er
statusByte, err := json.Marshal(statusArray)
err = s.eventProducer.PostStatus(statusByte)
if err != nil {
logger.Debugf("Failed sharing status for RunId: [%s], with reason %v", s.runId, err)
logger.Debugf("Failed sharing status for Run Id: '%s', with reason %v", s.runId, err)
return err
}
return nil
Expand All @@ -256,7 +278,7 @@ func (s *scanner) ShareAdminLogs(configId string, loglevel string, message strin
statusByte, err := json.Marshal(statusArray)
err = s.eventProducer.PostStatus(statusByte)
if err != nil {
logger.Debugf("Failed sharing admin logs for RunId: [%s], with reason %v", s.runId, err)
logger.Debugf("Failed sharing admin logs for Run Id: '%s', with reason %v", s.runId, err)
return err
}
return nil
Expand Down

0 comments on commit 2e9e687

Please sign in to comment.