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
Changes from 1 commit
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
@@ -0,0 +1,41 @@
/*
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 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
}
Loading