From 458f5edc96c64794d6e1b4e50c5557dd98a630d2 Mon Sep 17 00:00:00 2001 From: Tomer Tcherniak Date: Thu, 21 Mar 2024 11:47:12 +1100 Subject: [PATCH] Bump black from 22.3.0 to 24.3.0 in /cdk (#15) Bumps [black](https://github.com/psf/black) from 22.3.0 to 24.3.0. - [Release notes](https://github.com/psf/black/releases) - [Changelog](https://github.com/psf/black/blob/main/CHANGES.md) - [Commits](https://github.com/psf/black/compare/22.3.0...24.3.0) --- updated-dependencies: - dependency-name: black dependency-type: direct:development ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- Dockerfile | 2 +- README.md | 36 ++++++++++++ cdk/Pipfile.lock | 94 +++++++++++++++++-------------- deploy/kubernetes/deployment.yaml | 56 ++++++++++++++++++ deploy/kubernetes/svc.yaml | 16 ++++++ src/go.mod | 10 +++- src/go.sum | 16 ++++++ src/tools/aws.go | 31 +++++++++- src/tools/proxy.go | 47 ++++++++++++++-- 9 files changed, 259 insertions(+), 49 deletions(-) create mode 100644 deploy/kubernetes/deployment.yaml create mode 100644 deploy/kubernetes/svc.yaml diff --git a/Dockerfile b/Dockerfile index dfd3d27..9cd36b2 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.18-alpine as app-builder +FROM golang:1.20-alpine as app-builder WORKDIR /go/src/app COPY src . diff --git a/README.md b/README.md index bbf2193..9685555 100644 --- a/README.md +++ b/README.md @@ -185,6 +185,42 @@ Use the following permissions to grant the proxy ReadOnly access to the CodeArti ] } ``` +## Metrics + +Supporting prometheus counters : +``` +proxy_request_counter +proxy_response_counter +aws_auth_error_counter +aws_url_error_counter +aws_config_error_counter +``` +Prometheus endpoint is 8080 on /metrics + +## Multiple path support + +It is dynamic url creation based on the request , supporting multiple paths. + +CodeArtifactAuthInfo.Url = fmt.Sprintf("%s/%s", extractBaseURL(CodeArtifactAuthInfo.Url), strings.TrimPrefix(r.URL.Path, "/")) + +## Kubernetes deployments + +svc.yaml and deployment.yaml which are found in deploy/kubernetes can be used to deploy. + +The below env var should be set +``` + - name: CODEARTIFACT_DOMAIN + value: "" + - name: CODEARTIFACT_REPO + value: "" + - name: CODEARTIFACT_OWNER + value: "" + - name: CODEARTIFACT_TYPE + value: "" + - name: AWS_REGION + value: "" +``` + ## Contributing diff --git a/cdk/Pipfile.lock b/cdk/Pipfile.lock index bd9cb80..a1fb5a6 100644 --- a/cdk/Pipfile.lock +++ b/cdk/Pipfile.lock @@ -107,40 +107,40 @@ }, "black": { "hashes": [ - "sha256:06f9d8846f2340dfac80ceb20200ea5d1b3f181dd0556b47af4e8e0b24fa0a6b", - "sha256:10dbe6e6d2988049b4655b2b739f98785a884d4d6b85bc35133a8fb9a2233176", - "sha256:2497f9c2386572e28921fa8bec7be3e51de6801f7459dffd6e62492531c47e09", - "sha256:30d78ba6bf080eeaf0b7b875d924b15cd46fec5fd044ddfbad38c8ea9171043a", - "sha256:328efc0cc70ccb23429d6be184a15ce613f676bdfc85e5fe8ea2a9354b4e9015", - "sha256:35020b8886c022ced9282b51b5a875b6d1ab0c387b31a065b84db7c33085ca79", - "sha256:5795a0375eb87bfe902e80e0c8cfaedf8af4d49694d69161e5bd3206c18618bb", - "sha256:5891ef8abc06576985de8fa88e95ab70641de6c1fca97e2a15820a9b69e51b20", - "sha256:637a4014c63fbf42a692d22b55d8ad6968a946b4a6ebc385c5505d9625b6a464", - "sha256:67c8301ec94e3bcc8906740fe071391bce40a862b7be0b86fb5382beefecd968", - "sha256:6d2fc92002d44746d3e7db7cf9313cf4452f43e9ea77a2c939defce3b10b5c82", - "sha256:6ee227b696ca60dd1c507be80a6bc849a5a6ab57ac7352aad1ffec9e8b805f21", - "sha256:863714200ada56cbc366dc9ae5291ceb936573155f8bf8e9de92aef51f3ad0f0", - "sha256:9b542ced1ec0ceeff5b37d69838106a6348e60db7b8fdd245294dc1d26136265", - "sha256:a6342964b43a99dbc72f72812bf88cad8f0217ae9acb47c0d4f141a6416d2d7b", - "sha256:ad4efa5fad66b903b4a5f96d91461d90b9507a812b3c5de657d544215bb7877a", - "sha256:bc58025940a896d7e5356952228b68f793cf5fcb342be703c3a2669a1488cb72", - "sha256:cc1e1de68c8e5444e8f94c3670bb48a2beef0e91dddfd4fcc29595ebd90bb9ce", - "sha256:cee3e11161dde1b2a33a904b850b0899e0424cc331b7295f2a9698e79f9a69a0", - "sha256:e3556168e2e5c49629f7b0f377070240bd5511e45e25a4497bb0073d9dda776a", - "sha256:e8477ec6bbfe0312c128e74644ac8a02ca06bcdb8982d4ee06f209be28cdf163", - "sha256:ee8f1f7228cce7dffc2b464f07ce769f478968bfb3dd1254a4c2eeed84928aad", - "sha256:fd57160949179ec517d32ac2ac898b5f20d68ed1a9c977346efbac9c2f1e779d" + "sha256:2818cf72dfd5d289e48f37ccfa08b460bf469e67fb7c4abb07edc2e9f16fb63f", + "sha256:41622020d7120e01d377f74249e677039d20e6344ff5851de8a10f11f513bf93", + "sha256:4acf672def7eb1725f41f38bf6bf425c8237248bb0804faa3965c036f7672d11", + "sha256:4be5bb28e090456adfc1255e03967fb67ca846a03be7aadf6249096100ee32d0", + "sha256:4f1373a7808a8f135b774039f61d59e4be7eb56b2513d3d2f02a8b9365b8a8a9", + "sha256:56f52cfbd3dabe2798d76dbdd299faa046a901041faf2cf33288bc4e6dae57b5", + "sha256:65b76c275e4c1c5ce6e9870911384bff5ca31ab63d19c76811cb1fb162678213", + "sha256:65c02e4ea2ae09d16314d30912a58ada9a5c4fdfedf9512d23326128ac08ac3d", + "sha256:6905238a754ceb7788a73f02b45637d820b2f5478b20fec82ea865e4f5d4d9f7", + "sha256:79dcf34b33e38ed1b17434693763301d7ccbd1c5860674a8f871bd15139e7837", + "sha256:7bb041dca0d784697af4646d3b62ba4a6b028276ae878e53f6b4f74ddd6db99f", + "sha256:7d5e026f8da0322b5662fa7a8e752b3fa2dac1c1cbc213c3d7ff9bdd0ab12395", + "sha256:9f50ea1132e2189d8dff0115ab75b65590a3e97de1e143795adb4ce317934995", + "sha256:a0c9c4a0771afc6919578cec71ce82a3e31e054904e7197deacbc9382671c41f", + "sha256:aadf7a02d947936ee418777e0247ea114f78aff0d0959461057cae8a04f20597", + "sha256:b5991d523eee14756f3c8d5df5231550ae8993e2286b8014e2fdea7156ed0959", + "sha256:bf21b7b230718a5f08bd32d5e4f1db7fc8788345c8aea1d155fc17852b3410f5", + "sha256:c45f8dff244b3c431b36e3224b6be4a127c6aca780853574c00faf99258041eb", + "sha256:c7ed6668cbbfcd231fa0dc1b137d3e40c04c7f786e626b405c62bcd5db5857e4", + "sha256:d7de8d330763c66663661a1ffd432274a2f92f07feeddd89ffd085b5744f85e7", + "sha256:e19cb1c6365fd6dc38a6eae2dcb691d7d83935c10215aef8e6c38edee3f77abd", + "sha256:e2af80566f43c85f5797365077fb64a393861a3730bd110971ab7a0c94e873e7" ], "index": "pypi", - "version": "==22.3.0" + "markers": "python_version >= '3.8'", + "version": "==24.3.0" }, "click": { "hashes": [ - "sha256:24e1a4a9ec5bf6299411369b208c1df2188d9eb8d916302fe6bf03faed227f1e", - "sha256:479707fe14d9ec9a0757618b7a100a0ae4c4e236fac5b7f80ca68028141a1a72" + "sha256:ae74fb96c20a0277a1d615f1e4d73c8414f5a98db8b799a7931d1582f3390c28", + "sha256:ca9853ad459e787e2192211578cc907e7594e294c7ccc834310722b41b9ca6de" ], "markers": "python_version >= '3.7'", - "version": "==8.1.2" + "version": "==8.1.7" }, "iniconfig": { "hashes": [ @@ -151,33 +151,35 @@ }, "mypy-extensions": { "hashes": [ - "sha256:090fedd75945a69ae91ce1303b5824f428daf5a028d2f6ab8a299250a846f15d", - "sha256:2d82818f5bb3e369420cb3c4060a7970edba416647068eb4c5343488a6c604a8" + "sha256:4392f6c0eb8a5668a69e23d168ffa70f0be9ccfd32b5cc2d26a34ae5b844552d", + "sha256:75dbf8955dc00442a438fc4d0666508a9a97b6bd41aa2f0ffe9d2f2725af0782" ], - "version": "==0.4.3" + "markers": "python_version >= '3.5'", + "version": "==1.0.0" }, "packaging": { "hashes": [ - "sha256:dd47c42927d89ab911e606518907cc2d3a1f38bbd026385970643f9c5b8ecfeb", - "sha256:ef103e05f519cdc783ae24ea4e2e0f508a9c99b2d4969652eed6a2e1ea5bd522" + "sha256:2ddfb553fdf02fb784c234c7ba6ccc288296ceabec964ad2eae3777778130bc5", + "sha256:eb82c5e3e56209074766e6885bb04b8c38a0c015d0a30036ebe7ece34c9989e9" ], - "markers": "python_version >= '3.6'", - "version": "==21.3" + "markers": "python_version >= '3.7'", + "version": "==24.0" }, "pathspec": { "hashes": [ - "sha256:7d15c4ddb0b5c802d161efc417ec1a2558ea2653c2e8ad9c19098201dc1c993a", - "sha256:e564499435a2673d586f6b2130bb5b95f04a3ba06f81b8f895b651a3c76aabb1" + "sha256:a0d503e138a4c123b27490a4f7beda6a01c6f288df0e4a8b79c7eb0dc7b4cc08", + "sha256:a482d51503a1ab33b1c67a6c3813a26953dbdc71c31dacaef9a838c4e29f5712" ], - "version": "==0.9.0" + "markers": "python_version >= '3.8'", + "version": "==0.12.1" }, "platformdirs": { "hashes": [ - "sha256:7535e70dfa32e84d4b34996ea99c5e432fa29a708d0f4e394bbcb2a8faa4f16d", - "sha256:bcae7cab893c2d310a711b70b24efb93334febe65f8de776ee320b517471e227" + "sha256:0614df2a2f37e1a662acbd8e2b25b92ccf8632929bc6d43467e17fe89c75e068", + "sha256:ef0cc731df711022c174543cb70a9b5bd22e5a9337c8624ef2c2ceb8ddad8768" ], - "markers": "python_version >= '3.7'", - "version": "==2.5.1" + "markers": "python_version >= '3.8'", + "version": "==4.2.0" }, "pluggy": { "hashes": [ @@ -216,8 +218,16 @@ "sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc", "sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f" ], - "markers": "python_version >= '3.7'", + "markers": "python_version < '3.11'", "version": "==2.0.1" + }, + "typing-extensions": { + "hashes": [ + "sha256:69b1a937c3a517342112fb4c6df7e72fc39a38e7891a5730ed4985b5214b5475", + "sha256:b0abd7c89e8fb96f98db18d86106ff1d90ab692004eb746cf6eda2682f91b3cb" + ], + "markers": "python_version < '3.11'", + "version": "==4.10.0" } } } diff --git a/deploy/kubernetes/deployment.yaml b/deploy/kubernetes/deployment.yaml new file mode 100644 index 0000000..e442dc7 --- /dev/null +++ b/deploy/kubernetes/deployment.yaml @@ -0,0 +1,56 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app: artifactory-proxy + name: artifactory-proxy + namespace: default +spec: + replicas: 1 + selector: + matchLabels: + app: artifactory-proxy + strategy: + rollingUpdate: + maxSurge: 1 + maxUnavailable: 0 + type: RollingUpdate + template: + metadata: + labels: + app: artifactory-proxy + spec: + containers: + - env: + - name: CODEARTIFACT_DOMAIN + value: "" + - name: CODEARTIFACT_REPO + value: "" + - name: CODEARTIFACT_OWNER + value: "" + - name: CODEARTIFACT_TYPE + value: "" + - name: AWS_REGION + value: "" + image: image + imagePullPolicy: IfNotPresent + livenessProbe: + httpGet: + path: /metrics + port: 8080 + initialDelaySeconds: 5 + name: artifactory-proxy + ports: + - containerPort: 8080 + protocol: TCP + readinessProbe: + httpGet: + path: /metrics + port: 8080 + resources: + limits: + memory: 20Mi + requests: + cpu: 5m + memory: 10Mi + terminationGracePeriodSeconds: 30 diff --git a/deploy/kubernetes/svc.yaml b/deploy/kubernetes/svc.yaml new file mode 100644 index 0000000..932a59d --- /dev/null +++ b/deploy/kubernetes/svc.yaml @@ -0,0 +1,16 @@ +piVersion: v1 +kind: Service +metadata: + labels: + app: artifactory-proxy + name: artifactory-proxy + namespace: default +spec: + ports: + - name: http + port: 8080 + protocol: TCP + targetPort: 8080 + selector: + app: artifactory-proxy + type: ClusterIP \ No newline at end of file diff --git a/src/go.mod b/src/go.mod index deed35a..fda8c5d 100644 --- a/src/go.mod +++ b/src/go.mod @@ -1,6 +1,6 @@ module github.com/sktan/aws-codeartifact-proxy -go 1.18 +go 1.20 require ( github.com/aws/aws-sdk-go-v2 v1.16.2 @@ -18,4 +18,12 @@ require ( github.com/aws/aws-sdk-go-v2/service/sso v1.11.3 // indirect github.com/aws/aws-sdk-go-v2/service/sts v1.16.3 // indirect github.com/aws/smithy-go v1.11.2 // indirect + github.com/beorn7/perks v1.0.1 // indirect + github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/prometheus/client_golang v1.19.0 // indirect + github.com/prometheus/client_model v0.5.0 // indirect + github.com/prometheus/common v0.48.0 // indirect + github.com/prometheus/procfs v0.12.0 // indirect + golang.org/x/sys v0.16.0 // indirect + google.golang.org/protobuf v1.32.0 // indirect ) diff --git a/src/go.sum b/src/go.sum index e40df49..01cae16 100644 --- a/src/go.sum +++ b/src/go.sum @@ -22,14 +22,30 @@ github.com/aws/aws-sdk-go-v2/service/sts v1.16.3 h1:cJGRyzCSVwZC7zZZ1xbx9m32UnrK github.com/aws/aws-sdk-go-v2/service/sts v1.16.3/go.mod h1:bfBj0iVmsUyUg4weDB4NxktD9rDGeKSVWnjTnwbx9b8= github.com/aws/smithy-go v1.11.2 h1:eG/N+CcUMAvsdffgMvjMKwfyDzIkjM6pfxMJ8Mzc6mE= github.com/aws/smithy-go v1.11.2/go.mod h1:3xHYmszWVx2c0kIwQeEVf9uSm4fYZt67FBJnwub1bgM= +github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= +github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= +github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= +github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_golang v1.19.0 h1:ygXvpU1AoN1MhdzckN+PyD9QJOSD4x7kmXYlnfbA6JU= +github.com/prometheus/client_golang v1.19.0/go.mod h1:ZRM9uEAypZakd+q/x7+gmsvXdURP+DABIEIjnmDdp+k= +github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw= +github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI= +github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSzKKE= +github.com/prometheus/common v0.48.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc= +github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo= +github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= +golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I= +google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/src/tools/aws.go b/src/tools/aws.go index 7e3acb8..1819f7a 100644 --- a/src/tools/aws.go +++ b/src/tools/aws.go @@ -10,8 +10,34 @@ import ( "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/service/codeartifact" "github.com/aws/aws-sdk-go-v2/service/codeartifact/types" + "github.com/prometheus/client_golang/prometheus" ) +var awsAuthErrCounter = prometheus.NewCounter( + prometheus.CounterOpts{ + Name: "aws_auth_error_counter", + }, +) + +var awsUrlErrCounter = prometheus.NewCounter( + prometheus.CounterOpts{ + Name: "aws_url_error_counter", + }, +) + +var awsConfigErrCounter = prometheus.NewCounter( + prometheus.CounterOpts{ + Name: "aws_config_error_counter", + }, +) + +func init() { + // Register Prometheus metrics collectors + prometheus.MustRegister(awsAuthErrCounter) + prometheus.MustRegister(awsUrlErrCounter) + prometheus.MustRegister(awsConfigErrCounter) +} + type CodeArtifactAuthInfoStruct struct { Url string AuthorizationToken string @@ -27,6 +53,7 @@ func Authenticate() { // Authenticate against CodeArtifact cfg, cfgErr := config.LoadDefaultConfig(context.TODO()) if cfgErr != nil { + awsConfigErrCounter.Inc() log.Fatalf("unable to load SDK config, %v", cfgErr) } svc := codeartifact.NewFromConfig(cfg) @@ -61,6 +88,7 @@ func Authenticate() { } authResp, authErr := svc.GetAuthorizationToken(context.TODO(), authInput) if authErr != nil { + awsAuthErrCounter.Inc() log.Fatalf("unable to get authorization token, %v", authErr) } log.Printf("Authorization successful") @@ -81,7 +109,8 @@ func Authenticate() { urlResp, urlErr := svc.GetRepositoryEndpoint(context.TODO(), urlInput) if urlErr != nil { - log.Fatalf("unable to get repository endpoint, %v", urlErr) + awsUrlErrCounter.Inc() + log.Fatalf("unable to get repository endpoint, %v", urlErr) } CodeArtifactAuthInfo.Url = *urlResp.RepositoryEndpoint mutex.Unlock() diff --git a/src/tools/proxy.go b/src/tools/proxy.go index 734bda3..6b35356 100644 --- a/src/tools/proxy.go +++ b/src/tools/proxy.go @@ -13,15 +13,35 @@ import ( "strconv" "strings" "sync" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" +) + +var proxyRequestCounter = prometheus.NewCounter( + prometheus.CounterOpts{ + Name: "proxy_request_counter", + }, +) +var proxyResponseCounter = prometheus.NewCounter( + prometheus.CounterOpts{ + Name: "proxy_response_counter", + }, ) var originalUrlResolver = make(map[string]*url.URL) var mutex = &sync.Mutex{} +func init() { + // Register Prometheus metrics collectors + prometheus.MustRegister(proxyRequestCounter) + prometheus.MustRegister(proxyResponseCounter) +} + // ProxyRequestHandler intercepts requests to CodeArtifact and add the Authorization header + correct Host header func ProxyRequestHandler(p *httputil.ReverseProxy) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { - mutex.Lock() + mutex.Lock() // Store the original host header for each request originalUrlResolver[r.RemoteAddr] = r.URL originalUrlResolver[r.RemoteAddr].Host = r.Host @@ -32,6 +52,10 @@ func ProxyRequestHandler(p *httputil.ReverseProxy) func(http.ResponseWriter, *ht } else { originalUrlResolver[r.RemoteAddr].Scheme = "http" } + // Set URL with an updated path based on the request + log.Printf("baseURL %s", CodeArtifactAuthInfo.Url) + CodeArtifactAuthInfo.Url = fmt.Sprintf("%s/%s", extractBaseURL(CodeArtifactAuthInfo.Url), strings.TrimPrefix(r.URL.Path, "/")) + log.Printf("newURL %s", CodeArtifactAuthInfo.Url) // Override the Host header with the CodeArtifact Host u, _ := url.Parse(CodeArtifactAuthInfo.Url) @@ -44,14 +68,26 @@ func ProxyRequestHandler(p *httputil.ReverseProxy) func(http.ResponseWriter, *ht log.Printf("Sending request to %s%s", strings.Trim(CodeArtifactAuthInfo.Url, "/"), r.URL.RequestURI()) mutex.Unlock() - + proxyRequestCounter.Inc() + remote, err := url.Parse(extractBaseURL(CodeArtifactAuthInfo.Url)) + if err != nil { + panic(err) + } + p := httputil.NewSingleHostReverseProxy(remote) + p.ModifyResponse = ProxyResponseHandler() p.ServeHTTP(w, r) } } +// Function to extract the base URL +func extractBaseURL(fullURL string) string { + u, _ := url.Parse(fullURL) + return fmt.Sprintf("%s://%s", u.Scheme, u.Host) +} + // Handles the response back to the client once intercepted from CodeArtifact func ProxyResponseHandler() func(*http.Response) error { - return func(r *http.Response) error { + return func(r *http.Response) error { log.Printf("Received %d response from %s", r.StatusCode, r.Request.URL.String()) log.Printf("RES: %s \"%s\" %d \"%s\" \"%s\"", r.Request.RemoteAddr, r.Request.Method, r.StatusCode, r.Request.RequestURI, r.Request.UserAgent()) @@ -116,6 +152,7 @@ func ProxyResponseHandler() func(*http.Response) error { r.Body = ioutil.NopCloser(strings.NewReader(newResponseContent)) r.ContentLength = int64(len(newResponseContent)) r.Header.Set("Content-Length", strconv.Itoa(len(newResponseContent))) + proxyResponseCounter.Inc() } return nil @@ -123,6 +160,8 @@ func ProxyResponseHandler() func(*http.Response) error { } + + // ProxyInit initialises the CodeArtifact proxy and starts the HTTP listener func ProxyInit() { remote, err := url.Parse(CodeArtifactAuthInfo.Url) @@ -136,7 +175,7 @@ func ProxyInit() { proxy := httputil.NewSingleHostReverseProxy(remote) proxy.ModifyResponse = ProxyResponseHandler() - + http.Handle("/metrics", promhttp.Handler()) http.HandleFunc("/", ProxyRequestHandler(proxy)) err = http.ListenAndServe(":"+port, nil) if err != nil {