Skip to content

Commit

Permalink
Merge pull request #37 from kubescape/bugfix/logs
Browse files Browse the repository at this point in the history
Adding a lot of fixes
  • Loading branch information
amitschendel authored Jan 29, 2024
2 parents d2aa659 + 23019ce commit 8b071ca
Show file tree
Hide file tree
Showing 22 changed files with 125 additions and 95 deletions.
10 changes: 5 additions & 5 deletions cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strconv"
"strings"

"github.com/kubescape/kubescape-network-scanner/internal/pkg/networkscanner/portdiscovery"
"github.com/kubescape/kubescape-network-scanner/pkg/networkscanner/portdiscovery"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -91,7 +91,7 @@ func scan(cmd *cobra.Command, args []string) error {
fmt.Fprintf(os.Stderr, "Presentation Layer: %s\n", discoveryResult.PresentationLayer)
fmt.Fprintf(os.Stderr, "Application Layer: %s\n", discoveryResult.ApplicationLayer)
fmt.Fprintf(os.Stderr, "Authenticated: %v\n", discoveryResult.IsAuthenticated)
fmt.Fprintf(os.Stderr, "Properties: %s\n", discoveryResult.properties)
fmt.Fprintf(os.Stderr, "Properties: %s\n", discoveryResult.Properties)

// Store discovery results in a map
resultMap := map[string]interface{}{
Expand All @@ -103,7 +103,7 @@ func scan(cmd *cobra.Command, args []string) error {
"applicationlayer": discoveryResult.ApplicationLayer,
"service": discoveryResult.ApplicationLayer,
"authenticated": discoveryResult.IsAuthenticated,
"properties": discoveryResult.properties,
"properties": discoveryResult.Properties,
}

// Append results to discoveryResults slice
Expand All @@ -123,7 +123,7 @@ func scan(cmd *cobra.Command, args []string) error {
fmt.Fprintf(os.Stderr, "Presentation Layer: %s\n", discoveryResult.PresentationLayer)
fmt.Fprintf(os.Stderr, "Application Layer: %s\n", discoveryResult.ApplicationLayer)
fmt.Fprintf(os.Stderr, "Authenticated: %v\n", discoveryResult.IsAuthenticated)
fmt.Fprintf(os.Stderr, "Properties: %s\n", discoveryResult.properties)
fmt.Fprintf(os.Stderr, "Properties: %s\n", discoveryResult.Properties)
// Store discovery results in a map
resultMap := map[string]interface{}{
"host": target.Host,
Expand All @@ -134,7 +134,7 @@ func scan(cmd *cobra.Command, args []string) error {
"applicationlayer": discoveryResult.ApplicationLayer,
"service": discoveryResult.ApplicationLayer,
"authenticated": discoveryResult.IsAuthenticated,
"properties": discoveryResult.properties,
"properties": discoveryResult.Properties,
}

// Append results to discoveryResults slice
Expand Down
24 changes: 13 additions & 11 deletions cmd/servicediscovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ import (
"io"
"sync"

"github.com/kubescape/kubescape-network-scanner/internal/pkg/networkscanner/servicediscovery"
"github.com/kubescape/kubescape-network-scanner/internal/pkg/networkscanner/servicediscovery/applicationlayerdiscovery"
"github.com/kubescape/kubescape-network-scanner/internal/pkg/networkscanner/servicediscovery/presentationlayerdiscovery"
"github.com/kubescape/kubescape-network-scanner/internal/pkg/networkscanner/servicediscovery/sessionlayerdiscovery"
log "github.com/sirupsen/logrus"

"github.com/kubescape/kubescape-network-scanner/pkg/networkscanner/servicediscovery"
"github.com/kubescape/kubescape-network-scanner/pkg/networkscanner/servicediscovery/applicationlayerdiscovery"
"github.com/kubescape/kubescape-network-scanner/pkg/networkscanner/servicediscovery/presentationlayerdiscovery"
"github.com/kubescape/kubescape-network-scanner/pkg/networkscanner/servicediscovery/sessionlayerdiscovery"
)

type DiscoveryResult struct {
SessionLayer string
PresentationLayer string
ApplicationLayer string
IsAuthenticated bool
properties map[string]interface{}
Properties map[string]interface{}
}

func ScanTargets(host string, port int) (result DiscoveryResult, err error) {
Expand All @@ -34,7 +36,7 @@ func ScanTargets(host string, port int) (result DiscoveryResult, err error) {
sessionDiscoveryResult, err := sessionDiscoveryItem.Discovery.SessionLayerDiscover(host, port)
if err != nil {
if err != io.EOF {
fmt.Println("Error while discovering session layer protocol:", err)
log.Debugf("Error while discovering session layer protocol: %v", err)
}
return
}
Expand All @@ -57,7 +59,7 @@ func ScanTargets(host string, port int) (result DiscoveryResult, err error) {
sessionHandler, err := sessionDiscoveryResult.GetSessionHandler()
if err != nil {
if err != io.EOF {
fmt.Println("Error while discovering session layer protocol:", err)
log.Debugf("Error while discovering session layer protocol: %v", err)
}
continue
}
Expand All @@ -72,7 +74,7 @@ func ScanTargets(host string, port int) (result DiscoveryResult, err error) {
presentationDiscoveryResult, err := presentationDiscoveryItem.Discovery.Discover(sessionHandler)
if err != nil {
if err != io.EOF {
fmt.Println("Error while discovering session layer protocol:", err)
log.Debugf("Error while discovering presentation layer protocol: %v", err)
}
return
}
Expand Down Expand Up @@ -119,7 +121,7 @@ func ScanTargets(host string, port int) (result DiscoveryResult, err error) {
if applicationDiscoveryResult.GetIsDetected() {
result.ApplicationLayer = fmt.Sprintf("%v", applicationDiscoveryResult.Protocol())
result.IsAuthenticated = applicationDiscoveryResult.GetIsAuthRequired()
result.properties = applicationDiscoveryResult.GetProperties()
result.Properties = applicationDiscoveryResult.GetProperties()
break // Stop checking application layer protocol
}
}
Expand Down Expand Up @@ -155,13 +157,13 @@ func ScanTargets(host string, port int) (result DiscoveryResult, err error) {
if applicationDiscoveryResult.GetIsDetected() {
result.ApplicationLayer = fmt.Sprintf("%v", applicationDiscoveryResult.Protocol())
result.IsAuthenticated = applicationDiscoveryResult.GetIsAuthRequired()
result.properties = applicationDiscoveryResult.GetProperties()
result.Properties = applicationDiscoveryResult.GetProperties()
break
}
}
}
} else {
fmt.Println("No session layer protocol detected")
log.Debugf("No session layer protocol detected")
}
}

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"sync"
"time"

"github.com/kubescape/kubescape-network-scanner/internal/pkg/networkscanner"
"github.com/kubescape/kubescape-network-scanner/pkg/networkscanner"
)

type ScanConfig struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"time"

"github.com/gocql/gocql"
"github.com/kubescape/kubescape-network-scanner/internal/pkg/networkscanner/servicediscovery"
"github.com/kubescape/kubescape-network-scanner/pkg/networkscanner/servicediscovery"
)

const (
Expand Down Expand Up @@ -63,7 +63,7 @@ func (d *CassandraDiscovery) Discover(sessionHandler servicediscovery.ISessionHa
IsDetected: false,
isAuthenticated: true,
properties: nil, // Set properties to nil as it's not used in this case
}, nil
}, err
}
defer session.Close()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import (
"net/http"
"strings"

"github.com/kubescape/kubescape-network-scanner/internal/pkg/networkscanner/servicediscovery"
"github.com/kubescape/kubescape-network-scanner/pkg/networkscanner/servicediscovery"
)

// TODO: Fix this scanner
type ElasticsearchDiscoveryResult struct {
isDetected bool
properties map[string]interface{}
Expand Down Expand Up @@ -45,10 +46,10 @@ func (d *ElasticsearchDiscovery) Discover(sessionHandler servicediscovery.ISessi
// If there is an error connecting to Elasticsearch, return a result with isDetected set to false
result := &ElasticsearchDiscoveryResult{
isDetected: false,
isAuthenticated: false,
isAuthenticated: true,
properties: nil,
}
return result, nil
return result, err
}
defer response.Body.Close()

Expand All @@ -59,29 +60,22 @@ func (d *ElasticsearchDiscovery) Discover(sessionHandler servicediscovery.ISessi
return nil, fmt.Errorf("failed to read response body: %v", err)
}

if strings.Contains(string(body), "MongoDB") {
// If the response contains "MongoDB," set isDetected to false and return the result
// If the response body contains the Elasticsearch version, return a result with isDetected set to true
if strings.Contains(string(body), "version") {
result := &ElasticsearchDiscoveryResult{
isDetected: false,
isAuthenticated: false,
properties: nil,
isDetected: true,
isAuthenticated: false, // Set to true if authentication is required
properties: make(map[string]interface{}),
}
return result, nil
}

result := &ElasticsearchDiscoveryResult{
isDetected: true,
isAuthenticated: false, // Set to true if authentication is required
properties: make(map[string]interface{}),
}
// Parse the relevant data from the response body
result.properties["name"] = getValueFromBody(body, "name")
result.properties["cluster_name"] = getValueFromBody(body, "cluster_name")
result.properties["cluster_uuid"] = getValueFromBody(body, "cluster_uuid")
result.properties["version"] = getValueFromBody(body, "version.number")

// Parse the relevant data from the response body
result.properties["name"] = getValueFromBody(body, "name")
result.properties["cluster_name"] = getValueFromBody(body, "cluster_name")
result.properties["cluster_uuid"] = getValueFromBody(body, "cluster_uuid")
result.properties["version"] = getValueFromBody(body, "version.number")

return result, nil
return result, nil
}
}

// If the response status code is not OK (200), return a result with isDetected set to false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package applicationlayerdiscovery
import (
"context"
"fmt"
"strings"
"time"

"github.com/kubescape/kubescape-network-scanner/internal/pkg/networkscanner/servicediscovery"
"github.com/kubescape/kubescape-network-scanner/pkg/networkscanner/servicediscovery"
clientv3 "go.etcd.io/etcd/client/v3"
)

Expand Down Expand Up @@ -48,24 +47,29 @@ func (d *EtcdDiscovery) Discover(sessionHandler servicediscovery.ISessionHandler

client, err := clientv3.New(config)
if err != nil {
return nil, fmt.Errorf("failed to connect to etcd server: %v", err)
return &EtcdDiscoveryResult{
isDetected: false,
isAuthenticated: true,
properties: nil,
}, err
}
defer client.Close()

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
_, err = client.Get(ctx, "/")
cancel()
if err != nil {
if strings.Contains(err.Error(), "etcdserver: request timed out") {
return nil, fmt.Errorf("etcd request timed out")
}
return nil, fmt.Errorf("failed to discover etcd: %v", err)
return &EtcdDiscoveryResult{
isDetected: true,
isAuthenticated: true,
properties: nil,
}, nil
}

result := &EtcdDiscoveryResult{
isDetected: true,
isAuthenticated: true,
properties: nil, // Set properties to nil as it's not used in this case
isAuthenticated: false,
properties: nil,
}

return result, nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

log "github.com/sirupsen/logrus"

"github.com/kubescape/kubescape-network-scanner/internal/pkg/networkscanner/servicediscovery"
"github.com/kubescape/kubescape-network-scanner/pkg/networkscanner/servicediscovery"

"github.com/IBM/sarama"
)
Expand Down Expand Up @@ -46,7 +46,8 @@ func (k *KafkaDiscovery) Discover(sessionHandler servicediscovery.ISessionHandle
// Configure the producer
config := sarama.NewConfig()
config.Producer.RequiredAcks = sarama.WaitForAll
config.Producer.Retry.Max = 5
config.Producer.Retry.Max = 1
config.Producer.Timeout = 3
config.Producer.Return.Successes = true

// Create a new SyncProducer
Expand All @@ -56,11 +57,11 @@ func (k *KafkaDiscovery) Discover(sessionHandler servicediscovery.ISessionHandle
isDetected: false,
isAuthenticated: true,
properties: nil, // Set properties to nil as it's not used in this case
}, nil
}, err
}
defer func() {
if err := producer.Close(); err != nil {
log.Debug("Failed to close Kafka producer: ", err)
log.Debugf("Failed to close Kafka producer: %s", err)
}
}()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"
"time"

"github.com/kubescape/kubescape-network-scanner/internal/pkg/networkscanner/servicediscovery"
"github.com/kubescape/kubescape-network-scanner/pkg/networkscanner/servicediscovery"
)

type KubeApiServerDiscoveryResult struct {
Expand Down Expand Up @@ -55,7 +55,7 @@ func (d *KubeApiServerDiscovery) Discover(sessionHandler servicediscovery.ISessi
// Send a GET request to the Kubernetes API server
resp, err := client.Get(url)
if err != nil {
return nil, fmt.Errorf("failed to connect to Kubernetes API server: %v", err)
return nil, fmt.Errorf("failed to send request to Kubernetes API server: %v", err)
}
defer resp.Body.Close()

Expand Down Expand Up @@ -105,7 +105,7 @@ func (d *KubeApiServerDiscovery) Discover(sessionHandler servicediscovery.ISessi
// If the response status is neither OK (200) nor Unauthorized (401), the Kubernetes API server is not detected
result := &KubeApiServerDiscoveryResult{
isDetected: false,
isAuthRequired: false,
isAuthRequired: true,
properties: nil,
}
return result, nil
Expand Down
Loading

0 comments on commit 8b071ca

Please sign in to comment.