2021-01-08 21:22:31 +00:00
|
|
|
// Copyright 2020-2021 the Pinniped contributors. All Rights Reserved.
|
2020-09-16 14:19:51 +00:00
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2020-09-09 19:27:30 +00:00
|
|
|
|
2021-07-26 16:18:43 +00:00
|
|
|
// Package localuserauthenticator provides a authentication webhook program.
|
2020-09-09 19:27:30 +00:00
|
|
|
//
|
|
|
|
// This webhook is meant to be used in demo settings to play around with
|
|
|
|
// Pinniped. As well, it can come in handy in integration tests.
|
|
|
|
//
|
2020-09-10 02:06:39 +00:00
|
|
|
// This webhook is NOT meant for use in production systems.
|
2021-07-26 16:18:43 +00:00
|
|
|
package localuserauthenticator
|
2020-09-09 19:27:30 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"encoding/csv"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2020-09-10 22:00:53 +00:00
|
|
|
"mime"
|
2020-09-09 19:27:30 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
2020-09-11 16:14:12 +00:00
|
|
|
authenticationv1beta1 "k8s.io/api/authentication/v1beta1"
|
2020-09-09 19:27:30 +00:00
|
|
|
k8serrors "k8s.io/apimachinery/pkg/api/errors"
|
2020-09-11 19:19:05 +00:00
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
2020-09-09 19:27:30 +00:00
|
|
|
kubeinformers "k8s.io/client-go/informers"
|
|
|
|
corev1informers "k8s.io/client-go/informers/core/v1"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
"k8s.io/klog/v2"
|
|
|
|
|
2020-09-18 19:56:24 +00:00
|
|
|
"go.pinniped.dev/internal/constable"
|
|
|
|
"go.pinniped.dev/internal/controller/apicerts"
|
|
|
|
"go.pinniped.dev/internal/controllerlib"
|
2021-10-20 11:59:24 +00:00
|
|
|
"go.pinniped.dev/internal/crypto/ptls"
|
2020-09-23 12:26:59 +00:00
|
|
|
"go.pinniped.dev/internal/dynamiccert"
|
2021-01-05 22:07:33 +00:00
|
|
|
"go.pinniped.dev/internal/kubeclient"
|
2020-11-10 15:22:16 +00:00
|
|
|
"go.pinniped.dev/internal/plog"
|
2020-09-09 19:27:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-09-10 02:06:39 +00:00
|
|
|
// This string must match the name of the Namespace declared in the deployment yaml.
|
2020-09-10 22:20:02 +00:00
|
|
|
namespace = "local-user-authenticator"
|
2020-09-10 02:06:39 +00:00
|
|
|
// This string must match the name of the Service declared in the deployment yaml.
|
2020-09-10 22:20:02 +00:00
|
|
|
serviceName = "local-user-authenticator"
|
2020-09-09 19:27:30 +00:00
|
|
|
|
2020-09-10 02:06:39 +00:00
|
|
|
singletonWorker = 1
|
2020-09-09 19:27:30 +00:00
|
|
|
defaultResyncInterval = 3 * time.Minute
|
2020-09-11 01:34:18 +00:00
|
|
|
|
|
|
|
invalidRequest = constable.Error("invalid request")
|
2020-09-09 19:27:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type webhook struct {
|
2021-03-15 16:24:07 +00:00
|
|
|
certProvider dynamiccert.Private
|
2020-09-09 19:27:30 +00:00
|
|
|
secretInformer corev1informers.SecretInformer
|
|
|
|
}
|
|
|
|
|
|
|
|
func newWebhook(
|
2021-03-15 16:24:07 +00:00
|
|
|
certProvider dynamiccert.Private,
|
2020-09-09 19:27:30 +00:00
|
|
|
secretInformer corev1informers.SecretInformer,
|
|
|
|
) *webhook {
|
|
|
|
return &webhook{
|
|
|
|
certProvider: certProvider,
|
|
|
|
secretInformer: secretInformer,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// start runs the webhook in a separate goroutine and returns whether or not the
|
|
|
|
// webhook was started successfully.
|
|
|
|
func (w *webhook) start(ctx context.Context, l net.Listener) error {
|
2021-10-20 11:59:24 +00:00
|
|
|
c := ptls.Secure(nil)
|
|
|
|
c.GetCertificate = func(_ *tls.ClientHelloInfo) (*tls.Certificate, error) {
|
|
|
|
certPEM, keyPEM := w.certProvider.CurrentCertKeyContent()
|
|
|
|
cert, err := tls.X509KeyPair(certPEM, keyPEM)
|
|
|
|
return &cert, err
|
|
|
|
}
|
2020-09-09 19:27:30 +00:00
|
|
|
server := http.Server{
|
2021-10-20 11:59:24 +00:00
|
|
|
Handler: w,
|
|
|
|
TLSConfig: c,
|
2020-09-09 19:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
errCh := make(chan error)
|
|
|
|
go func() {
|
|
|
|
// Per ListenAndServeTLS doc, the {cert,key}File parameters can be empty
|
|
|
|
// since we want to use the certs from http.Server.TLSConfig.
|
|
|
|
errCh <- server.ServeTLS(l, "", "")
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
2020-11-10 15:22:16 +00:00
|
|
|
plog.Debug("server exited", "err", err)
|
2020-09-09 19:27:30 +00:00
|
|
|
case <-ctx.Done():
|
2020-11-10 15:22:16 +00:00
|
|
|
plog.Debug("server context cancelled", "err", ctx.Err())
|
2020-09-09 19:27:30 +00:00
|
|
|
if err := server.Shutdown(context.Background()); err != nil {
|
2020-11-10 15:22:16 +00:00
|
|
|
plog.Debug("server shutdown failed", "err", err)
|
2020-09-09 19:27:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *webhook) ServeHTTP(rsp http.ResponseWriter, req *http.Request) {
|
2020-09-11 01:34:18 +00:00
|
|
|
username, password, err := getUsernameAndPasswordFromRequest(rsp, req)
|
|
|
|
if err != nil {
|
2020-09-09 19:27:30 +00:00
|
|
|
return
|
|
|
|
}
|
2020-10-27 23:33:08 +00:00
|
|
|
defer func() { _ = req.Body.Close() }()
|
2020-09-09 19:27:30 +00:00
|
|
|
|
|
|
|
secret, err := w.secretInformer.Lister().Secrets(namespace).Get(username)
|
|
|
|
notFound := k8serrors.IsNotFound(err)
|
|
|
|
if err != nil && !notFound {
|
2020-11-10 15:22:16 +00:00
|
|
|
plog.Debug("could not get secret", "err", err)
|
2020-09-09 19:27:30 +00:00
|
|
|
rsp.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if notFound {
|
2020-11-10 15:22:16 +00:00
|
|
|
plog.Debug("user not found")
|
2020-09-09 19:27:30 +00:00
|
|
|
respondWithUnauthenticated(rsp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
passwordMatches := bcrypt.CompareHashAndPassword(
|
|
|
|
secret.Data["passwordHash"],
|
|
|
|
[]byte(password),
|
|
|
|
) == nil
|
|
|
|
if !passwordMatches {
|
2020-11-10 15:22:16 +00:00
|
|
|
plog.Debug("authentication failed: wrong password")
|
2020-09-09 19:27:30 +00:00
|
|
|
respondWithUnauthenticated(rsp)
|
2020-09-10 02:06:39 +00:00
|
|
|
return
|
2020-09-09 19:27:30 +00:00
|
|
|
}
|
|
|
|
|
2020-09-10 02:06:39 +00:00
|
|
|
groups := []string{}
|
2020-09-09 19:27:30 +00:00
|
|
|
groupsBuf := bytes.NewBuffer(secret.Data["groups"])
|
2020-09-10 02:06:39 +00:00
|
|
|
if groupsBuf.Len() > 0 {
|
|
|
|
groupsCSVReader := csv.NewReader(groupsBuf)
|
|
|
|
groups, err = groupsCSVReader.Read()
|
|
|
|
if err != nil {
|
2020-11-10 15:22:16 +00:00
|
|
|
plog.Debug("could not read groups", "err", err)
|
2020-09-10 02:06:39 +00:00
|
|
|
rsp.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
trimLeadingAndTrailingWhitespace(groups)
|
2020-09-09 19:27:30 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 15:22:16 +00:00
|
|
|
plog.Debug("successful authentication")
|
2021-05-03 19:19:28 +00:00
|
|
|
respondWithAuthenticated(rsp, secret.ObjectMeta.Name, groups)
|
2020-09-09 19:27:30 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 01:34:18 +00:00
|
|
|
func getUsernameAndPasswordFromRequest(rsp http.ResponseWriter, req *http.Request) (string, string, error) {
|
|
|
|
if req.URL.Path != "/authenticate" {
|
2020-11-10 15:22:16 +00:00
|
|
|
plog.Debug("received request path other than /authenticate", "path", req.URL.Path)
|
2020-09-11 01:34:18 +00:00
|
|
|
rsp.WriteHeader(http.StatusNotFound)
|
|
|
|
return "", "", invalidRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Method != http.MethodPost {
|
2020-11-10 15:22:16 +00:00
|
|
|
plog.Debug("received request method other than post", "method", req.Method)
|
2020-09-11 01:34:18 +00:00
|
|
|
rsp.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
return "", "", invalidRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
if !headerContains(req, "Content-Type", "application/json") {
|
2020-11-10 15:22:16 +00:00
|
|
|
plog.Debug("content type is not application/json", "Content-Type", req.Header.Values("Content-Type"))
|
2020-09-11 01:34:18 +00:00
|
|
|
rsp.WriteHeader(http.StatusUnsupportedMediaType)
|
|
|
|
return "", "", invalidRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
if !headerContains(req, "Accept", "application/json") &&
|
|
|
|
!headerContains(req, "Accept", "application/*") &&
|
|
|
|
!headerContains(req, "Accept", "*/*") {
|
2020-11-10 15:22:16 +00:00
|
|
|
plog.Debug("client does not accept application/json", "Accept", req.Header.Values("Accept"))
|
2020-09-11 01:34:18 +00:00
|
|
|
rsp.WriteHeader(http.StatusUnsupportedMediaType)
|
|
|
|
return "", "", invalidRequest
|
|
|
|
}
|
|
|
|
|
2020-09-11 16:44:45 +00:00
|
|
|
if req.Body == nil {
|
2020-11-10 15:22:16 +00:00
|
|
|
plog.Debug("invalid nil body")
|
2020-09-11 16:44:45 +00:00
|
|
|
rsp.WriteHeader(http.StatusBadRequest)
|
|
|
|
return "", "", invalidRequest
|
|
|
|
}
|
|
|
|
|
2020-09-11 16:14:12 +00:00
|
|
|
var body authenticationv1beta1.TokenReview
|
2020-09-11 01:34:18 +00:00
|
|
|
if err := json.NewDecoder(req.Body).Decode(&body); err != nil {
|
2020-11-10 15:22:16 +00:00
|
|
|
plog.Debug("failed to decode body", "err", err)
|
2020-09-11 01:34:18 +00:00
|
|
|
rsp.WriteHeader(http.StatusBadRequest)
|
|
|
|
return "", "", invalidRequest
|
|
|
|
}
|
|
|
|
|
2020-09-11 16:14:12 +00:00
|
|
|
if body.APIVersion != authenticationv1beta1.SchemeGroupVersion.String() {
|
2020-11-10 15:22:16 +00:00
|
|
|
plog.Debug("invalid TokenReview apiVersion", "apiVersion", body.APIVersion)
|
2020-09-11 16:06:50 +00:00
|
|
|
rsp.WriteHeader(http.StatusBadRequest)
|
|
|
|
return "", "", invalidRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
if body.Kind != "TokenReview" {
|
2020-11-10 15:22:16 +00:00
|
|
|
plog.Debug("invalid TokenReview kind", "kind", body.Kind)
|
2020-09-11 16:06:50 +00:00
|
|
|
rsp.WriteHeader(http.StatusBadRequest)
|
|
|
|
return "", "", invalidRequest
|
|
|
|
}
|
|
|
|
|
2020-09-11 01:34:18 +00:00
|
|
|
tokenSegments := strings.SplitN(body.Spec.Token, ":", 2)
|
|
|
|
if len(tokenSegments) != 2 {
|
2020-11-10 15:22:16 +00:00
|
|
|
plog.Debug("bad token format in request")
|
2020-09-11 01:34:18 +00:00
|
|
|
rsp.WriteHeader(http.StatusBadRequest)
|
|
|
|
return "", "", invalidRequest
|
|
|
|
}
|
|
|
|
|
|
|
|
return tokenSegments[0], tokenSegments[1], nil
|
|
|
|
}
|
|
|
|
|
2020-09-10 22:00:53 +00:00
|
|
|
func headerContains(req *http.Request, headerName, s string) bool {
|
|
|
|
headerValues := req.Header.Values(headerName)
|
|
|
|
for i := range headerValues {
|
|
|
|
mimeTypes := strings.Split(headerValues[i], ",")
|
|
|
|
for _, mimeType := range mimeTypes {
|
|
|
|
mediaType, _, _ := mime.ParseMediaType(mimeType)
|
|
|
|
if mediaType == s {
|
|
|
|
return true
|
|
|
|
}
|
2020-09-09 19:27:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-09-10 02:06:39 +00:00
|
|
|
func trimLeadingAndTrailingWhitespace(ss []string) {
|
2020-09-09 19:27:30 +00:00
|
|
|
for i := range ss {
|
|
|
|
ss[i] = strings.TrimSpace(ss[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func respondWithUnauthenticated(rsp http.ResponseWriter) {
|
|
|
|
rsp.Header().Add("Content-Type", "application/json")
|
2020-09-11 19:19:05 +00:00
|
|
|
|
|
|
|
body := authenticationv1beta1.TokenReview{
|
|
|
|
TypeMeta: metav1.TypeMeta{
|
|
|
|
Kind: "TokenReview",
|
|
|
|
APIVersion: authenticationv1beta1.SchemeGroupVersion.String(),
|
|
|
|
},
|
|
|
|
Status: authenticationv1beta1.TokenReviewStatus{
|
|
|
|
Authenticated: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := json.NewEncoder(rsp).Encode(body); err != nil {
|
2020-11-10 15:22:16 +00:00
|
|
|
plog.Debug("could not encode response", "err", err)
|
2020-09-11 19:19:05 +00:00
|
|
|
rsp.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
2020-09-09 19:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func respondWithAuthenticated(
|
|
|
|
rsp http.ResponseWriter,
|
2021-05-03 19:19:28 +00:00
|
|
|
username string,
|
2020-09-09 19:27:30 +00:00
|
|
|
groups []string,
|
|
|
|
) {
|
|
|
|
rsp.Header().Add("Content-Type", "application/json")
|
2020-09-11 19:19:05 +00:00
|
|
|
body := authenticationv1beta1.TokenReview{
|
|
|
|
TypeMeta: metav1.TypeMeta{
|
|
|
|
Kind: "TokenReview",
|
|
|
|
APIVersion: authenticationv1beta1.SchemeGroupVersion.String(),
|
|
|
|
},
|
|
|
|
Status: authenticationv1beta1.TokenReviewStatus{
|
|
|
|
Authenticated: true,
|
|
|
|
User: authenticationv1beta1.UserInfo{
|
|
|
|
Username: username,
|
|
|
|
Groups: groups,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := json.NewEncoder(rsp).Encode(body); err != nil {
|
2020-11-10 15:22:16 +00:00
|
|
|
plog.Debug("could not encode response", "err", err)
|
2020-09-09 19:27:30 +00:00
|
|
|
rsp.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-10 02:06:39 +00:00
|
|
|
func startControllers(
|
|
|
|
ctx context.Context,
|
2021-03-15 16:24:07 +00:00
|
|
|
dynamicCertProvider dynamiccert.Private,
|
2020-09-10 02:06:39 +00:00
|
|
|
kubeClient kubernetes.Interface,
|
|
|
|
kubeInformers kubeinformers.SharedInformerFactory,
|
|
|
|
) {
|
|
|
|
aVeryLongTime := time.Hour * 24 * 365 * 100
|
|
|
|
|
Rename many of resources that are created in Kubernetes by Pinniped
New resource naming conventions:
- Do not repeat the Kind in the name,
e.g. do not call it foo-cluster-role-binding, just call it foo
- Names will generally start with a prefix to identify our component,
so when a user lists all objects of that kind, they can tell to which
component it is related,
e.g. `kubectl get configmaps` would list one named "pinniped-config"
- It should be possible for an operator to make the word "pinniped"
mostly disappear if they choose, by specifying the app_name in
values.yaml, to the extent that is practical (but not from APIService
names because those are hardcoded in golang)
- Each role/clusterrole and its corresponding binding have the same name
- Pinniped resource names that must be known by the server golang code
are passed to the code at run time via ConfigMap, rather than
hardcoded in the golang code. This also allows them to be prepended
with the app_name from values.yaml while creating the ConfigMap.
- Since the CLI `get-kubeconfig` command cannot guess the name of the
CredentialIssuerConfig resource in advance anymore, it lists all
CredentialIssuerConfig in the app's namespace and returns an error
if there is not exactly one found, and then uses that one regardless
of its name
2020-09-18 22:56:50 +00:00
|
|
|
const certsSecretResourceName = "local-user-authenticator-tls-serving-certificate"
|
|
|
|
|
2020-09-10 02:06:39 +00:00
|
|
|
// Create controller manager.
|
|
|
|
controllerManager := controllerlib.
|
|
|
|
NewManager().
|
|
|
|
WithController(
|
|
|
|
apicerts.NewCertsManagerController(
|
|
|
|
namespace,
|
Rename many of resources that are created in Kubernetes by Pinniped
New resource naming conventions:
- Do not repeat the Kind in the name,
e.g. do not call it foo-cluster-role-binding, just call it foo
- Names will generally start with a prefix to identify our component,
so when a user lists all objects of that kind, they can tell to which
component it is related,
e.g. `kubectl get configmaps` would list one named "pinniped-config"
- It should be possible for an operator to make the word "pinniped"
mostly disappear if they choose, by specifying the app_name in
values.yaml, to the extent that is practical (but not from APIService
names because those are hardcoded in golang)
- Each role/clusterrole and its corresponding binding have the same name
- Pinniped resource names that must be known by the server golang code
are passed to the code at run time via ConfigMap, rather than
hardcoded in the golang code. This also allows them to be prepended
with the app_name from values.yaml while creating the ConfigMap.
- Since the CLI `get-kubeconfig` command cannot guess the name of the
CredentialIssuerConfig resource in advance anymore, it lists all
CredentialIssuerConfig in the app's namespace and returns an error
if there is not exactly one found, and then uses that one regardless
of its name
2020-09-18 22:56:50 +00:00
|
|
|
certsSecretResourceName,
|
2020-10-15 17:14:23 +00:00
|
|
|
map[string]string{
|
|
|
|
"app": "local-user-authenticator",
|
|
|
|
},
|
2020-09-10 02:06:39 +00:00
|
|
|
kubeClient,
|
|
|
|
kubeInformers.Core().V1().Secrets(),
|
|
|
|
controllerlib.WithInformer,
|
|
|
|
controllerlib.WithInitialEvent,
|
|
|
|
aVeryLongTime,
|
2020-09-10 22:20:02 +00:00
|
|
|
"local-user-authenticator CA",
|
2020-09-10 02:06:39 +00:00
|
|
|
serviceName,
|
|
|
|
),
|
|
|
|
singletonWorker,
|
|
|
|
).
|
|
|
|
WithController(
|
|
|
|
apicerts.NewCertsObserverController(
|
|
|
|
namespace,
|
Rename many of resources that are created in Kubernetes by Pinniped
New resource naming conventions:
- Do not repeat the Kind in the name,
e.g. do not call it foo-cluster-role-binding, just call it foo
- Names will generally start with a prefix to identify our component,
so when a user lists all objects of that kind, they can tell to which
component it is related,
e.g. `kubectl get configmaps` would list one named "pinniped-config"
- It should be possible for an operator to make the word "pinniped"
mostly disappear if they choose, by specifying the app_name in
values.yaml, to the extent that is practical (but not from APIService
names because those are hardcoded in golang)
- Each role/clusterrole and its corresponding binding have the same name
- Pinniped resource names that must be known by the server golang code
are passed to the code at run time via ConfigMap, rather than
hardcoded in the golang code. This also allows them to be prepended
with the app_name from values.yaml while creating the ConfigMap.
- Since the CLI `get-kubeconfig` command cannot guess the name of the
CredentialIssuerConfig resource in advance anymore, it lists all
CredentialIssuerConfig in the app's namespace and returns an error
if there is not exactly one found, and then uses that one regardless
of its name
2020-09-18 22:56:50 +00:00
|
|
|
certsSecretResourceName,
|
2020-09-10 02:06:39 +00:00
|
|
|
dynamicCertProvider,
|
|
|
|
kubeInformers.Core().V1().Secrets(),
|
|
|
|
controllerlib.WithInformer,
|
|
|
|
),
|
|
|
|
singletonWorker,
|
|
|
|
)
|
|
|
|
|
|
|
|
kubeInformers.Start(ctx.Done())
|
|
|
|
|
|
|
|
go controllerManager.Start(ctx)
|
2020-09-09 19:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func startWebhook(
|
|
|
|
ctx context.Context,
|
|
|
|
l net.Listener,
|
2021-03-15 16:24:07 +00:00
|
|
|
dynamicCertProvider dynamiccert.Private,
|
2020-09-09 19:27:30 +00:00
|
|
|
secretInformer corev1informers.SecretInformer,
|
|
|
|
) error {
|
2020-09-10 02:06:39 +00:00
|
|
|
return newWebhook(dynamicCertProvider, secretInformer).start(ctx, l)
|
2020-09-09 19:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func waitForSignal() os.Signal {
|
|
|
|
signalCh := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(signalCh, os.Interrupt)
|
|
|
|
return <-signalCh
|
|
|
|
}
|
|
|
|
|
|
|
|
func run() error {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2021-01-05 22:07:33 +00:00
|
|
|
client, err := kubeclient.New()
|
2020-09-09 19:27:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot create k8s client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
kubeInformers := kubeinformers.NewSharedInformerFactoryWithOptions(
|
2021-01-05 22:07:33 +00:00
|
|
|
client.Kubernetes,
|
2020-09-09 19:27:30 +00:00
|
|
|
defaultResyncInterval,
|
|
|
|
kubeinformers.WithNamespace(namespace),
|
|
|
|
)
|
|
|
|
|
2021-03-15 16:24:07 +00:00
|
|
|
dynamicCertProvider := dynamiccert.NewServingCert("local-user-authenticator-tls-serving-certificate")
|
2020-09-10 02:06:39 +00:00
|
|
|
|
2021-01-05 22:07:33 +00:00
|
|
|
startControllers(ctx, dynamicCertProvider, client.Kubernetes, kubeInformers)
|
2020-11-10 15:22:16 +00:00
|
|
|
plog.Debug("controllers are ready")
|
2020-09-09 19:27:30 +00:00
|
|
|
|
Rename many of resources that are created in Kubernetes by Pinniped
New resource naming conventions:
- Do not repeat the Kind in the name,
e.g. do not call it foo-cluster-role-binding, just call it foo
- Names will generally start with a prefix to identify our component,
so when a user lists all objects of that kind, they can tell to which
component it is related,
e.g. `kubectl get configmaps` would list one named "pinniped-config"
- It should be possible for an operator to make the word "pinniped"
mostly disappear if they choose, by specifying the app_name in
values.yaml, to the extent that is practical (but not from APIService
names because those are hardcoded in golang)
- Each role/clusterrole and its corresponding binding have the same name
- Pinniped resource names that must be known by the server golang code
are passed to the code at run time via ConfigMap, rather than
hardcoded in the golang code. This also allows them to be prepended
with the app_name from values.yaml while creating the ConfigMap.
- Since the CLI `get-kubeconfig` command cannot guess the name of the
CredentialIssuerConfig resource in advance anymore, it lists all
CredentialIssuerConfig in the app's namespace and returns an error
if there is not exactly one found, and then uses that one regardless
of its name
2020-09-18 22:56:50 +00:00
|
|
|
//nolint: gosec // Intentionally binding to all network interfaces.
|
2020-11-02 16:57:05 +00:00
|
|
|
l, err := net.Listen("tcp", ":8443")
|
2020-09-09 19:27:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot create listener: %w", err)
|
|
|
|
}
|
2020-10-27 23:33:08 +00:00
|
|
|
defer func() { _ = l.Close() }()
|
2020-09-09 19:27:30 +00:00
|
|
|
|
2020-09-10 02:06:39 +00:00
|
|
|
err = startWebhook(ctx, l, dynamicCertProvider, kubeInformers.Core().V1().Secrets())
|
|
|
|
if err != nil {
|
2020-09-09 19:27:30 +00:00
|
|
|
return fmt.Errorf("cannot start webhook: %w", err)
|
|
|
|
}
|
2020-11-10 15:22:16 +00:00
|
|
|
plog.Debug("webhook is ready", "address", l.Addr().String())
|
2020-09-09 19:27:30 +00:00
|
|
|
|
2020-09-10 02:06:39 +00:00
|
|
|
gotSignal := waitForSignal()
|
2020-11-10 15:22:16 +00:00
|
|
|
plog.Debug("webhook exiting", "signal", gotSignal)
|
2020-09-09 19:27:30 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-26 16:18:43 +00:00
|
|
|
func Main() {
|
2020-11-11 12:51:51 +00:00
|
|
|
// Hardcode the logging level to debug, since this is a test app and it is very helpful to have
|
|
|
|
// verbose logs to debug test failures.
|
|
|
|
if err := plog.ValidateAndSetLogLevelGlobally(plog.LevelDebug); err != nil {
|
|
|
|
klog.Fatal(err)
|
|
|
|
}
|
2020-09-09 19:27:30 +00:00
|
|
|
if err := run(); err != nil {
|
|
|
|
klog.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|