-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from perfectsense/feature/bucket-iam-policy
Bucket IAM Policy implementation
- Loading branch information
Showing
4 changed files
with
377 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright 2020, Perfect Sense, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package gyro.google.storage; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import com.google.api.services.storage.model.Policy; | ||
import gyro.core.resource.Diffable; | ||
import gyro.core.resource.Output; | ||
import gyro.core.resource.Updatable; | ||
import gyro.google.Copyable; | ||
|
||
/* | ||
The Bucket's IAM policy configuration. | ||
*/ | ||
public class BucketIamPolicy extends Diffable implements Copyable<Policy> { | ||
|
||
private List<BucketIamPolicyBinding> bindings; | ||
|
||
// Read-only | ||
private Integer version; | ||
private String resourceId; | ||
|
||
/** | ||
* The association between the policies' role and members who may assume that role. | ||
* | ||
* @subresource gyro.google.storage.BucketIamPolicyBinding | ||
*/ | ||
@Updatable | ||
public List<BucketIamPolicyBinding> getBindings() { | ||
if (bindings == null) { | ||
bindings = new ArrayList<>(); | ||
} | ||
return bindings; | ||
} | ||
|
||
public void setBindings(List<BucketIamPolicyBinding> bindings) { | ||
this.bindings = bindings; | ||
} | ||
|
||
/** | ||
* The ID of the resource to which this policy belongs. | ||
*/ | ||
@Output | ||
public String getResourceId() { | ||
return resourceId; | ||
} | ||
|
||
public void setResourceId(String resourceId) { | ||
this.resourceId = resourceId; | ||
} | ||
|
||
/** | ||
* The version of this policy. See also `Understanding Policies <https://cloud.google.com/iam/docs/policies#versions>`_. | ||
*/ | ||
@Output | ||
public Integer getVersion() { | ||
return version; | ||
} | ||
|
||
public void setVersion(Integer version) { | ||
this.version = version; | ||
} | ||
|
||
@Override | ||
public String primaryKey() { | ||
return ""; | ||
} | ||
|
||
@Override | ||
public void copyFrom(Policy model) { | ||
setVersion(model.getVersion()); | ||
setResourceId(model.getResourceId()); | ||
getBindings().clear(); | ||
if (model.getBindings() != null) { | ||
setBindings(model.getBindings().stream().map(binding -> { | ||
BucketIamPolicyBinding iamBinding = newSubresource(BucketIamPolicyBinding.class); | ||
iamBinding.copyFrom(binding); | ||
return iamBinding; | ||
}).collect(Collectors.toList()) | ||
); | ||
} | ||
} | ||
|
||
public Policy toPolicy() { | ||
return new Policy().setVersion(3).setResourceId(getResourceId()) | ||
.setBindings(getBindings().stream().map(BucketIamPolicyBinding::toBinding).collect(Collectors.toList())); | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
src/main/java/gyro/google/storage/BucketIamPolicyBinding.java
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,116 @@ | ||
/* | ||
* Copyright 2020, Perfect Sense, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package gyro.google.storage; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import com.google.api.services.storage.model.Expr; | ||
import com.google.api.services.storage.model.Policy; | ||
import gyro.core.resource.Diffable; | ||
import gyro.core.resource.Updatable; | ||
import gyro.core.validation.Required; | ||
import gyro.google.Copyable; | ||
|
||
/* | ||
* The Bucket's IAM policy binding configuration. | ||
*/ | ||
public class BucketIamPolicyBinding extends Diffable implements Copyable<Policy.Bindings> { | ||
|
||
private String role; | ||
private List<String> members; | ||
private BucketIamPolicyBindingCondition condition; | ||
|
||
/** | ||
* The role associated with this binding. (Required) | ||
*/ | ||
@Required | ||
public String getRole() { | ||
return role; | ||
} | ||
|
||
public void setRole(String role) { | ||
this.role = role; | ||
} | ||
|
||
/** | ||
* A list of identifiers for members who may assume the provided role. (Required) | ||
*/ | ||
@Required | ||
public List<String> getMembers() { | ||
if (members == null) { | ||
members = new ArrayList<>(); | ||
} | ||
return members.stream().sorted().collect(Collectors.toList()); | ||
} | ||
|
||
public void setMembers(List<String> members) { | ||
this.members = members; | ||
} | ||
|
||
/** | ||
* The condition object associated with this binding. | ||
* | ||
* @subresource gyro.google.storage.BucketIamPolicyBindingConfiguration | ||
*/ | ||
@Updatable | ||
public BucketIamPolicyBindingCondition getCondition() { | ||
return condition; | ||
} | ||
|
||
public void setCondition(BucketIamPolicyBindingCondition condition) { | ||
this.condition = condition; | ||
} | ||
|
||
@Override | ||
public String primaryKey() { | ||
return String.format( | ||
"with role '%s', members [ '%s' ] and condition '%s'", | ||
getRole(), | ||
String.join("','", getMembers()), | ||
(getCondition() == null) ? "" : getCondition().primaryKey()); | ||
} | ||
|
||
@Override | ||
public void copyFrom(Policy.Bindings model) { | ||
setRole(model.getRole()); | ||
setCondition(null); | ||
if (model.getCondition() != null) { | ||
Expr condition = model.getCondition(); | ||
BucketIamPolicyBindingCondition iamCondition = newSubresource(BucketIamPolicyBindingCondition.class); | ||
iamCondition.copyFrom(condition); | ||
setCondition(iamCondition); | ||
} | ||
|
||
getMembers().clear(); | ||
if (model.getMembers() != null) { | ||
setMembers(model.getMembers()); | ||
} | ||
} | ||
|
||
public Policy.Bindings toBinding() { | ||
Policy.Bindings policyBinding = new Policy.Bindings(); | ||
policyBinding.setMembers(getMembers()); | ||
policyBinding.setRole(getRole()); | ||
if (getCondition() != null) { | ||
policyBinding.setCondition(getCondition().toCondition()); | ||
} | ||
|
||
return policyBinding; | ||
} | ||
} |
85 changes: 85 additions & 0 deletions
85
src/main/java/gyro/google/storage/BucketIamPolicyBindingCondition.java
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,85 @@ | ||
/* | ||
* Copyright 2020, Perfect Sense, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package gyro.google.storage; | ||
|
||
import com.google.api.services.storage.model.Expr; | ||
import gyro.core.resource.Diffable; | ||
import gyro.core.resource.Updatable; | ||
import gyro.core.validation.Required; | ||
import gyro.google.Copyable; | ||
|
||
/* | ||
* The Bucket's IAM policy binding condition configuration. | ||
*/ | ||
public class BucketIamPolicyBindingCondition extends Diffable implements Copyable<Expr> { | ||
|
||
private String description; | ||
private String expression; | ||
private String title; | ||
|
||
/** | ||
* The description of the condition. | ||
*/ | ||
@Updatable | ||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
|
||
/** | ||
* The attribute-based logic expression. See also `Conditions Overview <https://cloud.google.com/iam/docs/conditions-overview#attributes>`_. (Required) | ||
*/ | ||
@Required | ||
public String getExpression() { | ||
return expression; | ||
} | ||
|
||
public void setExpression(String expression) { | ||
this.expression = expression; | ||
} | ||
|
||
/** | ||
* The title of the condition. (Required) | ||
*/ | ||
@Required | ||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
@Override | ||
public String primaryKey() { | ||
return String.format("with title '%s' and expression of '%s'", getTitle(), getExpression()); | ||
} | ||
|
||
@Override | ||
public void copyFrom(Expr model) { | ||
setDescription(model.getDescription()); | ||
setExpression(model.getExpression()); | ||
setTitle(model.getTitle()); | ||
} | ||
|
||
public Expr toCondition() { | ||
return new Expr().setDescription(getDescription()).setExpression(getExpression()).setTitle(getTitle()); | ||
} | ||
} |
Oops, something went wrong.