-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
k8s api auth proxy - add embedded client WhoIs API for Kubernetes auth proxy integration #4975
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
Open
shyam0904a
wants to merge
13
commits into
netbirdio:main
Choose a base branch
from
shyam0904a:feature/k8s-api-auth-proxy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3f73947
feat: add groups and userId to RemotePeerConfig for k8s auth proxy
shyam0904a f113e8b
feat: add kubeconfig CLI command for k8s auth proxy
shyam0904a e7f89ef
fix: update status_test.go for new AddPeer signature
shyam0904a a9ce130
fix: remove conflicting shorthand flag in kubeconfig command
shyam0904a a8ba428
embed client
shyam0904a 855326d
peer identity update, help in dynamic group updates
shyam0904a 5204970
fix: use strings.HasPrefix and add TLS warning in kubeconfig command
shyam0904a 731b8fe
fix: clone Groups slice and improve error message in WhoIs
shyam0904a 475706f
fix: use RLock for read-only GetPeerIdentityByIP and clone Groups slices
shyam0904a 0618370
perf: optimize AccountPeerGroupsLookup with pre-built reverse index
shyam0904a e0b2e91
docs: add docstrings to kubeconfig functions
shyam0904a 311e7c4
fix: clone groups slice in AddPeer for consistency
shyam0904a 21cda2c
Delete shared/management/proto/management.pb.go
shyam0904a File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,131 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "google.golang.org/grpc/status" | ||
|
|
||
| "github.com/netbirdio/netbird/client/proto" | ||
| ) | ||
|
|
||
| var ( | ||
| kubeconfigOutput string | ||
| kubeconfigCluster string | ||
| kubeconfigContext string | ||
| kubeconfigUser string | ||
| kubeconfigServer string | ||
| kubeconfigNamespace string | ||
| ) | ||
|
|
||
| var kubeconfigCmd = &cobra.Command{ | ||
| Use: "kubeconfig", | ||
| Short: "Generate kubeconfig for accessing Kubernetes via NetBird", | ||
| Long: `Generate a kubeconfig file that points to a Kubernetes cluster accessible via NetBird. | ||
|
|
||
| The generated kubeconfig uses a dummy bearer token for authentication when the | ||
| cluster's auth proxy is running in 'auth' mode. The actual authentication is | ||
| handled by the NetBird network - the auth proxy identifies users by their | ||
| NetBird peer IP and impersonates them in the Kubernetes API. | ||
|
|
||
| Example: | ||
| netbird kubeconfig --server https://k8s.example.netbird.cloud:6443 --cluster my-cluster | ||
| netbird kubeconfig --server https://10.100.0.1:6443 -o ~/.kube/netbird-config`, | ||
| RunE: kubeconfigFunc, | ||
| } | ||
|
|
||
| func init() { | ||
| kubeconfigCmd.Flags().StringVarP(&kubeconfigOutput, "output", "o", "", "Output file path (default: stdout)") | ||
| kubeconfigCmd.Flags().StringVar(&kubeconfigCluster, "cluster", "netbird-cluster", "Cluster name in kubeconfig") | ||
| kubeconfigCmd.Flags().StringVar(&kubeconfigContext, "context", "netbird", "Context name in kubeconfig") | ||
| kubeconfigCmd.Flags().StringVar(&kubeconfigUser, "user", "netbird-user", "User name in kubeconfig") | ||
| kubeconfigCmd.Flags().StringVar(&kubeconfigServer, "server", "", "Kubernetes API server URL (required)") | ||
| kubeconfigCmd.Flags().StringVar(&kubeconfigNamespace, "namespace", "default", "Default namespace") | ||
| _ = kubeconfigCmd.MarkFlagRequired("server") | ||
| } | ||
|
|
||
| // kubeconfigFunc generates a kubeconfig file for accessing Kubernetes via the NetBird auth proxy. | ||
| // It verifies NetBird connectivity and creates a kubeconfig with the appropriate server URL. | ||
| func kubeconfigFunc(cmd *cobra.Command, args []string) error { | ||
| ctx := context.Background() | ||
|
|
||
| // Get current NetBird status to verify connection | ||
| conn, err := DialClientGRPCServer(ctx, daemonAddr) | ||
| if err != nil { | ||
| cmd.PrintErrf("Warning: Could not connect to NetBird daemon: %v\n", err) | ||
| cmd.PrintErrln("Generating kubeconfig anyway, but make sure NetBird is running before using it.") | ||
| } else { | ||
| defer conn.Close() | ||
|
|
||
| resp, err := proto.NewDaemonServiceClient(conn).Status(ctx, &proto.StatusRequest{}) | ||
| if err != nil { | ||
| cmd.PrintErrf("Warning: Could not get NetBird status: %v\n", status.Convert(err).Message()) | ||
| } else if resp.Status != "Connected" { | ||
| cmd.PrintErrf("Warning: NetBird is not connected (status: %s)\n", resp.Status) | ||
| cmd.PrintErrln("Make sure to run 'netbird up' before using the generated kubeconfig.") | ||
| } | ||
| } | ||
|
|
||
| kubeconfig := generateKubeconfig(kubeconfigServer, kubeconfigCluster, kubeconfigContext, kubeconfigUser, kubeconfigNamespace) | ||
|
|
||
| if kubeconfigOutput == "" { | ||
| fmt.Println(kubeconfig) | ||
| return nil | ||
| } | ||
|
|
||
| // Expand ~ in path | ||
| if strings.HasPrefix(kubeconfigOutput, "~/") { | ||
| home, err := os.UserHomeDir() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to get home directory: %w", err) | ||
| } | ||
| kubeconfigOutput = filepath.Join(home, kubeconfigOutput[2:]) | ||
| } | ||
|
|
||
| // Create directory if needed | ||
| dir := filepath.Dir(kubeconfigOutput) | ||
| if err := os.MkdirAll(dir, 0700); err != nil { | ||
| return fmt.Errorf("failed to create directory %s: %w", dir, err) | ||
| } | ||
|
|
||
| if err := os.WriteFile(kubeconfigOutput, []byte(kubeconfig), 0600); err != nil { | ||
| return fmt.Errorf("failed to write kubeconfig: %w", err) | ||
| } | ||
|
|
||
| cmd.Printf("Kubeconfig written to %s\n", kubeconfigOutput) | ||
| cmd.PrintErrln("\nWarning: TLS verification is disabled (insecure-skip-tls-verify: true).") | ||
| cmd.PrintErrln("This is safe when traffic is encrypted via NetBird's WireGuard tunnel.") | ||
| cmd.Printf("\nTo use this kubeconfig:\n") | ||
| cmd.Printf(" export KUBECONFIG=%s\n", kubeconfigOutput) | ||
| cmd.Printf(" kubectl get nodes\n") | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // generateKubeconfig creates a kubeconfig YAML string with the given parameters. | ||
| // The config uses insecure-skip-tls-verify since traffic is encrypted via WireGuard. | ||
| func generateKubeconfig(server, cluster, context, user, namespace string) string { | ||
| return fmt.Sprintf(`apiVersion: v1 | ||
| kind: Config | ||
| clusters: | ||
| - cluster: | ||
| insecure-skip-tls-verify: true | ||
| server: %s | ||
| name: %s | ||
| contexts: | ||
| - context: | ||
| cluster: %s | ||
| namespace: %s | ||
| user: %s | ||
| name: %s | ||
| current-context: %s | ||
| users: | ||
| - name: %s | ||
| user: | ||
| token: netbird-auth-proxy | ||
| `, server, cluster, cluster, namespace, user, context, context, user) | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Security concern: TLS verification disabled by default.
Line 109 hardcodes
insecure-skip-tls-verify: truein the generated kubeconfig. This disables TLS certificate verification, which could expose users to man-in-the-middle attacks.Consider one of the following approaches:
--insecure-skip-tls-verify) and default tofalseThe dummy token on line 122 is acceptable per the PR description (auth handled by NetBird IP).
🤖 Prompt for AI Agents