Skip to content

Commit

Permalink
bump mod and Dockerfile
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Bertschy <matthias.bertschy@gmail.com>
  • Loading branch information
matthyx committed Mar 11, 2024
1 parent 7f66e67 commit abca3e9
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 38 deletions.
29 changes: 13 additions & 16 deletions build/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
14 changes: 9 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
)
28 changes: 19 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
27 changes: 19 additions & 8 deletions request.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package main

import (
"bytes"
"flag"
"fmt"
"net/http"
"net/url"
"os"
"slices"
"strings"

"github.com/armosec/utils-go/httputils"
Expand Down Expand Up @@ -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
Expand All @@ -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)
}

Expand Down

0 comments on commit abca3e9

Please sign in to comment.