Skip to content
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

Support Teleport #5

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- 'main'
jobs:
test:
name: Tests
name: E2E Test
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
Expand All @@ -18,3 +18,16 @@ jobs:
github_token: ${{ secrets.GITHUB_TOKEN }}
- run: make lint
- run: make test-e2e
test-teleport:
name: E2E Test with Teleport
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- uses: ./.github/actions/aqua
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
- run: make setup-teleport-cli
- run: make test-teleport
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
/docs/book

/vendor
/teleport
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ USER 10000:10000
ENTRYPOINT ["/local-session-tracker"]

# Build the login-protector binary
FROM scratch AS login-protector
FROM ghcr.io/cybozu/ubuntu-debug:22.04 AS login-protector
LABEL org.opencontainers.image.source="https://github.com/cybozu-go/login-protector"

COPY --from=build /go/bin/login-protector .
Expand Down
63 changes: 63 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
PROTECTOR_IMG ?= login-protector:dev
TRACKER_IMG ?= local-session-tracker:dev

TELEPORT_VERSION ?= 15.3.7

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
Expand Down Expand Up @@ -59,6 +61,10 @@ vet: ## Run go vet against code.
test-e2e: start-kind load-image deploy
go test ./test/e2e/ -v -ginkgo.v

.PHONY: test-teleport # Run the e2e tests with Teleport.
test-teleport: start-kind-teleport load-image deploy-teleport
go test ./test/teleport/ -v -ginkgo.v

.PHONY: lint
lint: setup ## Run golangci-lint linter & yamllint
golangci-lint run
Expand Down Expand Up @@ -112,6 +118,10 @@ endif
start-kind: setup
kind create cluster

.PHONY: start-kind-teleport
start-kind-teleport: setup
kind create cluster --config=./test/teleport/kind-config.yaml

.PHONY: stop-kind
stop-kind: setup
kind delete cluster
Expand All @@ -136,5 +146,58 @@ undeploy: setup ## Undeploy controller from the K8s cluster specified in ~/.kube

##@ Setup

.PHONY: setup
setup:
aqua install -l

##@ Teleport

.PHONY: update-teleport-manifests
update-teleport-manifests:
helm repo add teleport https://charts.releases.teleport.dev
helm repo update
helm template teleport --namespace teleport teleport/teleport-cluster \
--create-namespace \
--version $(TELEPORT_VERSION) \
--values ./test/teleport/cluster/values.yaml \
> ./test/teleport/cluster/teleport-cluster.yaml

.PHONY: setup-teleport-cli
setup-teleport-cli:
rm -rf teleport
wget https://cdn.teleport.dev/teleport-v$(TELEPORT_VERSION)-linux-amd64-bin.tar.gz
tar -xvf teleport-v$(TELEPORT_VERSION)-linux-amd64-bin.tar.gz
rm teleport-v$(TELEPORT_VERSION)-linux-amd64-bin.tar.gz

.PHONY: deploy-teleport
deploy-teleport:
# Setup Teleport Cluster
kubectl create namespace teleport
kustomize build ./test/teleport/cluster | kubectl apply -f -
kubectl -n teleport wait --for=condition=available --timeout=180s --all deployments

# Setup Teleport Node
TOKEN=$$(kubectl exec -it -n teleport deployment/teleport-auth -- tctl tokens add --type=node --format json | jq -r ".token") && \
sed -i "s/auth_token: .*/auth_token: $$TOKEN/g" ./test/teleport/node/teleport-secret.yaml
kustomize build ./test/teleport/node | kubectl apply -f -

# Deploy tbot
kubectl create namespace login-protector-system
kubectl apply -f ./test/teleport/tbot/rbac.yaml
kubectl exec -i -n teleport deployment/teleport-auth -- tctl create -f < ./test/teleport/role/member.yaml
kubectl exec -i -n teleport deployment/teleport-auth -- tctl create -f < ./test/teleport/role/api-access.yaml
kubectl exec -i -n teleport deployment/teleport-auth -- tctl create -f < ./test/teleport/tbot/bot.yaml
kubectl proxy -p 8080 &
sleep 3 # Wait for kubectl proxy to start
JWKS=$$(curl http://localhost:8080/openid/v1/jwks) && \
sed -i "s/ jwks: .*/ jwks: \'$$JWKS\'/g" ./test/teleport/tbot/token.yaml
kubectl exec -i -n teleport deployment/teleport-auth -- tctl create -f < ./test/teleport/tbot/token.yaml
kubectl apply -f ./test/teleport/tbot/configmap.yaml
kubectl apply -f ./test/teleport/tbot/deployment.yaml

# Deploy Login Protector with teleport-session-watcher
kustomize build ./config/teleport | kubectl apply -f -
kubectl -n login-protector-system wait --for=condition=available --timeout=180s --all deployments

kubectl get secret -n login-protector-system identity-output -o json | jq -r .data.identity | base64 -d > identity

1 change: 1 addition & 0 deletions aqua.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ packages:
- name: golangci/[email protected]
- name: tilt-dev/[email protected]
- name: tilt-dev/[email protected]
- name: helm/[email protected]
72 changes: 62 additions & 10 deletions cmd/login-protector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ package main
import (
"crypto/tls"
"flag"
"log"
"os"
"strings"
"time"

// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"

"github.com/cybozu-go/login-protector/internal/controller"
teleport_client "github.com/gravitational/teleport/api/client"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
Expand All @@ -20,6 +23,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"
//+kubebuilder:scaffold:imports
Expand All @@ -42,7 +46,11 @@ func main() {
var probeAddr string
var secureMetrics bool
var enableHTTP2 bool
var ttyCheckInterval time.Duration
var sessionCheckInterval time.Duration
var sessionWatcher string
var teleportIdentityFile string
var teleportNamespace string
var teleportAddrs 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 +60,11 @@ 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)")
flag.StringVar(&teleportIdentityFile, "teleport-identity-file", "", "The file path of Teleport Identity")
flag.StringVar(&teleportNamespace, "teleport-namespace", "teleport", "The namespace of Teleport")
flag.StringVar(&teleportAddrs, "teleport-addrs", "teleport-auth:3025", "The comma-separated list of Teleport addresses")
opts := zap.Options{
Development: true,
}
Expand Down Expand Up @@ -109,17 +121,57 @@ 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":
if teleportIdentityFile == "" {
setupLog.Error(nil, "teleport-identity-file is required for teleport session watcher")
os.Exit(1)
}
if teleportNamespace == "" {
setupLog.Error(nil, "teleport-namespace is required for teleport session watcher")
os.Exit(1)
}
if teleportAddrs == "" {
setupLog.Error(nil, "teleport-addrs is required for teleport session watcher")
os.Exit(1)
}
teleportClient, err := teleport_client.New(ctx, teleport_client.Config{
Addrs: strings.Split(teleportAddrs, ","),
Credentials: []teleport_client.Credentials{
teleport_client.LoadIdentityFile(teleportIdentityFile),
},
})

if err != nil {
log.Fatalf("failed to create client: %v", err)
}
defer teleportClient.Close() // nolint: errcheck
watcher = controller.NewTeleportSessionWatcher(
mgr.GetClient(),
teleportClient,
mgr.GetLogger().WithName("TeleportSessionWatcher"),
teleportNamespace,
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
4 changes: 4 additions & 0 deletions config/teleport/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resources:
- ../default
patchesStrategicMerge:
- ./manager.yaml
25 changes: 25 additions & 0 deletions config/teleport/manager.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: controller-manager
namespace: system
spec:
template:
spec:
containers:
- command:
- /login-protector
args:
- --leader-elect
- --session-watcher=teleport
- --teleport-identity-file=/etc/login-protector/identity
- --teleport-namespace=teleport
- --teleport-addrs=teleport-auth.teleport.svc.cluster.local:3025
name: manager
volumeMounts:
- mountPath: /etc/login-protector
name: identity
volumes:
- name: identity
secret:
secretName: identity-output
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