|
| 1 | +# Managed Cluster Validating Webhooks |
| 2 | + |
| 3 | +A Flask app designed to act as a webhook admission controller for OpenShift. |
| 4 | + |
| 5 | +Presently there is a single webhook, [group-validation](#group_validation), which is provided via `/group-validation` endpoint. |
| 6 | + |
| 7 | +## Group Validation |
| 8 | + |
| 9 | +Configuration for this webhook is provided by environment variables: |
| 10 | + |
| 11 | +* `GROUP_VALIDATION_PREFIX` - Group prefix to apply the webhook, such as `osd-` to apply to `CREATE`, `UPDATE`, `DELETE` operations on groups starting with `osd-`. |
| 12 | +* `GROUP_VALIDATION_ADMIN_GROUP` - Admin group, which the requestor must be a member in order to have access granted. |
| 13 | +* `DEBUG_GROUP_VALIDATION` - Debug the webhook? Set to `True` to enable, all other values (including absent) disable. |
| 14 | + |
| 15 | +## How it works |
| 16 | + |
| 17 | +In order for a validating webhook to talk to the code which is performing the validation (eg, the code in this repository), which is running in-cluster, Kubernetes needs to talk to it via a `Service` over HTTPS. This forces the Python Flask app to serve itself with a TLS certificate and the corresponding webhook configuration to specify the CA Bundle (`caBundle`) that matches up for those TLS certs. |
| 18 | + |
| 19 | +The TLS cert is provisioned by using the [openshift-ca-operator](https://github.com/openshift/service-ca-operator). Refer to its documentation for how TLS keys are requested and stored. See also: [02-webhook-cacert.configmap.yaml.tmpl](/templates/02-webhook-cacert.configmap.yaml.tmpl) and [05-group-validation-webhook.service.yaml.tmpl](/templates/05-group-validation-webhook.service.yaml.tmpl). |
| 20 | + |
| 21 | +Getting the TLS certificates is only part of the battle, as the operator does not inject them into the `ValidatingWebhookConfiguration`. To accomplish that, a small Python script has been written that is used as an `initContainer` in the Deployment of the webhook framework. The "injector" script, when run, will find all `ValidatingWebhookConfiguration` objects with an `managed.openshift.io/inject-cabundle-from` annotation. The annotation's value is in the format `namespace/configmap` from whence the CA Bundle can be found (as the key `service-ca.crt`). Thus an annotation `managed.openshift.io/inject-cabundle-from: openshift-validation-webhook/webhook-cert` will have the "injector" script look in the `openshift-validation-webhook` `Namespace` for the `webhook-cert` `ConfigMap` to contain a `service-ca.crt` key and therein, a PEM encoded certificate. The certificate is base64-encoded and set as the `caBundle` for each webhook defined in the `ValidatingWebhookConfiguration`. |
| 22 | + |
| 23 | +## Development |
| 24 | + |
| 25 | +### Adding New Webhooks |
| 26 | + |
| 27 | +In order to add new webhooks, create a new Python file in [src/webhook](src/webhook), following the pattern from [src/webhook/group_validation.py](src/webhook/group_validation.py). Add an entry to [src/webhook/__init__.py](src/webhook/__init__.py) in the pattern of the group validation webhook. |
| 28 | + |
| 29 | +#### Register with the Flask application |
| 30 | + |
| 31 | +To register your webhook with the Flask app: |
| 32 | + |
| 33 | +```python |
| 34 | +# src/webhook/__init__.py |
| 35 | +from flask import Flask |
| 36 | +from flask import request |
| 37 | + |
| 38 | +app = Flask(__name__,instance_relative_config=True) |
| 39 | + |
| 40 | +from webhook import group_validation |
| 41 | +app.register_blueprint(group_validation.bp) |
| 42 | + |
| 43 | +from webhook import your_hook |
| 44 | +app.register_blueprint(your_hook.bp) |
| 45 | +``` |
| 46 | + |
| 47 | +#### Adding YAML Manifests |
| 48 | + |
| 49 | +To add a new YAML Manifest: |
| 50 | + |
| 51 | +Create a new file in [templates](/templates) directory with a `10-` prefix, ex `10-your-hook.ValidatingWebhookConfiguration.yaml.tmpl` with contents: |
| 52 | + |
| 53 | +```yaml |
| 54 | +apiVersion: admissionregistration.k8s.io/v1beta1 |
| 55 | +kind: ValidatingWebhookConfiguration |
| 56 | +metadata: |
| 57 | + name: your-webhook-name-here |
| 58 | + annotations: |
| 59 | + # Typically managed.openshift.io/inject-cabundle-from: namespace/configmap |
| 60 | + # The configmap must have the cert in PEM format in a key named service-ca.crt. |
| 61 | + # Each webhook in this object with a service clientConfig will have the bundle injected. |
| 62 | + #VWC_ANNOTATION#: #NAMESPACE#/#CABUNDLECONFIGMAP# |
| 63 | +webhooks: |
| 64 | + - clientConfig: |
| 65 | + service: |
| 66 | + namespace: #NAMESPACE# |
| 67 | + name: #SVCNAME# |
| 68 | + path: /your-webhook |
| 69 | + failurePolicy: |
| 70 | + # What to do if the hook itself fails (Ignore/Fail) |
| 71 | + name: your-webhook.managed.openshift.io |
| 72 | + rules: |
| 73 | + - operations: |
| 74 | + # operations list |
| 75 | + apiGroups: |
| 76 | + # apiGroups list |
| 77 | + apiVersions: |
| 78 | + # apiVersions list |
| 79 | + resources: |
| 80 | + # resources List |
| 81 | +``` |
| 82 | + |
| 83 | +From here, `make render` will populate [deploy](/deploy) with YAML manifests that can be `oc apply` to the cluster in question. Note that new hooks require a restart of the Flask application. |
| 84 | + |
| 85 | +### Request Helpers |
| 86 | + |
| 87 | +There are helper methods within the [src/webhook/request_helper](src/webhook/request_helper) to aid with: |
| 88 | + |
| 89 | +* [Incoming request validation](src/webhook/request_helper/validate.py) |
| 90 | +* [Formulating the response JSON body](src/webhook/request_helper/responses.py) |
| 91 | + |
| 92 | +To use the request validation: |
| 93 | + |
| 94 | +```python |
| 95 | +# src/webhook/your_hook.py |
| 96 | +from flask import request, Blueprint |
| 97 | +import json |
| 98 | + |
| 99 | +from webhook.request_helper import validate, responses |
| 100 | +@bp.route('/your-webhook', methods=('GET','POST')) |
| 101 | +def handle_request(): |
| 102 | + valid = True |
| 103 | + try: |
| 104 | + valid = validate.validate_request_structure(request.json) |
| 105 | + except: |
| 106 | + # if anything goes wrong, it's not valid. |
| 107 | + valid = False |
| 108 | + if not valid: |
| 109 | + return responses.response_invalid() |
| 110 | + # ... normal hook flow |
| 111 | +``` |
| 112 | + |
| 113 | +To use the response helpers: |
| 114 | + |
| 115 | +```python |
| 116 | +# src/webhook/your_hook.py |
| 117 | +from flask import request, Blueprint |
| 118 | +import json |
| 119 | + |
| 120 | +from webhook.request_helper import responses |
| 121 | +@bp.route('/your-webhook', methods=('GET','POST')) |
| 122 | +def handle_request(): |
| 123 | + # ... |
| 124 | + |
| 125 | + # request is the object coming from the webhook |
| 126 | + # request.json converts to JSON document, and the request key therein has the interesting data |
| 127 | + request_body = request.json['request'] |
| 128 | + |
| 129 | + # Invalid request came in |
| 130 | + return responses.response_invalid() |
| 131 | + |
| 132 | + # Access granted: |
| 133 | + return responses.response_allow(req=request_body) |
| 134 | + |
| 135 | + # Access denied: |
| 136 | + return responses.response_deny(req=response_body, msg="Reason to deny") |
| 137 | + |
| 138 | + # ... |
| 139 | +``` |
0 commit comments