Commit Graph

82 Commits

Author SHA1 Message Date
Stavros Kontopoulos b8c14ce9f9
[CVE-2023-44487] Disable http2 for webhooks (#2871)
* disable http2 for webhooks

* add comment
2023-10-23 14:38:02 +00:00
Stavros Kontopoulos 8d3f951063
Allow overriding webhook secret data keys (#2662)
* override secret data keys when creating new webhooks

* fixes

* cleanup

* use webhook options

* remove unused env var keys

* fix docs

* update opt names and refactor integration test

* fixes
2023-08-02 19:19:48 +00:00
Hector Fernandez 15605c78a2
Defaulting Controller options for all kind of webhooks (#2738)
* functional options

* move options to its own package

Signed-off-by: Hector Fernandez <hector@chainguard.dev>

* add controller options to the webhook options

Signed-off-by: Hector Fernandez <hector@chainguard.dev>

* create custom options funcs for each webhook type

Signed-off-by: Hector Fernandez <hector@chainguard.dev>

* address comments from reviewers

Signed-off-by: Hector Fernandez <hector@chainguard.dev>

---------

Signed-off-by: Hector Fernandez <hector@chainguard.dev>
Co-authored-by: dprotaso <dprotaso@gmail.com>
2023-06-12 08:38:02 +00:00
Dave Protasowski 9bda38b216
Fix some webhook testing tech debt (#2751)
* TestRegistrationStopChanFire now uses ephemeral ports

* For TLS servers dial TLS

* have server error logs appear in zap

* log the correct error

* pass ephemeral listeners to the webhook for testing
2023-05-25 14:35:25 +00:00
Izabela Gomes 53f04b373c
Make minimum TLS version configurable for webhooks (#2721)
* make minimum tls version configurable

* change default min TLS version to 1.3

* change opencensus tls min version to 1.3

* Update env var name

Co-authored-by: Dave Protasowski <dprotaso@gmail.com>

* use webhook options to configure min tls version

* add unit tests for webhook tlsMinVersion option

* Update webhook/env.go

Co-authored-by: Dave Protasowski <dprotaso@gmail.com>

* address feeback

---------

Co-authored-by: Dave Protasowski <dprotaso@gmail.com>
2023-04-14 15:45:51 +00:00
kmahapatra 2f55fe00c5
Fix potential slowloris attack (#2682) 2023-02-15 23:47:51 +00:00
Dave Protasowski 44d1d7d978
Go1.19 changes (#2631)
* run goimports

* ignore linter errors for now

* fix boilerplate
2022-11-03 22:34:05 +00:00
Todd 9b5c41135d
allow overriding the default grace period of 45 seconds (#2423)
This allows users to configure a faster restart of their
webhook if desired while retaining the current behavior.
2022-02-28 11:40:09 -08:00
Matt Moore 552bbc1061
Support the webhook serving over non-TLS. (#2204)
This reworks the way we setup the TLS Config for the webhook, so that when the `opts.SecretName` is omitted we serve over non-TLS.
2021-08-02 20:22:47 -07:00
Matt Moore c367a9d696
Drop Client as it is only used in tests (#2203) 2021-07-30 18:05:05 -07:00
Markus Thömmes 808d62257d
Escape path before returning it in a response (#2075) 2021-03-30 09:22:21 -07:00
Dave Protasowski 6040b3af48
improve client error message when our webhook doesn't have a valid cert (#2000) 2021-01-25 14:20:30 -08:00
Markus Thömmes 565516e224
Add errorlint and fix all existing issues (#1855) 2020-10-29 01:14:35 -07:00
Josh Soref b39d5da935
Spelling (#1797)
* spelling: adopted

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: aliased

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: apierrs

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: assignment

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: available

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: coexistence

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: commit

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: conversions

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: creates

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: custom

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: determine

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: different

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: distribution

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: duplicate

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: editing

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: endpoint

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: environment

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: generate

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: implementation

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: identified

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: ignore

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: indicates

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: interface

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: interleaved

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: labels

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: label

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: mimic

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: namespaced

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: necessary

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: organization

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: populatable

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: prometheus

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: refer

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: reference

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: repetitive

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: response

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: something

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: specable

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: spoofing

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: synchronized

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: this

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: trailing

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: unsupported

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: validation

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* chore: reviewdog go header boilerplate

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>
2020-10-18 14:22:57 -07:00
Victor Agababov 247841408e
Apply the cleanup to the pkg (#1776)
* Apply the cleanup to the pkg

This is probably the final iteration of my cleanup application.

* codegen

* one more try
2020-10-05 21:41:20 -07:00
Victor Agababov 1e373a9e5d
take 2 (#1755) 2020-09-29 14:10:29 -07:00
Markus Thömmes e4ecf702a3
Apply gosec checks to the codebase, except tests. (#1725)
* Apply gosec checks to the codebase, except tests.

* Fix YAML whitespace.

* Tune back file mode change and add a comment.
2020-09-22 08:55:40 -07:00
Matt Moore e193c4be24
Implement a new shared "Drainer" handler. (#1517)
* Implement a new shared "Drainer" handler.

This implements a new `http.Handler` called `Drainer`, which is intended to wrap some inner `http.Handler` business logic with a new outer handler that can respond to Kubelet probes (successfully until told to "Drain()").

This takes over the webhook's relatively new probe handling and lame duck logic with one key difference.  Previously the webhook waited for a fixed period after SIGTERM before exitting, but the new logic waits for this same grace period AFTER THE LAST REQUEST.  So if the handler keeps getting (non-probe) requests, the timer will continually reset, and once it stops receiving requests for the configured grace period, "Drain()" will return and the webhook will exit.

The goal of this work is to try to better cope with what we believe to be high tail latencies of the API server seeing that a webhook replica is shutting down.

Related: https://github.com/knative/pkg/issues/1509

* Switch to RWLock
2020-07-17 21:25:34 -07:00
Matt Moore 0f78f8a8cc
Disable keep-alives on shutdown. (#1511)
See also: https://github.com/knative/pkg/issues/1509#issuecomment-659737054
2020-07-16 16:55:33 -07:00
Dave Protasowski caa444033b
use crd & webhook v1 APIs (#1391) 2020-06-19 08:43:25 -07:00
Victor Agababov 389d28f9b6
Use the generic networking constant for shutdown (#1365)
- use standard const, which is better
- stop modifying the default in the test, which is ugh a bit :)
2020-05-29 09:47:02 -07:00
Dave Protasowski b0e3201ad2
Kubelet probes would result in the webhook writing the HTTP status twice (#1355)
* Kubelet probes would result in the webhook writing the HTTP status twice

Doesn't seem like it affected anything - just writes out some extra
log messages

* nits

* nits

* nits

* nits
2020-05-26 10:34:50 -07:00
Matt Moore b52862b1b3
Implement the K8s lifecycle in webhook. (#1318)
* Implement the K8s lifecycle in webhook.

The webhook never properly implemented the Kubernetes SIGTERM/SIGKILL
lifecycle, and doesn't even really support readiness probes today.  This
change enables folks to use a block like this on their webhook container:

```yaml
        readinessProbe: &probe
          periodSeconds: 1
          httpGet:
            scheme: HTTPS
            port: 8443
            httpHeaders:
            - name: k-kubelet-probe
              value: "webhook"
        livenessProbe: *probe
```

With this, the webhook won't report as `Ready` until a probe has succeeded,
and when the SIGTERM is received, we will start failing probes for a grace
period (so our Endpoint drops) before shutting down the webhook's HTTP Server.

This was uncovered by running the webhook across 10 replicas in Serving with
the "Goose" (https://github.com/knative/pkg/pull/1316) enabled for the e2e
tests.  The failure mode I saw was conversion webhook requests failing across
random tests.

This also moves the Serving probe-detection function into PKG.

* Increase the log level when we start to fail probes

* Wait for go routines to terminate on all paths.
2020-05-09 16:44:45 -07:00
Victor Agababov 5ba6c1d101
Change %v to %w in errors and other nits (#1252)
* Change %v to %w in errors and other nits
Other are things I noticed when fixing the main %v->%w conversion

* fold
2020-04-27 15:04:51 -07:00
whaught d22642244e
Allow Unstructured callback from Validating Webhook (#1172)
* Add new callback pattern to pkg

* include the context

* typo

* Remove the empty instance of unstructured

* initialize the unstructured var

* Eliminate the unneeded pointer

* Pass a pointer to unstructured callback

* Create a validation specific context struct

* Move callback tests to own unit test case

* Switch from converting to decoding

* Update webhook/resourcesemantics/validation/validation.go

Co-Authored-By: Victor Agababov <vagababov@gmail.com>

* don't wrap context and include params

* split validation files

* include 2020 copyright

* include unit test for WithKubeClient

* Don't bother updating copyright date

* Inclue a unit test for panic

* Move dryRun to context

* Include context dry run unit test

* put the request operation in the context

* eliminate circular dep

* move kubeclient test out of context_test

* dont bother iterating callback map

* Callback takes a list of supported verbs

* Remove extra type

* Ensure Callback interface is public

* Alias Operation into validation

* alias Operation right in Webhook

* Update webhook/resourcesemantics/validation/validation_admit.go

Co-Authored-By: Victor Agababov <vagababov@gmail.com>

* Update webhook/resourcesemantics/validation/validation_admit_test.go

Co-Authored-By: Victor Agababov <vagababov@gmail.com>

* Update webhook/resourcesemantics/validation/validation_admit_test.go

Co-Authored-By: Victor Agababov <vagababov@gmail.com>

* Update webhook/resourcesemantics/validation/validation_admit.go

Co-Authored-By: Victor Agababov <vagababov@gmail.com>

* Update webhook/resourcesemantics/validation/validation_admit.go

Co-Authored-By: Victor Agababov <vagababov@gmail.com>

* Update webhook/resourcesemantics/validation/validation_admit_test.go

Co-Authored-By: Victor Agababov <vagababov@gmail.com>

* correct parens

* minor style fixes

* Rename Callback to Func

* Fix build error

* Switch callback to take a list with a factory

* keep descriptive names

* update comment

* Drop pointer, correct comments

* Add a unit test to disallow duplicate verbs

* fix comments, struct{} for set

* switch to variadic arg for NewCallback

Co-authored-by: Victor Agababov <vagababov@gmail.com>
2020-04-06 12:22:19 -07:00
Matt Moore e2ee5bed78
Start the webhook before informers sync. (#1180)
* Start the webhook before informers sync.

Some webhooks (e.g. conversion) are required to list resources, so by delaying those until after informers have synced, we create a deadlock when they run in the same process.  This change has two key parts:
1. Start the webhook immediately when our process starts, and issue a callback from sharedmain when the informers have synced.
2. Block `Admit` calls until informers have synced (all conversions are exempt), unless they have been designated by implementing `webhook.StatelessAdmissionController`.

Our built-in admission controllers (defaulting, validation, configmap validation) have all been marked as stateless, the main case where we want to block `Admit` calls is when we require the informer to have synchronized to populate indices for Bindings.

* Add missing err declaration
2020-03-30 18:14:50 -07:00
Matt Moore 1893541a0f
Scope the webhook secret informers to the system namespace. (#1158) 2020-03-12 18:29:29 -07:00
Dave Protasowski 88d4536182
Conversion Webhook Framework (#993)
* ConversionController implementation

This controller will reconcile target CRDs with the correct
conversion webhook configuration. Specifically, the HTTP path and
CA bundle will be updated.

Additionally, the conversion controller will perform the given
conversions through a hub and spoke model utilizing the
apis.Convertible interface.

* Webhook now can host ConversionControllers

* injection/sharedmain now supports webhook.ConversionControllers

These conversion controllers will be hosted by the webhook that
the sharedmain will start

* support defaulting & include godoc
2020-02-03 08:59:29 -08:00
Dave Protasowski cff115c2dd Refactor webhook to allow adding conversion support (#989)
* Refactor webhook to allow adding conversion support

* pr feedback

* fix memory leak

* We can use mux.Handle

* move admission integration tests to separate file
2020-01-20 09:20:05 -08:00
Slavomir Kaslev f890690397 De-race webhook integration tests (#938)
Webhook integration tests occasionally fail with on testgrid, e.g.
  https://prow.knative.dev/view/gcs/knative-prow/logs/ci-knative-pkg-continuous/1204193472353931264
  https://prow.knative.dev/view/gcs/knative-prow/logs/ci-knative-pkg-continuous/1203166664988823552

Fix this by waiting for all goroutines to finish before each test ends.
2019-12-13 06:59:54 -08:00
Matt Moore a55e24e80c Remove temporary logic to allow shared paths in webhook. (#943)
* Remove temporary logic to allow shared paths in webhook.

* Update webhook/webhook.go

Co-Authored-By: Victor Agababov <vagababov@gmail.com>
2019-12-12 11:14:52 -08:00
Matt Moore 4836f680bb Split the resource semantic webhooks into separate AdmissionControllers (#848)
By combining our validation logic into our mutating webhook we were previously allowing for mutating webhooks evaluated after our own to modify our resources into invalid shapes.  There are no guarantees around ordering of mutating webhooks (that I could find), so the only way to remedy this properly is to split apart the two into separate webhook configurations:
 - `defaulting`: which runs during the mutating admission webhook phase
 - `validation`: which runs during the validating admission webhook phase.

The diagram in [this post](https://kubernetes.io/blog/2019/03/21/a-guide-to-kubernetes-admission-controllers/) is very helpful in illustrating the flow of webhooks.

Fixes: https://github.com/knative/pkg/issues/847
2019-11-14 16:51:02 -08:00
Markus Thömmes 56c2594e4f Assorted linting fixes. (#840)
* Remove unused code.

* Remove unneeded loops.

* Remove unneeded Printf calls.

* Use time.Since instead of time.Now().Sub.

* Remove unused values.

* Rename error variable according to conventions.

* Return error last.

* Simplify array allocations.

* Remove leaky ticker.

* Remove Yoda conditions.

* Remove deprecated function to talk to GKE.

* Remove dot import.

* Remove empty critical section and replace with a channel operation.

* Add linter directives to explicitly state wanted weirdness.

* Update deps.

* Fix broken line.
2019-11-01 12:49:12 -07:00
Matt Moore d4ce001394 This refactors our core webhook logic to be reconciler-based. (#833)
This is the culmination of a large number of changes to refactor our webhook logic, and adopt a reconciler-based approach to make it resilient to unexected system events (e.g. rogue GCs!).

For more details on how this is consumed, see the new `webhook/README.md`.

Fixes: https://github.com/knative/pkg/issues/782
Closes: https://github.com/knative/pkg/issues/529
Fixes: https://github.com/knative/pkg/issues/450
Related: https://github.com/knative/pkg/issues/141
2019-10-31 10:17:13 -07:00
Matt Moore 3732de5802 Split secret creation into our reconciler resources style. (#830) 2019-10-29 23:08:11 -07:00
Matt Moore 7772e2f583 Drive GetCertificate from the lister cache of a secret informer. (#825) 2019-10-29 07:11:12 -07:00
Matt Moore bfff3b7d72 Switch to using GetCertificate in tls.Config. (#823)
GetCertificate allows us to start in TLS mode and dynamically fetch new certificates as they change.  This will eventually allow us to decouple the cert creation process from the core webhook logic, and in a subsequent change service this from a secret lister cache.
2019-10-28 17:32:11 -07:00
Matt Moore da49e89aa8 Remove options to specify ClientAuth. (#822)
We don't use this anywhere in Knative downstream and it adds a bunch of complexity.
2019-10-28 16:50:11 -07:00
Matt Moore 763c642d3c Streamline `webhook.New`. (#821)
This now pulls most of its configuration from context, which brings it a step closer to aligning with our `sharedmain` package.
2019-10-28 16:12:11 -07:00
Matt Moore 3f2100ca91 Move WithContext into the resource admission controller. (#820) 2019-10-28 15:05:11 -07:00
Matt Moore aaf36e26c7 Accept a list of AdmissionControllers instead of a map (#819) 2019-10-28 14:09:11 -07:00
Matt Moore de53b8f09f Move hook-specific configuration options out of shared options. (#818)
This builds on https://github.com/knative/pkg/pull/817 and makes further
breaking changes. The options pertinent to each admission controller are
now passed to their respective constructors, which leads to a cleaner
options struct, and better prepares for greater webhook diversity.
2019-10-28 13:31:11 -07:00
Matt Moore 070396a075 Eliminate the Namespace config option in favor of system.Namespace. (#817) 2019-10-28 11:41:43 -07:00
Matt Moore cad41c40cc Stop using OwnerRefs for webhook config lifecycle (#802)
* Stop using OwnerRefs for webhook config lifecycle

This changes the model by which we manage the lifecycle of our
`{mutating,validating}webhookconfiguration`, which previously used an owner ref
from the cluster-scoped configuration to the namespace-scoped Deployment.  The
new model adds an explicit yaml file for the webhook, which omits the fields
filled in by the deployment as it starts.

A few notable elements of this change:
1. Clear out OwnerReferences explicitly (avoids the linked bug),
2. Periodically reruna `Register()` to ensure our webhook exists,
3. Simplified logic around registration (all we need now is update!).

Related: https://github.com/knative/serving/issues/5845

* Incorporate feedback from @dgerd and a few other nits I noticed.
2019-10-25 11:53:35 -07:00
Nima Kaviani d90ec6a015 add config validation as admission controller (#636) 2019-09-22 07:23:10 -07:00
Nima Kaviani c39ee25c42 rename the variable for resource validation webhook (#653) 2019-09-11 06:40:29 -07:00
mattmoor-sockpuppet 014d54e62f golang format tools (#637)
Produced via:
  `gofmt -s -w $(find -path './vendor' -prune -o -type f -name '*.go' -print))`
  `goimports -w $(find -name '*.go' | grep -v vendor)`
/assign mattmoor
2019-09-06 07:26:05 -07:00
Nima Kaviani c270532140 introduce an interface for AdmissionControllers (#622) 2019-09-04 09:03:02 -07:00
Nima Kaviani 9118872a32 Refactor admission webhook (#595)
- decouple the webhool server from the controller
- move tests to respective files
2019-08-27 20:39:40 -07:00
Victor Agababov 74c5d67ea0 Fix the issues pointed out by staticcheck (#541)
* Fix the issues pointed out by staticcheck

* review fix
2019-07-23 13:13:36 -07:00