From aabebdd8add9dd8cf054461b8baefaedaee29eda Mon Sep 17 00:00:00 2001 From: Alok G Singh Date: Tue, 28 Jul 2020 23:59:35 +0530 Subject: [PATCH] newbuild endpoint implemented enough tests 76% coverage dockerfile --- .githooks/pre-commit | 15 +++++++--- Dockerfile | 16 +++++++++++ TODO => TODO.md | 3 +- cmd/client.go | 6 ++-- cmd/serve.go | 33 ---------------------- devenv/ecr.go | 14 ++++------ go.mod | 3 +- go.sum | 39 -------------------------- server/app.go | 65 ++++++++++++++++++++++++++++++++++++++++++-- server/app_test.go | 50 ++++++++++++++++++++++++++++++++++ 10 files changed, 154 insertions(+), 90 deletions(-) create mode 100644 Dockerfile rename TODO => TODO.md (51%) diff --git a/.githooks/pre-commit b/.githooks/pre-commit index defea3b1..a0b5100d 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -1,9 +1,16 @@ #!/bin/sh # -# Update the TODO file on commit +# Update $TODO_FILE on commit +# Hooks, except the push related ones run in repo root exec 1>&2 -echo Updating TODO file from $(pwd) -grep -ri TODO * > TODO -exec git add TODO +TODO_FILE=TODO.md +echo Updating $TODO_FILE from $(pwd) +cat > $TODO_FILE <> $TODO_FILE +exec git add $TODO_FILE diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..584177ec --- /dev/null +++ b/Dockerfile @@ -0,0 +1,16 @@ +FROM golang:1.14 as builder + +RUN CGO_ENABLED=0 go get github.com/TykTechnologies/gromit + +# generate clean, final image for end users +FROM alpine:latest +COPY --from=builder /go/bin/* /usr/bin/ + +EXPOSE 443 +VOLUME /cfssl +WORKDIR /cfssl + +# executable +ENTRYPOINT [ "gromit" ] +# arguments that can be overridden +CMD [ "serve", "--certpath=gromit/server" ] diff --git a/TODO b/TODO.md similarity index 51% rename from TODO rename to TODO.md index 23cad9ca..a9286b1b 100644 --- a/TODO +++ b/TODO.md @@ -1,3 +1,4 @@ -cmd/serve.go:// TODO: Implement newbuild ahndler in new mux code +# TODOs +Generated from lines with TODO in the repo , use the pre-commit hook in . Binary file gromit matches server/app.go:// TODO Implement listing of all environments diff --git a/cmd/client.go b/cmd/client.go index 68f38035..6e8ff8ec 100644 --- a/cmd/client.go +++ b/cmd/client.go @@ -90,11 +90,13 @@ var clientCmd = &cobra.Command{ } } host, _ := cmd.Flags().GetString("server") - healthcheck(client, fmt.Sprintf("https://%s/healthcheck", host)) + healthcheckURL := fmt.Sprintf("https://%s/healthcheck", host) + log.Debug().Msgf("Going to healthcheck %s", healthcheckURL) + makeTLSRequest(client, healthcheckURL) }, } -func healthcheck(client http.Client, url string) { +func makeTLSRequest(client http.Client, url string) { req, err := http.NewRequest("GET", url, nil) if err != nil { log.Fatal().Err(err).Msg("could not construct request") diff --git a/cmd/serve.go b/cmd/serve.go index ded636a4..ef89594c 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -45,36 +45,3 @@ func init() { serveCmd.Flags().StringVar(&certPath, "certpath", "certs", "path to rootca and key pair. Expects files named ca.pem, server(-key).pem") } - -// TODO: Implement newbuild ahndler in new mux code -// func newBuild(cfg *aws.Config, w http.ResponseWriter, r *http.Request) { -// var req Build -// err := json.NewDecoder(r.Body).Decode(&req) -// if err != nil { -// http.Error(w, err.Error(), http.StatusBadRequest) -// return -// } -// log.Trace().Interface("req", req).Msg("parsed from github") - -// // Github sends a path like refs/.../integration/ -// ss := strings.Split(req.Ref, "/") -// req.Ref = ss[len(ss)-1] - -// state, err := devenv.GetEnvState(ecr.New(*cfg), e.RegistryID, req.Ref, e.Repos) -// if err != nil { -// log.Warn().Err(err).Msg("could not unmarhsal state") -// http.Error(w, err.Error(), http.StatusInternalServerError) -// return -// } -// log.Trace().Interface("state", state).Msg("initial") -// state[req.Repo] = req.Sha -// log.Trace().Interface("state", state).Msg("final") - -// err = devenv.UpsertNewBuild(dynamodb.New(*cfg), e.TableName, req.Ref, state) -// if err != nil { -// log.Warn().Err(err).Msg("could not add new build") -// http.Error(w, err.Error(), http.StatusBadRequest) -// return -// } -// io.WriteString(w, "OK New build "+req.Ref) -// } diff --git a/devenv/ecr.go b/devenv/ecr.go index 92da9e2d..9143b13c 100644 --- a/devenv/ecr.go +++ b/devenv/ecr.go @@ -9,13 +9,11 @@ import ( "github.com/rs/zerolog/log" ) -// EnvState holds the branch name for an environment -type EnvState map[string]string - -// GetEnvState returns master as the ref if no env was found -func GetEnvState(svc ecriface.ClientAPI, registry string, env string, repos []string) (EnvState, error) { - var state = make(EnvState) - state["name"] = env +// GetECRState returns master as the ref if no env was found. +// Using DevEnv here to model the state of the repositories, the actual repos +// being used is abstracted away. Hopefully, this will turn out to be the right choice. +func GetECRState(svc ecriface.ClientAPI, registry string, env string, repos []string) (DevEnv, error) { + var state = make(DevEnv) for _, repo := range repos { tag, err := getExistingTag(svc, registry, repo, env) @@ -37,7 +35,7 @@ func getExistingTag(svc ecriface.ClientAPI, registry string, repo string, tag st input := &ecr.DescribeImagesInput{ RegistryId: aws.String(registry), RepositoryName: aws.String(repo), - MaxResults: func() *int64 { i := int64(1000); return &i }(), + MaxResults: aws.Int64(1000), } req := svc.DescribeImagesRequest(input) diff --git a/go.mod b/go.mod index e89c0884..f9bfd6ac 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,6 @@ go 1.14 require ( github.com/aws/aws-sdk-go-v2 v0.24.0 - github.com/cloudflare/cfssl v1.4.1 github.com/gorilla/mux v1.7.4 github.com/kelseyhightower/envconfig v1.4.0 github.com/mitchellh/go-homedir v1.1.0 @@ -12,4 +11,6 @@ require ( github.com/spf13/cobra v1.0.0 github.com/spf13/viper v1.4.0 github.com/stretchr/testify v1.4.0 + golang.org/x/sys v0.0.0-20190412213103-97732733099d // indirect + golang.org/x/text v0.3.2 // indirect ) diff --git a/go.sum b/go.sum index 589214d8..64ce154c 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,7 @@ -bitbucket.org/liamstask/goose v0.0.0-20150115234039-8488cc47d90c/go.mod h1:hSVuE3qU7grINVSwrmzHfpg9k87ALBk+XaualNyUzI4= cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= -github.com/GeertJohan/go.incremental v1.0.0/go.mod h1:6fAjUhbVuX1KcMD3c8TEgVUqmo4seqhv0i0kdATSkM0= -github.com/GeertJohan/go.rice v1.0.0/go.mod h1:eH6gbSOAUv07dQuZVnBmoDP8mgsM1rtixis4Tib9if0= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= -github.com/akavel/rsrc v0.8.0/go.mod h1:uLoCtb9J+EyAqh+26kdrTgmzRBFPGOolLWKpdxkKq+c= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= @@ -13,21 +9,14 @@ github.com/aws/aws-sdk-go-v2 v0.24.0 h1:R0lL0krk9EyTI1vmO1ycoeceGZotSzCKO51LbPGq github.com/aws/aws-sdk-go-v2 v0.24.0/go.mod h1:2LhT7UgHOXK3UXONKI5OMgIyoQL6zTAw/jwIeX6yqzw= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= -github.com/certifi/gocertifi v0.0.0-20180118203423-deb3ae2ef261/go.mod h1:GJKEexRPVJrBSOjoqN5VNOIKJ5Q3RViH6eu3puDRwx4= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= -github.com/cloudflare/backoff v0.0.0-20161212185259-647f3cdfc87a/go.mod h1:rzgs2ZOiguV6/NpiDgADjRLPNyZlApIWxKpkT+X8SdY= -github.com/cloudflare/cfssl v1.4.1 h1:vScfU2DrIUI9VPHBVeeAQ0q5A+9yshO1Gz+3QoUQiKw= -github.com/cloudflare/cfssl v1.4.1/go.mod h1:KManx/OJPb5QY+y0+o/898AMcM128sF0bURvoVUSjTo= -github.com/cloudflare/go-metrics v0.0.0-20151117154305-6a9aea36fb41/go.mod h1:eaZPlJWD+G9wseg1BuRXlHnjntPMrywMsyxf+LTOdP4= -github.com/cloudflare/redoctober v0.0.0-20171127175943-746a508df14c/go.mod h1:6Se34jNoqrd8bTxrmJB2Bg2aoZ2CdSXonils9NsiNgo= github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= -github.com/daaku/go.zipexe v1.0.0/go.mod h1:z8IiR6TsVLEYKwXAoE/I+8ys/sDkgTzSL0CLnGVd57E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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= @@ -35,12 +24,10 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZm github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= -github.com/getsentry/raven-go v0.0.0-20180121060056-563b81fc02b7/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= -github.com/go-sql-driver/mysql v1.3.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= @@ -51,7 +38,6 @@ github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfb github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= -github.com/google/certificate-transparency-go v1.0.21/go.mod h1:QeJfpSbVSfYc7RgB3gJFj9cbuQMMchQxrWXz8Ruopmg= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= @@ -65,19 +51,14 @@ github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= -github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= -github.com/jmhodges/clock v0.0.0-20160418191101-880ee4c33548/go.mod h1:hGT6jSUVzF6no3QaDSMLGLEHtHSBSefs+MgcDWnmhmo= -github.com/jmoiron/sqlx v0.0.0-20180124204410-05cef0741ade/go.mod h1:IiEW3SEiiErVyFdH8NTuWjSifiEQKUoyK3LNqr2kCHU= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8= github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg= github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= -github.com/kisielk/sqlstruct v0.0.0-20150923205031-648daed35d49/go.mod h1:yyMNCyc/Ib3bDTKd379tNMpB/7/H5TjM2Y9QJ5THLbE= -github.com/kisom/goutils v1.1.0/go.mod h1:+UBTfd78habUYWFbNWTJNG+jNG/i/lGURakr4A/yNRw= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= @@ -85,21 +66,15 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/kylelemons/go-gypsy v0.0.0-20160905020020-08cad365cd28/go.mod h1:T/T7jsxVqf9k/zYOqbgNAsANsjxTd1Yq3htjDhQ1H0c= -github.com/lib/pq v0.0.0-20180201184707-88edab080323/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= -github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= -github.com/mreiferson/go-httpclient v0.0.0-20160630210159-31f0106b4474/go.mod h1:OQA4XLvDbMgS8P0CevmM4m9Q3Jq4phKUzcocxuGJ5m8= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= -github.com/nkovacs/streamquote v0.0.0-20170412213628-49af9bddb229/go.mod h1:0aYXnNPJ8l7uZxf45rWW1a/uME32OF0rhiYGNQ2oF2E= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= -github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk= github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -122,7 +97,6 @@ github.com/rs/zerolog v1.19.0/go.mod h1:IzD0RJ65iWH0w97OQQebJEvTZYvsCUm9WVLWBQrJ github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= -github.com/sirupsen/logrus v1.3.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI= @@ -141,37 +115,24 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= -github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= -github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= -github.com/weppos/publicsuffix-go v0.4.0/go.mod h1:z3LCPQ38eedDQSwmsSRW4Y7t2L8Ln16JPQ02lHAdn5k= -github.com/weppos/publicsuffix-go v0.5.0/go.mod h1:z3LCPQ38eedDQSwmsSRW4Y7t2L8Ln16JPQ02lHAdn5k= github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= -github.com/ziutek/mymysql v1.5.4/go.mod h1:LMSpPZ6DbqWFxNCHW77HeMg9I646SAhApZ/wKdgO/C0= -github.com/zmap/rc2 v0.0.0-20131011165748-24b9757f5521/go.mod h1:3YZ9o3WnatTIZhuOtot4IcUfzoKVjUHqu6WALIyI0nE= -github.com/zmap/zcertificate v0.0.0-20180516150559-0e3d58b1bac4/go.mod h1:5iU54tB79AMBcySS0R2XIyZBAVmeHranShAFELYx7is= -github.com/zmap/zcrypto v0.0.0-20190729165852-9051775e6a2e/go.mod h1:w7kd3qXHh8FNaczNjslXqvFQiv5mMWRXlL9klTUAHc8= -github.com/zmap/zlint v0.0.0-20190806154020-fd021b4cfbeb/go.mod h1:29UiAJNsiVdvTBFCJW8e3q6dcDbOoPkhMgttOSCIMMY= go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= -golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= diff --git a/server/app.go b/server/app.go index 58030a74..16d698eb 100644 --- a/server/app.go +++ b/server/app.go @@ -10,6 +10,8 @@ import ( "io" "io/ioutil" "net/http" + "net/url" + "strings" "github.com/TykTechnologies/gromit/devenv" "github.com/aws/aws-sdk-go-v2/aws" @@ -88,7 +90,6 @@ func (a *App) Init(ca string) { a.tlsConfig = tlsConfig log.Debug().Msgf("CA cert loaded from %s", ca) - a.Router = mux.NewRouter() a.initRoutes() } @@ -96,6 +97,7 @@ func (a *App) Init(ca string) { func (a *App) Run(addr string, cert string, key string) { server := &http.Server{ Addr: addr, + Handler: a.Router, TLSConfig: a.tlsConfig, } log.Info().Msg("starting server") @@ -105,12 +107,16 @@ func (a *App) Run(addr string, cert string, key string) { } func (a *App) initRoutes() { + a.Router = mux.NewRouter() + a.Router.HandleFunc("/healthcheck", a.healthCheck).Methods("GET") a.Router.HandleFunc("/loglevel/{level}", a.setLoglevel).Methods("PUT") a.Router.HandleFunc("/loglevel", a.getLoglevel).Methods("GET") - //a.Router.HandleFunc("/newbuild", a.newBuild).Methods("POST") + // Endpoint for int-image GHA + a.Router.HandleFunc("/newbuild", a.newBuild).Methods("POST") + // ReST API a.Router.HandleFunc("/envs", a.getEnvs).Methods("GET") a.Router.HandleFunc("/env/{name}", a.createEnv).Methods("PUT") a.Router.HandleFunc("/env/{name}", a.updateEnv).Methods("PATCH") @@ -118,6 +124,8 @@ func (a *App) initRoutes() { a.Router.HandleFunc("/env/{name}", a.getEnv).Methods("GET") } +// Infra routes + func (a *App) healthCheck(w http.ResponseWriter, r *http.Request) { io.WriteString(w, "OK") log.Debug().Msg("Healthcheck") @@ -143,6 +151,59 @@ func (a *App) getLoglevel(w http.ResponseWriter, r *http.Request) { respondWithJSON(w, http.StatusOK, loglevel) } +func getTrailingElement(string string, separator string) string { + urlDecoded, err := url.QueryUnescape(string) + if err != nil { + log.Debug().Err(err).Msgf("could not decode %s, proceeding anyway", string) + } + stringArray := strings.Split(urlDecoded, separator) + return stringArray[len(stringArray)-1] +} + +// This is the handler that is invoked from github + +func (a *App) newBuild(w http.ResponseWriter, r *http.Request) { + newBuild := make(map[string]string) + err := json.NewDecoder(r.Body).Decode(&newBuild) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + log.Trace().Interface("newBuild", newBuild).Msg("parsed from github") + + // Github sends org/reponame + repo := getTrailingElement(newBuild["repo"], "/") + // Github sends a path like refs/.../integration/ + ref := getTrailingElement(newBuild["ref"], "/") + sha := newBuild["sha"] + + log.Debug().Msgf("sha %s for repo %s from ref %s", sha, repo, ref) + + ecrState, err := devenv.GetECRState(a.ECR, a.Env.RegistryID, ref, a.Env.Repos) + if err != nil { + log.Warn(). + Err(err). + Msgf("could not get ecr state for %s using registry %s with repo list %v", ref, a.Env.RegistryID, a.Env.Repos) + respondWithError(w, http.StatusInternalServerError, "could got retrieve ecr state") + return + } + log.Trace().Interface("ecrState", ecrState).Msgf("for ref %s", ref) + ecrState[repo] = sha + log.Trace().Interface("ecrState", ecrState).Msgf("for ref %s after update", ref) + + err = devenv.UpsertEnv(a.DB, a.Env.TableName, ref, ecrState) + if err != nil { + log.Warn(). + Interface("ecrState", ecrState). + Err(err). + Msgf("could not add new build for %s", ref) + respondWithError(w, http.StatusInternalServerError, err.Error()) + } + respondWithJSON(w, http.StatusOK, ecrState) +} + +// ReST API for /env + // TODO Implement listing of all environments func (a *App) getEnvs(w http.ResponseWriter, r *http.Request) { return diff --git a/server/app_test.go b/server/app_test.go index 31afe8b4..548cc2f1 100644 --- a/server/app_test.go +++ b/server/app_test.go @@ -152,6 +152,56 @@ func TestNegatives(t *testing.T) { runSubTests(t, cases) } +func TestNewBuild(t *testing.T) { + // Use this formulation of sub-tests when ordering matters + // GetLoglvl below works because InfoLvl has set it + cases := []APITestCase{ + { + Name: "PlainPump", + Endpoint: "/newbuild", + HTTPStatus: http.StatusOK, + Payload: `{"repo":"tyk-pump","ref":"test","sha":"sha-pump"}`, + HTTPMethod: "POST", + }, + { + Name: "CheckPlainPump", + Endpoint: "/env/test", + HTTPStatus: http.StatusOK, + ResponseJSON: `{"name":"test","state":"new","tyk":"master","tyk-analytics":"master","tyk-pump":"sha-pump"}`, + HTTPMethod: "GET", + }, + { + Name: "GHStyleGateway", + Endpoint: "/newbuild", + HTTPStatus: http.StatusOK, + Payload: `{"repo":"TykTechnologies/tyk","ref":"refs/heads/integration/test","sha":"sha-gw"}`, + HTTPMethod: "POST", + }, + { + Name: "CheckGHStyleGateway", + Endpoint: "/env/test", + HTTPStatus: http.StatusOK, + ResponseJSON: `{"name":"test","state":"new","tyk":"sha-gw","tyk-analytics":"master","tyk-pump":"master"}`, + HTTPMethod: "GET", + }, + { + Name: "URLEncGHStyleDashboard", + Endpoint: "/newbuild", + HTTPStatus: http.StatusOK, + Payload: `{"repo":"TykTechnologies%2Ftyk-analytics","ref":"refs%2Fheads%2Fintegration%2Ftest","sha":"sha-db"}`, + HTTPMethod: "POST", + }, + { + Name: "CheckURLEncStyleDashboard", + Endpoint: "/env/test", + HTTPStatus: http.StatusOK, + ResponseJSON: `{"name":"test","state":"new","tyk":"master","tyk-analytics":"sha-db","tyk-pump":"master"}`, + HTTPMethod: "GET", + }, + } + runSubTests(t, cases) +} + func runSubTests(t *testing.T, cases []APITestCase) { for _, tc := range cases { t.Run(tc.Name, func(t *testing.T) {