public void TestAccess()
        {
            var service = InitializeService();

            var role1          = CreateCustomRole(service);
            var role1NameShort = ParseRoleName(role1);

            try
            {
                var role2          = CreateCustomRole(service);
                var role2NameShort = ParseRoleName(role2);
                try
                {
                    // Test GetPolicy
                    var policy = AccessManager.GetPolicy(_project);

                    // Test AddBinding by adding _member1 to role1
                    policy = AccessManager.AddBinding(policy, role1NameShort, _member1);

                    // Test AddMember by adding _member2 to role1
                    policy = AccessManager.AddMember(policy, role1NameShort, _member2);

                    // Test RemoveMember where role binding doesn't exist (_member1 from role2)
                    policy = AccessManager.RemoveMember(policy, role2NameShort, _member1);

                    // Test RemoveMember where member doesn't exist (_member3 from role1)
                    policy = AccessManager.RemoveMember(policy, role1NameShort, _member3);

                    // Test RemoveMember by removing _member1 from role1
                    policy = AccessManager.RemoveMember(policy, role1NameShort, _member1);

                    // Test RemoveMember when removing last member from binding (_member2 from role1)
                    policy = AccessManager.RemoveMember(policy, role1NameShort, _member2);

                    // Test SetPolicy
                    policy = AccessManager.SetPolicy(_project, policy);
                }
                finally
                {
                    // Delete custom roles
                    service.Projects.Roles.Delete(role2.Name).Execute();
                }
            }
            finally
            {
                service.Projects.Roles.Delete(role1.Name).Execute();
            }
        }
 public void TestPermissions()
 {
     var permissions = AccessManager.TestIamPermissions(_project);
 }
Exemplo n.º 3
0
        public void TestAccess()
        {
            // Initializing service for role creation
            var credential = GoogleCredential.GetApplicationDefault()
                             .CreateScoped(IamService.Scope.CloudPlatform);
            var service = new IamService(new IamService.Initializer
            {
                HttpClientInitializer = credential
            });

            // Create custom roles for testing
            var role1 = new Role
            {
                Title               = "C# Test Custom Role",
                Description         = "Role for AccessTest",
                IncludedPermissions = new List <string> {
                    "iam.roles.get"
                },
                Stage = "GA"
            };

            var request = new CreateRoleRequest
            {
                Role   = role1,
                RoleId = "csharpTestCustomRole" + new Random().Next()
            };

            role1 = service.Projects.Roles.Create(request, "projects/" + _project).Execute();
            try
            {
                var role1NameComponents = role1.Name.Split('/');
                var role1NameShort      = role1NameComponents[2] + "/" + role1NameComponents[3];

                var role2 = new Role
                {
                    Title               = "C# Test Custom Role",
                    Description         = "Role for AccessTest",
                    IncludedPermissions = new List <string> {
                        "iam.roles.get"
                    },
                    Stage = "GA"
                };

                request = new CreateRoleRequest
                {
                    Role   = role2,
                    RoleId = "csharpTestCustomRole" + new Random().Next()
                };

                role2 = service.Projects.Roles.Create(request, "projects/" + _project).Execute();
                try
                {
                    var role2NameComponents = role2.Name.Split('/');
                    var role2NameShort      = role2NameComponents[2] + "/" + role2NameComponents[3];


                    // Test GetPolicy
                    var policy = AccessManager.GetPolicy(_project);

                    // Test AddBinding by adding _member1 to role1
                    policy = AccessManager.AddBinding(policy, role1NameShort, _member1);

                    // Test AddMember by adding _member2 to role1
                    policy = AccessManager.AddMember(policy, role1NameShort, _member2);

                    // Test RemoveMember where role binding doesn't exist (_member1 from role2)
                    policy = AccessManager.RemoveMember(policy, role2NameShort, _member1);

                    // Test RemoveMember where member doesn't exist (_member3 from role1)
                    policy = AccessManager.RemoveMember(policy, role1NameShort, _member3);

                    // Test RemoveMember by removing _member1 from role1
                    policy = AccessManager.RemoveMember(policy, role1NameShort, _member1);

                    // Test RemoveMember when removing last member from binding (_member2 from role1)
                    policy = AccessManager.RemoveMember(policy, role1NameShort, _member2);

                    // Test SetPolicy
                    policy = AccessManager.SetPolicy(_project, policy);
                }
                finally
                {
                    // Delete custom roles
                    service.Projects.Roles.Delete(role2.Name).Execute();
                }
            }
            finally
            {
                service.Projects.Roles.Delete(role1.Name).Execute();
            }
        }