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

feat: kubeconfig token passthrough authentication for WGE commands in CLI #2410

Merged
merged 17 commits into from
Jul 14, 2022

Conversation

jmickey
Copy link
Contributor

@jmickey jmickey commented Jul 6, 2022

Closes weaveworks/weave-gitops-enterprise#692
Closes weaveworks/weave-gitops-enterprise#582

What changed?

  • Move CLI config package out of internal as it needs to be accessed by higher level packages.
  • Rename the --kubeconfig flag on the gitops get clusters command as this now causes a conflict with the root --kubeconfig flag. The root flag is required in order to set the --kubeconfig flag within the flag package so that kubernetes-sigs/controller-runtime/client/config can find the kubeconfig if a custom location is passed.
  • Instantiate a *rest.Config from the kubeconfig, and generate a http.RoundTripper from the client-go/rest package. This http.RoundTripper will handle the authentication requirements, generating a transport.BearerToken and apply it to the request header.
  • Add token passthrough capabilities to the server. We don't want or need to validate the token, as the Kubernetes cluster will do this for us. Therefore if an Authorization header exists, we store the header within the current context. The token will then be applied to the KubeHTTPClient when requests are made to the management cluster.
  • Slight refactor of the usage of adapters.HTTPClient. The client is now instantiated with auth in main and passed to root, rather than passing a resty.Client. In the CmdRunE function for each command we configure the client with the client.ConfigureClientWithOptions method, which configures the client with the necessary information to complete a request (e.g. Endpoint, TLS config) and handles auth if enabled.

Why was this change made?

The gitops CLI doesn't currently support authentication and therefore all enterprise CLI commands fail.

How was this change implemented?

When the user issues a CLI command that requires authentication to the Weave GitOps Enterprise server they are able to supply either a username/password, or a kubeconfig file that is valid to the Kubernetes management cluster. If no username or password is provided, then we default to the kubeconfig with the following prededence:

  • --kubeconfig flag pointing at a file
  • KUBECONFIG environment variable pointing at a file
  • In-cluster config if running in cluster
  • $HOME/.kube/config if exists.

If a valid kubeconfig is located the CLI will generate a (client-go/rest) *rest.Config which is then used to generate a http.RoundTripper that will apply the necessary authentication information to the request (see: https://pkg.go.dev/k8s.io/client-go/rest#TransportFor).

Once the request is made and received by the server we check if the incoming request contains an Authorization header. If so, we store this token within the request context.Context and continue. This token is then extracted later when building the *rest.Config that will be used to make requests from the WGE server to the Kubernetes API on the management server.

How did you validate the change?

Change validated locally by running the cluster-service pointing to the demo cluster and issuing a command from the CLI to the local service. The CLI command returned successfully.

Release notes

  • BREAKING: The --kubeconfig flag on the gitops get clusters <cluster_name> command has been renamed to --print-kubeconfig
  • Enterprise customers can now authenticate to the Weave GitOps Enterprise API server when running CLI commands using a valid kubeconfig context for the management cluster which supplies an Authorization token.
  • gitops commands that require authentication now support the --kubeconfig <location> flag, allowing users to specify a custom kubeconfig location. The KUBECONFIG environment variable is also supported. If neither is specified $HOME/.kube/config will be used.

- The controller-runtime config package supports the --kubeconfig flag via the `flag` stdlib package.
- This change is made to avoid a conflict
- Pass config.Options to adapters.NewHTTPClient
@jmickey jmickey self-assigned this Jul 6, 2022
@jmickey jmickey marked this pull request as ready for review July 7, 2022 13:48
@jmickey jmickey requested review from a team July 7, 2022 14:58
pkg/kube/config_getter.go Outdated Show resolved Hide resolved
RootCmd now accepts *adapters.HTTPClient instead of *resty.Client.

The adapters.NewHTTPClient function now returns a barebones HTTPClient.
The *HTTPClient.EnableCLIAuth() configures an authFunc that is called
when the client is configured via the ConfigureClientWithOptions method
from within the PersistentPreRun in the RootCmd.

This removes unnecessary code duplication such as having to call
adapters.NewHttpClient in every command, and the need to configure
DisableAuth in unit tests, either with a CLI flag, or within the
config.Options object.
@foot
Copy link
Contributor

foot commented Jul 11, 2022

Whoop! Works for me, except w/ --insecure-skip-tls-verify :D, then SetTLSClientConfig seems to fail as its a k8s transport not an http transport:

2022/07/11 19:50:22.667805 ERROR RESTY current transport is not an *http.Transport instance

Copy link
Contributor

@foot foot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All looks good to me. 👌

@foot foot requested a review from a team July 14, 2022 09:45
@jmickey jmickey merged commit aa61365 into main Jul 14, 2022
@jmickey jmickey deleted the feat/cli-kubeconfig-auth branch July 14, 2022 10:09
This was referenced Jul 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team/pesto type/enhancement New feature or request
Projects
None yet
3 participants