Skip to content

Commit

Permalink
Merge branch 'main' into Add-utility-to-determine-if-ip-is-local-exte…
Browse files Browse the repository at this point in the history
…rnal
  • Loading branch information
pawsten authored Oct 5, 2023
2 parents b43731b + 3ed7eb6 commit aa968b8
Show file tree
Hide file tree
Showing 25 changed files with 386 additions and 97 deletions.
8 changes: 8 additions & 0 deletions .github/actions/build/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: Build

runs:
using: composite
steps:
- name: Build
shell: bash
run: cmake --build --preset conan-${{ env.LOWERCASE_BUILD_TYPE }}
14 changes: 14 additions & 0 deletions .github/actions/install-requirements/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Install requirements

runs:
using: composite
steps:
- name: Install requirements
shell: bash
run: |
sudo apt-get install -y libelf-dev
sudo apt-get install -y clang
pip install cmake==3.23.3
pip install conan==2.0
conan profile detect
conan install . --build=missing -s build_type=${{ env.CAPITALISED_BUILD_TYPE }}
32 changes: 32 additions & 0 deletions .github/actions/set-environment-variables/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Set environment variables

inputs:
build-type:
description: Release or Debug
required: true
type: string

runs:
using: composite
steps:
- name: Set environment variables
shell: bash
run: |
buildType=${{ inputs.build-type }}
printf "CAPITALISED_BUILD_TYPE=%s\n" "${buildType}" >> $GITHUB_ENV
printf "LOWERCASE_BUILD_TYPE=%s\n" "${buildType,,}" >> $GITHUB_ENV
newestTag="$(git describe --tags --abbrev=0)"
artifactVersionWithTimestamp="${newestTag}"-"$(date +%Y%m%d_%H%M%S)"
printf "GITHUB_OCI_REGISTRY_ARTIFACT_VERSION=%s\n" "${artifactVersionWithTimestamp}" >> $GITHUB_ENV
printf "RELEASE_PATH=%s\n" "${{ github.workspace }}/build/Release" >> $GITHUB_ENV
printf "ARTIFACT_PATH=%s\n" "${{ github.workspace }}/artifact" >> $GITHUB_ENV
repositoryName="$(printf "%s" "${{ github.repository }}" | sed -e "s/^${{ github.repository_owner }}\///")"
lowercaseRepositoryName="${repositoryName,,}"
printf "LOWERCASE_REPOSITORY_NAME=%s\n" "${lowercaseRepositoryName}" >> $GITHUB_ENV
githubOCIRegistryAddress=ghcr.io/${{ github.repository_owner }}/"${lowercaseRepositoryName}"
printf "GITHUB_OCI_REGISTRY_ADDRESS=%s\n" "${githubOCIRegistryAddress}" >> $GITHUB_ENV
8 changes: 8 additions & 0 deletions .github/actions/test/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: Test

runs:
using: composite
steps:
- name: Test
shell: bash
run: ctest --preset conan-${{ env.LOWERCASE_BUILD_TYPE }}
10 changes: 10 additions & 0 deletions .github/actions/update-packages/action.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: Update packages

runs:
using: composite
steps:
- name: Update packages
shell: bash
run: |
sudo apt-get update
sudo apt-get upgrade -y
12 changes: 0 additions & 12 deletions .github/workflows/build-and-test-debug-on-push.yaml

This file was deleted.

41 changes: 41 additions & 0 deletions .github/workflows/build-and-test-debug.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Build and test Debug

on:
push:
branches-ignore:
- main

jobs:
build-and-test-debug:
runs-on: ubuntu-latest
steps:
- name: Checkout source code of the repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Update packages
uses: ./.github/actions/update-packages

- name: Set environment variables
uses: ./.github/actions/set-environment-variables
with:
build-type: Debug

- name: Set environment variables
uses: ./.github/actions/set-environment-variables
with:
build-type: Debug

- name: Install requirements
uses: ./.github/actions/install-requirements

- name: Configure
shell: bash
run: cmake --preset conan-${{ env.LOWERCASE_BUILD_TYPE }} -DBUILD_TESTS=ON

- name: Build
uses: ./.github/actions/build

- name: Test
uses: ./.github/actions/test
12 changes: 0 additions & 12 deletions .github/workflows/build-and-test-release-on-merge.yaml

This file was deleted.

52 changes: 0 additions & 52 deletions .github/workflows/build-and-test.yaml

This file was deleted.

64 changes: 64 additions & 0 deletions .github/workflows/build-test-and-publish-release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Build, test and publish Release

on:
push:
branches:
- main

jobs:
build-test-and-publish-release:
runs-on: ubuntu-latest
steps:
- name: Checkout source code of the repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Update packages
uses: ./.github/actions/update-packages

- name: Set environment variables
uses: ./.github/actions/set-environment-variables
with:
build-type: Release

- name: Install requirements
uses: ./.github/actions/install-requirements

- name: Configure
shell: bash
run: cmake --preset conan-${{ env.LOWERCASE_BUILD_TYPE }} -DBUILD_TESTS=ON

- name: Build
uses: ./.github/actions/build

- name: Test
uses: ./.github/actions/test

- name: Create artifact directory
run: mkdir ${{ env.ARTIFACT_PATH }}

- name: Move license to artifact directory
run: mv ${{ github.workspace }}/LICENSE ${{ env.ARTIFACT_PATH }}/LICENSE

- name: Move protobuf model to artifact directory
run: mv ${{ env.RELEASE_PATH }}/libebpfdiscoveryproto/proto/ebpfdiscoveryproto ${{ env.ARTIFACT_PATH }}/ebpfdiscoveryproto

- name: Move binaries to artifact directory
run: mv ${{ env.RELEASE_PATH }}/bin ${{ env.ARTIFACT_PATH }}/bin

- name: Remove test binaries from artifact directory
run: find ${{ env.ARTIFACT_PATH }}/* -name 'test*' -exec rm {} \;

- name: Zip artifact directory contents
run: |
cd ${{ env.ARTIFACT_PATH }}
zip -r ${{ env.GITHUB_OCI_REGISTRY_ARTIFACT_VERSION }}.zip .
- name: Login to GitHub OCI registry
run: printf "%s" ${{ secrets.GITHUB_TOKEN }} | oras login ghcr.io -u ${{ github.actor }} --password-stdin

- name: Publish zip to GitHub OCI registry
run: |
cd ${{ env.ARTIFACT_PATH }}
oras push ${{ env.GITHUB_OCI_REGISTRY_ADDRESS }}:${{ env.GITHUB_OCI_REGISTRY_ARTIFACT_VERSION }} ${{ env.GITHUB_OCI_REGISTRY_ARTIFACT_VERSION }}.zip
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@
**/vmlinux.h
/compile_commands.json
/.cache

!/.github/actions/build
3 changes: 0 additions & 3 deletions CHANGELOG.md

This file was deleted.

12 changes: 8 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ if(CMAKE_BUILD_TYPE STREQUAL "Release")
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
endif()

file(READ "version.properties" ver)
string(REGEX MATCH "version=([0-9]+)\.([0-9]+)\.([0-9]+)" _ ${ver})
find_package(Git REQUIRED)
execute_process(
COMMAND ${GIT_EXECUTABLE} describe --tags --abbrev=0 OUTPUT_VARIABLE ver
)

string(REGEX MATCH "([0-9]+)\.([0-9]+)\.([0-9]+)" _ ${ver})
if(NOT CMAKE_MATCH_COUNT EQUAL 3)
message(FATAL_ERROR "Version parsing failed\n Got: ${ver}")
endif()
Expand Down Expand Up @@ -68,7 +72,7 @@ if(Boost_FOUND)
endif()

if(NOT TARGET fmt)
find_package(fmt 8.0.1 REQUIRED)
find_package(fmt REQUIRED)
endif(NOT TARGET fmt)

find_package(Protobuf REQUIRED)
Expand All @@ -81,7 +85,7 @@ endif()

set(INSTALL_GTEST OFF CACHE BOOL "Disable installation of googletest" FORCE)

if(BUILD_TESTS)
if(BUILD_TESTS OR BUILD_BPF_TESTS)
include(FetchContent)
FetchContent_Declare(googletest URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip)
FetchContent_MakeAvailable(googletest)
Expand Down
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ eBPF Discovery is a tool for discovering HTTP endpoints on a given host based on
## Build requirements

```
conan 2.0
cmake 3.23
conan >= 2.0
cmake >= 3.23
libelf-dev
libboost-all-dev
libfmt-dev
clang
```

Expand All @@ -33,7 +31,7 @@ Build debug:

```
conan install . --build=missing -s build_type=Debug
cmake --preset conan-debug -DTHIRDPARTY_MAKE_JOBS_COUNT=$((`nproc` / 2))
cmake --preset conan-debug -DTHIRDPARTY_MAKE_JOBS_COUNT=$((`nproc` / 2)) -DBUILD_BPF_TESTS=On
cmake --build --preset conan-debug
```

Expand Down
4 changes: 4 additions & 0 deletions conanfile.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
protobuf/3.21.9
protobuf-c/1.4.1
ms-gsl/4.0.0
fmt/10.1.1
spdlog/1.12.0
gtest/1.14.0
boost/1.83.0
[generators]
CMakeDeps
CMakeToolchain
Expand Down
19 changes: 17 additions & 2 deletions libebpfdiscoveryskel/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
set(BPF_C_FLAGS ${BPF_C_FLAGS} -I${PROJECT_SOURCE_DIR}/libebpfdiscoveryshared/headers)
set(TARGET ebpfdiscoveryskel)

set(BPF_C_FLAGS ${BPF_C_FLAGS} -I${CMAKE_CURRENT_SOURCE_DIR}/src -I${PROJECT_SOURCE_DIR}/libebpfdiscoveryshared/headers)
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
find_package(BpfObject REQUIRED)
bpf_object(ebpfdiscoveryskel src/discovery.bpf.c)
bpf_object(${TARGET} src/discovery.bpf.c)

if(BUILD_BPF_TESTS)
set(TEST_SKEL_TARGET ebpfdiscoverytestskel)
set(BPF_C_FLAGS ${BPF_C_FLAGS} -I${CMAKE_CURRENT_SOURCE_DIR}/test)
bpf_object(${TEST_SKEL_TARGET} testbpf/discoveryTest.bpf.c)

list(APPEND TEST_SOURCES test/DataFunctionsTest.cpp)
set(TEST_TARGET test${TARGET})

add_executable(${TEST_TARGET} ${TEST_SOURCES})
target_link_libraries(${TEST_TARGET} GTest::gtest_main ${TEST_SKEL_TARGET})
gtest_discover_tests(${TEST_TARGET})
endif()
10 changes: 5 additions & 5 deletions libebpfdiscoveryskel/src/DataFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

#include <bpf/bpf_helpers.h>

__attribute__((always_inline)) inline static int dataProbeIsEqualToString(const char* src, const char* str, size_t len) {
__attribute__((always_inline)) inline static int dataProbeEqualToString(const char* src, const char* str, size_t len) {
char ch;
for (size_t i = 0; i < len; ++i) {
int result = bpf_probe_read(&ch, sizeof(char), (char*)src + i);
if (result < 0) {
return result;
int res = bpf_probe_read(&ch, sizeof(char), (char*)src + i);
if (res < 0) {
return res;
}

if (ch != str[i] || ch == '\0') {
Expand All @@ -26,5 +26,5 @@ __attribute__((always_inline)) inline static bool dataProbeIsBeginningOfHttpRequ
// We expect only GET and POST requests. We expect request URI's to start with a slash as absolute urls are mainly used in
// requests to proxy servers.
return len >= DISCOVERY_MIN_HTTP_REQUEST_LENGTH &&
(dataProbeIsEqualToString(ptr, "GET /", 5) || dataProbeIsEqualToString(ptr, "POST /", 6));
(dataProbeEqualToString(ptr, "GET /", 5) == 5 || dataProbeEqualToString(ptr, "POST /", 6) == 6);
}
Loading

0 comments on commit aa968b8

Please sign in to comment.