summaryrefslogtreecommitdiffstats
path: root/controlloop/common/guard/src/test/java/org/onap/policy/guard/PolicyGuardXacmlHelperTest.java
blob: 628bfedc2d5c3f3e5139ddd536ad02197f8036fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*-
 * ============LICENSE_START=======================================================
 * guard
 * ================================================================================
 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.guard;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.att.research.xacml.api.Advice;
import com.att.research.xacml.api.Attribute;
import com.att.research.xacml.api.AttributeCategory;
import com.att.research.xacml.api.AttributeValue;
import com.att.research.xacml.api.Decision;
import com.att.research.xacml.api.IdReference;
import com.att.research.xacml.api.Identifier;
import com.att.research.xacml.api.Obligation;
import com.att.research.xacml.api.Response;
import com.att.research.xacml.api.Result;
import com.att.research.xacml.api.Status;
import com.att.research.xacml.std.IdentifierImpl;
import com.att.research.xacml.std.StdAttribute;
import com.att.research.xacml.std.StdAttributeCategory;
import com.att.research.xacml.std.StdAttributeValue;
import com.att.research.xacml.std.StdResponse;
import com.att.research.xacml.std.StdResult;
import com.att.research.xacml.std.StdStatus;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Properties;
import java.util.UUID;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.onap.policy.drools.http.server.HttpServletServer;
import org.onap.policy.drools.system.PolicyEngine;
import org.onap.policy.drools.utils.logging.LoggerUtil;

public class PolicyGuardXacmlHelperTest {

    /**
     * Set up test class.
     */
    @BeforeClass
    public static void setupSimulator() {
        LoggerUtil.setLevel("ROOT", "INFO");
        LoggerUtil.setLevel("org.eclipse.jetty", "WARN");
        try {
            org.onap.policy.simulators.Util.buildGuardSim();
        } catch (Exception e) {
            fail(e.getMessage());
        }
        //
        // Set guard properties
        //
        org.onap.policy.guard.Util.setGuardEnvProps("http://localhost:6669/pdp/api/getDecision", "python", "test",
                "python", "test", "DEVL");
    }

    /**
     * Shuts down simulator and performs 1 more test for the case where the connection fails.
     */
    @AfterClass
    public static void tearDownSimulator() {
        HttpServletServer.factory.destroy();

        // Null/ Bad Connection Case
        PolicyGuardXacmlRequestAttributes xacmlReq = new PolicyGuardXacmlRequestAttributes(
                org.onap.policy.simulators.GuardSimulatorJaxRs.DENY_CLNAME, "actor", "recipe", "target", "requestId");
        String rawDecision = new PolicyGuardXacmlHelper().callPDP(xacmlReq);
        assertNotNull(rawDecision);
        assertEquals(0, Util.INDETERMINATE.compareToIgnoreCase(rawDecision));
    }

    @Test
    public void testSimulator() {
        PolicyGuardXacmlRequestAttributes request = new PolicyGuardXacmlRequestAttributes("clname_id", "actor_id",
                "operation_id", "target_id", "request_id");
        String xacmlResponse = new PolicyGuardXacmlHelper().callPDP(request);
        assertNotNull(xacmlResponse);
    }

    @Test
    /**
     * Tests PolicyGuardXacmlHelper.callPDP method to determine if it returns DENY, PERMIT, or
     * INDETERMINATE as expected.
     */
    public void testCallPdp() {
        // Deny Case
        PolicyGuardXacmlRequestAttributes xacmlReq = new PolicyGuardXacmlRequestAttributes(
                org.onap.policy.simulators.GuardSimulatorJaxRs.DENY_CLNAME, "actor", "recipe", "target", "requestId");
        String rawDecision = new PolicyGuardXacmlHelper().callPDP(xacmlReq);
        assertNotNull(rawDecision);
        assertTrue(0 == Util.DENY.compareToIgnoreCase(rawDecision));

        // Permit Case
        xacmlReq = new PolicyGuardXacmlRequestAttributes("clname", "actor", "recipe", "target", "requestId");
        rawDecision = new PolicyGuardXacmlHelper().callPDP(xacmlReq);
        assertNotNull(rawDecision);
        assertEquals(0, Util.PERMIT.compareToIgnoreCase(rawDecision));

        // Indeterminate case is in tearDown for efficiency
    }

    @Test
    /**
     * Tests PolicyGuardXacmlHelper.callPDP method to exercise all branches
     */
    public void testCallPdpExtra() {
        PolicyGuardXacmlRequestAttributes xacmlReq = new PolicyGuardXacmlRequestAttributes(
                org.onap.policy.simulators.GuardSimulatorJaxRs.DENY_CLNAME, "actor", "recipe", "target", "requestId");

        xacmlReq.setClnameID(null);
        String rawDecision = new PolicyGuardXacmlHelper().callPDP(xacmlReq);
        assertNotNull(rawDecision);
        assertEquals(-5, Util.DENY.compareToIgnoreCase(rawDecision));

        org.onap.policy.guard.Util.setGuardEnvProps("http://localhost:6669/pdp/api/getDecision", "", "", "", "", "");

        rawDecision = new PolicyGuardXacmlHelper().callPDP(xacmlReq);
        assertNotNull(rawDecision);

        org.onap.policy.guard.Util.setGuardEnvProps("http://localhost:6669/pdp/api/getDecision", "python", "test",
                "python", "test", "DEVL");

    }

    @Test
    public void testParseXacmlPdpResponse() throws URISyntaxException {
        PolicyGuardResponse pgResponse = PolicyGuardXacmlHelper.parseXACMLPDPResponse(null);
        assertEquals("Indeterminate", pgResponse.getResult());

        Decision decision = Decision.PERMIT;
        Status status = new StdStatus(StdStatus.STATUS_OK);
        Result result = new StdResult(decision, status);
        Response xacmlResponse = new StdResponse(result);
        pgResponse = PolicyGuardXacmlHelper.parseXACMLPDPResponse(xacmlResponse);
        assertEquals("Permit", pgResponse.getResult());


        final Collection<Obligation> obligationsIn = null;
        final Collection<Advice> adviceIn = null;
        final Collection<IdReference> policyIdentifiersIn = null;
        final Collection<IdReference> policySetIdentifiersIn = null;

        Collection<AttributeCategory> attributesIn = new ArrayList<>();
        Identifier identifierCategory = new IdentifierImpl(new URI("http://somewhere.over.the.rainbow"));
        Collection<Attribute> listAttributes = new ArrayList<>();
        Identifier categoryIdIn = new IdentifierImpl(new URI("http://somewhere.over.the.rainbow/category"));
        Identifier attributeIdIn0 = new IdentifierImpl(new URI("urn:oasis:names:tc:xacml:1.0:request:request-id"));
        Identifier dataTypeIdIn = new IdentifierImpl(new URI("http://somewhere.over.the.rainbow.dataType"));
        AttributeValue<String> valueIn = new StdAttributeValue<String>(dataTypeIdIn, UUID.randomUUID().toString());
        Attribute attribute0 = new StdAttribute(categoryIdIn, attributeIdIn0, valueIn);
        listAttributes.add(attribute0);

        Identifier attributeIdIn1 = new IdentifierImpl(new URI("urn:oasis:names:tc:xacml:1.0:operation:operation-id"));
        Attribute attribute1 = new StdAttribute(categoryIdIn, attributeIdIn1, valueIn);
        listAttributes.add(attribute1);
        attributesIn.add(new StdAttributeCategory(identifierCategory, listAttributes));

        Identifier attributeIdIn2 = new IdentifierImpl(new URI("Http://somewhere.over.the.rainbow/attributeId"));
        Attribute attribute2 = new StdAttribute(categoryIdIn, attributeIdIn2, valueIn);
        listAttributes.add(attribute2);
        attributesIn.add(new StdAttributeCategory(identifierCategory, listAttributes));

        Result fullResult = new StdResult(Decision.DENY, obligationsIn, adviceIn, attributesIn, policyIdentifiersIn,
                policySetIdentifiersIn);
        Response fullXacmlResponse = new StdResponse(fullResult);
        PolicyGuardResponse fullPgResponse = PolicyGuardXacmlHelper.parseXACMLPDPResponse(fullXacmlResponse);
        assertEquals("Deny", fullPgResponse.getResult());
    }

    @Test
    public void testInit() {
        final Properties savedEnvironment = (Properties) PolicyEngine.manager.getEnvironment().clone();

        assertNotNull(new PolicyGuardXacmlHelper());

        PolicyEngine.manager.getEnvironment().setProperty("guard.url",
                "http://localhost:6669/pdp/api/getDecision,Dorothy");
        assertNotNull(new PolicyGuardXacmlHelper());

        PolicyEngine.manager.getEnvironment().setProperty("guard.url",
                "http://localhost:6669/pdp/api/getDecision,Dorothy,Toto");
        assertNotNull(new PolicyGuardXacmlHelper());

        PolicyEngine.manager.getEnvironment().setProperty("guard.url", "http://localhost:6669/pdp/api/getDecision");

        PolicyEngine.manager.getEnvironment().setProperty("pdpx.timeout", "thisIsNotANumber");
        assertNotNull(new PolicyGuardXacmlHelper());

        PolicyEngine.manager.getEnvironment().setProperty("pdpx.timeout", "1000");
        assertNotNull(new PolicyGuardXacmlHelper());

        PolicyEngine.manager.getEnvironment().remove("pdpx.password");
        assertNotNull(new PolicyGuardXacmlHelper());

        PolicyEngine.manager.getEnvironment().setProperty("pdpx.username", "python");
        assertNotNull(new PolicyGuardXacmlHelper());

        PolicyEngine.manager.getEnvironment().remove("pdpx.client.password");
        assertNotNull(new PolicyGuardXacmlHelper());

        PolicyEngine.manager.getEnvironment().remove("pdpx.client.username");
        assertNotNull(new PolicyGuardXacmlHelper());

        PolicyEngine.manager.getEnvironment().setProperty("guard.url", "///");
        assertNotNull(new PolicyGuardXacmlHelper());

        PolicyEngine.manager.getEnvironment().setProperty("guard.disabled", "");
        assertNotNull(new PolicyGuardXacmlHelper());

        PolicyEngine.manager.getEnvironment().setProperty("guard.disabled", "true");
        assertNotNull(new PolicyGuardXacmlHelper());

        PolicyEngine.manager.getEnvironment().clear();
        assertNotNull(new PolicyGuardXacmlHelper());

        PolicyEngine.manager.setEnvironment(savedEnvironment);
    }
}