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

knowledgepixels / nanopub-registry / 45431388774

24 Nov 2026 03:56PM UTC coverage: 86.937% (+0.08%) from 86.861%
45431388774

push

github

web-flow
Merge pull request #236 from knowledgepixels/show-running-coverage-config

fix: report the coverage settings the registry runs with

1393 of 1686 branches covered (82.62%)

Branch coverage included in aggregate %.

3758 of 4239 relevant lines covered (88.65%)

13.8 hits per line

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

91.13
src/main/java/com/knowledgepixels/registry/CoverageFilter.java
1
package com.knowledgepixels.registry;
2

3
import com.mongodb.client.ClientSession;
4
import net.trustyuri.TrustyUriUtils;
5
import org.bson.Document;
6
import org.eclipse.rdf4j.model.IRI;
7
import org.nanopub.Nanopub;
8
import org.nanopub.NanopubUtils;
9
import org.nanopub.extra.security.MalformedCryptoElementException;
10
import org.nanopub.extra.security.NanopubSignatureElement;
11
import org.nanopub.extra.security.SignatureUtils;
12
import org.slf4j.Logger;
13
import org.slf4j.LoggerFactory;
14

15
import java.util.Collections;
16
import java.util.HashSet;
17
import java.util.Set;
18

19
/**
20
 * Determines whether a nanopub's types are covered by this registry's configured
21
 * coverage types. Core types (intro, endorsement) are always covered regardless
22
 * of the configuration. If no coverage types are configured, everything is covered.
23
 * A nanopub that invalidates one this registry holds is taken whatever its types.
24
 */
25
public final class CoverageFilter {
26

27
    private static final Logger logger = LoggerFactory.getLogger(CoverageFilter.class);
9 ✔
28

29
    /**
30
     * Cached set of covered type hashes, or null if all types are covered.
31
     */
32
    private static volatile Set<String> coveredTypeHashes;
33
    private static volatile String configuredTypes = "all";
6 ✔
34
    private static volatile boolean initialized = false;
9 ✔
35

36
    private CoverageFilter() {}
37

38
    /**
39
     * Initializes the coverage filter from the REGISTRY_COVERAGE_TYPES env var.
40
     * Format: whitespace-separated type URIs (e.g. "http://example.org/TypeA http://example.org/TypeB").
41
     * If unset or empty, all types are covered.
42
     */
43
    public static void init() {
44
        String config = Utils.getEnv("REGISTRY_COVERAGE_TYPES", null);
12 ✔
45
        if (config == null || config.isBlank() || "all".equalsIgnoreCase(config.trim())) {
30 !
46
            coveredTypeHashes = null;
6 ✔
47
            configuredTypes = "all";
6 ✔
48
            logger.info("Coverage filter initialized: all types covered (REGISTRY_COVERAGE_TYPES={})", config == null ? "null" : "\"" + config.trim() + "\"");
33 ✔
49
        } else {
50
            Set<String> hashes = new HashSet<>();
12 ✔
51
            for (String typeUri : config.trim().split("\\s+")) {
57 ✔
52
                if (!typeUri.isEmpty()) {
9 !
53
                    String hash = Utils.getHash(typeUri);
9 ✔
54
                    hashes.add(hash);
12 ✔
55
                    logger.debug("Configured coverage type '{}' -> hash='{}'", typeUri, hash);
15 ✔
56
                }
57
            }
58
            // Always include core types (intro, endorsement)
59
            hashes.add(NanopubLoader.INTRO_TYPE_HASH);
12 ✔
60
            hashes.add(NanopubLoader.ENDORSE_TYPE_HASH);
12 ✔
61
            coveredTypeHashes = Collections.unmodifiableSet(hashes);
9 ✔
62
            configuredTypes = String.join(" ", config.trim().split("\\s+"));
21 ✔
63
            logger.info("Coverage filter: {} types covered (including core types)", hashes.size());
18 ✔
64
        }
65
        initialized = true;
6 ✔
66
        logger.debug("CoverageFilter.initialized set to true");
9 ✔
67
    }
3 ✔
68

69
    /**
70
     * Returns the coverage types this registry runs with, as it reports them on its main
71
     * page, in its info JSON and in the {@code Nanopub-Registry-Coverage-Types} header: the
72
     * type URIs of REGISTRY_COVERAGE_TYPES separated by single spaces, or "all". This is the
73
     * configuration in effect, read when the filter was initialized, so it cannot differ
74
     * from what the registry does.
75
     */
76
    public static String getConfiguredTypes() {
77
        return configuredTypes;
6 ✔
78
    }
79

80
    /**
81
     * Returns true if all types are covered (no restriction configured).
82
     */
83
    public static boolean coversAllTypes() {
84
        return coveredTypeHashes == null;
18 ✔
85
    }
86

87
    /**
88
     * Returns the set of covered type hashes, or null if all types are covered.
89
     */
90
    public static Set<String> getCoveredTypeHashes() {
91
        return coveredTypeHashes;
6 ✔
92
    }
93

94
    /**
95
     * Returns the covered type hashes as a comma-separated string for use in
96
     * HTTP headers, or null if all types are covered.
97
     */
98
    public static String getCoveredTypeHashesAsString() {
99
        if (coveredTypeHashes == null) {
6 ✔
100
            return null;
6 ✔
101
        }
102
        return String.join(",", coveredTypeHashes);
12 ✔
103
    }
104

105
    /**
106
     * Returns true if the given nanopub has at least one type that is covered
107
     * by this registry, or if no coverage restriction is configured.
108
     * Core types (intro, endorsement) are always considered covered.
109
     * <p>
110
     * This looks at the types only. Whether the registry takes a nanopub is answered by
111
     * {@link #isCovered(ClientSession, Nanopub)}.
112
     */
113
    public static boolean isCovered(Nanopub nanopub) {
114
        if (coveredTypeHashes == null) {
6 ✔
115
            logger.trace("isCovered: all types allowed (no restriction)");
9 ✔
116
            return true;
6 ✔
117
        }
118

119
        Set<IRI> types = NanopubUtils.getTypes(nanopub);
9 ✔
120
        logger.trace("Checking coverage for nanopub: {} declared types", types.size());
18 ✔
121

122
        // Check nanopub's declared types
123
        for (IRI type : types) {
30 ✔
124
            String typeUri = type.stringValue();
9 ✔
125
            String hash = Utils.getHash(typeUri);
9 ✔
126
            if (coveredTypeHashes.contains(hash)) {
12 ✔
127
                logger.debug("Nanopub accepted: found covered type '{}' with hash='{}'", typeUri, hash);
15 ✔
128
                return true;
6 ✔
129
            } else {
130
                logger.trace("Type not covered: '{}' (hash='{}')", typeUri, hash);
15 ✔
131
            }
132
        }
3 ✔
133

134
        // Nanopubs with no declared types are accepted (they may be core infrastructure)
135
        if (types.isEmpty()) {
9 ✔
136
            logger.debug("Nanopub has no declared types - accepted as covered");
9 ✔
137
            return true;
6 ✔
138
        }
139

140
        logger.debug("Nanopub rejected: no covered types found");
9 ✔
141
        return false;
6 ✔
142
    }
143

144
    /**
145
     * Whether this registry takes the given nanopub: it is covered by its types (see
146
     * {@link #isCovered(Nanopub)}), or it invalidates a nanopub this registry holds.
147
     * <p>
148
     * A retraction has a type of its own, not the type of what it retracts. Judged by its types
149
     * alone, a registry restricted to some types would take the nanopubs of those types and
150
     * refuse their retractions, and go on listing as valid what its peers list as invalidated.
151
     * The lists of the invalidated nanopub apply to the invalidating one, so it belongs here as
152
     * much as the one it invalidates. That only holds for the pubkey that signed the invalidated
153
     * nanopub, as nobody else can invalidate it.
154
     * <p>
155
     * In a parallel load this waits for the nanopub's turn (see {@link AppendOrder}) before it
156
     * looks for the invalidated nanopub, so that one that comes earlier in the same stream is
157
     * stored by then. A nanopub that arrives before the one it invalidates is refused; it is on
158
     * the peers' lists of the invalidated one, and comes again with the next load of those.
159
     */
160
    public static boolean isCovered(ClientSession mongoSession, Nanopub nanopub) {
161
        if (isCovered(nanopub)) {
9 ✔
162
            return true;
6 ✔
163
        }
164
        Set<IRI> invalidatedIds = Utils.getInvalidatedNanopubIds(nanopub);
9 ✔
165
        if (invalidatedIds.isEmpty()) {
9 ✔
166
            return false;
6 ✔
167
        }
168
        String pubkeyHash = declaredPubkeyHash(nanopub);
9 ✔
169
        if (pubkeyHash == null) {
6 !
170
            return false;
×
171
        }
172
        AppendOrder.awaitTurn();
3 ✔
173
        for (IRI invalidatedId : invalidatedIds) {
30 ✔
174
            String invalidatedAc = TrustyUriUtils.getArtifactCode(invalidatedId.stringValue());
12 ✔
175
            if (invalidatedAc != null && RegistryDB.has(mongoSession, Collection.NANOPUBS.toString(),
42 !
176
                    new Document("_id", invalidatedAc).append("pubkey", pubkeyHash))) {
3 ✔
177
                logger.debug("Nanopub accepted: it invalidates {}, which this registry holds", invalidatedAc);
12 ✔
178
                return true;
6 ✔
179
            }
180
        }
3 ✔
181
        logger.debug("Nanopub rejected: no covered types found, and it invalidates nothing this registry holds");
9 ✔
182
        return false;
6 ✔
183
    }
184

185
    /**
186
     * The hash of the public key a nanopub says it is signed with, or null if it has none.
187
     * The signature is not verified here; loading the nanopub does that.
188
     */
189
    private static String declaredPubkeyHash(Nanopub nanopub) {
190
        try {
191
            NanopubSignatureElement el = SignatureUtils.getSignatureElement(nanopub);
9 ✔
192
            if (el != null && el.getPublicKeyString() != null) {
15 !
193
                return Utils.getHash(el.getPublicKeyString());
12 ✔
194
            }
195
        } catch (MalformedCryptoElementException ex) {
×
196
            logger.debug("Nanopub {} has a malformed signature element: {}", nanopub.getUri(), ex.getMessage());
×
197
        }
×
198
        return null;
×
199
    }
200

201
    /**
202
     * Returns true if the given type hash is covered by this registry.
203
     */
204
    public static boolean isCoveredType(String typeHash) {
205
        if (coveredTypeHashes == null) {
6 ✔
206
            logger.trace("isCoveredType('{}'): all types allowed", typeHash);
12 ✔
207
            return true;
6 ✔
208
        }
209
        if ("$".equals(typeHash)) {
12 ✔
210
            logger.trace("isCoveredType('{}'): special core token accepted", typeHash);
12 ✔
211
            return true;
6 ✔
212
        }
213
        boolean contains = coveredTypeHashes.contains(typeHash);
12 ✔
214
        logger.debug("isCoveredType('{}') -> {}", typeHash, contains);
18 ✔
215
        return contains;
6 ✔
216
    }
217
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc