2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
iampb "google.golang.org/genproto/googleapis/iam/v1"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAddIAMPolicyBindings(t *testing.T) {
|
|
|
|
someErr := errors.New("someErr")
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
projectsAPI stubProjectsAPI
|
|
|
|
input AddIAMPolicyBindingInput
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"successful set without new bindings": {
|
|
|
|
input: AddIAMPolicyBindingInput{
|
|
|
|
Bindings: []PolicyBinding{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"successful set with bindings": {
|
|
|
|
input: AddIAMPolicyBindingInput{
|
|
|
|
Bindings: []PolicyBinding{
|
|
|
|
{
|
|
|
|
ServiceAccount: "service-account",
|
|
|
|
Role: "role",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"retrieving iam policy fails": {
|
|
|
|
projectsAPI: stubProjectsAPI{
|
|
|
|
getPolicyErr: someErr,
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
"setting iam policy fails": {
|
|
|
|
projectsAPI: stubProjectsAPI{
|
|
|
|
setPolicyErr: someErr,
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
client := Client{
|
|
|
|
project: "project",
|
|
|
|
zone: "zone",
|
|
|
|
name: "name",
|
|
|
|
uid: "uid",
|
|
|
|
projectsAPI: tc.projectsAPI,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := client.addIAMPolicyBindings(ctx, tc.input)
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddIAMPolicy(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
binding PolicyBinding
|
|
|
|
policy *iampb.Policy
|
|
|
|
wantErr bool
|
|
|
|
wantPolicy *iampb.Policy
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"successful on empty policy": {
|
|
|
|
binding: PolicyBinding{
|
|
|
|
ServiceAccount: "service-account",
|
|
|
|
Role: "role",
|
|
|
|
},
|
|
|
|
policy: &iampb.Policy{
|
|
|
|
Bindings: []*iampb.Binding{},
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantPolicy: &iampb.Policy{
|
2022-03-22 11:03:15 -04:00
|
|
|
Bindings: []*iampb.Binding{
|
|
|
|
{
|
|
|
|
Role: "role",
|
|
|
|
Members: []string{"serviceAccount:service-account"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"successful on existing policy with different role": {
|
|
|
|
binding: PolicyBinding{
|
|
|
|
ServiceAccount: "service-account",
|
|
|
|
Role: "role",
|
|
|
|
},
|
|
|
|
policy: &iampb.Policy{
|
|
|
|
Bindings: []*iampb.Binding{
|
|
|
|
{
|
|
|
|
Role: "other-role",
|
|
|
|
Members: []string{"other-member"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantPolicy: &iampb.Policy{
|
2022-03-22 11:03:15 -04:00
|
|
|
Bindings: []*iampb.Binding{
|
|
|
|
{
|
|
|
|
Role: "other-role",
|
|
|
|
Members: []string{"other-member"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Role: "role",
|
|
|
|
Members: []string{"serviceAccount:service-account"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"successful on existing policy with existing role": {
|
|
|
|
binding: PolicyBinding{
|
|
|
|
ServiceAccount: "service-account",
|
|
|
|
Role: "role",
|
|
|
|
},
|
|
|
|
policy: &iampb.Policy{
|
|
|
|
Bindings: []*iampb.Binding{
|
|
|
|
{
|
|
|
|
Role: "role",
|
|
|
|
Members: []string{"other-member"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantPolicy: &iampb.Policy{
|
2022-03-22 11:03:15 -04:00
|
|
|
Bindings: []*iampb.Binding{
|
|
|
|
{
|
|
|
|
Role: "role",
|
|
|
|
Members: []string{"other-member", "serviceAccount:service-account"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"already a member": {
|
|
|
|
binding: PolicyBinding{
|
|
|
|
ServiceAccount: "service-account",
|
|
|
|
Role: "role",
|
|
|
|
},
|
|
|
|
policy: &iampb.Policy{
|
|
|
|
Bindings: []*iampb.Binding{
|
|
|
|
{
|
|
|
|
Role: "role",
|
|
|
|
Members: []string{"serviceAccount:service-account"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantPolicy: &iampb.Policy{
|
2022-03-22 11:03:15 -04:00
|
|
|
Bindings: []*iampb.Binding{
|
|
|
|
{
|
|
|
|
Role: "role",
|
|
|
|
Members: []string{"serviceAccount:service-account"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
addIAMPolicy(tc.policy, tc.binding)
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.True(proto.Equal(tc.wantPolicy, tc.policy))
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|