-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support BigQuery dataset ACLs in absence of IAM endpoints
- Loading branch information
1 parent
5435aa2
commit c57b163
Showing
9 changed files
with
888 additions
and
310 deletions.
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,71 @@ | ||
package iamutil | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// NOTE: BigQuery does not conform to the typical REST for IAM policies | ||
// instead it has an access array with bindings on the dataset | ||
// object. https://cloud.google.com/bigquery/docs/reference/rest/v2/datasets#Dataset | ||
type AccessBinding struct { | ||
Role string `json:"role,omitempty"` | ||
UserByEmail string `json:"userByEmail,omitempty"` | ||
} | ||
|
||
type Dataset struct { | ||
Access []*AccessBinding `json:"access,omitempty"` | ||
} | ||
|
||
func (p *Policy) AsDataset() Dataset { | ||
dataset := Dataset{} | ||
if p == nil { | ||
return dataset | ||
} | ||
for _, binding := range p.Bindings { | ||
for _, member := range binding.Members { | ||
var email string | ||
memberSplit := strings.Split(member, ":") | ||
if len(memberSplit) == 2 { | ||
email = memberSplit[1] | ||
} else { | ||
email = member | ||
} | ||
|
||
if email != "" { | ||
dataset.Access = append(dataset.Access, &AccessBinding{ | ||
Role: binding.Role, | ||
UserByEmail: email, | ||
}) | ||
} | ||
} | ||
} | ||
return dataset | ||
} | ||
|
||
func (ds *Dataset) AsPolicy() Policy { | ||
policy := Policy{} | ||
if ds == nil { | ||
return policy | ||
} | ||
bindingMap := make(map[string]*Binding) | ||
for _, accessBinding := range ds.Access { | ||
email := fmt.Sprintf("serviceAccount:%s", accessBinding.UserByEmail) | ||
if binding, ok := bindingMap[accessBinding.Role]; ok { | ||
binding.Members = append(binding.Members, email) | ||
} else { | ||
bindingMap[accessBinding.Role] = &Binding{ | ||
Role: accessBinding.Role, | ||
Members: []string{email}, | ||
} | ||
} | ||
} | ||
for k := range bindingMap { | ||
policy.Bindings = append(policy.Bindings, bindingMap[k]) | ||
} | ||
return policy | ||
} | ||
|
||
func (r *parsedIamResource) IsBigqueryResource() bool { | ||
return r.config.TypeKey == "projects/datasets" && r.config.Service == "bigquery" | ||
} |
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,75 @@ | ||
package iamutil | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
) | ||
|
||
func TestPolicyToDataset(t *testing.T) { | ||
policy, expectedDataset := getTestFixtures() | ||
expectedDatasetBytes, err := json.Marshal(expectedDataset) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
actualDataset := policy.AsDataset() | ||
actualDatasetBytes, err := json.Marshal(actualDataset) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if string(actualDatasetBytes) != string(expectedDatasetBytes) { | ||
t.Fatalf("%v should be equal to %v", string(actualDatasetBytes), string(expectedDatasetBytes)) | ||
} | ||
} | ||
|
||
func TestDatasetToPolicy(t *testing.T) { | ||
expectedPolicy, dataset := getTestFixtures() | ||
expectedPolicyBytes, err := json.Marshal(expectedPolicy) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
actualPolicy := dataset.AsPolicy() | ||
actualPolicyBytes, err := json.Marshal(actualPolicy) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if string(actualPolicyBytes) != string(expectedPolicyBytes) { | ||
t.Fatalf("%v should be equal to %v", string(actualPolicyBytes), string(expectedPolicyBytes)) | ||
} | ||
} | ||
|
||
func getTestFixtures() (*Policy, *Dataset) { | ||
policy := &Policy{ | ||
Bindings: []*Binding{ | ||
&Binding{ | ||
Members: []string{ | ||
"serviceAccount:[email protected]", | ||
"serviceAccount:[email protected]", | ||
}, | ||
Role: "roles/bigquery.dataViewer", | ||
}, | ||
&Binding{ | ||
Members: []string{ | ||
"serviceAccount:[email protected]", | ||
}, | ||
Role: "roles/bigquery.dataOwner", | ||
}, | ||
}, | ||
} | ||
dataset := &Dataset{ | ||
Access: []*AccessBinding{ | ||
&AccessBinding{ | ||
Role: "roles/bigquery.dataViewer", | ||
UserByEmail: "[email protected]", | ||
}, | ||
&AccessBinding{ | ||
Role: "roles/bigquery.dataViewer", | ||
UserByEmail: "[email protected]", | ||
}, | ||
&AccessBinding{ | ||
Role: "roles/bigquery.dataOwner", | ||
UserByEmail: "[email protected]", | ||
}, | ||
}, | ||
} | ||
return policy, dataset | ||
} |
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.