From 4e8926f36c939c72f52efd0d0bc5c0b2b96fa673 Mon Sep 17 00:00:00 2001 From: Matthias Bertschy Date: Mon, 11 Mar 2024 22:42:41 +0100 Subject: [PATCH] bump mod and Dockerfile Signed-off-by: Matthias Bertschy --- .github/workflows/pr-created.yaml | 8 ++++---- .github/workflows/pr-merged.yaml | 12 ++++++------ build/Dockerfile | 29 +++++++++++++---------------- go.mod | 14 +++++++++----- go.sum | 28 +++++++++++++++++++--------- request.go | 27 +++++++++++++++++++-------- 6 files changed, 70 insertions(+), 48 deletions(-) diff --git a/.github/workflows/pr-created.yaml b/.github/workflows/pr-created.yaml index 50de0e0..9bc8dbd 100644 --- a/.github/workflows/pr-created.yaml +++ b/.github/workflows/pr-created.yaml @@ -2,10 +2,10 @@ name: pull_request_created on: pull_request: types: [opened, reopened, synchronize, ready_for_review] - branches: + branches: - 'main' paths-ignore: - - '*.md' + - '*.md' - '*.yaml' - '.github/workflows/*' @@ -17,6 +17,6 @@ jobs: pr-created: uses: kubescape/workflows/.github/workflows/incluster-comp-pr-created.yaml@main with: - GO_VERSION: "1.20" + GO_VERSION: "1.21" CGO_ENABLED: 0 - secrets: inherit \ No newline at end of file + secrets: inherit diff --git a/.github/workflows/pr-merged.yaml b/.github/workflows/pr-merged.yaml index 87044b9..15ba06e 100644 --- a/.github/workflows/pr-merged.yaml +++ b/.github/workflows/pr-merged.yaml @@ -2,20 +2,20 @@ name: pr-merged on: pull_request_target: types: [closed] - branches: + branches: - 'main' paths-ignore: - - '*.md' + - '*.md' - '*.yaml' - '.github/workflows/*' concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true - + jobs: pr-merged: - if: ${{ github.event.pull_request.merged == true }} ## Skip if not merged + if: ${{ github.event.pull_request.merged == true }} ## Skip if not merged uses: kubescape/workflows/.github/workflows/incluster-comp-pr-merged.yaml@main with: IMAGE_NAME: quay.io/${{ github.repository_owner }}/http-request @@ -24,7 +24,7 @@ jobs: CGO_ENABLED: 0 GO111MODULE: "on" BUILD_PLATFORM: linux/amd64,linux/arm64 - GO_VERSION: "1.20" + GO_VERSION: "1.21" REQUIRED_TESTS: '[ "vulnerability_scanning_triggering_with_cron_job", "registry_scanning_triggering_with_cron_job", @@ -39,4 +39,4 @@ jobs: ]' COSIGN: true HELM_E2E_TEST: true - secrets: inherit \ No newline at end of file + secrets: inherit diff --git a/build/Dockerfile b/build/Dockerfile index 4cd41dd..7f24f87 100644 --- a/build/Dockerfile +++ b/build/Dockerfile @@ -1,25 +1,22 @@ -FROM golang:1.20-alpine as builder - -ENV RELEASE=$image_version - -ENV GO111MODULE= - -ENV CGO_ENABLED=0 +FROM --platform=$BUILDPLATFORM golang:1.21-bullseye as builder +ENV GO111MODULE=on CGO_ENABLED=0 WORKDIR /work -ADD . . +ARG TARGETOS TARGETARCH -RUN apk add git +RUN --mount=target=. \ + --mount=type=cache,target=/root/.cache/go-build \ + --mount=type=cache,target=/go/pkg \ + GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o /out/http_request -WORKDIR /work -RUN go build -o build/http_request +FROM gcr.io/distroless/static-debian11:nonroot -FROM alpine +USER nonroot +WORKDIR /home/nonroot/ -RUN addgroup -S ks && adduser -S ks -G ks -USER ks -WORKDIR /home/ks/ +COPY --from=builder /out/http_request /usr/bin/http_request -COPY --from=builder /work/build/http_request /usr/bin/http_request +ARG image_version +ENV RELEASE=$image_version ENTRYPOINT ["http_request"] diff --git a/go.mod b/go.mod index 956266f..2900a7c 100644 --- a/go.mod +++ b/go.mod @@ -1,14 +1,18 @@ module github.com/kubescape/http-request -go 1.20 +go 1.21 + +toolchain go1.21.6 require ( - github.com/armosec/utils-go v0.0.26 - github.com/stretchr/testify v1.8.4 + github.com/armosec/utils-go v0.0.57 + github.com/stretchr/testify v1.9.0 ) require ( - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/cenkalti/backoff v2.2.1+incompatible // indirect + github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect + github.com/kr/text v0.2.0 // indirect + github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 9ef40d4..dfdb490 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,22 @@ -github.com/armosec/utils-go v0.0.26 h1:EPHu0P8XfoCrsNDf3OlO1RexFlgy77wGS1lY5aNI9Fk= -github.com/armosec/utils-go v0.0.26/go.mod h1:ZEFiSv8KpTFNT19jHis1IengiF/BGDvg7tHmXo+cwxs= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +github.com/armosec/utils-go v0.0.57 h1:0RaqexK+t7HeKWfldBv2C1JiLLGuUx9FP0DGWDNRJpg= +github.com/armosec/utils-go v0.0.57/go.mod h1:4wfINE8JTQ6EHvSL2jki0Q3/D1j6oDi6sxxrtAEug74= +github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4= +github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/request.go b/request.go index 29e219d..334b19c 100644 --- a/request.go +++ b/request.go @@ -1,11 +1,13 @@ package main import ( + "bytes" "flag" "fmt" "net/http" "net/url" "os" + "slices" "strings" "github.com/armosec/utils-go/httputils" @@ -71,6 +73,14 @@ func loadBody(f *FlagParser) ([]byte, error) { return []byte{}, nil } +func setHeaders(req *http.Request, headers map[string]string) { + if headers != nil && len(headers) > 0 { + for k, v := range headers { + req.Header.Set(k, v) + } + } +} + // Request run a http request func Request(f *FlagParser) (string, error) { var resp *http.Response @@ -87,14 +97,15 @@ func Request(f *FlagParser) (string, error) { fmt.Printf("method: %s, url: %s, headers: %v, body: %s\n", f.method, f.fullURL.String(), headers, body) - switch strings.ToUpper(f.method) { - case http.MethodPost: - resp, err = httputils.HttpPost(http.DefaultClient, f.fullURL.String(), headers, body) - case http.MethodGet: - resp, err = httputils.HttpGet(http.DefaultClient, f.fullURL.String(), headers) - case http.MethodDelete: - resp, err = httputils.HttpDelete(http.DefaultClient, f.fullURL.String(), headers) - default: + methods := []string{http.MethodPost, http.MethodGet, http.MethodDelete} + if slices.Contains(methods, strings.ToUpper(f.method)) { + req, err := http.NewRequest(f.method, f.fullURL.String(), bytes.NewReader(body)) + if err != nil { + return "", err + } + setHeaders(req, headers) + resp, err = http.DefaultClient.Do(req) + } else { return "", fmt.Errorf("method %s not supported", f.method) }