|
| 1 | +--- |
| 2 | +title: Building your Go application with Datadog's WAF |
| 3 | +code_lang: docker |
| 4 | +type: multi-code-lang |
| 5 | +code_lang_weight: 10 |
| 6 | +further_reading: |
| 7 | +- link: "/security/application_security/how-it-works/" |
| 8 | + tag: "Documentation" |
| 9 | + text: "How App and API Protection Works" |
| 10 | +- link: "/security/default_rules/?category=cat-application-security" |
| 11 | + tag: "Documentation" |
| 12 | + text: "OOTB App and API Protection Rules" |
| 13 | +- link: "/security/application_security/troubleshooting" |
| 14 | + tag: "Documentation" |
| 15 | + text: "Troubleshooting App and API Protection" |
| 16 | +--- |
| 17 | + |
| 18 | +# Introdution |
| 19 | + |
| 20 | +App and API Protection for Go installation requirements can be abstract and the Go toolchain |
| 21 | +cross-compilations capabilities can make it hard to understand what has to be done precisely. |
| 22 | + |
| 23 | +In these cases, a more precise way to materialize these examples like a Dockerfile can be interesting. |
| 24 | +The goal of this guide is to be a step-by-step guide to a working Dockerfile. |
| 25 | + |
| 26 | +## Wallthrough |
| 27 | + |
| 28 | +This dockerfile can be found like in the [appsec-go-test-app][4] repository. To try it out, first clone the repository: |
| 29 | + |
| 30 | +```sh |
| 31 | +git clone https://github.com/DataDog/appsec-go-test-app.git |
| 32 | +cd appsec-go-test-app |
| 33 | +``` |
| 34 | + |
| 35 | +A list of `Dockerfile` examples can be found in the [`examples/docker`][3] directory. |
| 36 | +Here is an example of it in its simplest form: |
| 37 | + |
| 38 | +```dockerfile |
| 39 | +# |
| 40 | +FROM golang AS build |
| 41 | +WORKDIR /app |
| 42 | +COPY . . |
| 43 | + |
| 44 | +RUN go install github.com/DataDog/orchestrion@latest |
| 45 | + |
| 46 | +# The appsec build tag is mandatory if CGO is disabled, which is the default in alpine. |
| 47 | +RUN orchestrion go build -v -tags appsec -o main . |
| 48 | + |
| 49 | +FROM alpine |
| 50 | +COPY --from=build /app/main /usr/local/bin |
| 51 | + |
| 52 | +# Every required shared library is already present in alpine, but the C library |
| 53 | +# doesn't have the standard name on alpine by default. Adding the libc6-compat |
| 54 | +# package allows to add symlinks with the expected names. |
| 55 | +RUN apk update && apk add libc6-compat |
| 56 | + |
| 57 | +# Enable the App and API Protection |
| 58 | +ENV DD_APPSEC_ENABLED=true |
| 59 | +ENTRYPOINT [ "/usr/local/bin/main" ] |
| 60 | +``` |
| 61 | + |
| 62 | +Multiple remarks can be made here: |
| 63 | +* The first stage uses [Orchestrion][5] compile-time instrumentation to instrument the Go application with the App and API Protection features. |
| 64 | +* The flag `-tags appsec` or CGO being enabled are requirements at build time for C++ Datadog's WAF. If none of these requirements are met, your service will be marked as not compatible is Datadog's UI. |
| 65 | +* the `libc6-compat` package is required because Datadog's WAF needs the following shared libraries on Linux: `libc.so.6` and `libpthread.so.0`. If you are using `CGO_ENABLED=0` and `-tags` appsec at the same time and those shared libraries are not present at runtime you app will refuse to start with the error `No such file or directory`. |
| 66 | + |
| 67 | +## Run your application |
| 68 | + |
| 69 | +Now that the dockerfile is ready you can build the [appsec-go-test-app][4]: |
| 70 | + |
| 71 | +```sh |
| 72 | +docker build -f ./examples/alpine/Dockerfile -t appsec-go-test-app . |
| 73 | +docker run appsec-go-test-app |
| 74 | +``` |
| 75 | + |
| 76 | +{{% app_and_api_protection_verify_setup %}} |
| 77 | + |
| 78 | +## Troubleshooting |
| 79 | + |
| 80 | +If you encounter issues while setting up App and API Protection for your application, see the [Go App and API Protection troubleshooting guide][2]. |
| 81 | + |
| 82 | +## Further Reading |
| 83 | + |
| 84 | +{{< partial name="whats-next/whats-next.html" >}} |
| 85 | + |
| 86 | +[1]: /security/application_security/setup/go/compatibility |
| 87 | +[2]: /security/application_security/setup/go/troubleshooting |
| 88 | +[3]: https://github.com/DataDog/appsec-go-test-app/blob/main/examples/docker |
| 89 | +[4]: https://github.com/DataDog/appsec-go-test-app |
| 90 | +[5]: /tracing/trace_collection/automatic_instrumentation/dd_libraries/go/?tab=compiletimeinstrumentation |
0 commit comments