-
Notifications
You must be signed in to change notification settings - Fork 21
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
feat: initial implementation of metabase group migration into guardian #167
Draft
singhvikash11
wants to merge
21
commits into
main
Choose a base branch
from
metabase_migration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
47c3881
feat: adding table, group as metabase resource and add groups-databas…
singhvikash11 61580ff
fix: fix test case
singhvikash11 bf17893
Fix test case
singhvikash11 16f517b
Add constants for string literal
singhvikash11 e79c96e
Change type for database and collection in group
singhvikash11 2d93d9c
Resolve feedback
singhvikash11 4257dd2
fix:
singhvikash11 1c1f807
Add grant and revoke method for group, table
singhvikash11 f8e6386
Fix test
singhvikash11 eda6c8c
Fix lint
singhvikash11 76c6510
Resolve feedback and add fetch database/collection while fetching groups
singhvikash11 5d01b7c
fix: custom group naming convention for metabase provider and filter …
singhvikash11 1268389
fix: custom group naming convention for metabase provider and filter …
singhvikash11 15ba0eb
Merge remote-tracking branch 'origin/metabase_resources' into metabas…
singhvikash11 207ac02
fix: resolve merge conflict
singhvikash11 38ffeed
fix: update the log to use key/value pair logging
singhvikash11 158ea99
feat: initial implementation of metabase group migration into guardian
singhvikash11 3293d72
Merge branch 'metabase_resources' into metabase_migration to enable m…
singhvikash11 2dff81c
Update doc of Metabase resources and add `member` as role in group
singhvikash11 291c036
Merge branch 'metabase_resources' into metabase_migration
singhvikash11 55af1a2
Add migrate command to migrate access for a given provider
singhvikash11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/MakeNowJust/heredoc" | ||
. "github.com/odpf/guardian/api/proto/odpf/guardian/v1beta1" | ||
"github.com/odpf/guardian/domain" | ||
"github.com/odpf/guardian/internal/crypto" | ||
"github.com/odpf/guardian/plugins/migrations" | ||
mb "github.com/odpf/guardian/plugins/migrations/metabase" | ||
"github.com/spf13/cobra" | ||
"google.golang.org/protobuf/types/known/structpb" | ||
) | ||
|
||
const ( | ||
pending = "pending" | ||
active = "active" | ||
) | ||
|
||
func MigrationCmd(config *Config) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "migrate", | ||
Short: "Guardian migration", | ||
Long: heredoc.Doc(` | ||
Migrate target system ACL into Guardian. | ||
`), | ||
Example: heredoc.Doc(` | ||
$ guardian migration <provider-urn> | ||
`), | ||
Annotations: map[string]string{ | ||
"group:core": "true", | ||
}, | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
providerId := args[0] | ||
|
||
say := crypto.NewAES(config.EncryptionSecretKey) | ||
client, cancel, err := createClient(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
defer cancel() | ||
|
||
context := cmd.Context() | ||
provider, err := getProviderConfig(client, context, providerId, say) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
resources, err := getResources(client, context, provider) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
appealResponse, appeals, err := getActiveAndPendingAppeals(client, context, provider) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var migration migrations.Client | ||
if provider.Type == migrations.Metabase { | ||
migration = mb.NewMigration(provider.Config, resources, appeals) | ||
} else { | ||
return errors.New(fmt.Sprintf("Migration not supported for provider %s", provider.Type)) | ||
} | ||
|
||
appealRequests, err := migration.PopulateAccess() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
//migrate past-run pending appeals | ||
for _, a := range appealResponse { | ||
if a.Status == pending { | ||
err := approveAppeal(a, client, context) | ||
if err != nil { | ||
return err | ||
} else { | ||
fmt.Println(a.Resource.Name, a.AccountId) | ||
} | ||
} | ||
} | ||
|
||
//migrate pending appeals | ||
for _, appealRequest := range appealRequests { | ||
resource := appealRequest.Resource | ||
option, _ := structpb.NewStruct(map[string]interface{}{migrations.Duration: resource.Duration}) | ||
|
||
accountID := appealRequest.AccountID | ||
appeal, err := client.CreateAppeal(context, &CreateAppealRequest{ | ||
AccountId: accountID, | ||
Resources: []*CreateAppealRequest_Resource{ | ||
{Id: resource.ID, Role: resource.Role, Options: option}}, | ||
AccountType: "", | ||
}) | ||
if err != nil { | ||
return err | ||
} else { | ||
appeals := appeal.GetAppeals() | ||
for _, appeal := range appeals { | ||
err := approveAppeal(appeal, client, context) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
bindFlagsFromConfig(cmd) | ||
|
||
return cmd | ||
} | ||
|
||
func getActiveAndPendingAppeals(client GuardianServiceClient, context context.Context, provider *domain.Provider) ([]*Appeal, []domain.Appeal, error) { | ||
appeals := make([]domain.Appeal, 0) | ||
listAppeals, err := client.ListAppeals(context, &ListAppealsRequest{ProviderUrns: []string{provider.URN}, Statuses: []string{pending, active}}) | ||
if err != nil { | ||
return nil, appeals, err | ||
} | ||
|
||
appealResponses := listAppeals.GetAppeals() | ||
for _, a := range appealResponses { | ||
appeals = append(appeals, domain.Appeal{ | ||
ID: a.Id, | ||
ResourceID: a.ResourceId, | ||
Status: a.Status, | ||
AccountID: a.AccountId, | ||
AccountType: a.AccountType, | ||
Role: a.Role, | ||
}) | ||
} | ||
return appealResponses, appeals, nil | ||
} | ||
|
||
func getResources(client GuardianServiceClient, context context.Context, provider *domain.Provider) ([]domain.Resource, error) { | ||
listResources, err := client.ListResources(context, &ListResourcesRequest{ProviderUrn: provider.URN, IsDeleted: false}) | ||
resources := make([]domain.Resource, 0) | ||
if err != nil { | ||
return resources, err | ||
} | ||
for _, r := range listResources.GetResources() { | ||
resources = append(resources, domain.Resource{ | ||
ID: r.Id, | ||
ProviderType: r.ProviderType, | ||
ProviderURN: r.ProviderUrn, | ||
Type: r.Type, | ||
URN: r.Urn, | ||
Name: r.Name, | ||
}) | ||
} | ||
return resources, nil | ||
} | ||
|
||
func getProviderConfig(client GuardianServiceClient, context context.Context, providerId string, say *crypto.AES) (*domain.Provider, error) { | ||
providerResponse, err := client.GetProvider(context, &GetProviderRequest{Id: providerId}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
provider := providerResponse.GetProvider() | ||
fields := provider.Config.Credentials.GetStructValue().GetFields() | ||
abc, err := say.Decrypt(fields[migrations.Password].GetStringValue()) | ||
|
||
return &domain.Provider{ | ||
ID: providerId, | ||
Type: provider.Type, | ||
URN: provider.Urn, | ||
Config: &domain.ProviderConfig{ | ||
Type: provider.Config.Type, | ||
URN: provider.Config.Urn, | ||
Credentials: map[string]string{ | ||
migrations.Username: fields[migrations.Username].GetStringValue(), | ||
migrations.Password: abc, | ||
migrations.Host: fields[migrations.Host].GetStringValue(), | ||
}, | ||
}, | ||
}, err | ||
} | ||
|
||
func approveAppeal(appeal *Appeal, client GuardianServiceClient, context context.Context) error { | ||
approvals := appeal.Approvals | ||
for _, approval := range approvals { | ||
if approval.Status == pending { | ||
_, err := client.UpdateApproval(context, &UpdateApprovalRequest{ | ||
Id: appeal.Id, | ||
ApprovalName: approval.Name, | ||
Action: &UpdateApprovalRequest_Action{Action: "approve", Reason: "Metabase migration"}, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@rahmatrhd @singhvikash11 I think we need to think of a better approach than attaching it on migrate. One possible suggestion could be to call it import and scope it with the provider itself.
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.
@ravisuhag this is a good idea, we need to implement migrate for bigquery provider and other providers too. Then we could expose it as a sub-command on the provider
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.
@ravisuhag @rahmatrhd If you folks are okay with Metabase migrate for now then I can move this as sub-command to provider.
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.
@singhvikash11 What do you mean okay with metabase provider?
Also, i would recommend checking out https://github.com/GoogleCloudPlatform/terraformer for some inspiration on how command could look like.
what do you guys think? cc @rahmatrhd
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.
@ravisuhag we already have the command
guardian provider create
command to register the resources with guardian. Let's use the following command as suggested by you to import ACL of existing resources from a source system:guardian provider import <provide-name> acl
guardian provider import <provide-name> acl -r <resources>
what do you guys think? cc @rahmatrhd
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.
@singhvikash11 What does
acl
represent in this command?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.
@ravisuhag Bigquery, Metabase resources like table, dataset, collection etc are defined by a set of permissions associated with a set of people, groups, service-account in the source system. By importing access-control of these resources into our system we'll replicate it in the model of appeal-resource in Guardian.
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.
@singhvikash11 Yes, understood. But what i meant is that we don't need acl word in the comamnd right? the command can plainly be
guardian provider import <provide-name> -r <resources>
where-r
flag will take the kind of resources you want to import from that provider.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.
@ravisuhag This makes sense, we can go ahead with this command.
cc @rahmatrhd