Skip to content

Commit

Permalink
Support Teleport Session Watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
zoetrope committed Sep 17, 2024
1 parent 9be8dd3 commit be0febb
Show file tree
Hide file tree
Showing 5 changed files with 1,806 additions and 50 deletions.
56 changes: 46 additions & 10 deletions cmd/login-protector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package main
import (
"crypto/tls"
"flag"
teleport_client "github.com/gravitational/teleport/api/client"
"log"
"os"
"sigs.k8s.io/controller-runtime/pkg/manager"
"time"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
Expand Down Expand Up @@ -42,7 +45,8 @@ func main() {
var probeAddr string
var secureMetrics bool
var enableHTTP2 bool
var ttyCheckInterval time.Duration
var sessionCheckInterval time.Duration
var sessionWatcher string
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
Expand All @@ -52,7 +56,8 @@ func main() {
"If set the metrics endpoint is served securely")
flag.BoolVar(&enableHTTP2, "enable-http2", false,
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
flag.DurationVar(&ttyCheckInterval, "tty-check-interval", 5*time.Second, "interval to check TTY")
flag.DurationVar(&sessionCheckInterval, "session-check-interval", 5*time.Second, "interval to check session")
flag.StringVar(&sessionWatcher, "session-watcher", "local", "session watcher to use (local or teleport)")
opts := zap.Options{
Development: true,
}
Expand Down Expand Up @@ -109,17 +114,48 @@ func main() {
os.Exit(1)
}

setupLog.Info("creating local session watcher")
setupLog.Info("creating session watcher", "type", sessionWatcher)
ch := make(chan event.TypedGenericEvent[*corev1.Pod])
watcher := controller.NewLocalSessionWatcher(
mgr.GetClient(),
mgr.GetLogger().WithName("LocalSessionWatcher"),
ttyCheckInterval,
ch,
)

var watcher manager.Runnable
switch sessionWatcher {
case "local":
watcher = controller.NewLocalSessionWatcher(
mgr.GetClient(),
mgr.GetLogger().WithName("LocalSessionWatcher"),
sessionCheckInterval,
ch,
)
case "teleport":
teleportClient, err := teleport_client.New(ctx, teleport_client.Config{
Addrs: []string{
"localhost:3080",
"localhost:3025",
"localhost:3024",
},
Credentials: []teleport_client.Credentials{
teleport_client.LoadIdentityFile("api-access.pem"),
},
})

if err != nil {
log.Fatalf("failed to create client: %v", err)
}
defer teleportClient.Close()

Check failure on line 144 in cmd/login-protector/main.go

View workflow job for this annotation

GitHub Actions / Tests

Error return value of `teleportClient.Close` is not checked (errcheck)
watcher = controller.NewTeleportSessionWatcher(
mgr.GetClient(),
teleportClient,
mgr.GetLogger().WithName("TeleportSessionWatcher"),
sessionCheckInterval,
ch,
)
default:
setupLog.Error(nil, "unknown session watcher", "type", sessionWatcher)
os.Exit(1)
}
err = mgr.Add(watcher)
if err != nil {
setupLog.Error(err, "unable to add runnable", "runnable", "LocalSessionWatcher")
setupLog.Error(err, "unable to add watcher", "type", sessionWatcher)
os.Exit(1)
}

Expand Down
57 changes: 44 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,94 @@ module github.com/cybozu-go/login-protector

go 1.22.2

// for Teleport v15.3.7
require github.com/gravitational/teleport/api v0.0.0-20240523232127-d8e06e874f3b

require (
github.com/creack/pty v1.1.21
github.com/go-logr/logr v1.4.2
github.com/onsi/ginkgo/v2 v2.19.0
github.com/onsi/gomega v1.33.1
github.com/prometheus/client_golang v1.19.1
github.com/prometheus/client_model v0.5.0
github.com/prometheus/common v0.48.0
go.uber.org/zap v1.27.0
golang.org/x/term v0.20.0
k8s.io/api v0.30.1
k8s.io/apimachinery v0.30.1
k8s.io/client-go v0.30.1
sigs.k8s.io/controller-runtime v0.18.3
)

require (
github.com/beevik/etree v1.3.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/coreos/go-semver v0.3.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-logr/zapr v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/go-piv/piv-go v1.11.0 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/gobwas/httphead v0.1.0 // indirect
github.com/gobwas/pool v0.2.1 // indirect
github.com/gobwas/ws v1.4.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gravitational/trace v1.4.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/jonboulle/clockwork v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattermost/xml-roundtrip-validator v0.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/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/russellhaering/gosaml2 v0.9.1 // indirect
github.com/russellhaering/goxmldsig v1.4.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.53.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0 // indirect
go.opentelemetry.io/otel v1.28.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.28.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.28.0 // indirect
go.opentelemetry.io/otel/metric v1.28.0 // indirect
go.opentelemetry.io/otel/sdk v1.28.0 // indirect
go.opentelemetry.io/otel/trace v1.28.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/oauth2 v0.22.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/term v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.21.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.33.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240822170219-fc7c04adadcd // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240822170219-fc7c04adadcd // indirect
google.golang.org/grpc v1.66.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading

0 comments on commit be0febb

Please sign in to comment.