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

Built-in role definitions should be skiped by asoctl #4263

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ func (extension *RoleAssignmentExtension) Import(
return extensions.ImportResult{}, err
}

// If this cast doesn't compile, update the `api` import to reference the now latest
// stable version of the authorization group (this will happen when we import a new
// API version in the generator.)
if assignment, ok := rsrc.(*api.RoleAssignment); ok {
// Check to see whether this role assignment is inherited or not
// (we can tell by looking at the scope of the assignment)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright (c) Microsoft Corporation.
Licensed under the MIT license.
*/

package customizations

import (
"context"
"strings"

api "github.com/Azure/azure-service-operator/v2/api/authorization/v1api20220401"
Copy link
Member

Choose a reason for hiding this comment

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

Does this need to be the storage version? (I checked other Import impls and it seems it doesn't, I guess this is because we call import on the latest public API shape not the latest storage shape which makes some sense...)

Assuming above is true and this doesn't need to be the storage version, how do we catch when we update to support a newer API version and this cast is no longer correct? We can do that statically by asserting the type impls the Hub interface, but there's no such static assertion here so the code will compile even though it doesn't work.

Do we have a test that covers this case?

Wondering if at they very least we should do something like:

// If you change this, make sure to also change the api import reference.
storage "github.com/Azure/azure-service-operator/v2/api/authorization/v1api20220401/storage"

...


// Type assert that we are the hub type. This will fail to compile if
// the hub type has been changed but this extension has not
var _ conversion.Hub = &storage.RoleDefinition{}

Copy link
Member

Choose a reason for hiding this comment

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

(I think the other imports could use this check too, or one like it - we could fabricate an interface on the latest API version of each resource and then avoid the weird indirection through the storage version for this if we prefer, but in some ways that feels even more awkward)

Copy link
Member Author

@theunrepentantgeek theunrepentantgeek Sep 15, 2024

Choose a reason for hiding this comment

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

The interface is only ever generated for a single version of any one resource - usually the latest stable version, though a preview version will be used if no stable version is available.

If we import a new stable version of a resource and that interface moves, we get a compilation error here in the extension because the cast from genruntime.ImportableResource can never succeed. It will look something like this:

# github.com/Azure/azure-service-operator/v2/api/authorization/customizations
..\..\api\authorization\customizations\role_assignment_extensions.go:31:23: impossible type assertion: rsrc.(*api.RoleAssignment)
        *v1api20200801preview.RoleAssignment does not implement genruntime.ImportableResource (missing method InitializeSpec)

To provoke this, I changed the import from authorization/v1api20220401 (which works) to authorization/v1api20200801preview.

So while we don't have an explicit type assertion, we still get the same level of protection.


"github.com/Azure/azure-service-operator/v2/pkg/genruntime"
"github.com/Azure/azure-service-operator/v2/pkg/genruntime/extensions"
)

var _ extensions.Importer = &RoleDefinitionExtension{}

func (extension *RoleDefinitionExtension) Import(
ctx context.Context,
rsrc genruntime.ImportableResource,
owner *genruntime.ResourceReference,
next extensions.ImporterFunc,
) (extensions.ImportResult, error) {
result, err := next(ctx, rsrc, owner)
if err != nil {
return extensions.ImportResult{}, err
}

// If this cast doesn't compile, update the `api` import to reference the now latest
// stable version of the authorization group (this will happen when we import a new
// API version in the generator.)
if definition, ok := rsrc.(*api.RoleDefinition); ok {
// If this role definition is built in, we don't need to export it
if definition.Spec.Type != nil {
if strings.EqualFold(*definition.Spec.Type, "BuiltInRole") {
return extensions.ImportSkipped("role definition is built-in"), nil
}
}
}

return result, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ func (extension *FlexibleServersConfigurationExtension) Import(
return extensions.ImportResult{}, err
}

// If this cast doesn't compile, update the `api` import to reference the now latest
// stable version of the authorization group (this will happen when we import a new
// API version in the generator.)
if config, ok := rsrc.(*api.FlexibleServersConfiguration); ok {
// Skip system defaults
if config.Spec.Source != nil && *config.Spec.Source == "system-default" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func (extension *FlexibleServersDatabaseExtension) Import(
owner *genruntime.ResourceReference,
next extensions.ImporterFunc,
) (extensions.ImportResult, error) {
// If this cast doesn't compile, update the `api` import to reference the now latest
// stable version of the authorization group (this will happen when we import a new
// API version in the generator.)
if server, ok := rsrc.(*api.FlexibleServersDatabase); ok {
if server.Spec.AzureName == "azure_maintenance" {
return extensions.ImportSkipped("azure_maintenance database is not accessible by users"), nil
Expand Down
Loading