forked from tektoncd/experimental
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pipeline/trusted-resources]Add admission webhook
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
1 parent
9a361f4
commit 78ecbb3
Showing
20 changed files
with
732 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
pipeline/trusted-resources/cmd/webhook_trusted_resource/main.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", "feature-flags", "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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
pipeline/trusted-resources/config/201-clusterrolebinding.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.