-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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 API service agents in BootstrapPSARole()
utils
#12784
Support API service agents in BootstrapPSARole()
utils
#12784
Conversation
Some service agents are in the form `[email protected]` Support this via a hacky solution where if `@` is present, the interpolation of service accounts will be more limited. Thus either: `acctest.BootstrapPSARole(t, projectNumber, "@cloudservices", "roles/foo.bar")` (ab)using prefix, or `acctest.BootstrapPSARole(t, "", "NNNNNNNNNNN@cloudservices", "roles/foo.bar")` can be used for [email protected], for example. https://cloud.google.com/iam/docs/service-account-types#service-agents
Hello! I am a robot. Tests will require approval from a repository maintainer to run. @ScottSuarez, a repository maintainer, has been assigned to review your changes. If you have not received review feedback within 2 business days, please leave a comment on this PR asking them to take a look. You can help make sure that review is quick by doing a self-review and by running impacted tests locally. |
#12728 has more context on the reasoning behind this; totally get that it's hacky, and happy to make adjustments if someone has another idea. My thinking is that the history would be cleaner if this can be merged on its own, though we can close this if #12728 comes through first. Side note: there are some other bits like: magic-modules/mmv1/third_party/terraform/services/container/resource_container_cluster_test.go.tmpl Lines 9930 to 9942 in b5bdbf8
that might make some sense to move to the bootstrap pattern once this is supported. |
I am okay with this pattern @melinath, any resistance or thoughts? |
Hi there, I'm the Modular magician. I've detected the following information about your changes: Diff reportYour PR generated some diffs in downstreams - here they are.
|
I guess an alternative would be a set of |
Overall this seems like a reasonable thing to support in the bootstrap helper functions. I might be inclined to make the helper functions take an object instead of individual arguments at this point; then we could say things like "if no domain is specified, use the default" instead of having to introspect whether there's an (There are also some broader things that would be nice to change like: Removing PSA terminology, since folks aren't familiar with it, and maybe reducing the number of helper functions to reduce what users need to know about - but those are likely beyond the scope of this PR.) |
Yeah, 100% - this is probably a little clunky, and not ideal interface-wise, and also agree as a user who's not super steeped in all the internal terminology, something more generic would probably be easier to work with. An object could make some sense (and might make default behavior simpler?), and also just using an array all the time (for both the member(s) and role(s), ideally) vs. having both the array and string variants would be a bit tidier? I'd be happy to file a separate GH issue if it would help in tracking making some of these improvements. I'm guessing trying to change the interface too dramatically could get kind of messy, given the number of tests that would probably need adjustment? But I'm totally happy to make small adjustments or changes here if they can be done relatively simply. |
@@ -14,6 +15,9 @@ import ( | |||
// policy grants the given service agents the given roles. | |||
// prefix is usually "service-" and indicates the service agent should have the | |||
// given prefix before the project number. | |||
// If an address with an `@` (x@y) is passed, the address will be used | |||
// verbatim, but still prefixed with `serviceAccount:prefix` and suffixed with |
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.
In accordance with Stephen's suggestion. Wdyt about the following pattern? I can understand how it may get difficult to understand weird implicit logic. This change might make things more explicit overall at the consumer level without needing to know about special behavior for certain characters.
// ServiceAccountConfig holds the configuration for service account creation
type ServiceAccountConfig struct {
// Name of the service account
Name string
// Optional domain override. If empty, defaults to project's iam.gserviceaccount.com
Domain string
// Optional project number override. If empty, uses the current project number
ProjectNumber string
}
// BootstrapRoleConfig holds the configuration for bootstrapping IAM roles
type DesiredIAMRoles struct {
// Prefix to be added to all service account emails
Prefix string
// List of service account configurations
ServiceAccounts []ServiceAccountConfig
// IAM roles to be assigned
Roles []string
}
// BootstrapIAMRoles bootstraps IAM roles for service accounts
// Returns whether the bindings changed
func BootstrapIAMRoles(t *testing.T, config DesiredIAMRoles) bool {
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.
High level, this seems to be a much better design, albeit one I may not want to get too deep into implementing as an afternoon project 😅
Couple questions:
- So basically, new function, with the idea that we'd create this method, and then phase out the wrapper functions later? And it would still wrap
BootstrapAllPSARoles()
for now, just providing a cleaner interface, but with the idea of doing more structural fixes later? - It seems like there's really two basic common patterns, so like
${prefix}-${projectNumber}@foo.iam.gserviceaccount.com
(is this PSA?), where "prefix" is usuallyservice-
and then[project_number]@foo.gserviceaccount.com
(Google Service Agent?). So I guess in this example, you'd have prefix as nil or empty string to do the latter, and set it toservice-
for the former?
In fact, even in my current use case, could possibly hard-code the project number bit into the function, which would simplify my use case further. - If we're making naming it "IAM" vs. something specific to service accounts, maybe needs a param for whether to include the
serviceAccount:
prefix too? Not sure if that's too much YAGNI?
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.
- Exactly, or we can maintain some of the higher level wrappers if we feel they are applicable still. While allowing us to more flexibly use what's underneath.
- hmm good point on
{prefix}-${projectNumber}@foo.iam.gserviceaccount.com
. We might want to omit project number altogether from the ServiceAccountConfig object. With the existing calling into our function. I believe you are correct on PSA (Project Service Account) being this specific pattern. - That sound reasonable to me! I would prefer it to be optional but customizability is always good.
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.
Ok, let me see if I can come up with something that uses that pattern, and if it's possible to do this while keeping BootstrapAllPSARoles()
more or less as it is. It might delay #12728, which I've now rolled a fix for another breaking test into, so up to you if you want to ship that first or wait for this.
Ideally, there would be some tests as well, for the logic of constructing email addresses, at least, and maybe for some of the role related stuff... but that may also have to wait for some bigger project, especially since adding them with the way the functions are structured now might involve either adding some new functions, or figuring out how to stub some of the network calls.
I left a suggestion. If it's too complex of a change we can merge this as is and open a ticket! I think the number of changes could be rather small since we usually use wrapper functions to call this, but feel free to take a look. |
Tests analyticsTotal tests: 4449 Click here to see the affected service packages
Action takenFound 5 affected test(s) by replaying old test recordings. Starting RECORDING based on the most recent commit. Click here to see the affected tests
|
🟢 Tests passed during RECORDING mode: 🔴 Tests failed when rerunning REPLAYING mode: Tests failed due to non-determinism or randomness when the VCR replayed the response after the HTTP request was made. Please fix these to complete your PR. If you believe these test failures to be incorrect or unrelated to your change, or if you have any questions, please raise the concern with your reviewer. 🔴 Tests failed during RECORDING mode: 🔴 Errors occurred during RECORDING mode. Please fix them to complete your PR. |
Actually, after thinking this over some more, I think we should make an even more basic change... one of the struggles with the current implementation is that from the tests, it's pretty hard to understand what is actually being granted to what. I think we should stop doing templating at all (except for project_number replacement) and just allow a passthrough of the members being bootstrapped. I have some time & can take a stab at this today. |
@melinath Ok, thanks! I thought about this a little more last night, but haven't started doing anything... if you come up with something that's usable, that works great for me, and then I can adjust that other PR with the tests! I actually kind of like some of the magic here, because there really are only 2-3 patterns in the existing (and probably future?) use cases, but I get what you're saying as well. Hopefully these changes will produce an outcome that will be easier to understand / read for everyone, as well as be more in line with the overall style of the project. |
yeah, I think there's maybe room for additional helper functions to make it easier to build IAM member names, but that should be separate IMO from the bootstrapping logic. |
opened #12796 |
Some service agents are in the form
[email protected]
Support this via a hacky solution where if
@
is present, the interpolation of service accounts will be more limited. Thus either:acctest.BootstrapPSARole(t, projectNumber, "@cloudservices", "roles/foo.bar")
(ab)using prefix, oracctest.BootstrapPSARole(t, "", "NNNNNNNNNNN@cloudservices", "roles/foo.bar")
can be used for
[email protected]
, for example.https://cloud.google.com/iam/docs/service-account-types#service-agents
Release Note Template for Downstream PRs (will be copied)
See Write release notes for guidance.