Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Http object type mb #146

Merged
merged 36 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
f62b005
WIP: http endpoints types
afek854 Aug 29, 2024
ca05ab0
add generated code
matthyx Aug 29, 2024
746675d
WIP: added NetworkDirection
afek854 Sep 1, 2024
52c19dd
bump code generation script
matthyx Aug 21, 2024
fa4339b
WIP: Fixed generation script
afek854 Sep 1, 2024
f732160
WIP: Removed HTTPEndpoint deepcopy
afek854 Sep 1, 2024
29e5c27
WIP: Removed HTTPEndpoint from softwarecomposition client
afek854 Sep 1, 2024
d18f37f
WIP: Fixed generated files
afek854 Sep 1, 2024
b7247ee
WIP: Fixed types.go
afek854 Sep 2, 2024
55f2415
WIP: Removed NetworkDirection
afek854 Sep 2, 2024
b4af75f
WIP: Removed logs
afek854 Sep 2, 2024
475efb4
WIP: Move to string
afek854 Sep 2, 2024
b146246
WIP: Convert to string
afek854 Sep 2, 2024
92f49a3
WIP: Added consts
afek854 Sep 2, 2024
afd4787
WIP: Multiple header values
afek854 Sep 4, 2024
398f45b
WIP: Added Path & URL analyzing
afek854 Sep 4, 2024
247343c
WIP: removed debug logs
afek854 Sep 4, 2024
e74c136
WIP: Handle duplicates url's
afek854 Sep 5, 2024
72918a9
Http object type
afek854 Aug 29, 2024
2da000d
add profobufs and fixes
matthyx Sep 6, 2024
2019ba4
WIP: Fixed typos & added benchmark tests
afek854 Sep 8, 2024
3baaf64
Added coverage & benchmark tests
afek854 Sep 8, 2024
0990541
WIP: Fixed bugs due tests
afek854 Sep 8, 2024
ee792eb
WIP: Added extra and tests
afek854 Sep 9, 2024
08d18d2
WIP: Added tests for extra
afek854 Sep 9, 2024
d8b79ec
WIP: Removed Extra field
afek854 Sep 9, 2024
a499d26
Merge branch http-object-type into http-object-type-mb
afek854 Sep 9, 2024
bc5bb47
WIP: Code regeneration
afek854 Sep 9, 2024
9a93364
WIP: Remove omitempty for bool valuie
afek854 Sep 9, 2024
8a8b33a
WIP: Headers into RawMessage
afek854 Sep 9, 2024
9055d68
WIP: Added GetHeaders
afek854 Sep 9, 2024
10d0802
WIP: tidy
afek854 Sep 9, 2024
e94a217
Removed typo & change log level
afek854 Sep 9, 2024
59db408
Added tests for endpoints
afek854 Sep 10, 2024
88ce281
WIP: Added comment
afek854 Sep 10, 2024
b3a16df
Fixed application profile tests
afek854 Sep 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading