<commit_before>package eu.h2020.symbiote.security.services;

import eu.h2020.symbiote.security.commons.Certificate;
import eu.h2020.symbiote.security.commons.SecurityConstants;
import eu.h2020.symbiote.security.commons.enums.IssuingAuthorityType;
import eu.h2020.symbiote.security.communication.payloads.AAM;
import eu.h2020.symbiote.security.communication.payloads.AvailableAAMsCollection;
import eu.h2020.symbiote.security.repositories.ComponentCertificatesRepository;
import eu.h2020.symbiote.security.repositories.PlatformRepository;
import eu.h2020.symbiote.security.repositories.entities.ComponentCertificate;
import eu.h2020.symbiote.security.repositories.entities.Platform;
import eu.h2020.symbiote.security.services.helpers.CertificationAuthorityHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.cert.CertificateException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@Service
public class AAMServices {

    private final CertificationAuthorityHelper certificationAuthorityHelper;
    private final PlatformRepository platformRepository;
    private final ComponentCertificatesRepository componentCertificatesRepository;
    private final RestTemplate restTemplate = new RestTemplate();

    @Value("${aam.environment.coreInterfaceAddress:https://localhost:8443}")
    private String coreInterfaceAddress;
    @Value("${aam.environment.platformAAMSuffixAtInterWorkingInterface:/paam}")
    private String platformAAMSuffixAtInterWorkingInterface = "/paam";
    @Value("${aam.environment.interworkingInterfacePort::8101}")
    private String interworkingInterfacePort = ":8101";
    @Value("${symbiote.coreaam.url:localhost}")
    private String coreAAMAddress = "";

    @Autowired
    public AAMServices(CertificationAuthorityHelper certificationAuthorityHelper, PlatformRepository platformRepository, ComponentCertificatesRepository componentCertificatesRepository) {
        this.certificationAuthorityHelper = certificationAuthorityHelper;
        this.platformRepository = platformRepository;
        this.componentCertificatesRepository = componentCertificatesRepository;
    }


    public Map<String, AAM> getAvailableAAMs() throws NoSuchProviderException, KeyStoreException, IOException, CertificateException, NoSuchAlgorithmException {
        Map<String, AAM> availableAAMs = new TreeMap<>();
        if (certificationAuthorityHelper.getDeploymentType() == IssuingAuthorityType.CORE) {
            // if Core AAM then we know the available AAMs
            Certificate coreCertificate = new Certificate(certificationAuthorityHelper.getAAMCert());

            // adding component certificates
            Map<String, Certificate> componentsCertificatesMap = new HashMap<>();
            List<ComponentCertificate> componentCertificatesFromRepository = componentCertificatesRepository.findAll();
            for (ComponentCertificate certificate : componentCertificatesFromRepository) {
                componentsCertificatesMap.put(certificate.getName(), certificate.getCertificate());
            }

            // adding core aam info to the response
            availableAAMs.put(SecurityConstants.CORE_AAM_INSTANCE_ID, new AAM(coreInterfaceAddress,
                    SecurityConstants.CORE_AAM_FRIENDLY_NAME,
                    SecurityConstants.CORE_AAM_INSTANCE_ID,
                    coreCertificate, componentsCertificatesMap));

            // registered platforms' AAMs
            for (Platform platform : platformRepository.findAll()) {
                AAM platformAAM = new AAM(platform.getPlatformInterworkingInterfaceAddress() + platformAAMSuffixAtInterWorkingInterface, platform.getPlatformInstanceFriendlyName(), platform
                        .getPlatformInstanceId(), platform.getPlatformAAMCertificate(), platform.getComponentCertificates());
                // add the platform AAM entry point to the results
                availableAAMs.put(platformAAM.getAamInstanceId(), platformAAM);
            }
        } else {
            // a PAAM needs to fetch them from core
            availableAAMs = restTemplate.getForEntity(coreAAMAddress + SecurityConstants
                    .AAM_GET_AVAILABLE_AAMS, AvailableAAMsCollection.class).getBody().getAvailableAAMs();
        }
        return availableAAMs;
    }
}<commit_msg>SYM-522 platform returns its component certs<commit_after>package eu.h2020.symbiote.security.services;

import eu.h2020.symbiote.security.commons.Certificate;
import eu.h2020.symbiote.security.commons.SecurityConstants;
import eu.h2020.symbiote.security.commons.enums.IssuingAuthorityType;
import eu.h2020.symbiote.security.communication.payloads.AAM;
import eu.h2020.symbiote.security.communication.payloads.AvailableAAMsCollection;
import eu.h2020.symbiote.security.repositories.ComponentCertificatesRepository;
import eu.h2020.symbiote.security.repositories.PlatformRepository;
import eu.h2020.symbiote.security.repositories.entities.ComponentCertificate;
import eu.h2020.symbiote.security.repositories.entities.Platform;
import eu.h2020.symbiote.security.services.helpers.CertificationAuthorityHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.cert.CertificateException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@Service
public class AAMServices {

    private final CertificationAuthorityHelper certificationAuthorityHelper;
    private final PlatformRepository platformRepository;
    private final ComponentCertificatesRepository componentCertificatesRepository;
    private final RestTemplate restTemplate = new RestTemplate();

    @Value("${aam.environment.coreInterfaceAddress:https://localhost:8443}")
    private String coreInterfaceAddress;
    @Value("${aam.environment.platformAAMSuffixAtInterWorkingInterface:/paam}")
    private String platformAAMSuffixAtInterWorkingInterface = "/paam";
    @Value("${aam.environment.interworkingInterfacePort::8101}")
    private String interworkingInterfacePort = ":8101";
    @Value("${symbiote.coreaam.url:localhost}")
    private String coreAAMAddress = "";

    @Autowired
    public AAMServices(CertificationAuthorityHelper certificationAuthorityHelper, PlatformRepository platformRepository, ComponentCertificatesRepository componentCertificatesRepository) {
        this.certificationAuthorityHelper = certificationAuthorityHelper;
        this.platformRepository = platformRepository;
        this.componentCertificatesRepository = componentCertificatesRepository;
    }


    public Map<String, AAM> getAvailableAAMs() throws NoSuchProviderException, KeyStoreException, IOException, CertificateException, NoSuchAlgorithmException {
        Map<String, AAM> availableAAMs = new TreeMap<>();
        if (certificationAuthorityHelper.getDeploymentType() == IssuingAuthorityType.CORE) {
            // if Core AAM then we know the available AAMs
            Certificate coreCertificate = new Certificate(certificationAuthorityHelper.getAAMCert());

            // adding component certificates
            Map<String, Certificate> componentsCertificatesMap = new HashMap<>();
            List<ComponentCertificate> componentCertificatesFromRepository = componentCertificatesRepository.findAll();
            for (ComponentCertificate certificate : componentCertificatesFromRepository) {
                componentsCertificatesMap.put(certificate.getName(), certificate.getCertificate());
            }

            // adding core aam info to the response
            availableAAMs.put(SecurityConstants.CORE_AAM_INSTANCE_ID, new AAM(coreInterfaceAddress,
                    SecurityConstants.CORE_AAM_FRIENDLY_NAME,
                    SecurityConstants.CORE_AAM_INSTANCE_ID,
                    coreCertificate, componentsCertificatesMap));

            // registered platforms' AAMs
            for (Platform platform : platformRepository.findAll()) {
                AAM platformAAM = new AAM(platform.getPlatformInterworkingInterfaceAddress() + platformAAMSuffixAtInterWorkingInterface, platform.getPlatformInstanceFriendlyName(), platform
                        .getPlatformInstanceId(), platform.getPlatformAAMCertificate(), platform.getComponentCertificates());
                // add the platform AAM entry point to the results
                availableAAMs.put(platformAAM.getAamInstanceId(), platformAAM);
            }
        } else {
            // a PAAM needs to fetch them from core
            availableAAMs = restTemplate.getForEntity(coreAAMAddress + SecurityConstants
                    .AAM_GET_AVAILABLE_AAMS, AvailableAAMsCollection.class).getBody().getAvailableAAMs();

            String deploymentId = certificationAuthorityHelper.getAAMInstanceIdentifier();

            AAM aam = availableAAMs.get(deploymentId);

            Map<String, Certificate> componentsCertificatesMap = new HashMap<>();
            List<ComponentCertificate> componentCertificatesFromRepository = componentCertificatesRepository.findAll();
            for (ComponentCertificate certificate : componentCertificatesFromRepository) {
                componentsCertificatesMap.put(certificate.getName(), certificate.getCertificate());
            }
            aam.setComponentCertificates(componentsCertificatesMap);
            availableAAMs.put(deploymentId, aam);

        }

        return availableAAMs;
    }
}
{'fraction_non_alphanumeric': 0.07812809835415427, 'fraction_numerical': 0.01140194328772556, 'mean_word_length': 4.292235047219307, 'pattern_counts': {'":': 2, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2561349', 'n_tokens_mistral': 2561, 'n_tokens_neox': 2450, 'n_words': 444}