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

Don't use global credentials in low-level methods #1309

Merged
merged 26 commits into from
Nov 16, 2020

Conversation

babbageclunk
Copy link
Member

@babbageclunk babbageclunk commented Nov 4, 2020

This is a refactoring to pull the use of global credentials up to the level of the reconcile loop for resources to enable switching where we look for credentials in a future PR. This is part of implementing #1173 based on the work in #1206.

This PR doesn't change the behaviour of the operator - the controller loops will still use global credentials. Making this mechanical change first in a separate PR to simplify reviewing.

Tips for reviewers - the first three commits are the most interesting ones, the others are basically just mechanical. Although the resource groups one was slightly fiddlier than the others.

How does this PR make you feel:
gif

These are now retrieved using config.GlobalCredentials(), which will
eventually be pushed further and further up the call stack, to make
implementing flexible auth easier.
All functions that used the global credentials now require creds
passed in - all callers are updated to pass in the global credentials.

Eventually this will push all creds-getting up to the level of the
reconcile loop so that we can change it to get them from somewhere
else.
This allows tests to pass their own credentials without having to set
the global ones.
...Other than in tests.

Credentials are now passed in when creating a Manager or
APIMgmtServiceManager.
@babbageclunk babbageclunk force-pushed the creds-refactor branch 6 times, most recently from 0347fd8 to 14d3b5f Compare November 10, 2020 01:50
babbageclunk and others added 17 commits November 10, 2020 16:29
Credentials are now passed in when constructing a Manager.
Credentials are now passed in when constructing a manager.
Credentials are now passed in when constructing a Manager.
Credentials are now passed in when constructing Managers.
...Other than in tests.

Credentials are now passed in when constructing a manager.

Eliminate global KeyVaultManager used in tests - now it's constructed
with credentials in the affected tests instead.
Credentials are now passed in when constructing a Client.
Credentials are now passed in when constructing managers/clients.
Credentials are now passed in when constructing the Client.
Credentials are now passed in when constructing a Client.
Credentials are now passed in when constructing a PollClient.
Credentials are now passed in when constructing a client or manager.
Credentials are now passed in when constructing managers.
Credentials are now passed in when constructing a manager.

Converted package level functions ListGroups, GetGroup,
DeleteAllGroupsWithPrefix and WaitForDeleteCompletion into methods on
AzureResourceGroupManager. There didn't seem to be a reason that they
were functions when they would now need credentials.
Credentials are now passed in when constructing a manager.
Credentials are now passed in when constructing the client.
Credentials are now passed in when constructing a client.
Credentials are now passed in when a client is constructed.
Credentials are now passed in when constructing a manager.
@babbageclunk babbageclunk changed the title [WIP] Don't use global credentials in low-level methods Don't use global credentials in low-level methods Nov 10, 2020
@babbageclunk babbageclunk marked this pull request as ready for review November 10, 2020 03:36
matthchr
matthchr previously approved these changes Nov 12, 2020
Copy link
Member

@matthchr matthchr left a comment

Choose a reason for hiding this comment

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

That was quite a PR to review!

pkg/resourcemanager/config/config.go Outdated Show resolved Hide resolved
controllers/suite_test.go Outdated Show resolved Hide resolved
pkg/resourcemanager/iam/authorizers.go Show resolved Hide resolved
main.go Show resolved Hide resolved
pkg/resourcemanager/config/credentials.go Outdated Show resolved Hide resolved
pkg/resourcemanager/iam/authorizers.go Outdated Show resolved Hide resolved
// TODO(creds-refactor): can we just use the tenant and client id from
// the creds here? Depends whether the client code uses them to
// override the creds.
func getObjectID(ctx context.Context, creds config.Credentials, tenantID string, clientID string) (*string, error) {
Copy link
Member

Choose a reason for hiding this comment

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

This function is used in 2 places. One of those places is CreateVaultWithAccessPolicies which as far as I can tell is only used in tests... may be worth moving to tests directly if that's really a test helper method not used at all in production code.

The other location is ParseAccessPolicy which seems to use a clientId/tenantId other than the one in the credentials (supplied by the user?)

Copy link
Member Author

Choose a reason for hiding this comment

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

I've removed the comment - I've left the method for now even though it's used from tests. If it was only used in the tests for keyvault I'd move it, but it's kind of tricky to share testing code between two packages.

pkg/resourcemanager/keyvaults/keyvault.go Outdated Show resolved Hide resolved
@@ -13,8 +13,6 @@ import (
"github.com/Azure/azure-service-operator/pkg/resourcemanager"
)

var AzureKeyVaultManager KeyVaultManager = &azureKeyVaultManager{}
Copy link
Member

Choose a reason for hiding this comment

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

Why remove this?

Copy link
Member

Choose a reason for hiding this comment

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

Instead just do var _ KeyVaultManager = &azureKeyVaultManager{} seems reasonable?

Copy link
Member Author

Choose a reason for hiding this comment

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

This was being used by code in other packages - I needed to remove the global variable because it needs to have creds when it's constructed. I'll reinstate the interface assertion part though!

@babbageclunk babbageclunk merged commit 8acdb61 into Azure:master Nov 16, 2020
@babbageclunk babbageclunk deleted the creds-refactor branch November 16, 2020 23:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants