-
Notifications
You must be signed in to change notification settings - Fork 113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversion webhook #1302
Conversion webhook #1302
Changes from all commits
ce6f84f
513f1f2
7ff040f
1506af3
42b97c3
7ef4ddc
831ea01
6e2ccab
29fe106
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Copyright The Shipwright Contributors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"flag" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"path" | ||
"runtime" | ||
"time" | ||
|
||
"github.com/shipwright-io/build/pkg/ctxlog" | ||
"github.com/shipwright-io/build/pkg/webhook/conversion" | ||
"github.com/shipwright-io/build/version" | ||
"github.com/spf13/pflag" | ||
"knative.dev/pkg/signals" | ||
) | ||
|
||
var ( | ||
versionGiven = flag.String("version", "devel", "Version of Shipwright webhook running") | ||
) | ||
|
||
func printVersion(ctx context.Context) { | ||
ctxlog.Info(ctx, fmt.Sprintf("Shipwright Build Webhook Version: %s", version.Version)) | ||
ctxlog.Info(ctx, fmt.Sprintf("Go Version: %s", runtime.Version())) | ||
ctxlog.Info(ctx, fmt.Sprintf("Go OS/Arch: %s/%s", runtime.GOOS, runtime.GOARCH)) | ||
} | ||
|
||
func main() { | ||
// Add the zap logger flag set to the CLI. The flag set must | ||
// be added before calling pflag.Parse(). | ||
pflag.CommandLine.AddGoFlagSet(ctxlog.CustomZapFlagSet()) | ||
|
||
// Add flags registered by imported packages (e.g. glog and | ||
// controller-runtime) | ||
pflag.CommandLine.AddGoFlagSet(flag.CommandLine) | ||
|
||
pflag.Parse() | ||
|
||
if err := Execute(); err != nil { | ||
os.Exit(1) | ||
} | ||
|
||
} | ||
|
||
func Execute() error { | ||
l := ctxlog.NewLogger("shp-build-webhook") | ||
|
||
ctx := ctxlog.NewParentContext(l) | ||
|
||
version.SetVersion(*versionGiven) | ||
printVersion(ctx) | ||
|
||
mux := http.NewServeMux() | ||
mux.HandleFunc("/health", health) | ||
ctxlog.Info(ctx, "adding handlefunc() /health") | ||
|
||
// convert endpoint handles ConversionReview API object serialized to JSON | ||
mux.HandleFunc("/convert", conversion.CRDConvertHandler(ctx)) | ||
ctxlog.Info(ctx, "adding handlefunc() /convert") | ||
|
||
server := &http.Server{ | ||
Addr: ":8443", | ||
Handler: mux, | ||
ReadHeaderTimeout: 32 * time.Second, | ||
TLSConfig: &tls.Config{ | ||
MinVersion: tls.VersionTLS12, | ||
CurvePreferences: []tls.CurveID{tls.CurveP256, tls.CurveP384, tls.X25519}, | ||
CipherSuites: []uint16{ | ||
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, | ||
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, | ||
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, | ||
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, | ||
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, | ||
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, | ||
}, | ||
Comment on lines
+73
to
+82
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 on setting the min TLS version - I have seen scanners pick this up as a recommended practice. Are we seeing the same for the curve preferences and cipher suites? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the recommendation, following https://wiki.mozilla.org/Security/Server_Side_TLS per the Intermediate category. For example:
The curve preferences are inline with the Intermediate category, the same as for the cipher suites. |
||
}, | ||
} | ||
|
||
go func() { | ||
ctxlog.Info(ctx, "starting webhook server") | ||
// blocking call, returns on error | ||
if err := server.ListenAndServeTLS(path.Join("/etc/webhook/certs", "tls.crt"), path.Join("/etc/webhook/certs", "tls.key")); err != nil { | ||
ctxlog.Error(ctx, err, "webhook server failed to start") | ||
} | ||
}() | ||
|
||
stopCh := signals.SetupSignalHandler() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not blocking: unsure if the signal handler should be set up earlier in the main function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need to think more on this |
||
sig := <-stopCh | ||
|
||
l.Info("Shutting down server.", "signal", sig) | ||
ctxlog.Info(ctx, "shutting down webhook server,", "signal:", sig) | ||
if err := server.Shutdown(context.Background()); err != nil { | ||
l.Error(err, "Failed to gracefully shutdown the server.") | ||
return err | ||
} | ||
return nil | ||
|
||
} | ||
|
||
func health(resp http.ResponseWriter, _ *http.Request) { | ||
resp.WriteHeader(http.StatusNoContent) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRole | ||
metadata: | ||
name: shipwright-build-webhook | ||
rules: | ||
- apiGroups: | ||
- "" | ||
resources: | ||
- pods | ||
- events | ||
- configmaps | ||
- secrets | ||
- limitranges | ||
- namespaces | ||
- services | ||
verbs: | ||
- '*' | ||
- apiGroups: | ||
- admissionregistration.k8s.io | ||
- admissionregistration.k8s.io/v1beta1 | ||
resources: | ||
- validatingwebhookconfigurations | ||
verbs: | ||
- '*' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: ClusterRoleBinding | ||
metadata: | ||
name: shipwright-build-webhook | ||
namespace: shipwright-build | ||
roleRef: | ||
apiGroup: rbac.authorization.k8s.io | ||
kind: ClusterRole | ||
name: shipwright-build-webhook | ||
subjects: | ||
- kind: ServiceAccount | ||
name: shipwright-build-webhook | ||
namespace: shipwright-build |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
apiVersion: v1 | ||
kind: ServiceAccount | ||
metadata: | ||
name: shipwright-build-webhook | ||
namespace: shipwright-build |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: shp-build-webhook | ||
namespace: shipwright-build | ||
spec: | ||
ports: | ||
- name: https-webhook | ||
port: 443 | ||
targetPort: 8443 | ||
selector: | ||
name: shp-build-webhook |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
|
||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: shipwright-build-webhook | ||
namespace: shipwright-build | ||
labels: | ||
app: shp-build-webhook | ||
spec: | ||
replicas: 1 | ||
strategy: | ||
rollingUpdate: | ||
maxSurge: 0 | ||
maxUnavailable: 1 | ||
type: RollingUpdate | ||
selector: | ||
matchLabels: | ||
name: shp-build-webhook | ||
template: | ||
metadata: | ||
name: shp-build-webhook | ||
labels: | ||
name: shp-build-webhook | ||
spec: | ||
securityContext: | ||
runAsNonRoot: true | ||
serviceAccountName: shipwright-build-webhook | ||
containers: | ||
- name: shp-build-webhook | ||
image: ko://github.com/shipwright-io/build/cmd/shipwright-build-webhook | ||
volumeMounts: | ||
- name: webhook-certs | ||
mountPath: /etc/webhook/certs | ||
readOnly: true | ||
ports: | ||
- containerPort: 8443 | ||
name: https-port | ||
securityContext: | ||
allowPrivilegeEscalation: false | ||
capabilities: | ||
drop: | ||
- ALL | ||
readOnlyRootFilesystem: true | ||
runAsUser: 1000 | ||
runAsGroup: 1000 | ||
seccompProfile: | ||
type: RuntimeDefault | ||
volumes: | ||
- name: webhook-certs | ||
secret: | ||
secretName: shipwright-build-webhook-cert |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12231,7 +12231,7 @@ spec: | |
required: | ||
- spec | ||
type: object | ||
served: false | ||
served: true | ||
storage: false | ||
subresources: | ||
status: {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4083,7 +4083,7 @@ spec: | |
required: | ||
- spec | ||
type: object | ||
served: false | ||
served: true | ||
storage: false | ||
subresources: | ||
status: {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ require ( | |
github.com/go-logr/logr v1.2.4 | ||
github.com/golang-jwt/jwt/v4 v4.5.0 | ||
github.com/google/go-containerregistry v0.16.1 | ||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 | ||
github.com/onsi/ginkgo/v2 v2.12.0 | ||
github.com/onsi/gomega v1.27.10 | ||
github.com/prometheus/client_golang v1.16.0 | ||
|
@@ -17,6 +18,7 @@ require ( | |
github.com/tektoncd/pipeline v0.44.0 | ||
go.uber.org/zap v1.25.0 | ||
k8s.io/api v0.25.6 | ||
k8s.io/apiextensions-apiserver v0.25.2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit (not blocking): z-stream version skew for k8s library. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pls elaborate, I was not able to follow (keep in mind I'm sleepy :) ) |
||
k8s.io/apimachinery v0.25.6 | ||
k8s.io/client-go v0.25.6 | ||
k8s.io/code-generator v0.25.6 | ||
|
@@ -89,7 +91,6 @@ require ( | |
github.com/mitchellh/go-homedir v1.1.0 // indirect | ||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
github.com/modern-go/reflect2 v1.0.2 // indirect | ||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect | ||
github.com/opencontainers/go-digest v1.0.0 // indirect | ||
github.com/opencontainers/image-spec v1.1.0-rc3 // indirect | ||
github.com/pjbgf/sha1cd v0.3.0 // indirect | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
spec: | ||
conversion: | ||
strategy: Webhook | ||
webhook: | ||
clientConfig: | ||
caBundle: CA_BUNDLE | ||
service: | ||
namespace: shipwright-build | ||
name: shp-build-webhook | ||
path: /convert | ||
conversionReviewVersions: | ||
- v1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this - is this data available/meaningful once the webhook is compiled into a binary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the version could be an "easy to get" indicator of which go version was used to build this image, assuming you are dealing with an specific CVE for go X version. We do this as well for the controller pod.