Skip to content

Commit

Permalink
feat(decl/resources): add resource builder
Browse files Browse the repository at this point in the history
Signed-off-by: Leonardo Di Giovanna <[email protected]>
Co-authored-by: Aldo Lacuku <[email protected]>
  • Loading branch information
ekoops and alacuku committed Oct 27, 2024
1 parent 28352d6 commit 4c43470
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 1 deletion.
138 changes: 138 additions & 0 deletions pkg/test/resource/builder/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2024 The Falco Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package builder

import (
"fmt"

"github.com/go-logr/logr"

"github.com/falcosecurity/event-generator/pkg/test/loader"
"github.com/falcosecurity/event-generator/pkg/test/resource"
"github.com/falcosecurity/event-generator/pkg/test/resource/clientserver"
"github.com/falcosecurity/event-generator/pkg/test/resource/fd/directory"
"github.com/falcosecurity/event-generator/pkg/test/resource/fd/epoll"
"github.com/falcosecurity/event-generator/pkg/test/resource/fd/event"
"github.com/falcosecurity/event-generator/pkg/test/resource/fd/file"
"github.com/falcosecurity/event-generator/pkg/test/resource/fd/inotify"
"github.com/falcosecurity/event-generator/pkg/test/resource/fd/mem"
"github.com/falcosecurity/event-generator/pkg/test/resource/fd/pipe"
"github.com/falcosecurity/event-generator/pkg/test/resource/fd/signal"
)

// builder is an implementation of resource.Builder.
type builder struct{}

// Verify that builder implements resource.Builder interface.
var _ resource.Builder = (*builder)(nil)

// New creates a new builder.
func New() (resource.Builder, error) {
return &builder{}, nil
}

func (b *builder) Build(logger logr.Logger, testResource *loader.TestResource) (resource.Resource, error) {
resourceType := testResource.Type
resourceName := testResource.Name
logger = logger.WithValues("resourceType", resourceType, "resourceName", resourceName)
switch resourceType {
case loader.TestResourceTypeClientServer:
clientServerSpec, ok := testResource.Spec.(*loader.TestResourceClientServerSpec)
if !ok {
return nil, fmt.Errorf("cannot parse clientServer spec")
}

// TODO: remove the cast and use a dedicated type for l4Proto
res := clientserver.New(logger, resourceName, string(clientServerSpec.L4Proto), clientServerSpec.Address)
return res, nil
case loader.TestResourceTypeFD:
fdSpec, ok := testResource.Spec.(*loader.TestResourceFDSpec)
if !ok {
return nil, fmt.Errorf("cannot parse fd spec")
}

res, err := b.buildFD(logger, resourceName, fdSpec)
if err != nil {
return nil, fmt.Errorf("cannot build fd resource: %w", err)
}

return res, nil
default:
return nil, fmt.Errorf("unknown test resource type %q", resourceType)
}
}

// buildFD builds a fd test resource.
func (b *builder) buildFD(logger logr.Logger, resourceName string,
fdSpec *loader.TestResourceFDSpec) (resource.Resource, error) {
subtype := fdSpec.Subtype
logger = logger.WithValues("resourceSubtype", subtype)
switch subtype {
case loader.TestResourceFDSubtypeFile:
subSpec, ok := fdSpec.Spec.(*loader.TestResourceFDFileSpec)
if !ok {
return nil, fmt.Errorf("cannot parse file spec")
}

return file.New(logger, resourceName, subSpec.FilePath), nil
case loader.TestResourceFDSubtypeDirectory:
subSpec, ok := fdSpec.Spec.(*loader.TestResourceFDDirectorySpec)
if !ok {
return nil, fmt.Errorf("cannot parse directory spec")
}

return directory.New(logger, resourceName, subSpec.DirPath), nil
case loader.TestResourceFDSubtypePipe:
if _, ok := fdSpec.Spec.(*loader.TestResourceFDPipeSpec); !ok {
return nil, fmt.Errorf("cannot parse pipe spec")
}

return pipe.New(logger, resourceName), nil
case loader.TestResourceFDSubtypeEvent:
if _, ok := fdSpec.Spec.(*loader.TestResourceFDEventSpec); !ok {
return nil, fmt.Errorf("cannot parse event spec")
}

return event.New(logger, resourceName), nil
case loader.TestResourceFDSubtypeSignal:
if _, ok := fdSpec.Spec.(*loader.TestResourceFDSignalSpec); !ok {
return nil, fmt.Errorf("cannot parse signalfd spec")
}

return signal.New(logger, resourceName), nil
case loader.TestResourceFDSubtypeEpoll:
if _, ok := fdSpec.Spec.(*loader.TestResourceFDEpollSpec); !ok {
return nil, fmt.Errorf("cannot parse eventpoll spec")
}

return epoll.New(logger, resourceName), nil
case loader.TestResourceFDSubtypeInotify:
if _, ok := fdSpec.Spec.(*loader.TestResourceFDInotifySpec); !ok {
return nil, fmt.Errorf("cannot parse inotify spec")
}

return inotify.New(logger, resourceName), nil
case loader.TestResourceFDSubtypeMem:
subSpec, ok := fdSpec.Spec.(*loader.TestResourceFDMemSpec)
if !ok {
return nil, fmt.Errorf("cannot parse memfd spec")
}

return mem.New(logger, resourceName, subSpec.FileName), nil
default:
return nil, fmt.Errorf("unknown fd test resource subtype %q", subtype)
}
}
17 changes: 17 additions & 0 deletions pkg/test/resource/builder/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2024 The Falco Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package builder provides an implementation of resource.Builder.
package builder
3 changes: 2 additions & 1 deletion pkg/test/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package resource

import (
"context"
"github.com/go-logr/logr"

"github.com/falcosecurity/event-generator/pkg/test/field"
"github.com/falcosecurity/event-generator/pkg/test/loader"
Expand All @@ -37,5 +38,5 @@ type Resource interface {
type Builder interface {
// Build builds a new test resource.
// TODO: replace loader.TestResource with a dedicated type.
Build(testResource *loader.TestResource) (Resource, error)
Build(logger logr.Logger, testResource *loader.TestResource) (Resource, error)
}

0 comments on commit 4c43470

Please sign in to comment.