Skip to content

Commit 81e420b

Browse files
committed
fix cr + change scan result handaling
Signed-off-by: idohu <[email protected]>
1 parent ed57126 commit 81e420b

File tree

3 files changed

+44
-36
lines changed

3 files changed

+44
-36
lines changed

servicehandler/portsscan.go

+8-13
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,21 @@ type Port struct {
1414
sessionLayer string
1515
presentationLayer string
1616
applicationLayer string
17-
authenticated bool
17+
authenticated *bool
1818
}
1919

2020
func (port *Port) scan(ctx context.Context, ip string) {
2121
result, err := cmd.ScanTargets(ctx, ip, port.port)
22+
if err != nil {
23+
logger.L().Ctx(ctx).Error(err.Error())
24+
return
25+
}
26+
2227
port.applicationLayer = result.ApplicationLayer
2328
port.presentationLayer = result.PresentationLayer
2429
port.sessionLayer = result.SessionLayer
25-
port.authenticated = result.IsAuthenticated
26-
27-
if result.ApplicationLayer == "" {
28-
// if we can't get the application layer, then we change to Unknown
29-
port.authenticated = true
30-
}
31-
32-
if err != nil {
33-
//if we have an error, we log it and set all layers to Unknown
34-
logger.L().Ctx(ctx).Error(err.Error())
35-
port.applicationLayer = "failed_to_scan"
36-
port.authenticated = false
30+
if result.ApplicationLayer != "" {
31+
port.authenticated = &result.IsAuthenticated
3732
}
3833
}
3934

servicehandler/servicediscovery.go

+17-9
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func deleteServices(ctx context.Context, client dynamic.NamespaceableResourceInt
5959
err := client.Namespace(service.GetNamespace()).Delete(ctx, service.GetName(), metav1.DeleteOptions{})
6060
if err != nil {
6161
logger.L().Ctx(ctx).Error(err.Error())
62+
continue
6263
}
6364
logger.L().Ctx(ctx).Info("Authentication Service " + service.GetName() + " in namespace " + service.GetNamespace() + " deleted")
6465
}
@@ -91,20 +92,21 @@ func (sra serviceAuthentication) unstructured() (*unstructured.Unstructured, err
9192
return &unstructured.Unstructured{Object: a}, err
9293
}
9394

94-
func (sra *serviceAuthentication) applyCrd(ctx context.Context, client dynamic.NamespaceableResourceInterface) {
95+
func (sra *serviceAuthentication) applyCrd(ctx context.Context, client dynamic.NamespaceableResourceInterface) error {
9596
serviceObj, structuredErr := sra.unstructured()
9697
if structuredErr != nil {
9798
logger.L().Ctx(ctx).Error(structuredErr.Error())
98-
return
99+
return nil
99100
}
100101

101102
_, applyErr := client.Namespace(sra.metadata.namespace).Apply(ctx, sra.metadata.name, serviceObj, metav1.ApplyOptions{FieldManager: fieldManager})
102103
if applyErr != nil {
103-
logger.L().Ctx(ctx).Error(applyErr.Error())
104+
return applyErr
104105
}
105-
106+
return nil
106107
}
107-
func (sra *serviceAuthentication) serviceScan(ctx context.Context, client dynamic.NamespaceableResourceInterface) {
108+
109+
func (sra *serviceAuthentication) serviceScan(ctx context.Context, client dynamic.NamespaceableResourceInterface) error {
108110
// get all ports , each port equal different address
109111
for idx := range sra.spec.ports {
110112

@@ -119,7 +121,7 @@ func (sra *serviceAuthentication) serviceScan(ctx context.Context, client dynami
119121
pr.scan(ctx, srvDnsName)
120122
}
121123

122-
sra.applyCrd(ctx, client)
124+
return sra.applyCrd(ctx, client)
123125
}
124126

125127
func getClusterServices(ctx context.Context, regularClient kubernetes.Interface) (*v1.ServiceList, error) {
@@ -161,9 +163,15 @@ func discoveryService(ctx context.Context, regularClient kubernetes.Interface, d
161163

162164
scanWg := sync.WaitGroup{}
163165
p, err := ants.NewPoolWithFunc(workerNum, func(i interface{}) {
164-
sra := i.(serviceAuthentication)
165-
sra.serviceScan(ctx, dynamicClient.Resource(ServiceScanSchema))
166-
scanWg.Done()
166+
defer scanWg.Done()
167+
sra, ok := i.(serviceAuthentication)
168+
if !ok {
169+
return
170+
}
171+
scanErr := sra.serviceScan(ctx, dynamicClient.Resource(ServiceScanSchema))
172+
if scanErr != nil {
173+
logger.L().Ctx(ctx).Error(scanErr.Error())
174+
}
167175
})
168176

169177
if err != nil {

servicehandler/servicediscovery_test.go

+19-14
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import (
77
"github.com/stretchr/testify/assert"
88
"github.com/stretchr/testify/require"
99
v1 "k8s.io/api/core/v1"
10+
"k8s.io/apimachinery/pkg/api/errors"
1011
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1112
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1213
"k8s.io/apimachinery/pkg/runtime"
1314
"k8s.io/apimachinery/pkg/runtime/schema"
1415
dynamicFake "k8s.io/client-go/dynamic/fake"
1516
kubernetesFake "k8s.io/client-go/kubernetes/fake"
17+
"k8s.io/utils/ptr"
1618
)
1719

1820
var TestAuthentications = serviceAuthentication{
@@ -29,15 +31,15 @@ var TestAuthentications = serviceAuthentication{
2931
port: 80,
3032
protocol: "TCP",
3133
applicationLayer: "sql",
32-
authenticated: true,
34+
authenticated: ptr.To(true),
3335
sessionLayer: "tcp",
3436
presentationLayer: "http",
3537
},
3638
{
3739
port: 443,
3840
protocol: "TCP",
3941
applicationLayer: "kafka",
40-
authenticated: true,
42+
authenticated: ptr.To(true),
4143
sessionLayer: "tcp",
4244
presentationLayer: "http",
4345
},
@@ -111,6 +113,7 @@ func Test_translate(t *testing.T) {
111113
func TestDiscoveryServiceHandler(t *testing.T) {
112114
//write a component test that creates fake client and test the service discovery and see if it creates a crd
113115
//and if it deletes the crd
116+
//IMPORTANT: fake cilent doesnt have an Apply option like the real client so we need to create the crd and check if it exists -it will blog errors but will pass
114117
testCases := []struct {
115118
name string
116119
services []runtime.Object
@@ -163,15 +166,15 @@ func TestDiscoveryServiceHandler(t *testing.T) {
163166
"metadata": map[string]interface{}{"name": "service1", "namespace": "test1"},
164167
"spec": map[string]interface{}{"clusterIP": "",
165168
"ports": []interface{}{
166-
map[string]interface{}{"applicationLayer": "", "authenticated": false, "port": int64(80), "presentationLayer": "", "protocol": "TCP", "sessionLayer": ""},
167-
map[string]interface{}{"applicationLayer": "", "authenticated": false, "port": int64(443), "presentationLayer": "", "protocol": "UDP", "sessionLayer": ""},
169+
map[string]interface{}{"applicationLayer": "", "authenticated": nil, "port": int64(80), "presentationLayer": "", "protocol": "TCP", "sessionLayer": ""},
170+
map[string]interface{}{"applicationLayer": "", "authenticated": nil, "port": int64(443), "presentationLayer": "", "protocol": "UDP", "sessionLayer": ""},
168171
}}}},
169172
{Object: map[string]interface{}{"apiVersion": "kubescape.io/v1", "kind": "ServiceScanResult",
170173
"metadata": map[string]interface{}{"name": "service2", "namespace": "test2"},
171174
"spec": map[string]interface{}{"clusterIP": "",
172175
"ports": []interface{}{
173-
map[string]interface{}{"applicationLayer": "", "authenticated": false, "port": int64(80), "presentationLayer": "", "protocol": "TCP", "sessionLayer": ""},
174-
map[string]interface{}{"applicationLayer": "", "authenticated": false, "port": int64(443), "presentationLayer": "", "protocol": "UDP", "sessionLayer": ""},
176+
map[string]interface{}{"applicationLayer": "", "authenticated": nil, "port": int64(80), "presentationLayer": "", "protocol": "TCP", "sessionLayer": ""},
177+
map[string]interface{}{"applicationLayer": "", "authenticated": nil, "port": int64(443), "presentationLayer": "", "protocol": "UDP", "sessionLayer": ""},
175178
}}}},
176179
},
177180
},
@@ -207,8 +210,8 @@ func TestDiscoveryServiceHandler(t *testing.T) {
207210
"metadata": map[string]interface{}{"name": "service1", "namespace": "test1"},
208211
"spec": map[string]interface{}{"clusterIP": "",
209212
"ports": []interface{}{
210-
map[string]interface{}{"applicationLayer": "", "authenticated": false, "port": int64(80), "presentationLayer": "", "protocol": "TCP", "sessionLayer": ""},
211-
map[string]interface{}{"applicationLayer": "", "authenticated": false, "port": int64(443), "presentationLayer": "", "protocol": "UDP", "sessionLayer": ""},
213+
map[string]interface{}{"applicationLayer": "", "authenticated": nil, "port": int64(80), "presentationLayer": "", "protocol": "TCP", "sessionLayer": ""},
214+
map[string]interface{}{"applicationLayer": "", "authenticated": nil, "port": int64(443), "presentationLayer": "", "protocol": "UDP", "sessionLayer": ""},
212215
}}}},
213216
},
214217
},
@@ -261,7 +264,7 @@ func TestDiscoveryServiceHandler(t *testing.T) {
261264
want: []unstructured.Unstructured{
262265
{Object: map[string]interface{}{"apiVersion": "kubescape.io/v1", "kind": "ServiceScanResult",
263266
"metadata": map[string]interface{}{"name": "service1", "namespace": "test1"},
264-
"spec": map[string]interface{}{"clusterIP": "", "ports": []interface{}{map[string]interface{}{"applicationLayer": "", "authenticated": false, "port": int64(80), "presentationLayer": "", "protocol": "TCP", "sessionLayer": ""}}}}},
267+
"spec": map[string]interface{}{"clusterIP": "", "ports": []interface{}{map[string]interface{}{"applicationLayer": "", "authenticated": nil, "port": int64(80), "presentationLayer": "", "protocol": "TCP", "sessionLayer": ""}}}}},
265268
},
266269
delete: []runtime.Object{
267270
&v1.Service{
@@ -301,12 +304,12 @@ func TestDiscoveryServiceHandler(t *testing.T) {
301304
for i := 0; i < 10; i++ {
302305
services, _ := serviceExtractor(ctx, regClient)
303306
for _, service := range services {
304-
existObj, _ := dynamicClient.Resource(ServiceScanSchema).Namespace(service.metadata.namespace).Get(ctx, service.metadata.name, metav1.GetOptions{})
305-
if existObj == nil {
306-
obj, _ := service.unstructured()
307-
_, err := dynamicClient.Resource(ServiceScanSchema).Namespace(service.metadata.namespace).Create(ctx, obj, metav1.CreateOptions{})
307+
obj, _ := service.unstructured()
308+
_, err := dynamicClient.Resource(ServiceScanSchema).Namespace(service.metadata.namespace).Create(ctx, obj, metav1.CreateOptions{})
309+
if !errors.IsAlreadyExists(err) {
308310
require.NoError(t, err)
309311
}
312+
310313
}
311314

312315
err := discoveryService(context.Background(), regClient, dynamicClient)
@@ -315,7 +318,9 @@ func TestDiscoveryServiceHandler(t *testing.T) {
315318
crds, _ = dynamicClient.Resource(ServiceScanSchema).List(ctx, metav1.ListOptions{})
316319
if tc.delete != nil {
317320
for _, delService := range tc.delete {
318-
_ = regClient.CoreV1().Services(delService.(*v1.Service).Namespace).Delete(ctx, delService.(*v1.Service).Name, metav1.DeleteOptions{})
321+
err = regClient.CoreV1().Services(delService.(*v1.Service).Namespace).Delete(ctx, delService.(*v1.Service).Name, metav1.DeleteOptions{})
322+
require.NoError(t, err)
323+
tc.delete = nil
319324
}
320325
}
321326
}

0 commit comments

Comments
 (0)