Skip to content

Commit

Permalink
Http object type mb (#146)
Browse files Browse the repository at this point in the history
* WIP: http endpoints types

* add generated code

Signed-off-by: Matthias Bertschy <[email protected]>

* WIP: added NetworkDirection

* bump code generation script

Signed-off-by: Matthias Bertschy <[email protected]>

* WIP: Fixed generation script

* WIP: Removed HTTPEndpoint deepcopy

* WIP: Removed HTTPEndpoint from softwarecomposition client

* WIP: Fixed generated files

* WIP: Fixed types.go

* WIP: Removed NetworkDirection

* WIP: Removed logs

* WIP: Move to string

* WIP: Convert to string

* WIP: Added consts

* WIP: Multiple header values

* WIP: Added Path & URL analyzing

* WIP: removed debug logs

* WIP: Handle duplicates url's

* Http object type

* add profobufs and fixes

Signed-off-by: Matthias Bertschy <[email protected]>

* WIP: Fixed typos & added benchmark tests

* Added coverage & benchmark tests

* WIP: Fixed bugs due tests

* WIP: Added extra and tests

* WIP: Added tests for extra

* WIP: Removed Extra field

* WIP: Code regeneration

* WIP: Remove omitempty for bool valuie

* WIP: Headers into RawMessage

* WIP: Added GetHeaders

* WIP: tidy

* Removed typo & change log level

* Added tests for endpoints

* WIP: Added comment

* Fixed application profile tests

---------

Signed-off-by: Matthias Bertschy <[email protected]>
Co-authored-by: Matthias Bertschy <[email protected]>
  • Loading branch information
afek854 and matthyx committed Sep 12, 2024
1 parent 955f3e5 commit f4be3c3
Show file tree
Hide file tree
Showing 23 changed files with 2,365 additions and 694 deletions.
8 changes: 8 additions & 0 deletions pkg/apis/softwarecomposition/consts/consts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package consts

type NetworkDirection string

const (
Inbound NetworkDirection = "inbound"
Outbound NetworkDirection = "outbound"
)
83 changes: 83 additions & 0 deletions pkg/apis/softwarecomposition/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ limitations under the License.
package softwarecomposition

import (
"encoding/json"
"fmt"
"strings"

"github.com/containers/common/pkg/seccomp"
"github.com/kubescape/storage/pkg/apis/softwarecomposition/consts"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -227,6 +230,7 @@ type ApplicationProfileContainer struct {
Opens []OpenCalls
Syscalls []string
SeccompProfile SingleSeccompProfile
Endpoints []HTTPEndpoint
}

type ExecCalls struct {
Expand Down Expand Up @@ -616,6 +620,85 @@ type Arg struct {
Op seccomp.Operator
}

type HTTPEndpoint struct {
Endpoint string
Methods []string
Internal bool
Direction consts.NetworkDirection
Headers json.RawMessage
}

func (e *HTTPEndpoint) GetHeaders() (map[string][]string, error) {
headers := make(map[string][]string)

// Unmarshal the JSON into the map
err := json.Unmarshal([]byte(e.Headers), &headers)
if err != nil {
return nil, err
}
return headers, nil
}

func (e *HTTPEndpoint) Equal(other *HTTPEndpoint) bool {
if e == nil || other == nil {
return e == other
}
return e.Endpoint == other.Endpoint && e.Direction == other.Direction && e.Internal == other.Internal
}

func (e HTTPEndpoint) String() string {
const sep = "␟"
var s strings.Builder

// Append Endpoint
s.WriteString(e.Endpoint)

// Append Methods
if len(e.Methods) > 0 {
if s.Len() > 0 {
s.WriteString(sep)
}
s.WriteString(strings.Join(e.Methods, ","))
}

// Append Internal status
if e.Internal {
if s.Len() > 0 {
s.WriteString(sep)
}
s.WriteString("Internal")
}

// Append Direction
if e.Direction != "" {
if s.Len() > 0 {
s.WriteString(sep)
}
// Capitalize the first letter of the direction
s.WriteString(strings.Title(string(e.Direction)))
}

headers, err := e.GetHeaders()
if err == nil {
// Append Headers
if len(headers) > 0 {
// Define the order of headers
orderedHeaders := []string{"Content-Type", "Authorization"}

for _, k := range orderedHeaders {
if values, ok := headers[k]; ok {
if s.Len() > 0 {
s.WriteString(sep)
}
s.WriteString(fmt.Sprintf("%s: %s", k, strings.Join(values, ",")))
}
}
}
}

return s.String()
}

type SpecBase struct {
Disabled bool
}
Expand Down
48 changes: 48 additions & 0 deletions pkg/apis/softwarecomposition/types_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package softwarecomposition

import (
"encoding/json"
"testing"

"github.com/kubescape/storage/pkg/apis/softwarecomposition/consts"
"github.com/stretchr/testify/assert"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -345,3 +347,49 @@ func TestOpenCalls_String(t *testing.T) {
})
}
}

func TestHTTPEndpoint_String(t *testing.T) {
headers := map[string][]string{
"Content-Type": {"application/json"},
"Authorization": {"Bearer token123", "ApiKey abcdef"},
}

rawJSON, _ := json.Marshal(headers)

tests := []struct {
name string
e HTTPEndpoint
want string
}{
{
name: "Empty",
e: HTTPEndpoint{},
want: "",
},
{
name: "Endpoint and Methods only",
e: HTTPEndpoint{
Endpoint: "/api/v1/users",
Methods: []string{"GET", "POST"},
},
want: "/api/v1/users␟GET,POST",
},
{

name: "Full HTTPEndpoint",
e: HTTPEndpoint{
Endpoint: "/api/v1/users",
Methods: []string{"GET", "POST"},
Internal: true,
Direction: consts.Inbound,
Headers: rawJSON,
},
want: "/api/v1/users␟GET,POST␟Internal␟Inbound␟Content-Type: application/json␟Authorization: Bearer token123,ApiKey abcdef",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, tt.e.String(), "String()")
})
}
}
Loading

0 comments on commit f4be3c3

Please sign in to comment.