aboutsummaryrefslogtreecommitdiffstats
path: root/kubernetes/aaf/components/aaf-hello
AgeCommit message (Expand)AuthorFilesLines
2021-12-07[GLOBAL] Migrate to helm v3efiacor2-26/+14
2021-10-15[COMMON] Bump ONAP versionSylvain Desbureaux2-5/+6
2021-03-24[DOC][COMMON] Prepare Honolulu releaseSylvain Desbureaux2-4/+4
2021-01-15[AAF] Externalizes init data out from aaf-cassSylvain Desbureaux1-1/+1
2020-11-30[COMMON][DOC] Bump version GuilinSylvain Desbureaux2-4/+4
2020-11-27Merge "Revert "[AAF] externalizes init data out from aaf-cass image to chart""Krzysztof Opasiak1-1/+1
2020-11-27Revert "[AAF] externalizes init data out from aaf-cass image to chart"Sylvain Desbureaux1-1/+1
2020-11-24[AAF] Uses new tpls for repos / imagesSylvain Desbureaux2-2/+5
2020-11-18[AAF] externalizes init data out from aaf-cass image to chartJohn J. Franey1-1/+1
2020-10-21[AAF] change comment styleJakub Latusek3-0/+6
2020-06-08[AAF SMS] Use certInitializer for certificatesSylvain Desbureaux9-0/+274
='#n182'>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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
/*-
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2019-2020 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.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.pdp.xacml.application.common.std;

import com.att.research.xacml.api.Request;
import com.att.research.xacml.api.Response;
import com.att.research.xacml.api.pdp.PDPEngine;
import com.att.research.xacml.api.pdp.PDPEngineFactory;
import com.att.research.xacml.api.pdp.PDPException;
import com.att.research.xacml.util.FactoryException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import lombok.Getter;
import org.apache.commons.lang3.tuple.Pair;
import org.onap.policy.common.endpoints.parameters.RestServerParameters;
import org.onap.policy.models.decisions.concepts.DecisionRequest;
import org.onap.policy.models.decisions.concepts.DecisionResponse;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslator;
import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class StdXacmlApplicationServiceProvider implements XacmlApplicationServiceProvider {

    private static final Logger LOGGER = LoggerFactory.getLogger(StdXacmlApplicationServiceProvider.class);

    protected String applicationName = "Please Override";
    protected List<String> actions = Collections.emptyList();
    protected List<ToscaPolicyTypeIdentifier> supportedPolicyTypes = new ArrayList<>();

    private Path pathForData = null;
    @Getter
    private RestServerParameters policyApiParameters;
    private Properties pdpProperties = null;
    private PDPEngine pdpEngine = null;
    private Map<ToscaPolicy, Path> mapLoadedPolicies = new HashMap<>();

    protected StdXacmlApplicationServiceProvider() {
        super();
    }

    @Override
    public String applicationName() {
        return applicationName;
    }

    @Override
    public List<String> actionDecisionsSupported() {
        return actions;
    }

    @Override
    public void initialize(Path pathForData, RestServerParameters policyApiParameters)
            throws XacmlApplicationException {
        //
        // Save our path
        //
        this.pathForData = pathForData;
        LOGGER.info("New Path is {}", this.pathForData.toAbsolutePath());
        //
        // Save our params
        //
        this.policyApiParameters = policyApiParameters;
        //
        // Look for and load the properties object
        //
        try {
            pdpProperties = XacmlPolicyUtils.loadXacmlProperties(XacmlPolicyUtils.getPropertiesPath(pathForData));
            LOGGER.info("{}", pdpProperties);
        } catch (IOException e) {
            throw new XacmlApplicationException("Failed to load " + XacmlPolicyUtils.XACML_PROPERTY_FILE, e);
        }
        //
        // Create an engine
        //
        createEngine(pdpProperties);
    }

    @Override
    public List<ToscaPolicyTypeIdentifier> supportedPolicyTypes() {
        return supportedPolicyTypes;
    }

    @Override
    public boolean canSupportPolicyType(ToscaPolicyTypeIdentifier policyTypeId) {
        throw new UnsupportedOperationException("Please override and implement canSupportPolicyType");
    }

    @Override
    public synchronized void loadPolicy(ToscaPolicy toscaPolicy) throws XacmlApplicationException {
        try {
            //
            // Convert the policies first
            //
            Object xacmlPolicy = this.getTranslator(toscaPolicy.getType()).convertPolicy(toscaPolicy);
            if (xacmlPolicy == null) {
                throw new ToscaPolicyConversionException("Failed to convert policy");
            }
            //
            // Create a copy of the properties object
            //
            Properties newProperties = this.getProperties();
            //
            // Construct the filename
            //
            Path refPath = XacmlPolicyUtils.constructUniquePolicyFilename(xacmlPolicy, this.getDataPath());
            //
            // Write the policy to disk
            // Maybe check for an error
            //
            if (XacmlPolicyUtils.writePolicyFile(refPath, xacmlPolicy) == null) {
                throw new ToscaPolicyConversionException("Unable to writePolicyFile");
            }
            if (LOGGER.isInfoEnabled()) {
                LOGGER.info("Xacml Policy is {}{}", XacmlPolicyUtils.LINE_SEPARATOR,
                    new String(Files.readAllBytes(refPath), StandardCharsets.UTF_8));
            }
            //
            // Add root policy to properties object
            //
            XacmlPolicyUtils.addRootPolicy(newProperties, refPath);
            //
            // Write the properties to disk
            //
            XacmlPolicyUtils.storeXacmlProperties(newProperties,
                    XacmlPolicyUtils.getPropertiesPath(this.getDataPath()));
            //
            // Reload the engine
            //
            this.createEngine(newProperties);
            //
            // Save the properties
            //
            this.pdpProperties = newProperties;
            //
            // Save in our map
            //
            this.mapLoadedPolicies.put(toscaPolicy, refPath);
        } catch (IOException | ToscaPolicyConversionException e) {
            throw new XacmlApplicationException("loadPolicy failed", e);
        }
    }

    @Override
    public synchronized boolean unloadPolicy(ToscaPolicy toscaPolicy) throws XacmlApplicationException {
        //
        // Find it in our map
        //
        Path refPolicy = this.mapLoadedPolicies.get(toscaPolicy);
        if (refPolicy == null) {
            LOGGER.error("Failed to find ToscaPolicy {} in our map size {}", toscaPolicy.getMetadata(),
                    this.mapLoadedPolicies.size());
            return false;
        }
        //
        // Create a copy of the properties object
        //
        Properties newProperties = this.getProperties();
        //
        // Remove it from the properties
        //
        XacmlPolicyUtils.removeRootPolicy(newProperties, refPolicy);
        //
        // We can delete the file
        //
        try {
            Files.delete(refPolicy);
        } catch (IOException e) {
            LOGGER.error("Failed to delete policy {} from disk {}", toscaPolicy.getMetadata(),
                    refPolicy.toAbsolutePath(), e);
        }
        //
        // Write the properties to disk
        //
        try {
            XacmlPolicyUtils.storeXacmlProperties(newProperties,
                    XacmlPolicyUtils.getPropertiesPath(this.getDataPath()));
        } catch (IOException e) {
            LOGGER.error("Failed to save the properties to disk {}", newProperties, e);
        }
        //
        // Reload the engine
        //
        this.createEngine(newProperties);
        //
        // Save the properties
        //
        this.pdpProperties = newProperties;
        //
        // Save in our map
        //
        if (this.mapLoadedPolicies.remove(toscaPolicy) == null) {
            LOGGER.error("Failed to remove toscaPolicy {} from internal map size {}", toscaPolicy.getMetadata(),
                    this.mapLoadedPolicies.size());
        }
        //
        // Not sure if any of the errors above warrant returning false
        //
        return true;
    }

    @Override
    public Pair<DecisionResponse, Response> makeDecision(DecisionRequest request,
            Map<String, String[]> requestQueryParams) {
        //
        // Convert to a XacmlRequest
        //
        Request xacmlRequest;
        try {
            xacmlRequest = this.getTranslator().convertRequest(request);
        } catch (ToscaPolicyConversionException e) {
            LOGGER.error("Failed to convert request", e);
            DecisionResponse response = new DecisionResponse();
            response.setStatus("error");
            response.setMessage(e.getLocalizedMessage());
            return Pair.of(response, null);
        }
        //
        // Now get a decision
        //
        Response xacmlResponse = this.xacmlDecision(xacmlRequest);
        //
        // Convert to a DecisionResponse
        //
        return Pair.of(this.getTranslator().convertResponse(xacmlResponse), xacmlResponse);
    }

    protected abstract ToscaPolicyTranslator getTranslator(String type);

    protected ToscaPolicyTranslator getTranslator() {
        return this.getTranslator("");
    }

    protected synchronized PDPEngine getEngine() {
        return this.pdpEngine;
    }

    protected synchronized Properties getProperties() {
        Properties newProperties = new Properties();
        newProperties.putAll(pdpProperties);
        return newProperties;
    }

    protected synchronized Path getDataPath() {
        return pathForData;
    }

    /**
     * Creates an instance of PDP engine given the Properties object.
     */
    protected synchronized void createEngine(Properties properties) {
        //
        // Now initialize the XACML PDP Engine
        //
        try {
            PDPEngineFactory factory = getPdpEngineFactory();
            PDPEngine engine = factory.newEngine(properties);
            if (engine != null) {
                //
                // If there is a previous engine have it shutdown.
                //
                this.destroyEngine();
                //
                // Save it off
                //
                this.pdpEngine = engine;
            }
        } catch (FactoryException e) {
            LOGGER.error("Failed to create XACML PDP Engine", e);
        }
    }

    protected synchronized void destroyEngine() {
        if (this.pdpEngine == null) {
            return;
        }
        try {
            this.pdpEngine.shutdown();
        } catch (Exception e) {
            LOGGER.warn("Exception thrown when destroying XACML PDP engine.", e);
        }
        this.pdpEngine = null;
    }

    /**
     * Make a decision call.
     *
     * @param request Incoming request object
     * @return Response object
     */
    protected synchronized Response xacmlDecision(Request request) {
        //
        // This is what we need to return
        //
        Response response = null;
        //
        // Track some timing
        //
        long timeStart = System.currentTimeMillis();
        try {
            response = this.pdpEngine.decide(request);
        } catch (PDPException e) {
            LOGGER.error("Xacml PDP Engine decide failed", e);
        } finally {
            //
            // Track the end of timing
            //
            long timeEnd = System.currentTimeMillis();
            LOGGER.info("Elapsed Time: {}ms", (timeEnd - timeStart));
        }
        return response;
    }

    // these may be overridden by junit tests

    protected PDPEngineFactory getPdpEngineFactory() throws FactoryException {
        return PDPEngineFactory.newInstance();
    }
}