Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.ranger.plugin.conditionevaluator;

import org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemCondition;
import org.apache.ranger.plugin.model.RangerServiceDef;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.mockito.Mockito.mock;

/**
* @generated by Cursor
* @description <Unit Test for RangerAbstractConditionEvaluator class>
*/
@ExtendWith(MockitoExtension.class)
@TestMethodOrder(MethodOrderer.MethodName.class)
public class TestRangerAbstractConditionEvaluator {
@Test
public void test01_settersAndAlwaysMatchPaths() {
RangerSimpleMatcher matcher = new RangerSimpleMatcher();

RangerServiceDef serviceDef = mock(RangerServiceDef.class);
matcher.setServiceDef(serviceDef);

RangerPolicyItemCondition condition = new RangerPolicyItemCondition("__simple", null);
matcher.setPolicyItemCondition(condition);
matcher.setConditionDef(null);
matcher.init();

Assertions.assertEquals(condition, matcher.getPolicyItemCondition());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.ranger.plugin.conditionevaluator;

import org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemCondition;
import org.apache.ranger.plugin.policyengine.RangerAccessRequest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Arrays;
import java.util.Collections;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* @generated by Cursor
* @description <Unit Test for RangerAccessedFromClusterConditions class>
*/
@ExtendWith(MockitoExtension.class)
@TestMethodOrder(MethodOrderer.MethodName.class)
public class TestRangerAccessedFromClusterConditions {
@Test
public void test01_fromCluster_withEmptyList_isAlwaysTrue() {
RangerAccessedFromClusterCondition evaluator = new RangerAccessedFromClusterCondition();
RangerPolicyItemCondition condition = mock(RangerPolicyItemCondition.class);
when(condition.getValues()).thenReturn(Collections.emptyList());
evaluator.setPolicyItemCondition(condition);
evaluator.init();

RangerAccessRequest req = mock(RangerAccessRequest.class);
Assertions.assertTrue(evaluator.isMatched(req));

Assertions.assertTrue(evaluator.isMatched(req));
}

@Test
public void test02_fromCluster_withValues_matchesOnlyIfContained() {
RangerAccessedFromClusterCondition evaluator = new RangerAccessedFromClusterCondition();
RangerPolicyItemCondition condition = mock(RangerPolicyItemCondition.class);
when(condition.getValues()).thenReturn(Arrays.asList("c1", "c2"));
evaluator.setPolicyItemCondition(condition);
evaluator.init();

RangerAccessRequest req = mock(RangerAccessRequest.class);
when(req.getClusterName()).thenReturn("c2");
Assertions.assertTrue(evaluator.isMatched(req));
when(req.getClusterName()).thenReturn("c3");
Assertions.assertFalse(evaluator.isMatched(req));
}

@Test
public void test03_notFromCluster_withEmptyList_alwaysTrue() {
RangerAccessedNotFromClusterCondition evaluator = new RangerAccessedNotFromClusterCondition();
RangerPolicyItemCondition condition = mock(RangerPolicyItemCondition.class);
when(condition.getValues()).thenReturn(Collections.emptyList());
evaluator.setPolicyItemCondition(condition);
evaluator.init();

RangerAccessRequest req = mock(RangerAccessRequest.class);
Assertions.assertTrue(evaluator.isMatched(req));
}

@Test
public void test04_notFromCluster_withValues_trueWhenNotContained() {
RangerAccessedNotFromClusterCondition evaluator = new RangerAccessedNotFromClusterCondition();
RangerPolicyItemCondition condition = mock(RangerPolicyItemCondition.class);
when(condition.getValues()).thenReturn(Arrays.asList("c1", "c2"));
evaluator.setPolicyItemCondition(condition);
evaluator.init();

RangerAccessRequest req = mock(RangerAccessRequest.class);
when(req.getClusterName()).thenReturn("c3");
Assertions.assertTrue(evaluator.isMatched(req));
when(req.getClusterName()).thenReturn("c1");
Assertions.assertFalse(evaluator.isMatched(req));
}

@Test
public void test05_fromClusterType_withValues() {
RangerAccessedFromClusterTypeCondition evaluator = new RangerAccessedFromClusterTypeCondition();
RangerPolicyItemCondition condition = mock(RangerPolicyItemCondition.class);
when(condition.getValues()).thenReturn(Arrays.asList("onprem", "cloud"));
evaluator.setPolicyItemCondition(condition);
evaluator.init();

RangerAccessRequest req = mock(RangerAccessRequest.class);
when(req.getClusterType()).thenReturn("cloud");
Assertions.assertTrue(evaluator.isMatched(req));
when(req.getClusterType()).thenReturn("edge");
Assertions.assertFalse(evaluator.isMatched(req));
}

@Test
public void test06_notFromClusterType_withValues() {
RangerAccessedNotFromClusterTypeCondition evaluator = new RangerAccessedNotFromClusterTypeCondition();
RangerPolicyItemCondition condition = mock(RangerPolicyItemCondition.class);
when(condition.getValues()).thenReturn(Arrays.asList("onprem", "cloud"));
evaluator.setPolicyItemCondition(condition);
evaluator.init();

RangerAccessRequest req = mock(RangerAccessRequest.class);
when(req.getClusterType()).thenReturn("edge");
Assertions.assertTrue(evaluator.isMatched(req));
when(req.getClusterType()).thenReturn("cloud");
Assertions.assertFalse(evaluator.isMatched(req));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.ranger.plugin.conditionevaluator;

import org.apache.ranger.plugin.model.RangerPolicy.RangerPolicyItemCondition;
import org.apache.ranger.plugin.model.RangerServiceDef.RangerPolicyConditionDef;
import org.apache.ranger.plugin.policyengine.RangerAccessRequest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* @generated by Cursor
* @description <Unit Test for RangerContextAttributeValueConditions class>
*/
@ExtendWith(MockitoExtension.class)
@TestMethodOrder(MethodOrderer.MethodName.class)
public class TestRangerContextAttributeValueConditions {
@Test
public void test01_inCondition_attributeMissing_returnsTrue() {
RangerContextAttributeValueInCondition evaluator = new RangerContextAttributeValueInCondition();

RangerPolicyConditionDef conditionDef = mock(RangerPolicyConditionDef.class);
when(conditionDef.getEvaluatorOptions()).thenReturn(Collections.singletonMap("attributeName", "site"));
evaluator.setConditionDef(conditionDef);

RangerPolicyItemCondition condition = mock(RangerPolicyItemCondition.class);
when(condition.getValues()).thenReturn(Arrays.asList("10", "20"));
evaluator.setPolicyItemCondition(condition);

evaluator.init();

RangerAccessRequest request = mock(RangerAccessRequest.class);
when(request.getContext()).thenReturn(new HashMap<>());

Assertions.assertTrue(evaluator.isMatched(request));
}

@Test
public void test02_inCondition_attributePresent_withMatch_returnsTrue() {
RangerContextAttributeValueInCondition evaluator = new RangerContextAttributeValueInCondition();

RangerPolicyConditionDef conditionDef = mock(RangerPolicyConditionDef.class);
when(conditionDef.getEvaluatorOptions()).thenReturn(Collections.singletonMap("attributeName", "dept"));
evaluator.setConditionDef(conditionDef);

RangerPolicyItemCondition condition = mock(RangerPolicyItemCondition.class);
when(condition.getValues()).thenReturn(Arrays.asList("ENGG", "PROD"));
evaluator.setPolicyItemCondition(condition);

evaluator.init();

Map<String, Object> ctx = new HashMap<>();
ctx.put("dept", "ENGG");
RangerAccessRequest request = mock(RangerAccessRequest.class);
when(request.getContext()).thenReturn(ctx);

Assertions.assertTrue(evaluator.isMatched(request));
}

@Test
public void test03_inCondition_attributePresent_noMatch_returnsFalse() {
RangerContextAttributeValueInCondition evaluator = new RangerContextAttributeValueInCondition();

RangerPolicyConditionDef conditionDef = mock(RangerPolicyConditionDef.class);
when(conditionDef.getEvaluatorOptions()).thenReturn(Collections.singletonMap("attributeName", "dept"));
evaluator.setConditionDef(conditionDef);

RangerPolicyItemCondition condition = mock(RangerPolicyItemCondition.class);
when(condition.getValues()).thenReturn(Arrays.asList("ENGG", "PROD"));
evaluator.setPolicyItemCondition(condition);

evaluator.init();

Map<String, Object> ctx = new HashMap<>();
ctx.put("dept", "SALES");
RangerAccessRequest request = mock(RangerAccessRequest.class);
when(request.getContext()).thenReturn(ctx);

Assertions.assertFalse(evaluator.isMatched(request));
}

@Test
public void test04_notInCondition_attributeMissing_returnsTrue() {
RangerContextAttributeValueNotInCondition evaluator = new RangerContextAttributeValueNotInCondition();

RangerPolicyConditionDef conditionDef = mock(RangerPolicyConditionDef.class);
when(conditionDef.getEvaluatorOptions()).thenReturn(Collections.singletonMap("attributeName", "site"));
evaluator.setConditionDef(conditionDef);

RangerPolicyItemCondition condition = mock(RangerPolicyItemCondition.class);
when(condition.getValues()).thenReturn(Arrays.asList("10", "20"));
evaluator.setPolicyItemCondition(condition);

evaluator.init();

RangerAccessRequest request = mock(RangerAccessRequest.class);
when(request.getContext()).thenReturn(new HashMap<>());

Assertions.assertTrue(evaluator.isMatched(request));
}

@Test
public void test05_notInCondition_attributePresent_withMatch_returnsFalse() {
RangerContextAttributeValueNotInCondition evaluator = new RangerContextAttributeValueNotInCondition();

RangerPolicyConditionDef conditionDef = mock(RangerPolicyConditionDef.class);
when(conditionDef.getEvaluatorOptions()).thenReturn(Collections.singletonMap("attributeName", "dept"));
evaluator.setConditionDef(conditionDef);

RangerPolicyItemCondition condition = mock(RangerPolicyItemCondition.class);
when(condition.getValues()).thenReturn(Arrays.asList("ENGG", "PROD"));
evaluator.setPolicyItemCondition(condition);

evaluator.init();

Map<String, Object> ctx = new HashMap<>();
ctx.put("dept", "PROD");
RangerAccessRequest request = mock(RangerAccessRequest.class);
when(request.getContext()).thenReturn(ctx);

Assertions.assertFalse(evaluator.isMatched(request));
}

@Test
public void test06_notInCondition_attributePresent_noMatch_returnsTrue() {
RangerContextAttributeValueNotInCondition evaluator = new RangerContextAttributeValueNotInCondition();

RangerPolicyConditionDef conditionDef = mock(RangerPolicyConditionDef.class);
when(conditionDef.getEvaluatorOptions()).thenReturn(Collections.singletonMap("attributeName", "dept"));
evaluator.setConditionDef(conditionDef);

RangerPolicyItemCondition condition = mock(RangerPolicyItemCondition.class);
when(condition.getValues()).thenReturn(Arrays.asList("ENGG", "PROD"));
evaluator.setPolicyItemCondition(condition);

evaluator.init();

Map<String, Object> ctx = new HashMap<>();
ctx.put("dept", "SALES");
RangerAccessRequest request = mock(RangerAccessRequest.class);
when(request.getContext()).thenReturn(ctx);

Assertions.assertTrue(evaluator.isMatched(request));
}
}
Loading
Loading