• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

FIWARE / trusted-issuers-registry / #13

07 Jun 2024 12:28PM UTC coverage: 77.376% (+2.9%) from 74.517%
#13

push

web-flow
Merge pull request #20 from FIWARE/skipFailingDids

Skip failing dids when building partylist

17 of 20 new or added lines in 1 file covered. (85.0%)

407 of 526 relevant lines covered (77.38%)

0.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.48
/src/main/java/org/fiware/iam/tir/repository/InMemoryPartiesRepo.java
1
package org.fiware.iam.tir.repository;
2

3
import io.micronaut.scheduling.annotation.Scheduled;
4
import jakarta.inject.Singleton;
5
import lombok.extern.slf4j.Slf4j;
6
import org.fiware.iam.satellite.model.TrustedCAVO;
7
import org.fiware.iam.tir.auth.CertificateMapper;
8
import org.fiware.iam.tir.configuration.Party;
9
import org.fiware.iam.tir.configuration.SatelliteProperties;
10
import org.fiware.iam.tir.issuers.IssuersProvider;
11
import org.fiware.iam.tir.issuers.TrustedIssuer;
12
import reactor.core.publisher.Mono;
13

14
import java.security.cert.CertificateEncodingException;
15
import java.security.cert.CertificateExpiredException;
16
import java.security.cert.CertificateNotYetValidException;
17
import java.security.cert.X509Certificate;
18
import java.util.ArrayList;
19
import java.util.Arrays;
20
import java.util.List;
21
import java.util.Optional;
22

23
@Slf4j
1✔
24
@Singleton
25
public class InMemoryPartiesRepo implements PartiesRepo {
26

27
    private final SatelliteProperties satelliteProperties;
28
    private final IssuersProvider issuersProvider;
29
    private final List<Party> parties;
30
    private final DidService didService;
31
    private final CertificateMapper certificateMapper;
32

33
    public InMemoryPartiesRepo(SatelliteProperties satelliteProperties, IssuersProvider issuersProvider, DidService didService, CertificateMapper certificateMapper) {
1✔
34
        this.parties = new ArrayList<>(satelliteProperties.getParties());
1✔
35
        this.satelliteProperties = satelliteProperties;
1✔
36
        this.issuersProvider = issuersProvider;
1✔
37
        this.didService = didService;
1✔
38
        this.certificateMapper = certificateMapper;
1✔
39
    }
1✔
40

41
    private Optional<TrustedCAVO> toTrustedCaVO(X509Certificate caCert) {
42

43
        try {
44
            String subject = caCert.getSubjectX500Principal().toString();
1✔
45
            String validity = isValid(caCert);
1✔
46
            String fingerprint = certificateMapper.getThumbprint(caCert);
1✔
47
            return Optional.of(new TrustedCAVO().status("granted").certificateFingerprint(fingerprint)
1✔
48
                    .validity(validity).subject(subject));
1✔
49
        } catch (CertificateEncodingException e) {
×
50
            log.warn("Was not able to get the fingerprint.");
×
51
        }
52
        return Optional.empty();
×
53
    }
54

55
    private String isValid(X509Certificate cert) {
56
        try {
57
            cert.checkValidity();
1✔
58
            return "valid";
1✔
59
        } catch (CertificateExpiredException | CertificateNotYetValidException e) {
×
60
            return "invalid";
×
61
        }
62
    }
63

64
    @Scheduled(fixedDelay = "15s")
65
    public void updateParties() {
66
        try {
67
            List<Party> updatedParties = new ArrayList<>(satelliteProperties.getParties());
1✔
68
            issuersProvider.getAllTrustedIssuers()
1✔
69
                    .flatMap(til -> Mono.zip(til.stream().map(this::getPartyForIssuer).toList(), parties -> Arrays.stream(parties).toList()))
1✔
70
                    .subscribe(partiesList -> {
1✔
71
                        for (Object partyObject : partiesList) {
1✔
72
                            if (partyObject instanceof Optional<?> optional && optional.isPresent() && optional.get() instanceof Party party) {
1✔
73
                                updatedParties.add(party);
1✔
74
                            } else {
75
                                log.warn("Optional Object {} is not a party or was empty.", partyObject);
1✔
76
                            }
77
                        }
1✔
78
                        parties.clear();
1✔
79
                        parties.addAll(updatedParties);
1✔
80
                        log.trace("Current parties: {}", updatedParties.stream().map(party -> "%s -> %s".formatted(party.did(), party.crt())));
1✔
81
                    });
1✔
NEW
82
        } catch (Exception e) {
×
NEW
83
            log.error("Exception occurred while updating parties", e);
×
NEW
84
            throw e;
×
85
        }
1✔
86
    }
1✔
87

88

89
    private Mono<Optional<Party>> getPartyForIssuer(TrustedIssuer trustedIssuer) {
90

91
        return didService.retrieveDidDocument(trustedIssuer.getIssuer())
1✔
92
                .filter(Optional::isPresent)
1✔
93
                .map(Optional::get)
1✔
94
                .flatMap(didDoc -> didService
1✔
95
                        .getCertificate(didDoc)
1✔
96
                        .filter(Optional::isPresent)
1✔
97
                        .map(Optional::get)
1✔
98
                        .map(cert -> Optional.of(new Party(didDoc.getId(), didDoc.getId(), didDoc.getId(), "Active", cert, didDoc)))
1✔
99
                ).onErrorResume(err -> {
1✔
100
                    log.error("Failed to retrieve data for issuer {}", trustedIssuer.getIssuer(), err);
1✔
101
                    return Mono.just(Optional.empty());
1✔
102
                });
103
    }
104

105
    @Override
106
    public List<Party> getParties() {
107
        return parties;
1✔
108
    }
109

110
    @Override
111
    public List<TrustedCAVO> getTrustedCAs() {
112
        List<TrustedCAVO> trustedCAVOS = new ArrayList<>();
1✔
113

114
        satelliteProperties.getTrustedList().stream()
1✔
115
                .forEach(trustedCA -> toTrustedCaVO(certificateMapper.getCertificates(trustedCA.crt()).get(0)).ifPresent(
1✔
116
                        trustedCAVOS::add));
1✔
117

118
        return trustedCAVOS;
1✔
119
    }
120

121
    @Override
122
    public Optional<Party> getPartyById(String id) {
123
        return parties.stream().filter(party -> party.id().equals(id)).findFirst();
1✔
124
    }
125

126
    @Override
127
    public Optional<Party> getPartyByDID(String did) {
128
        return parties.stream().filter(party -> party.did().equals(did)).findFirst();
1✔
129
    }
130

131
    @Override
132
    public void addParty(Party party) {
133

134
    }
×
135
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc