Skip to content

Commit

Permalink
fix(bigquery): cache grantable roles to avoid fetch multiple fetches …
Browse files Browse the repository at this point in the history
…which might cause timeout
  • Loading branch information
rahmatrhd committed Oct 12, 2023
1 parent 0b995ed commit 974181f
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions plugins/providers/bigquery/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ type Config struct {

crypto domain.Crypto
validator *validator.Validate

cachedDatasetGrantableRoles []string
cachedTableGrantableRoles []string
}

// NewConfig returns bigquery config struct
Expand Down Expand Up @@ -198,9 +201,10 @@ func (c *Config) validatePermission(value interface{}, resourceType string, clie
return nil, ErrInvalidPermissionConfig
}

ctx := context.TODO()
if resourceType == ResourceTypeDataset {
if !utils.ContainsString([]string{DatasetRoleReader, DatasetRoleWriter, DatasetRoleOwner}, permision) {
grantableRoles, err := client.getGrantableRolesForDataset(context.TODO())
grantableRoles, err := c.getGrantableRolesForDataset(ctx, client)
if err != nil {
if errors.Is(err, ErrEmptyResource) {
return nil, fmt.Errorf("cannot verify dataset permission: %v", permision)
Expand All @@ -213,7 +217,7 @@ func (c *Config) validatePermission(value interface{}, resourceType string, clie
}
}
} else if resourceType == ResourceTypeTable {
roles, err := client.getGrantableRolesForTables()
roles, err := c.getGrantableRolesForTables(ctx, client)
if err != nil {
if err == ErrEmptyResource {
return nil, ErrCannotVerifyTablePermission
Expand All @@ -231,3 +235,31 @@ func (c *Config) validatePermission(value interface{}, resourceType string, clie
configValue := Permission(permision)
return &configValue, nil
}

func (c *Config) getGrantableRolesForDataset(ctx context.Context, client *bigQueryClient) ([]string, error) {
if len(c.cachedDatasetGrantableRoles) > 0 {
return c.cachedDatasetGrantableRoles, nil
}

roles, err := client.getGrantableRolesForDataset(ctx)
if err != nil {
return nil, err
}

c.cachedDatasetGrantableRoles = roles
return roles, nil
}

func (c *Config) getGrantableRolesForTables(ctx context.Context, client *bigQueryClient) ([]string, error) {
if len(c.cachedTableGrantableRoles) > 0 {
return c.cachedTableGrantableRoles, nil
}

roles, err := client.getGrantableRolesForTables()
if err != nil {
return nil, err
}

c.cachedTableGrantableRoles = roles
return roles, nil
}

0 comments on commit 974181f

Please sign in to comment.