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

✨ Envtest exports rest.Config for the secure endpoint of kube-apiserver #984

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 32 additions & 2 deletions pkg/envtest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,34 @@ type Environment struct {
// ControlPlane is the ControlPlane including the apiserver and etcd
ControlPlane integration.ControlPlane

// Config can be used to talk to the apiserver. It's automatically
// populated if not set using the standard controller-runtime config
// Config can be used to talk to the apiserver (insecure endpoint).
// It's automatically populated if not set using the standard controller-runtime config
// loading.
Config *rest.Config

// SecureConfig can be used to talk to the apiserver (secure endpoint).
// It's automatically populated if not set using the standard controller-runtime config
// loading. This just contains secure endpoint and tlsconfig (no authn info).
// To use this config, you have to configure kube-apiserver with some authn module(static token, basic auth, etc.)
// and set your authentication info to this config. For example:
//
// // basic authn plugin case
// te := &envtest.Environment{
// KubeAPIServerFlags: append(
// envtest.DefaultKubeAPIServerFlags,
// "--basic-auth-file=my-file", "--authorization-mode=RBAC",
Copy link
Member

Choose a reason for hiding this comment

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

@everpeace what about extending envtest to generate a token-auth-file with a token that is member of the system:masters group so this just works by default?

Also, a test would be great 🙃

If you do not have time to pick that up, we can also merge this as-is and I will address my comments in a follow-up.

Copy link
Author

Choose a reason for hiding this comment

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

@alvaroaleman Thank you for your review!!

If you do not have time to pick that up, we can also merge this as-is and I will address my comments in a follow-up.

sorry... I couldn't spare time to do this currently. I'm very glad if we take this way 🙇 🙇

Copy link
Contributor

Choose a reason for hiding this comment

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

@alvaroaleman As I understand it you suggestion is just to provide a static user in the token-auth-file? I think it would be better to have an option to add individual users (maybe in addition to a default) so that you can specify particular groups that the user may be a member of.

One example of where this may be useful is testing CSRs, where the API server uses the requesting parties credentials to become part of the CSR request. Typically here you'd want to be able to specify the exact groups that the user belongs to to be able to get the right groups plumbed into that CSR

Copy link
Member

Choose a reason for hiding this comment

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

@JoelSpeed you can just create a SA and then use impersonation for that. @DirectXMan12 is working on an updated version of this afaik

// ),
// }
// te.Start()
//
// cfg := rest.CopyConfig(te.SecureConfig)
// cfg.Username = "myname"
// cfg.Password = "mypassword"
//
// // This client can send a request as "myname" user.
// cli := client.New(cfg)
SecureConfig *rest.Config

// CRDInstallOptions are the options for installing CRDs.
CRDInstallOptions CRDInstallOptions

Expand Down Expand Up @@ -249,6 +272,13 @@ func (te *Environment) Start() (*rest.Config, error) {
QPS: 1000.0,
Burst: 2000.0,
}
te.SecureConfig = &rest.Config{
Host: fmt.Sprintf("%s:%d", te.ControlPlane.APIURL().Hostname(), te.ControlPlane.APIServer.SecurePort),
TLSClientConfig: te.ControlPlane.APIServer.TLSClientConfig,
// gotta go fast during tests -- we don't really care about overwhelming our test API server
QPS: 1000.0,
Burst: 2000.0,
}
}

log.V(1).Info("installing CRDs")
Expand Down
9 changes: 9 additions & 0 deletions pkg/internal/testing/integration/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"path/filepath"
"time"

"k8s.io/client-go/rest"

"sigs.k8s.io/controller-runtime/pkg/internal/testing/integration/addr"
"sigs.k8s.io/controller-runtime/pkg/internal/testing/integration/internal"
)
Expand All @@ -23,6 +25,9 @@ type APIServer struct {
// SecurePort is the additional secure port that the APIServer should listen on.
SecurePort int

// TLSconfig is tls configuration to connect to its secure endpoint.
TLSClientConfig rest.TLSClientConfig

// Path is the path to the apiserver binary.
//
// If this is left as the empty string, we will attempt to locate a binary,
Expand Down Expand Up @@ -157,6 +162,10 @@ func (s *APIServer) populateAPIServerCerts() error {
return err
}

s.TLSClientConfig = rest.TLSClientConfig{
CAData: ca.CA.CertBytes(),
}

return nil
}

Expand Down