Skip to content

Commit

Permalink
fix(IAM Policy Management): add nested conditions to rule.conditions (#…
Browse files Browse the repository at this point in the history
…221)

* fix(IAM Policy Management): Added nest conditions to rule.conditions

Signed-off-by: Shaun Colley <[email protected]>
  • Loading branch information
swcolley authored Nov 7, 2023
1 parent 067e461 commit 9753e83
Show file tree
Hide file tree
Showing 12 changed files with 618 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@
import com.ibm.cloud.platform_services.iam_policy_management.v1.model.V2PolicyTemplateMetaData;
import com.ibm.cloud.platform_services.iam_policy_management.v1.model.V2PolicyResourceAttribute;
import com.ibm.cloud.platform_services.iam_policy_management.v1.model.V2PolicyResourceTag;
import com.ibm.cloud.platform_services.iam_policy_management.v1.model.RuleAttributeWithConditions;
import com.ibm.cloud.platform_services.iam_policy_management.v1.model.NestedCondition;
import com.ibm.cloud.platform_services.iam_policy_management.v1.model.NestedConditionRuleAttribute;
import com.ibm.cloud.platform_services.iam_policy_management.v1.model.Control;
import com.ibm.cloud.platform_services.iam_policy_management.v1.model.Grant;
import com.ibm.cloud.platform_services.iam_policy_management.v1.model.V2PolicyResource;
import com.ibm.cloud.platform_services.iam_policy_management.v1.model.V2PolicyRuleRuleWithConditions;
import com.ibm.cloud.platform_services.iam_policy_management.v1.model.V2PolicyRuleRuleWithNestedConditions;
import com.ibm.cloud.platform_services.iam_policy_management.v1.model.V2PolicySubject;
import com.ibm.cloud.platform_services.iam_policy_management.v1.model.V2PolicyCollection;
import com.ibm.cloud.platform_services.iam_policy_management.v1.model.ReplaceV2PolicyOptions;
Expand Down Expand Up @@ -394,27 +395,27 @@ public static void main(String[] args) throws Exception {
.grant(policyGrant)
.build();

RuleAttributeWithConditions weeklyConditionAttribute = new RuleAttributeWithConditions.Builder()
NestedConditionRuleAttribute weeklyConditionAttribute = new NestedConditionRuleAttribute.Builder()
.key("{{environment.attributes.day_of_week}}")
.value(new ArrayList<String>(Arrays.asList("1+00:00", "2+00:00", "3+00:00", "4+00:00", "5+00:00")))
.operator("dayOfWeekAnyOf")
.build();

RuleAttributeWithConditions startConditionAttribute = new RuleAttributeWithConditions.Builder()
NestedConditionRuleAttribute startConditionAttribute = new NestedConditionRuleAttribute.Builder()
.key("{{environment.attributes.current_time}}")
.value("09:00:00+00:00")
.operator("timeGreaterThanOrEquals")
.build();

RuleAttributeWithConditions endConditionAttribute = new RuleAttributeWithConditions.Builder()
NestedConditionRuleAttribute endConditionAttribute = new NestedConditionRuleAttribute.Builder()
.key("{{environment.attributes.current_time}}")
.value("17:00:00+00:00")
.operator("timeLessThanOrEquals")
.build();

V2PolicyRuleRuleWithConditions policyRule = new V2PolicyRuleRuleWithConditions.Builder()
V2PolicyRuleRuleWithNestedConditions policyRule = new V2PolicyRuleRuleWithNestedConditions.Builder()
.operator("and")
.conditions(new ArrayList<RuleAttributeWithConditions>(Arrays.asList(weeklyConditionAttribute, startConditionAttribute, endConditionAttribute)))
.conditions(new ArrayList<NestedCondition>(Arrays.asList(weeklyConditionAttribute, startConditionAttribute, endConditionAttribute)))
.build();

CreateV2PolicyOptions options = new CreateV2PolicyOptions.Builder()
Expand Down Expand Up @@ -511,27 +512,27 @@ public static void main(String[] args) throws Exception {
.grant(policyGrant)
.build();

RuleAttributeWithConditions weeklyConditionAttribute = new RuleAttributeWithConditions.Builder()
NestedConditionRuleAttribute weeklyConditionAttribute = new NestedConditionRuleAttribute.Builder()
.key("{{environment.attributes.day_of_week}}")
.value(new ArrayList<String>(Arrays.asList("1+00:00", "2+00:00", "3+00:00", "4+00:00", "5+00:00")))
.operator("dayOfWeekAnyOf")
.build();

RuleAttributeWithConditions startConditionAttribute = new RuleAttributeWithConditions.Builder()
NestedConditionRuleAttribute startConditionAttribute = new NestedConditionRuleAttribute.Builder()
.key("{{environment.attributes.current_time}}")
.value("09:00:00+00:00")
.operator("timeGreaterThanOrEquals")
.build();

RuleAttributeWithConditions endConditionAttribute = new RuleAttributeWithConditions.Builder()
NestedConditionRuleAttribute endConditionAttribute = new NestedConditionRuleAttribute.Builder()
.key("{{environment.attributes.current_time}}")
.value("17:00:00+00:00")
.operator("timeLessThanOrEquals")
.build();

V2PolicyRuleRuleWithConditions policyRule = new V2PolicyRuleRuleWithConditions.Builder()
V2PolicyRuleRuleWithNestedConditions policyRule = new V2PolicyRuleRuleWithNestedConditions.Builder()
.operator("and")
.conditions(new ArrayList<RuleAttributeWithConditions>(Arrays.asList(weeklyConditionAttribute, startConditionAttribute, endConditionAttribute)))
.conditions(new ArrayList<NestedCondition>(Arrays.asList(weeklyConditionAttribute, startConditionAttribute, endConditionAttribute)))
.build();

ReplaceV2PolicyOptions options = new ReplaceV2PolicyOptions.Builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* (C) Copyright IBM Corp. 2023.
*
* 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 com.ibm.cloud.platform_services.iam_policy_management.v1.model;

import java.util.List;

import com.ibm.cloud.sdk.core.service.model.GenericModel;

/**
* Condition that specifies additional conditions or RuleAttribute to grant access.s.
*
* Classes which extend this class:
* - NestedConditionRuleAttribute
* - NestedConditionRuleWithConditions
*/
public class NestedCondition extends GenericModel {

/**
* The operator of an attribute.
*/
public interface Operator {
/** timeLessThan. */
String TIMELESSTHAN = "timeLessThan";
/** timeLessThanOrEquals. */
String TIMELESSTHANOREQUALS = "timeLessThanOrEquals";
/** timeGreaterThan. */
String TIMEGREATERTHAN = "timeGreaterThan";
/** timeGreaterThanOrEquals. */
String TIMEGREATERTHANOREQUALS = "timeGreaterThanOrEquals";
/** dateTimeLessThan. */
String DATETIMELESSTHAN = "dateTimeLessThan";
/** dateTimeLessThanOrEquals. */
String DATETIMELESSTHANOREQUALS = "dateTimeLessThanOrEquals";
/** dateTimeGreaterThan. */
String DATETIMEGREATERTHAN = "dateTimeGreaterThan";
/** dateTimeGreaterThanOrEquals. */
String DATETIMEGREATERTHANOREQUALS = "dateTimeGreaterThanOrEquals";
/** dayOfWeekEquals. */
String DAYOFWEEKEQUALS = "dayOfWeekEquals";
/** dayOfWeekAnyOf. */
String DAYOFWEEKANYOF = "dayOfWeekAnyOf";
}

protected String key;
protected String operator;
protected Object value;
protected List<RuleAttribute> conditions;

protected NestedCondition() { }

/**
* Gets the key.
*
* The name of an attribute.
*
* @return the key
*/
public String key() {
return key;
}

/**
* Gets the operator.
*
* The operator of an attribute.
*
* @return the operator
*/
public String operator() {
return operator;
}

/**
* Gets the value.
*
* The value of a rule or resource attribute; can be boolean or string for resource attribute. Can be string or an
* array of strings (e.g., array of days to permit access) for rule attribute.
*
* @return the value
*/
public Object value() {
return value;
}

/**
* Gets the conditions.
*
* List of conditions associated with a policy, e.g., time-based conditions that grant access over a certain time
* period.
*
* @return the conditions
*/
public List<RuleAttribute> conditions() {
return conditions;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* (C) Copyright IBM Corp. 2023.
*
* 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 com.ibm.cloud.platform_services.iam_policy_management.v1.model;

/**
* Rule that specifies additional access granted (e.g., time-based condition).
*/
public class NestedConditionRuleAttribute extends NestedCondition {

/**
* The operator of an attribute.
*/
public interface Operator {
/** timeLessThan. */
String TIMELESSTHAN = "timeLessThan";
/** timeLessThanOrEquals. */
String TIMELESSTHANOREQUALS = "timeLessThanOrEquals";
/** timeGreaterThan. */
String TIMEGREATERTHAN = "timeGreaterThan";
/** timeGreaterThanOrEquals. */
String TIMEGREATERTHANOREQUALS = "timeGreaterThanOrEquals";
/** dateTimeLessThan. */
String DATETIMELESSTHAN = "dateTimeLessThan";
/** dateTimeLessThanOrEquals. */
String DATETIMELESSTHANOREQUALS = "dateTimeLessThanOrEquals";
/** dateTimeGreaterThan. */
String DATETIMEGREATERTHAN = "dateTimeGreaterThan";
/** dateTimeGreaterThanOrEquals. */
String DATETIMEGREATERTHANOREQUALS = "dateTimeGreaterThanOrEquals";
/** dayOfWeekEquals. */
String DAYOFWEEKEQUALS = "dayOfWeekEquals";
/** dayOfWeekAnyOf. */
String DAYOFWEEKANYOF = "dayOfWeekAnyOf";
}


/**
* Builder.
*/
public static class Builder {
private String key;
private String operator;
private Object value;

/**
* Instantiates a new Builder from an existing NestedConditionRuleAttribute instance.
*
* @param nestedConditionRuleAttribute the instance to initialize the Builder with
*/
public Builder(NestedCondition nestedConditionRuleAttribute) {
this.key = nestedConditionRuleAttribute.key;
this.operator = nestedConditionRuleAttribute.operator;
this.value = nestedConditionRuleAttribute.value;
}

/**
* Instantiates a new builder.
*/
public Builder() {
}

/**
* Instantiates a new builder with required properties.
*
* @param key the key
* @param operator the operator
* @param value the value
*/
public Builder(String key, String operator, Object value) {
this.key = key;
this.operator = operator;
this.value = value;
}

/**
* Builds a NestedConditionRuleAttribute.
*
* @return the new NestedConditionRuleAttribute instance
*/
public NestedConditionRuleAttribute build() {
return new NestedConditionRuleAttribute(this);
}

/**
* Set the key.
*
* @param key the key
* @return the NestedConditionRuleAttribute builder
*/
public Builder key(String key) {
this.key = key;
return this;
}

/**
* Set the operator.
*
* @param operator the operator
* @return the NestedConditionRuleAttribute builder
*/
public Builder operator(String operator) {
this.operator = operator;
return this;
}

/**
* Set the value.
*
* @param value the value
* @return the NestedConditionRuleAttribute builder
*/
public Builder value(Object value) {
this.value = value;
return this;
}
}

protected NestedConditionRuleAttribute() { }

protected NestedConditionRuleAttribute(Builder builder) {
com.ibm.cloud.sdk.core.util.Validator.notNull(builder.key,
"key cannot be null");
com.ibm.cloud.sdk.core.util.Validator.notNull(builder.operator,
"operator cannot be null");
com.ibm.cloud.sdk.core.util.Validator.notNull(builder.value,
"value cannot be null");
key = builder.key;
operator = builder.operator;
value = builder.value;
}

/**
* New builder.
*
* @return a NestedConditionRuleAttribute builder
*/
public Builder newBuilder() {
return new Builder(this);
}
}

Loading

0 comments on commit 9753e83

Please sign in to comment.