Skip to content
This repository was archived by the owner on Dec 3, 2023. It is now read-only.

Commit 61e2d19

Browse files
authored
feat: support conditional policies (#110)
* Base implementation * Update with unit tests * lint * correct copyright date * lint * Revert removal of helper functions * use auto-value * reformat Binding.java and Condition.java * remove unnecessary dep * code format * add dep on com.google.code.findbugs in google-cloud-core * address comments * Clean up * respond to comments * address comments * format * address feedback * remove unnecessary null check * lint * address feedback * remove ImmutableList from Binding AutoValue surface * address feedback * split up unit test * use guava beta annotation * surface ImmutableList<> for Binding class. * use BetaApi from api.core * return as expected * partial addressing of feedback * address feedback pt2 * address remaining feedback * address one last feedback
1 parent 28c9859 commit 61e2d19

7 files changed

Lines changed: 682 additions & 79 deletions

File tree

google-cloud-core/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
<groupId>com.google.api</groupId>
2828
<artifactId>gax</artifactId>
2929
</dependency>
30+
<dependency>
31+
<groupId>com.google.auto.value</groupId>
32+
<artifactId>auto-value-annotations</artifactId>
33+
</dependency>
3034
<dependency>
3135
<groupId>com.google.protobuf</groupId>
3236
<artifactId>protobuf-java-util</artifactId>
@@ -83,6 +87,10 @@
8387
<artifactId>objenesis</artifactId>
8488
<scope>test</scope>
8589
</dependency>
90+
<dependency>
91+
<groupId>com.google.code.findbugs</groupId>
92+
<artifactId>jsr305</artifactId>
93+
</dependency>
8694
<dependency>
8795
<groupId>com.google.truth</groupId>
8896
<artifactId>truth</artifactId>
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud;
18+
19+
import static com.google.common.base.Predicates.in;
20+
import static com.google.common.base.Predicates.not;
21+
22+
import com.google.api.core.BetaApi;
23+
import com.google.auto.value.AutoValue;
24+
import com.google.common.base.Predicate;
25+
import com.google.common.collect.Collections2;
26+
import com.google.common.collect.ImmutableList;
27+
import com.google.common.collect.Lists;
28+
import java.util.Arrays;
29+
import java.util.Collection;
30+
import java.util.List;
31+
import javax.annotation.Nullable;
32+
33+
/**
34+
* Class for Identity and Access Management (IAM) policies. IAM policies are used to specify access
35+
* settings for Cloud Platform resources. A policy is a list of bindings. A binding assigns a set of
36+
* identities to a role, where the identities can be user accounts, Google groups, Google domains,
37+
* and service accounts. A role is a named list of permissions defined by IAM.
38+
*
39+
* @see <a href="https://cloud.google.com/iam/docs/reference/rest/v1/Policy">Policy</a>
40+
*/
41+
@BetaApi("This is a Beta API is not stable yet and may change in the future.")
42+
@AutoValue
43+
public abstract class Binding {
44+
/** Get IAM Policy Binding Role */
45+
public abstract String getRole();
46+
47+
/** Get IAM Policy Binding Members */
48+
public abstract ImmutableList<String> getMembers();
49+
50+
/** Get IAM Policy Binding Condition */
51+
@Nullable
52+
public abstract Condition getCondition();
53+
54+
/** Create a Binding.Builder from an existing Binding */
55+
public abstract Builder toBuilder();
56+
57+
/** Create a new Binding.Builder */
58+
public static Builder newBuilder() {
59+
List<String> emptyMembers = ImmutableList.of();
60+
return new AutoValue_Binding.Builder().setMembers(emptyMembers);
61+
}
62+
63+
@AutoValue.Builder
64+
public abstract static class Builder {
65+
/**
66+
* Set IAM Role for Policy Binding
67+
*
68+
* @throws NullPointerException if the role is null.
69+
*/
70+
public abstract Builder setRole(String role);
71+
72+
/**
73+
* Set IAM Members for Policy Binding
74+
*
75+
* @throws NullPointerException if a member is null.
76+
*/
77+
public abstract Builder setMembers(Iterable<String> members);
78+
79+
/** Set IAM Condition for Policy Binding */
80+
public abstract Builder setCondition(Condition condition);
81+
82+
/** Internal use to getMembers() in addMembers() and removeMembers() */
83+
abstract ImmutableList<String> getMembers();
84+
85+
/**
86+
* Add members to Policy Binding.
87+
*
88+
* @throws NullPointerException if a member is null.
89+
*/
90+
public Builder addMembers(String member, String... moreMembers) {
91+
ImmutableList.Builder<String> membersBuilder = ImmutableList.builder();
92+
membersBuilder.addAll(getMembers());
93+
membersBuilder.addAll(Lists.asList(member, moreMembers));
94+
setMembers(membersBuilder.build());
95+
return this;
96+
}
97+
98+
/**
99+
* Remove members to Policy Binding.
100+
*
101+
* @throws NullPointerException if a member is null.
102+
*/
103+
public Builder removeMembers(String... members) {
104+
Predicate<String> selectMembersNotInList = not(in(Arrays.asList(members)));
105+
Collection<String> filter = Collections2.filter(getMembers(), selectMembersNotInList);
106+
setMembers(filter);
107+
return this;
108+
}
109+
110+
public abstract Binding build();
111+
}
112+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud;
18+
19+
import com.google.api.core.BetaApi;
20+
import com.google.auto.value.AutoValue;
21+
22+
/**
23+
* Class for Identity and Access Management (IAM) policies. IAM policies are used to specify access
24+
* settings for Cloud Platform resources. A policy is a list of bindings. A binding assigns a set of
25+
* identities to a role, where the identities can be user accounts, Google groups, Google domains,
26+
* and service accounts. A role is a named list of permissions defined by IAM.
27+
*
28+
* @see <a href="https://cloud.google.com/iam/docs/reference/rest/v1/Policy">Policy</a>
29+
* @see <a href="https://cloud.google.com/iam/docs/conditions-overview">IAM Conditions</a>
30+
*/
31+
@BetaApi("This is a Beta API is not stable yet and may change in the future.")
32+
@AutoValue
33+
public abstract class Condition {
34+
/** Get IAM Policy Binding Condition Title */
35+
public abstract String getTitle();
36+
37+
/** Get IAM Policy Binding Condition Description */
38+
public abstract String getDescription();
39+
40+
/** Get IAM Policy Binding Condition Expression */
41+
public abstract String getExpression();
42+
43+
/** Create a new Condition.Builder from an existing Condition */
44+
public abstract Builder toBuilder();
45+
46+
/** Create a new Condition.Builder */
47+
public static Builder newBuilder() {
48+
return new AutoValue_Condition.Builder();
49+
}
50+
51+
@AutoValue.Builder
52+
public abstract static class Builder {
53+
/** Set IAM Policy Binding Condition Title */
54+
public abstract Builder setTitle(String title);
55+
56+
/** Set IAM Policy Binding Condition Description */
57+
public abstract Builder setDescription(String description);
58+
59+
/** Set IAM Policy Binding Condition Expression */
60+
public abstract Builder setExpression(String expression);
61+
62+
/** Build Builder which creates a Condition instance */
63+
public abstract Condition build();
64+
}
65+
}

0 commit comments

Comments
 (0)