Skip to content

Commit 450d9dd

Browse files
kolea2ad548
authored andcommitted
feat: backup level IAM (googleapis#450)
* feat: backup level IAM * docs * extract out common logic * code feedback * lint
1 parent 4247e5a commit 450d9dd

File tree

3 files changed

+373
-35
lines changed

3 files changed

+373
-35
lines changed

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminClient.java

Lines changed: 223 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,21 +1371,8 @@ public Policy getIamPolicy(String tableId) {
13711371
*/
13721372
@SuppressWarnings("WeakerAccess")
13731373
public ApiFuture<Policy> getIamPolicyAsync(String tableId) {
1374-
String name = NameUtil.formatTableName(projectId, instanceId, tableId);
1375-
1376-
GetIamPolicyRequest request = GetIamPolicyRequest.newBuilder().setResource(name).build();
1377-
1378-
final IamPolicyMarshaller marshaller = new IamPolicyMarshaller();
1379-
1380-
return ApiFutures.transform(
1381-
stub.getIamPolicyCallable().futureCall(request),
1382-
new ApiFunction<com.google.iam.v1.Policy, Policy>() {
1383-
@Override
1384-
public Policy apply(com.google.iam.v1.Policy proto) {
1385-
return marshaller.fromPb(proto);
1386-
}
1387-
},
1388-
MoreExecutors.directExecutor());
1374+
String tableName = NameUtil.formatTableName(projectId, instanceId, tableId);
1375+
return getResourceIamPolicy(tableName);
13891376
}
13901377

13911378
/**
@@ -1443,24 +1430,8 @@ public Policy setIamPolicy(String tableId, Policy policy) {
14431430
*/
14441431
@SuppressWarnings("WeakerAccess")
14451432
public ApiFuture<Policy> setIamPolicyAsync(String tableId, Policy policy) {
1446-
String name = NameUtil.formatTableName(projectId, instanceId, tableId);
1447-
final IamPolicyMarshaller marshaller = new IamPolicyMarshaller();
1448-
1449-
SetIamPolicyRequest request =
1450-
SetIamPolicyRequest.newBuilder()
1451-
.setResource(name)
1452-
.setPolicy(marshaller.toPb(policy))
1453-
.build();
1454-
1455-
return ApiFutures.transform(
1456-
stub.setIamPolicyCallable().futureCall(request),
1457-
new ApiFunction<com.google.iam.v1.Policy, Policy>() {
1458-
@Override
1459-
public Policy apply(com.google.iam.v1.Policy proto) {
1460-
return marshaller.fromPb(proto);
1461-
}
1462-
},
1463-
MoreExecutors.directExecutor());
1433+
String tableName = NameUtil.formatTableName(projectId, instanceId, tableId);
1434+
return setResourceIamPolicy(policy, tableName);
14641435
}
14651436

14661437
/**
@@ -1515,9 +1486,227 @@ public List<String> testIamPermission(String tableId, String... permissions) {
15151486
*/
15161487
@SuppressWarnings({"WeakerAccess"})
15171488
public ApiFuture<List<String>> testIamPermissionAsync(String tableId, String... permissions) {
1489+
String tableName = NameUtil.formatTableName(projectId, instanceId, tableId);
1490+
return testResourceIamPermissions(tableName, permissions);
1491+
}
1492+
1493+
/**
1494+
* Gets the IAM access control policy for the specified backup.
1495+
*
1496+
* <p>Sample code:
1497+
*
1498+
* <pre>{@code
1499+
* Policy policy = client.getBackupIamPolicy("my-cluster-id", "my-backup-id");
1500+
* for(Map.Entry<Role, Set<Identity>> entry : policy.getBindings().entrySet()) {
1501+
* System.out.printf("Role: %s Identities: %s\n", entry.getKey(), entry.getValue());
1502+
* }
1503+
* }</pre>
1504+
*
1505+
* @see <a
1506+
* href="https://cloud.google.com/bigtable/docs/access-control#iam-management-table">Table-level
1507+
* IAM management</a>
1508+
*/
1509+
@SuppressWarnings("WeakerAccess")
1510+
public Policy getBackupIamPolicy(String clusterId, String backupId) {
1511+
return ApiExceptions.callAndTranslateApiException(getBackupIamPolicyAsync(clusterId, backupId));
1512+
}
1513+
1514+
/**
1515+
* Asynchronously gets the IAM access control policy for the specified backup.
1516+
*
1517+
* <p>Sample code:
1518+
*
1519+
* <pre>{@code
1520+
* ApiFuture<Policy> policyFuture = client.getBackupIamPolicyAsync("my-cluster-id", "my-backup-id");
1521+
*
1522+
* ApiFutures.addCallback(policyFuture,
1523+
* new ApiFutureCallback<Policy>() {
1524+
* public void onSuccess(Policy policy) {
1525+
* for (Entry<Role, Set<Identity>> entry : policy.getBindings().entrySet()) {
1526+
* System.out.printf("Role: %s Identities: %s\n", entry.getKey(), entry.getValue());
1527+
* }
1528+
* }
1529+
*
1530+
* public void onFailure(Throwable t) {
1531+
* t.printStackTrace();
1532+
* }
1533+
* },
1534+
* MoreExecutors.directExecutor());
1535+
* }</pre>
1536+
*
1537+
* @see <a
1538+
* href="https://cloud.google.com/bigtable/docs/access-control#iam-management-table">Table-level
1539+
* IAM management</a>
1540+
*/
1541+
@SuppressWarnings("WeakerAccess")
1542+
public ApiFuture<Policy> getBackupIamPolicyAsync(String clusterId, String backupId) {
1543+
String backupName = NameUtil.formatBackupName(projectId, instanceId, clusterId, backupId);
1544+
return getResourceIamPolicy(backupName);
1545+
}
1546+
1547+
/**
1548+
* Replaces the IAM policy associated with the specified backup.
1549+
*
1550+
* <p>Sample code:
1551+
*
1552+
* <pre>{@code
1553+
* Policy newPolicy = client.setBackupIamPolicy("my-cluster-id", "my-backup-id",
1554+
* Policy.newBuilder()
1555+
* .addIdentity(Role.of("bigtable.user"), Identity.user("[email protected]"))
1556+
* .addIdentity(Role.of("bigtable.admin"), Identity.group("[email protected]"))
1557+
* .build());
1558+
* }</pre>
1559+
*
1560+
* @see <a
1561+
* href="https://cloud.google.com/bigtable/docs/access-control#iam-management-table">Table-level
1562+
* IAM management</a>
1563+
*/
1564+
@SuppressWarnings("WeakerAccess")
1565+
public Policy setBackupIamPolicy(String clusterId, String backupId, Policy policy) {
1566+
return ApiExceptions.callAndTranslateApiException(
1567+
setBackupIamPolicyAsync(clusterId, backupId, policy));
1568+
}
1569+
1570+
/**
1571+
* Asynchronously replaces the IAM policy associated with the specified backup.
1572+
*
1573+
* <p>Sample code:
1574+
*
1575+
* <pre>{@code
1576+
* ApiFuture<Policy> newPolicyFuture = client.setBackupIamPolicyAsync("my-cluster-id", "my-backup-id",
1577+
* Policy.newBuilder()
1578+
* .addIdentity(Role.of("bigtable.user"), Identity.user("[email protected]"))
1579+
* .addIdentity(Role.of("bigtable.admin"), Identity.group("[email protected]"))
1580+
* .build());
1581+
*
1582+
* ApiFutures.addCallback(newPolicyFuture,
1583+
* new ApiFutureCallback<Policy>() {
1584+
* public void onSuccess(Policy policy) {
1585+
* for (Entry<Role, Set<Identity>> entry : policy.getBindings().entrySet()) {
1586+
* System.out.printf("Role: %s Identities: %s\n", entry.getKey(), entry.getValue());
1587+
* }
1588+
* }
1589+
*
1590+
* public void onFailure(Throwable t) {
1591+
* t.printStackTrace();
1592+
* }
1593+
* },
1594+
* MoreExecutors.directExecutor());
1595+
* }</pre>
1596+
*
1597+
* @see <a
1598+
* href="https://cloud.google.com/bigtable/docs/access-control#iam-management-table">Table-level
1599+
* IAM management</a>
1600+
*/
1601+
@SuppressWarnings("WeakerAccess")
1602+
public ApiFuture<Policy> setBackupIamPolicyAsync(
1603+
String clusterId, String backupId, Policy policy) {
1604+
String backupName = NameUtil.formatBackupName(projectId, instanceId, clusterId, backupId);
1605+
return setResourceIamPolicy(policy, backupName);
1606+
}
1607+
1608+
/**
1609+
* Tests whether the caller has the given permissions for the specified backup. Returns a subset
1610+
* of the specified permissions that the caller has.
1611+
*
1612+
* <p>Sample code:
1613+
*
1614+
* <pre>{@code
1615+
* List<String> grantedPermissions = client.testBackupIamPermission("my-cluster-id", "my-backup-id",
1616+
* "bigtable.backups.restore", "bigtable.backups.delete");
1617+
* }</pre>
1618+
*
1619+
* System.out.println("Has restore access: " +
1620+
* grantedPermissions.contains("bigtable.backups.restore"));
1621+
*
1622+
* <p>System.out.println("Has delete access: " +
1623+
* grantedPermissions.contains("bigtable.backups.delete"));
1624+
*
1625+
* @see <a href="https://cloud.google.com/bigtable/docs/access-control#permissions">Cloud Bigtable
1626+
* permissions</a>
1627+
*/
1628+
@SuppressWarnings({"WeakerAccess"})
1629+
public List<String> testBackupIamPermission(
1630+
String clusterId, String backupId, String... permissions) {
1631+
return ApiExceptions.callAndTranslateApiException(
1632+
testBackupIamPermissionAsync(clusterId, backupId, permissions));
1633+
}
1634+
1635+
/**
1636+
* Asynchronously tests whether the caller has the given permissions for the specified backup.
1637+
* Returns a subset of the specified permissions that the caller has.
1638+
*
1639+
* <p>Sample code:
1640+
*
1641+
* <pre>{@code
1642+
* ApiFuture<List<String>> grantedPermissionsFuture = client.testBackupIamPermissionAsync("my-cluster-id", "my-backup-id",
1643+
* "bigtable.backups.restore", "bigtable.backups.delete");
1644+
*
1645+
* ApiFutures.addCallback(grantedPermissionsFuture,
1646+
* new ApiFutureCallback<List<String>>() {
1647+
* public void onSuccess(List<String> grantedPermissions) {
1648+
* System.out.println("Has restore access: " + grantedPermissions.contains("bigtable.backups.restore"));
1649+
* System.out.println("Has delete access: " + grantedPermissions.contains("bigtable.backups.delete"));
1650+
* }
1651+
*
1652+
* public void onFailure(Throwable t) {
1653+
* t.printStackTrace();
1654+
* }
1655+
* },
1656+
* MoreExecutors.directExecutor());
1657+
* }</pre>
1658+
*
1659+
* @see <a href="https://cloud.google.com/bigtable/docs/access-control#permissions">Cloud Bigtable
1660+
* permissions</a>
1661+
*/
1662+
@SuppressWarnings({"WeakerAccess"})
1663+
public ApiFuture<List<String>> testBackupIamPermissionAsync(
1664+
String clusterId, String backupId, String... permissions) {
1665+
String backupName = NameUtil.formatBackupName(projectId, instanceId, clusterId, backupId);
1666+
return testResourceIamPermissions(backupName, permissions);
1667+
}
1668+
1669+
private ApiFuture<Policy> getResourceIamPolicy(String name) {
1670+
GetIamPolicyRequest request = GetIamPolicyRequest.newBuilder().setResource(name).build();
1671+
1672+
final IamPolicyMarshaller marshaller = new IamPolicyMarshaller();
1673+
1674+
return ApiFutures.transform(
1675+
stub.getIamPolicyCallable().futureCall(request),
1676+
new ApiFunction<com.google.iam.v1.Policy, Policy>() {
1677+
@Override
1678+
public Policy apply(com.google.iam.v1.Policy proto) {
1679+
return marshaller.fromPb(proto);
1680+
}
1681+
},
1682+
MoreExecutors.directExecutor());
1683+
}
1684+
1685+
private ApiFuture<Policy> setResourceIamPolicy(Policy policy, String name) {
1686+
final IamPolicyMarshaller marshaller = new IamPolicyMarshaller();
1687+
1688+
SetIamPolicyRequest request =
1689+
SetIamPolicyRequest.newBuilder()
1690+
.setResource(name)
1691+
.setPolicy(marshaller.toPb(policy))
1692+
.build();
1693+
1694+
return ApiFutures.transform(
1695+
stub.setIamPolicyCallable().futureCall(request),
1696+
new ApiFunction<com.google.iam.v1.Policy, Policy>() {
1697+
@Override
1698+
public Policy apply(com.google.iam.v1.Policy proto) {
1699+
return marshaller.fromPb(proto);
1700+
}
1701+
},
1702+
MoreExecutors.directExecutor());
1703+
}
1704+
1705+
private ApiFuture<List<String>> testResourceIamPermissions(
1706+
String resourceName, String[] permissions) {
15181707
TestIamPermissionsRequest request =
15191708
TestIamPermissionsRequest.newBuilder()
1520-
.setResource(NameUtil.formatTableName(projectId, instanceId, tableId))
1709+
.setResource(resourceName)
15211710
.addAllPermissions(Arrays.asList(permissions))
15221711
.build();
15231712

0 commit comments

Comments
 (0)