Skip to content

Commit

Permalink
[pipeline/trusted-resources]Add admission webhook
Browse files Browse the repository at this point in the history
This commit is a followup work for Trust Task.(Forked from tektoncd/community#537)
Prior this commit we have added the verification of taskspec but didn’t include the webhook configuration and setup.
This commit includes the configuration and setup of the admission webhook and related docs.With this commit we can deploy a standalone webhookfor verification.
  • Loading branch information
Yongxuanzhang committed Mar 7, 2022
1 parent 9a361f4 commit 4cce2bc
Show file tree
Hide file tree
Showing 26 changed files with 926 additions and 43 deletions.
6 changes: 0 additions & 6 deletions pipeline/trusted-resources/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ cosign.*
payload.json
signatures.json

# tmp
/config
/cmd
/examples

# Precompiled Headers
*.gch
*.pch
Expand Down Expand Up @@ -63,7 +58,6 @@ cmd/*/kodata/source.tar.gz
test/pullrequest/pullrequest-init
/.bin/
/bin/
/etc

# allow vendor
!vendor/
64 changes: 63 additions & 1 deletion pipeline/trusted-resources/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,69 @@

[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/tektoncd/experimental/blob/master/LICENSE)

This is an experimental project to provide a seperate admission webhook for remote resources verification.
This is an experimental project to provide a seperate webhook for remote resources verification.

- [Install](#install)
- [Uninstall](#uninstall)
- [Development](#development)

## Install

Install and configure [`ko`](https://github.com/google/ko).

Install tekton pipeline. To install from source, checkout to pipeline repo and execute:
```bash
ko apply -f config/
```

Generate cosign key pair
```bash
# cosign generate-key-pair k8s://tekton-pipelines/signing-secrets
cosign generate-key-pair
```

Generate signed files
```bash
go run etc/signing.go
```

Then install the new admission webhook:
```bash
# delete secret if already exists
# kubectl delete secret signing-secrets -n tekton-trusted-resources
kubectl create secret generic signing-secrets \
--from-file=cosign.key=./cosign.key \
--from-literal=cosign.password='1234'\
--from-file=cosign.pub=./cosign.pub \
-n tekton-trusted-resources

ko apply -f config/
```

Examples:
```bash
# Test API taskrun
ko apply -f examples/1-test-taskrun-signed.yaml

# Test OCI Bundle
# add this secret to controller's service account
kubectl create secret generic ${SECRET_NAME} \
--from-file=.dockerconfigjson=<path/to/.docker/config.json> \
--type=kubernetes.io/dockerconfigjson
--namespace=tekton-pipelines

tkn bundle push docker.io/my-dockerhub-username/testtask:latest -f examples/test_task.yaml
cosign sign --key cosign.key docker.io/my-dockerhub-username/testtask:latest
```

## Uninstall

```bash
ko delete -f config/
```


## Development

generate deepcopy code
```bash
Expand Down
113 changes: 113 additions & 0 deletions pipeline/trusted-resources/cmd/webhook_trusted_resource/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
Copyright 2022 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"context"
"log"
"net/http"
"os"

taskvalidation "github.com/tektoncd/experimental/pipelines/trusted-resources/pkg/trustedtask"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"k8s.io/apimachinery/pkg/runtime/schema"
"knative.dev/pkg/configmap"
"knative.dev/pkg/controller"
"knative.dev/pkg/injection"
"knative.dev/pkg/injection/sharedmain"
"knative.dev/pkg/signals"
"knative.dev/pkg/system"
"knative.dev/pkg/webhook"
"knative.dev/pkg/webhook/certificates"
"knative.dev/pkg/webhook/resourcesemantics"
"knative.dev/pkg/webhook/resourcesemantics/validation"
)

var types = map[schema.GroupVersionKind]resourcesemantics.GenericCRD{
// TODO: Add other types

// v1beta1
v1beta1.SchemeGroupVersion.WithKind("TaskRun"): &taskvalidation.TrustedTaskRun{},
}

func newValidationAdmissionController(ctx context.Context, cmw configmap.Watcher) *controller.Impl {
return validation.NewAdmissionController(ctx,

// Name of the resource webhook.
"validation.trustedresources.webhook.pipeline.tekton.dev",

// The path on which to serve the webhook.
"/resource-validation",

// The resources to validate and default.
types,

// A function that infuses the context passed to Validate/SetDefaults with custom metadata.
nil,

// Whether to disallow unknown fields.
true,
)
}

func main() {
serviceName := os.Getenv("WEBHOOK_SERVICE_NAME")
if serviceName == "" {
serviceName = "tekton-trusted-resources-webhook"
}

secretName := os.Getenv("WEBHOOK_SECRET_NAME")
if secretName == "" {
secretName = "trusted-resources-webhook-certs" // #nosec
}

// Scope informers to the webhook's namespace instead of cluster-wide
ctx := injection.WithNamespaceScope(signals.NewContext(), system.Namespace())

// Set up a signal context with our webhook options
ctx = webhook.WithOptions(ctx, webhook.Options{
ServiceName: serviceName,
Port: 8443,
SecretName: secretName,
})

mux := http.NewServeMux()

mux.HandleFunc("/", handler)
mux.HandleFunc("/health", handler)
mux.HandleFunc("/readiness", handler)

port := os.Getenv("PROBES_PORT")
if port == "" {
port = "8080"
}

go func() {
// start the web server on port and accept requests
log.Printf("Readiness and health check server listening on port %s", port)
log.Fatal(http.ListenAndServe(":"+port, mux))
}()

sharedmain.MainWithContext(ctx, serviceName,
certificates.NewController,
newValidationAdmissionController,
)
}

func handler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
21 changes: 21 additions & 0 deletions pipeline/trusted-resources/config/100-namespace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2019 The Tekton Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: v1
kind: Namespace
metadata:
name: tekton-trusted-resources
labels:
app.kubernetes.io/instance: default
app.kubernetes.io/part-of: admissioncontrol
43 changes: 43 additions & 0 deletions pipeline/trusted-resources/config/200-clusterrole.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Copyright 2022 The Tekton Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: tekton-trusted-resources-webhook-cluster-access
labels:
app.kubernetes.io/component: tekton-trusted-resources
app.kubernetes.io/instance: default
app.kubernetes.io/part-of: admissioncontrol
rules:
- apiGroups: ["admissionregistration.k8s.io"]
# The webhook performs a reconciliation on these two resources and continuously
# updates configuration.
resources: ["mutatingwebhookconfigurations", "validatingwebhookconfigurations"]
# knative starts informers on these things, which is why we need get, list and watch.
verbs: ["list", "watch"]
- apiGroups: ["admissionregistration.k8s.io"]
resources: ["validatingwebhookconfigurations"]
resourceNames: ["validation.trustedresources.webhook.pipeline.tekton.dev"]
# When there are changes to the configs or secrets, knative updates the validatingwebhook config
# with the updated certificates or the refreshed set of rules.
verbs: ["get", "update"]
- apiGroups: ["admissionregistration.k8s.io"]
resources: ["mutatingwebhookconfigurations"]
# This mutating webhook is responsible for applying defaults to tekton objects
# as they are received.
resourceNames: ["trustedresources.webhook.pipeline.tekton.dev"]
# When there are changes to the configs or secrets, knative updates the mutatingwebhook config
# with the updated certificates or the refreshed set of rules.
verbs: ["get", "update", "delete"]
68 changes: 68 additions & 0 deletions pipeline/trusted-resources/config/200-role.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Copyright 2022 The Tekton Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: tekton-trusted-resources-webhook
namespace: tekton-trusted-resources
labels:
app.kubernetes.io/component: tekton-trusted-resources
app.kubernetes.io/instance: default
app.kubernetes.io/part-of: admissioncontrol
rules:
# Webhook needs to list and watch configmaps for reconciliation.
- apiGroups: [""]
resources: ["configmaps"]
verbs: ["list", "watch"]
# The webhook needs access to these configmaps for logging information.
- apiGroups: [""]
resources: ["configmaps"]
verbs: ["get"]
resourceNames: ["config-logging", "config-observability", "config-leader-election", "config-trusted-resources"]
# The webhook daemon makes a reconciliation loop on webhook-certs. Whenever
# the secret changes it updates the webhook configurations with the certificates
# stored in the secret.
- apiGroups: [""]
resources: ["secrets"]
verbs: ["list", "watch"]
# The webhook needs access to these secrets for certs and verificaiton
- apiGroups: [""]
resources: ["secrets"]
verbs: ["get", "update"]
resourceNames: ["trusted-resources-webhook-certs", "signing-secret"]
# The webhook needs access to it's SA to make fetch resources
- apiGroups: [""]
resources: ["serviceaccounts"]
verbs: ["get"]
resourceNames: ["tekton-trusted-resources-webhook"]
# The webhook needs access to it's ns to fetch resources in this ns
- apiGroups: [""]
resources: ["namespaces"]
verbs: ["get"]
resourceNames: ["tekton-trusted-resources"]
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: tekton-trusted-resources-leader-election
namespace: tekton-trusted-resources
labels:
app.kubernetes.io/instance: default
app.kubernetes.io/part-of: admissioncontrol
rules:
# We uses leases for leaderelection
- apiGroups: ["coordination.k8s.io"]
resources: ["leases"]
verbs: ["get", "list", "create", "update", "delete", "patch", "watch"]
25 changes: 25 additions & 0 deletions pipeline/trusted-resources/config/200-serviceaccount.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2022 The Tekton Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
apiVersion: v1
kind: ServiceAccount
metadata:
name: tekton-trusted-resources-webhook
namespace: tekton-trusted-resources
labels:
app.kubernetes.io/component: tekton-trusted-resources
app.kubernetes.io/instance: default
app.kubernetes.io/part-of: admissioncontrol
# Uncomment this and create imagePullSecrets according to README to pull OCI bundle from registry
#imagePullSecrets:
#- name: myregistrykey
30 changes: 30 additions & 0 deletions pipeline/trusted-resources/config/201-clusterrolebinding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2022 The Tekton Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: tekton-trusted-resources-webhook-cluster-access
labels:
app.kubernetes.io/component: tekton-trusted-resources
app.kubernetes.io/instance: default
app.kubernetes.io/part-of: admissioncontrol
subjects:
- kind: ServiceAccount
name: tekton-trusted-resources-webhook
namespace: tekton-trusted-resources
roleRef:
kind: ClusterRole
name: tekton-trusted-resources-webhook-cluster-access
apiGroup: rbac.authorization.k8s.io
Loading

0 comments on commit 4cce2bc

Please sign in to comment.