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

Implement bosh integration #17

Merged
Merged
Show file tree
Hide file tree
Changes from 75 commits
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
185d1b9
refactor: Protobuf renaming
a18e Feb 21, 2023
bebe283
[WIP] feat: Implement pcap-bosh-cli
a18e Feb 21, 2023
5c95135
[WIP] refactor: Protobuf object rename:
a18e Feb 22, 2023
f02b81e
[WIP] feat: Implement bosh director communication in pcap-api
a18e Feb 22, 2023
062ec5d
[WIP] feat: Move shared logic from pcap-bosh-cli to pcap-client (brok…
a18e Feb 24, 2023
d77a12f
[WIP] various:
a18e Mar 1, 2023
f12c4c6
[WIP] feat: refactor bosh cli & client
a18e Mar 2, 2023
4d90cdd
[WIP] chore: cleanup after rebase on current main branch
a18e Mar 3, 2023
db52f75
[WIP] feat: Fix incorrect pcap file handling
a18e Mar 3, 2023
a3ad671
[WIP] feat: Fix incorrect pcap file handling
a18e Mar 3, 2023
3c25a08
[WIP] feat: Unify Protobuf naming
a18e Mar 3, 2023
aedd5f7
[WIP] feat:
a18e Mar 6, 2023
adda019
[WIP] feat:
a18e Mar 6, 2023
0f7a3b1
[WIP] feat:
a18e Mar 6, 2023
f19c4c8
Add local resolver as custom resolver so the basic api integration te…
peanball Mar 7, 2023
0245327
Remove integration test that checks the BOSH resolver
peanball Mar 7, 2023
fadcbf6
[WIP] feat: various
a18e Mar 7, 2023
0e05103
Align Bosh- & CfAgentResolver with changed interface
a18e Mar 9, 2023
f105647
client/bosh-cli: - Use context instead of signal channel
a18e Mar 9, 2023
f583f9e
rename variables to avoid conflicts with package names
a18e Mar 10, 2023
252fada
remove unnecessary build statements
a18e Mar 10, 2023
772d609
Adapt time comparison in client test
peanball Mar 13, 2023
549993d
[WIP] set up zap logging in pcap-bosh-cli
a18e Mar 13, 2023
9288758
- remove boshToken, use environment instead
a18e Mar 13, 2023
0488f77
- error handling in bosh-cli/main.go
a18e Mar 14, 2023
5ea64d0
refactor: rename Resolvers
a18e Mar 14, 2023
c29421c
- improve logging in client & cli
a18e Mar 16, 2023
874aa79
- Rename CfResolver to CloudfoundryResolver
a18e Mar 16, 2023
df70aa7
Refactor structure of the integration test
peanball Mar 16, 2023
b99e1ed
Use loopback device in client integration test
peanball Mar 16, 2023
782bdec
- implement support for multiple bosh-environments
a18e Mar 17, 2023
a4efb44
dissolve bosh package:
a18e Mar 17, 2023
4fbc74c
small fixes (review notes)
a18e Mar 17, 2023
06dd86a
- implement force output-file overwrite flag
a18e Mar 17, 2023
19bff25
Send a Stop command in client instead of cancelling the context
peanball Mar 18, 2023
f5c4340
Logging of messages to the console in a more readable fashion
peanball Mar 21, 2023
dc1a7d0
minor fixes
a18e Mar 21, 2023
cfb7060
Adjust log level to Panic from Fatal in pcap-cli main
peanball Mar 21, 2023
1747853
Disband `cmd` package, move functions to other files
peanball Mar 21, 2023
2dae4a7
Exclude .pcap files via gitignore
peanball Mar 21, 2023
43f1803
Compare zap log level better
peanball Mar 21, 2023
cbbbc48
pcap-api:
a18e Mar 21, 2023
8a59926
- move Jwt validation to BoshResolver
a18e Mar 21, 2023
abb2b12
- remove ManualEndpoints
a18e Mar 22, 2023
98be0ec
Remove logrus logger
peanball Mar 29, 2023
e874929
Pull out client message writer into an interface with a sane default …
peanball Mar 29, 2023
ec58904
Extract integration test helpers into separate file
peanball Mar 29, 2023
30ffb1a
Migrate to Go 1.20 and context.WithCancelCause
peanball Mar 30, 2023
533b08e
Enable per-node IPs for tests
peanball Mar 30, 2023
71089c5
Move bosh resolver test to separate package to avoid circular depende…
peanball Mar 31, 2023
7d7d107
Expose mock bosh resolver API server to retrieve URL
peanball Mar 31, 2023
8a08963
Completed integration tests. Corrected identified issues
peanball Apr 3, 2023
6213a95
Fix linter issues
peanball Apr 4, 2023
7e8f4c2
Address linter issues
peanball Apr 4, 2023
524b01c
fix(ci): add custom CI image with golang 1.20
peanball Apr 5, 2023
476415f
Address review feedback, simplifying JWT token validation
peanball Apr 5, 2023
53483e3
Reverse if ok logic for token validation
peanball Apr 6, 2023
aa51a06
Close response bodies in closure
peanball Apr 6, 2023
d05ae6c
Remove panics from pcap-api main
peanball Apr 11, 2023
0ec4fba
Better workaround for golang URL path bug
peanball Apr 12, 2023
699af65
Remove use of log.Panic in CLIs
peanball Apr 12, 2023
c8e206f
Improve error propagation for rejected tokens and address other TODOs
peanball Apr 12, 2023
542196b
Use a channel instead of wait group to communicate
peanball Apr 12, 2023
b7fc3f8
Avoid logging to stdout.
peanball Apr 12, 2023
c49bae4
Address further linter errors and fatal logs
peanball Apr 12, 2023
caeb735
Require configured resolvers to be healthy on startup
peanball Apr 12, 2023
d575551
Add connection checks for bosh resolver
peanball Apr 12, 2023
8094e80
Split the Stoppable interface
peanball Apr 12, 2023
46f9d48
Remove unused response status from getInstances()
peanball Apr 12, 2023
2e89e42
Make explicit that we're requiring an rsa.PublicKey
peanball Apr 12, 2023
4c8fdb0
Docs fixes and wording changes
peanball Apr 12, 2023
e96cdea
Fix ticker leak and remove unnecessary copy
peanball Apr 12, 2023
143e16b
Gave the default status timeout a name (constant)
peanball Apr 12, 2023
8b2db46
Add documentation on how to run tests on MacOS
peanball Apr 12, 2023
942dde3
Fix import order for linter
peanball Apr 12, 2023
80fac63
Remove client check in bosh resolver.
peanball Apr 13, 2023
a6ac53b
Rework the signal handling stop.
peanball Apr 13, 2023
7777fcf
Format wrapped errors correctly
peanball Apr 14, 2023
0b1c8a5
Apply suggestions from code review
peanball Apr 14, 2023
7fe1965
Exit on errors after removing log.Fatal
peanball Apr 14, 2023
fb03161
Handle exit flag parsing error on pcap-bosh-cli better
peanball Apr 14, 2023
3051ee7
Use json.NewDecoder instead of trying to read JSON data
peanball Apr 14, 2023
67ed09c
Fix pcap-bosh-cli api health check behaviour
peanball Apr 14, 2023
9954f41
refactor: simplify signal handling
maxmoehl Apr 14, 2023
3b9a187
Simplify config parsing logic for pcap-bosh-cli
peanball Apr 14, 2023
f159379
Simplify the StopOnSignal further
peanball Apr 14, 2023
88ffeea
Explicitly quote package paths for go test
peanball Apr 14, 2023
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ releases/**/*.tgz
#*
*.pem
*.key

# pcap files
*.pcap
13 changes: 13 additions & 0 deletions ci/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Dockerfile for image cf-routing.common.repositories.cloud.sap/pcap-release-testflight
FROM bosh/integration:main

ARG GINKGO_VERSION=latest
ARG GOLANGCILINT_VERSION=latest
RUN apt-get update && apt-get install -y libpcap-dev && rm -rf /var/lib/apt/lists/*

RUN rm -rf /usr/local/go
COPY --from=golang:1.20 /usr/local/go/ /usr/local/go/
ENV GOPATH=/go PATH=${PATH}:/go/bin

RUN go install "github.com/onsi/ginkgo/v2/ginkgo@${GINKGO_VERSION}" \
&& go install "github.com/golangci/golangci-lint/cmd/golangci-lint@${GOLANGCILINT_VERSION}"
16 changes: 12 additions & 4 deletions ci/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ jobs:
image_resource:
type: docker-image
source:
repository: bosh/integration
repository: cf-routing.common.repositories.cloud.sap/pcap-release-testflight
tag: latest
username: ((docker.username))
password: ((docker.password))
inputs:
- { name: git }
caches:
Expand All @@ -37,8 +39,10 @@ jobs:
image_resource:
type: docker-image
source:
repository: bosh/integration
repository: cf-routing.common.repositories.cloud.sap/pcap-release-testflight
tag: latest
username: ((docker.username))
password: ((docker.password))
inputs:
- { name: git }

Expand Down Expand Up @@ -75,8 +79,10 @@ jobs:
image_resource:
type: docker-image
source:
repository: bosh/integration
repository: cf-routing.common.repositories.cloud.sap/pcap-release-testflight
tag: latest
username: ((docker.username))
password: ((docker.password))
inputs:
- { name: git-pull-requests }
caches:
Expand All @@ -93,8 +99,10 @@ jobs:
image_resource:
type: docker-image
source:
repository: bosh/integration
repository: cf-routing.common.repositories.cloud.sap/pcap-release-testflight
tag: latest
username: ((docker.username))
password: ((docker.password))
inputs:
- { name: git-pull-requests }
caches:
Expand Down
4 changes: 1 addition & 3 deletions ci/scripts/lint
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#!/bin/bash

set -euo pipefail

apt-get update && apt-get install -y libpcap-dev # TODO: remove and move to dockerfile

cd "${REPO_ROOT}"

echo "> Running 'bundle exec rake lint'"
Expand All @@ -13,4 +10,5 @@ bundle exec rake lint
echo "> Running 'go vet'"
pushd src/pcap
go vet ./...
golangci-lint run
popd
13 changes: 3 additions & 10 deletions ci/scripts/unit-tests
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

set -euo pipefail

apt-get update && apt-get install -y libpcap-dev # TODO: remove and move to dockerfile

cd "${REPO_ROOT}"

echo "> Running 'bundle exec rake spec'"
Expand All @@ -12,15 +10,10 @@ bundle exec rake spec

echo "> Running unit tests"
pushd src/pcap
go test $(go list ./... | grep -v /integrationtests)
go test -count=1 -v $(go list ./... | grep -v test/integration)
popd

echo "> Running integration tests"
pushd src/pcap/integrationtests
mkdir /go
export GOPATH=/go
go install "github.com/onsi/ginkgo/v2/ginkgo@latest"
export PATH=$PATH:$GOPATH/bin
ginkgo version
echo "> Running integration tests (ginkgo)"
pushd src/pcap/test/integration
ginkgo -r -trace -vv
popd
2 changes: 1 addition & 1 deletion packages/pcap-agent/packaging
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ set -e
# $BOSH_COMPILE_TARGET - where this package & spec'd source files are available
# $BOSH_INSTALL_TARGET - where you copy/install files to be included in package

tar xzf golang/go1.18.5.linux-amd64.tar.gz
tar xzf golang/go1.20.2.linux-amd64.tar.gz
export GOROOT="${BOSH_COMPILE_TARGET}/go"
export PATH="${PATH}:${GOROOT}/bin"

Expand Down
2 changes: 1 addition & 1 deletion packages/pcap-api/packaging
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -e
# $BOSH_INSTALL_TARGET - where you copy/install files to be included in package

# FIXME: Adjust to vendored BOSH package
tar xzf golang/go1.18.5.linux-amd64.tar.gz
tar xzf golang/go1.20.2.linux-amd64.tar.gz
export GOROOT=$BOSH_COMPILE_TARGET/go

# TODO: maybe these are not needed? Where does this get executed?
Expand Down
5 changes: 5 additions & 0 deletions src/pcap/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM golang:1.20-bullseye

RUN apt update && apt install libpcap-dev -y


46 changes: 46 additions & 0 deletions src/pcap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,49 @@ Install `ChmodBPF`, available as part of Wireshark.
The path to the installer can be found in Wireshark > About Wireshark > Folders, MacOS Extras.

The installer is called `Install ChmodBPF.pkg`.

### Running the tests on MacOS

All unit tests can be run locally via `go test`. The integration tests are included in `go test ./...` however and require some special handling.

The integration tests open mock servers on different IPs on the loopback device in the CIDR `127.0.0.0/8`.
MacOS does not by default support addresses other than `127.0.0.1`. It can be configured via aliases, but attempts to do so have not led to successful test execution.

Instead, a Dockerfile and docker-compose file are provided, which will automatically execute all tests (go test and ginkgo tests via test suite).

```shell
cd src/pcap
# the dockerfile is relative to src/pcap and mounts the current code as volume into the container.
docker compose up
```

This will build the image from `Dockerfile` and run `go test -v ./...`. This should give you confidence in the tests completing successfully.

### Running in IntelliJ

Using the Dockerfile or Docker Compose file, you can also run the tests using the Go integration in IntelliJ.

When selecting the run configuration, set up a Docker run target, either with Docker or Docker Compose. [Documentation on setting up run targets](https://www.jetbrains.com/help/idea/run-targets.html#target-types) os available from JetBrains.

### Running Ginkgo Tests in IntelliJ and Docker

There is a [Ginkgo plugin for IntelliJ](https://plugins.jetbrains.com/plugin/17554-ginkgo), which helps develop ginkgo tests. It can be used to visualize the structure of the test file (via the Structure view) and execute specific tests in Focus mode.

Some of the integration tests require the aforementioned multiple IP addresses and will not run directly. The Ginkgo plugin does unfortunately not integrate with run targets.

A run configuration with `go test` can be created, which will call the Ginkgo tests via Test Suite integration.
Furthermore, Ginkgo command line flags can be passed on to this command as well.

When developing Ginkgo tests, you often want to execute a subset of tests in Focus mode. By adding the `-ginkgo.focus "Your ginkgo focus test expression"` flag to the `go test` command, you can still use focus mode.

Example:

```
go test github.com/cloudfoundry/pcap-release/src/pcap/test/integration -test.v -ginkgo.focus="Some Test Names"
```

You can configure an appropriate run configuration with a Docker based run target, as shown in the screenshot below.

**Important:** Select the "Build on remote Target" checkbox as cross-compiling pcap-release with libpcap is not easily possible.

![Build configuration in IntelliJ](docs/run-config.png)
12 changes: 6 additions & 6 deletions src/pcap/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (a *Agent) Capture(stream Agent_CaptureServer) (err error) {
}
}()

ctx, cancel := WithCancelCause(stream.Context())
ctx, cancel := context.WithCancelCause(stream.Context())
defer cancel(nil)

ctx, log = setVcapID(ctx, log, nil)
Expand Down Expand Up @@ -143,7 +143,7 @@ func (a *Agent) Capture(stream Agent_CaptureServer) (err error) {
<-ctx.Done()
}

err = Cause(ctx)
err = context.Cause(ctx)
// Cancelling the context with nil causes context.Cancelled to be set
// which is a non-error in our case.
if err != nil && !errors.Is(err, context.Canceled) {
Expand Down Expand Up @@ -216,7 +216,7 @@ type pcapHandle interface {
// channel. If the given context errors the loop breaks with the next read.
// If an error is encountered while reading packets the cancel function is
// called and the loop is stopped.
func readPackets(ctx context.Context, cancel CancelCauseFunc, handle pcapHandle, bufSize int) <-chan *CaptureResponse {
func readPackets(ctx context.Context, cancel context.CancelCauseFunc, handle pcapHandle, bufSize int) <-chan *CaptureResponse {
out := make(chan *CaptureResponse, bufSize)

go func() {
Expand All @@ -234,13 +234,13 @@ func readPackets(ctx context.Context, cancel CancelCauseFunc, handle pcapHandle,
return
}

data, _, err := handle.ReadPacketData()
data, captureInfo, err := handle.ReadPacketData()
if err != nil {
cancel(fmt.Errorf("read packet: %w", err))
return
}

out <- newPacketResponse(data)
out <- newPacketResponse(data, captureInfo)
}
}()

Expand All @@ -260,7 +260,7 @@ type agentRequestReceiver interface {
// agentStopCmd reads the next message from the stream. It ensures that the message
// has a payload of StopAgentCapture. If any error is encountered or the payload is
// of a different type an appropriate cause is set and the cancel function is called.
func agentStopCmd(cancel CancelCauseFunc, stream agentRequestReceiver) {
func agentStopCmd(cancel context.CancelCauseFunc, stream agentRequestReceiver) {
go func() {
msg, err := stream.Recv()
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions src/pcap/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ func TestAgentStopCmd(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx := context.Background()
ctx, cancel := WithCancelCause(ctx)
ctx, cancel := context.WithCancelCause(ctx)
agentStopCmd(cancel, test.recv)
<-ctx.Done()

err := Cause(ctx)
err := context.Cause(ctx)

if (err != nil) != test.wantErr {
t.Errorf("wantErr = %v, error = %v", test.wantErr, err)
Expand Down Expand Up @@ -136,7 +136,7 @@ func TestReadPackets(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx := context.Background()
ctx, cancel := WithCancelCause(ctx)
ctx, cancel := context.WithCancelCause(ctx)
if test.contextCancelled {
// cancel context before read packets in order to get edge case
cancel(errContextCancelled)
Expand All @@ -146,7 +146,7 @@ func TestReadPackets(t *testing.T) {

<-ctx.Done()

err := Cause(ctx)
err := context.Cause(ctx)
if err != nil && !errors.Is(err, test.expectedErr) && !errors.Is(err, errTestEnded) {
t.Errorf("expectedErr = %v, got err = %v", test.expectedErr, err)
}
Expand Down
Loading