integration: use restricted RBAC credentials for some tests#1695
Conversation
|
For an explanation of what I'm scripting here see this article. |
maiamcc
left a comment
There was a problem hiding this comment.
I uh... THINK this lgtm? (def try to get some of the tests to go red to make sure you're testing what you think you're testing.)
this is all opaque and weird enough that i'd recommend a lotttt of comments in the setup code about what's going on and why we have to do it this way, otherwise this'll be hell on the next person who has to change it
|
|
||
| func (f *k8sFixture) getSecrets() { | ||
| outWriter := bytes.NewBuffer(nil) | ||
| cmdStr := `kubectl get secrets -n tilt-integration -o json | jq -r '.items[] | select(.metadata.name | startswith("tilt-integration-user-token-")) | .data.token'` |
There was a problem hiding this comment.
diff between these two blocks is not immediately apparent, consider comments for readability
| f.t.Fatalf("Error getting secrets: %v. Cmd: %s", err, cmdStr) | ||
| } | ||
|
|
||
| cmdStr = `kubectl get secrets -n tilt-integration -o json | jq -r '.items[] | select(.metadata.name | startswith("tilt-integration-user-token-")) | .data["ca.crt"]'` |
There was a problem hiding this comment.
if this kubectl command takes longer than a few hundred ms, might make sense to run the kubectl get secrets bit only once and run the output of that through jq -- i don't want to add multiple seconds of overhead to every integration test if we can help it
There was a problem hiding this comment.
This code is only called if you enable restricted credentials in the test, so it's opt in.
| func (f *k8sFixture) SetRestrictedCredentials() { | ||
| f.getSecrets() | ||
|
|
||
| err := ioutil.WriteFile("/tmp/cert", []byte(f.cert), 0644) |
There was a problem hiding this comment.
worth a note about why we have to write it to disk instead of passing it to the cmd
There was a problem hiding this comment.
...also it makes me a little sad that we're doing this unnecessarily for local integration tests, but 🤷♀
nicks
left a comment
There was a problem hiding this comment.
Have you considered making all the namespace setup into a separate reusable script, instead of baking it into the integration test fixture?
I expect we'll want to do this in other places. And other teams will want to use it for their tiltfile tests
|
@nicks I didn't, but I think that's a good idea! |
| f.t.Fatalf("Error writing cert: %v", err) | ||
| } | ||
| outWriter := bytes.NewBuffer(nil) | ||
| cmd := exec.CommandContext(f.ctx, "kubectl", "config", "set-credentials", "tilt-integration-user", "--client-certificate=/tmp/cert", fmt.Sprintf("--token=%s", f.token)) |
There was a problem hiding this comment.
modifying the user's kubeconfig seems very dangerous and likely to break something (e.g., if the user ctrl-c's a test)
have you considered setting the KUBECONFIG env var to use a custom kubeconfig?
There was a problem hiding this comment.
Yeah I think that would be a better approach and once I get this working at all I will do it that way instead.
| func (f *k8sFixture) SetRestrictedCredentials() { | ||
| f.getSecrets() | ||
|
|
||
| err := ioutil.WriteFile("/tmp/cert", []byte(f.cert), 0644) |
There was a problem hiding this comment.
are you aware of client-certificate-data so you can embed the data in the kubeconfig file?
There was a problem hiding this comment.
Yes, but you can't specify that via kubectl config set-credentials and I'm trying to avoid having to parse/write a kubeconfig.
I don't think I can avoid it though, tragically.
There was a problem hiding this comment.
have you tried kubectl config set? its instructions explicitly mention certificate-authority-data
There was a problem hiding this comment.
Aha, no! Thank you for the tip
| o.LabelSelector = ls.String() | ||
| })) | ||
|
|
||
| logger.Get(ctx).Infof("DERP namespace: %s", ns) |
There was a problem hiding this comment.
ignore this for now 😬
531d417 to
2bf4f1c
Compare
maiamcc
left a comment
There was a problem hiding this comment.
ooh this IS much better! 🙌
| func (f *k8sFixture) CreateNamespaceIfNecessary() { | ||
| outWriter := bytes.NewBuffer(nil) | ||
| cmd := exec.CommandContext(f.ctx, "kubectl", "apply", "-f", "namespace.yaml") | ||
| if f.usingOverriddenConfig { |
There was a problem hiding this comment.
instead of having to remember this every time you make a new Command object, consider an f.CommandContext(...) method that does this automatically (and maybe even passes the ctx for you). Otherwise, seems like a recipe of someone invoking a command wrong and breaking stuff in mysterious ways
| f.t.Fatalf("Error getting secrets: %v. Cmd: %s", err, cmdStr) | ||
| } | ||
|
|
||
| cmdStr = `kubectl get secrets -n tilt-integration -o json | jq -r '.items[] | select(.metadata.name | startswith("tilt-integration-user-token-")) | .data["ca.crt"]'` |
233ff6f to
b73e785
Compare
…that just set up the environment
This reverts commit abb5008.
No description provided.