Skip to content

Commit

Permalink
split out verifiers from Parameters to allow de/serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Hellblazer committed Jan 5, 2024
1 parent 2035ace commit 2aa995f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.salesforce.apollo.gorgoneion.comm.admissions.AdmissionsService;
import com.salesforce.apollo.gorgoneion.proto.SignedNonce;
import com.salesforce.apollo.membership.Context;
import com.salesforce.apollo.membership.Member;
import com.salesforce.apollo.membership.stereotomy.ControlledIdentifierMember;
import com.salesforce.apollo.stereotomy.StereotomyImpl;
import com.salesforce.apollo.stereotomy.event.proto.Validations;
Expand All @@ -36,7 +35,6 @@
import java.time.Duration;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.IntStream;
Expand All @@ -61,7 +59,7 @@ public void clientSmoke() throws Exception {
var stereotomy = new StereotomyImpl(new MemKeyStore(), kerl, entropy);
final var prefix = UUID.randomUUID().toString();
var member = new ControlledIdentifierMember(stereotomy.newIdentifier());
var context = Context.<Member>newBuilder().setCardinality(1).build();
var context = Context.newBuilder().setCardinality(1).build();
context.activate(member);

// Gorgoneion service comms
Expand All @@ -72,8 +70,7 @@ public void clientSmoke() throws Exception {
var observer = mock(ProtoEventObserver.class);
final var parameters = Parameters.newBuilder().setKerl(kerl).build();
@SuppressWarnings("unused")
var gorgon = new Gorgoneion(parameters, member, context, observer, gorgonRouter,
Executors.newScheduledThreadPool(1, Thread.ofVirtual().factory()), null);
var gorgon = new Gorgoneion(t -> true, parameters, member, context, observer, gorgonRouter, null);

// The registering client
var client = new ControlledIdentifierMember(stereotomy.newIdentifier());
Expand Down Expand Up @@ -142,7 +139,7 @@ public Void answer(InvocationOnMock invocation) {
}
}).when(observer).publish(Mockito.any(), Mockito.anyList());

var context = Context.<Member>newBuilder().setCardinality(members.size()).build();
var context = Context.newBuilder().setCardinality(members.size()).build();
for (ControlledIdentifierMember member : members) {
context.activate(member);
}
Expand All @@ -154,11 +151,9 @@ public Void answer(InvocationOnMock invocation) {
router.start();
return router;
})
.map(r -> new Gorgoneion(parameters, (ControlledIdentifierMember) r.getFrom(),
context, observer, r,
Executors.newScheduledThreadPool(2, Thread.ofVirtual()
.factory()),
null))
.map(r -> new Gorgoneion(t -> true, parameters,
(ControlledIdentifierMember) r.getFrom(), context,
observer, r, null))
.toList();

// The registering client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,33 @@
import com.codahale.metrics.Timer;
import com.google.protobuf.Empty;
import com.google.protobuf.Timestamp;
import com.salesforce.apollo.gorgoneion.proto.*;
import com.salesforce.apollo.stereotomy.event.proto.Ident;
import com.salesforce.apollo.stereotomy.event.proto.KERL_;
import com.salesforce.apollo.stereotomy.event.proto.Validation_;
import com.salesforce.apollo.stereotomy.event.proto.Validations;
import com.salesforce.apollo.cryptography.proto.Digeste;
import com.salesforce.apollo.archipelago.Router;
import com.salesforce.apollo.archipelago.RouterImpl.CommonCommunications;
import com.salesforce.apollo.cryptography.Digest;
import com.salesforce.apollo.cryptography.JohnHancock;
import com.salesforce.apollo.cryptography.Signer;
import com.salesforce.apollo.cryptography.Verifier;
import com.salesforce.apollo.cryptography.Verifier.DefaultVerifier;
import com.salesforce.apollo.cryptography.proto.Digeste;
import com.salesforce.apollo.gorgoneion.comm.GorgoneionMetrics;
import com.salesforce.apollo.gorgoneion.comm.admissions.AdmissionsServer;
import com.salesforce.apollo.gorgoneion.comm.admissions.AdmissionsService;
import com.salesforce.apollo.gorgoneion.comm.endorsement.Endorsement;
import com.salesforce.apollo.gorgoneion.comm.endorsement.EndorsementClient;
import com.salesforce.apollo.gorgoneion.comm.endorsement.EndorsementServer;
import com.salesforce.apollo.gorgoneion.comm.endorsement.EndorsementService;
import com.salesforce.apollo.gorgoneion.proto.*;
import com.salesforce.apollo.membership.Context;
import com.salesforce.apollo.membership.Member;
import com.salesforce.apollo.membership.stereotomy.ControlledIdentifierMember;
import com.salesforce.apollo.ring.SliceIterator;
import com.salesforce.apollo.stereotomy.EventCoordinates;
import com.salesforce.apollo.stereotomy.event.EstablishmentEvent;
import com.salesforce.apollo.stereotomy.event.InceptionEvent;
import com.salesforce.apollo.stereotomy.event.proto.Ident;
import com.salesforce.apollo.stereotomy.event.proto.KERL_;
import com.salesforce.apollo.stereotomy.event.proto.Validation_;
import com.salesforce.apollo.stereotomy.event.proto.Validations;
import com.salesforce.apollo.stereotomy.event.protobuf.ProtobufEventFactory;
import com.salesforce.apollo.stereotomy.identifier.Identifier;
import com.salesforce.apollo.stereotomy.identifier.SelfAddressingIdentifier;
Expand All @@ -51,41 +51,41 @@
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.*;
import java.util.function.Predicate;

import static com.salesforce.apollo.stereotomy.event.protobuf.ProtobufEventFactory.digestOf;

/**
* @author hal.hildebrand
*/
public class Gorgoneion {
public static final Logger log = LoggerFactory.getLogger(
Gorgoneion.class);
public static final Logger log = LoggerFactory.getLogger(Gorgoneion.class);

@SuppressWarnings("unused")
private final CommonCommunications<?, AdmissionsService> admissionsComm;
private final Context<Member> context;
private final CommonCommunications<Endorsement, EndorsementService> endorsementComm;
private final ControlledIdentifierMember member;
private final ProtoEventObserver observer;
private final Parameters parameters;
private final ScheduledExecutorService scheduler;

public Gorgoneion(Parameters parameters, ControlledIdentifierMember member, Context<Member> context,
ProtoEventObserver observer, Router router, ScheduledExecutorService scheduler,
GorgoneionMetrics metrics) {
this(parameters, member, context, observer, router, scheduler, metrics, router);
private final CommonCommunications<?, AdmissionsService> admissionsComm;
private final Context<Member> context;
private final CommonCommunications<Endorsement, EndorsementService> endorsementComm;
private final ControlledIdentifierMember member;
private final ProtoEventObserver observer;
private final Parameters parameters;
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1,
Thread.ofVirtual()
.factory());
private final Predicate<SignedAttestation> verifier;

public Gorgoneion(Predicate<SignedAttestation> verifier, Parameters parameters, ControlledIdentifierMember member,
Context<Member> context, ProtoEventObserver observer, Router router, GorgoneionMetrics metrics) {
this(verifier, parameters, member, context, observer, router, metrics, router);
}

public Gorgoneion(Parameters parameters, ControlledIdentifierMember member, Context<Member> context,
ProtoEventObserver observer, Router admissionsRouter, ScheduledExecutorService scheduler,
public Gorgoneion(Predicate<SignedAttestation> verifier, Parameters parameters, ControlledIdentifierMember member,
Context<Member> context, ProtoEventObserver observer, Router admissionsRouter,
GorgoneionMetrics metrics, Router endorsementRouter) {
this.verifier = verifier;
this.member = member;
this.context = context;
this.parameters = parameters;
this.scheduler = scheduler;
this.observer = observer;

admissionsComm = admissionsRouter.create(member, context.getId(), new Admit(), ":admissions",
Expand Down Expand Up @@ -145,7 +145,7 @@ private SignedNonce generateNonce(KERL_ application) {
if (identifier == null) {
throw new IllegalArgumentException("No identifier");
}
log.debug("Generating nonce for: {} contacting: {} on: {}", identifier, identifier, member.getId());
log.info("Generating nonce for: {} contacting: {} on: {}", identifier, identifier, member.getId());
var now = parameters.clock().instant();
final var ident = identifier.toIdent();
var nonce = Nonce.newBuilder()
Expand Down Expand Up @@ -220,7 +220,7 @@ private void notarize(Credentials credentials, Validations validations) {
SliceIterator<Endorsement> redirecting = new SliceIterator<>("Enrollment", member, successors, endorsementComm);
var completed = new HashSet<Member>();
redirecting.iterate((link, m) -> {
log.debug("Enrolling: {} contacting: {} on: {}", identifier, link.getMember().getId(), member.getId());
log.info("Enrolling: {} contacting: {} on: {}", identifier, link.getMember().getId(), member.getId());
link.enroll(notarization, parameters.registrationTimeout());
return Empty.getDefaultInstance();
}, (futureSailor, link, m) -> completeEnrollment(futureSailor, m, completed), () -> {
Expand Down Expand Up @@ -292,7 +292,7 @@ private Validation_ validate(Credentials credentials) {
}

private Validation_ verificationOf(Credentials credentials) {
if (parameters.verifier().test(credentials.getAttestation())) {
if (verifier.test(credentials.getAttestation())) {
return validate(credentials);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/
package com.salesforce.apollo.gorgoneion;

import com.salesforce.apollo.gorgoneion.proto.SignedAttestation;
import com.salesforce.apollo.cryptography.DigestAlgorithm;
import com.salesforce.apollo.gorgoneion.proto.SignedAttestation;
import com.salesforce.apollo.stereotomy.KERL;

import java.time.Clock;
Expand All @@ -17,8 +17,8 @@
/**
* @author hal.hildebrand
*/
public record Parameters(Predicate<SignedAttestation> verifier, Clock clock, Duration registrationTimeout,
Duration frequency, DigestAlgorithm digestAlgorithm, Duration maxDuration, KERL kerl) {
public record Parameters(Clock clock, Duration registrationTimeout, Duration frequency, DigestAlgorithm digestAlgorithm,
Duration maxDuration, KERL kerl) {

public static Builder newBuilder() {
return new Builder();
Expand All @@ -38,10 +38,8 @@ public static class Builder {
private Duration maxDuration = Duration.ofSeconds(30);
private Duration registrationTimeout = Duration.ofSeconds(30);

private Predicate<SignedAttestation> verifier = defaultVerifier;

public Parameters build() {
return new Parameters(verifier, clock, registrationTimeout, frequency, digestAlgorithm, maxDuration, kerl);
return new Parameters(clock, registrationTimeout, frequency, digestAlgorithm, maxDuration, kerl);
}

public Clock getClock() {
Expand Down Expand Up @@ -97,15 +95,6 @@ public Builder setRegistrationTimeout(Duration registrationTimeout) {
this.registrationTimeout = registrationTimeout;
return this;
}

public Predicate<SignedAttestation> getVerifier() {
return verifier;
}

public Builder setVerifier(Predicate<SignedAttestation> verifier) {
this.verifier = verifier;
return this;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@

import com.google.protobuf.Any;
import com.google.protobuf.Timestamp;
import com.salesforce.apollo.gorgoneion.proto.Attestation;
import com.salesforce.apollo.gorgoneion.proto.Credentials;
import com.salesforce.apollo.gorgoneion.proto.SignedAttestation;
import com.salesforce.apollo.stereotomy.event.proto.KERL_;
import com.salesforce.apollo.stereotomy.event.proto.Validations;
import com.salesforce.apollo.archipelago.LocalServer;
import com.salesforce.apollo.archipelago.ServerConnectionCache;
import com.salesforce.apollo.cryptography.DigestAlgorithm;
import com.salesforce.apollo.gorgoneion.comm.admissions.AdmissionsServer;
import com.salesforce.apollo.gorgoneion.comm.admissions.AdmissionsService;
import com.salesforce.apollo.gorgoneion.proto.Attestation;
import com.salesforce.apollo.gorgoneion.proto.Credentials;
import com.salesforce.apollo.gorgoneion.proto.SignedAttestation;
import com.salesforce.apollo.membership.Context;
import com.salesforce.apollo.membership.Member;
import com.salesforce.apollo.membership.stereotomy.ControlledIdentifierMember;
import com.salesforce.apollo.stereotomy.StereotomyImpl;
import com.salesforce.apollo.stereotomy.event.proto.KERL_;
import com.salesforce.apollo.stereotomy.event.proto.Validations;
import com.salesforce.apollo.stereotomy.mem.MemKERL;
import com.salesforce.apollo.stereotomy.mem.MemKeyStore;
import com.salesforce.apollo.stereotomy.services.proto.ProtoEventObserver;
Expand All @@ -31,7 +30,6 @@
import java.time.Duration;
import java.time.Instant;
import java.util.UUID;
import java.util.concurrent.Executors;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
Expand All @@ -49,7 +47,7 @@ public void smokin() throws Exception {
var stereotomy = new StereotomyImpl(new MemKeyStore(), kerl, entropy);
final var prefix = UUID.randomUUID().toString();
var member = new ControlledIdentifierMember(stereotomy.newIdentifier());
var context = Context.<Member>newBuilder().setCardinality(1).build();
var context = Context.newBuilder().setCardinality(1).build();
context.activate(member);

// Gorgoneion service comms
Expand All @@ -59,25 +57,24 @@ public void smokin() throws Exception {
// The kerl observer to publish admitted client KERLs to
var observer = mock(ProtoEventObserver.class);
@SuppressWarnings("unused")
var gorgon = new Gorgoneion(Parameters.newBuilder().setKerl(kerl).build(), member, context, observer,
gorgonRouter, Executors.newScheduledThreadPool(1, Thread.ofVirtual().factory()),
null);
var gorgon = new Gorgoneion(t -> true, Parameters.newBuilder().setKerl(kerl).build(), member, context, observer,
gorgonRouter, null);

// The registering client
var client = new ControlledIdentifierMember(stereotomy.newIdentifier());

// Registering client comms
var clientRouter = new LocalServer(prefix, client).router(ServerConnectionCache.newBuilder().setTarget(2));
AdmissionsService admissions = mock(AdmissionsService.class);
var clientComminications = clientRouter.create(client, context.getId(), admissions, ":admissions",
var clientCommunications = clientRouter.create(client, context.getId(), admissions, ":admissions",
r -> new AdmissionsServer(
clientRouter.getClientIdentityProvider(), r, null),
AdmissionsClient.getCreate(),
Admissions.getLocalLoopback(client));
clientRouter.start();

// Admin client link
var admin = clientComminications.connect(member);
var admin = clientCommunications.connect(member);

assertNotNull(admin);

Expand All @@ -86,9 +83,8 @@ public void smokin() throws Exception {
final KERL_ cKerl = client.kerl();
var fs = admin.apply(cKerl, Duration.ofSeconds(1));
assertNotNull(fs);
var signedNonce = fs;
assertNotNull(signedNonce.getNonce());
assertEquals(client.getIdentifier().getIdentifier().toIdent(), signedNonce.getNonce().getMember());
assertNotNull(fs.getNonce());
assertEquals(client.getIdentifier().getIdentifier().toIdent(), fs.getNonce().getMember());

// Create attestation
final var now = Instant.now();
Expand All @@ -98,7 +94,7 @@ public void smokin() throws Exception {
.setTimestamp(Timestamp.newBuilder()
.setSeconds(now.getEpochSecond())
.setNanos(now.getNano()))
.setNonce(client.sign(signedNonce.toByteString()).toSig())
.setNonce(client.sign(fs.toByteString()).toSig())
.setKerl(client.kerl())
.setAttestation(attestationDocument)
.build();
Expand All @@ -109,7 +105,7 @@ public void smokin() throws Exception {
.setSignature(client.sign(
attestation.toByteString()).toSig())
.build())
.setNonce(signedNonce)
.setNonce(fs)
.build(), Duration.ofSeconds(1));
gorgonRouter.close(Duration.ofSeconds(1));
clientRouter.close(Duration.ofSeconds(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import java.security.SecureRandom;
import java.time.Clock;
import java.time.Duration;
import java.util.concurrent.Executors;
import java.util.function.BiFunction;
import java.util.function.Function;

Expand Down Expand Up @@ -67,8 +66,7 @@ public void smokin() throws Exception {
var gorgoneions = routers.values().stream().map(r -> {
var k = dhts.get(r.getFrom()).asKERL();
return new Gorgoneion(Parameters.newBuilder().setKerl(k).build(), (ControlledIdentifierMember) r.getFrom(),
context, new DirectPublisher(new ProtoKERLAdapter(k)), r,
Executors.newScheduledThreadPool(2, Thread.ofVirtual().factory()), null);
context, new DirectPublisher(new ProtoKERLAdapter(k)), r, null);
}).toList();

final var dht = (KerlDHT) dhts.values().stream().findFirst().get();
Expand Down

0 comments on commit 2aa995f

Please sign in to comment.