Skip to content

Commit

Permalink
Extend Gorgoneion protocol to provide an establishment provisioning p…
Browse files Browse the repository at this point in the history
…ackage as part of the cycle
  • Loading branch information
Hellblazer committed Aug 6, 2024
1 parent c596c3e commit 9347485
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,10 @@

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.gorgoneion.proto.SignedNonce;
import com.salesforce.apollo.stereotomy.event.proto.KERL_;
import com.salesforce.apollo.stereotomy.event.proto.Validations;
import com.salesforce.apollo.gorgoneion.client.client.comm.Admissions;
import com.salesforce.apollo.gorgoneion.proto.*;
import com.salesforce.apollo.membership.stereotomy.ControlledIdentifierMember;
import com.salesforce.apollo.stereotomy.event.proto.KERL_;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -42,7 +38,7 @@ public GorgoneionClient(ControlledIdentifierMember member, Function<SignedNonce,
this.client = client;
}

public Validations apply(Duration timeout) {
public Establishment apply(Duration timeout) {
KERL_ application = member.kerl();
var fs = client.apply(application, timeout);
Credentials credentials = credentials(fs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
*/
package com.salesforce.apollo.gorgoneion.client.client.comm;

import com.salesforce.apollo.archipelago.Link;
import com.salesforce.apollo.gorgoneion.proto.Credentials;
import com.salesforce.apollo.gorgoneion.proto.Establishment;
import com.salesforce.apollo.gorgoneion.proto.SignedNonce;
import com.salesforce.apollo.stereotomy.event.proto.KERL_;
import com.salesforce.apollo.stereotomy.event.proto.Validations;
import com.salesforce.apollo.archipelago.Link;
import com.salesforce.apollo.membership.Member;
import com.salesforce.apollo.stereotomy.event.proto.KERL_;

import java.io.IOException;
import java.time.Duration;
Expand Down Expand Up @@ -39,13 +39,13 @@ public Member getMember() {
}

@Override
public Validations register(Credentials credentials, Duration timeout) {
public Establishment register(Credentials credentials, Duration timeout) {
return null;
}
};
}

SignedNonce apply(KERL_ application, Duration timeout);

Validations register(Credentials credentials, Duration timeout);
Establishment register(Credentials credentials, Duration timeout);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
import com.salesforce.apollo.archipelago.ServerConnectionCache.CreateClientCommunications;
import com.salesforce.apollo.gorgoneion.proto.AdmissionsGrpc;
import com.salesforce.apollo.gorgoneion.proto.Credentials;
import com.salesforce.apollo.gorgoneion.proto.Establishment;
import com.salesforce.apollo.gorgoneion.proto.SignedNonce;
import com.salesforce.apollo.membership.Member;
import com.salesforce.apollo.stereotomy.event.proto.KERL_;
import com.salesforce.apollo.stereotomy.event.proto.Validations;

import java.time.Duration;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -66,7 +66,7 @@ public Member getMember() {
}

@Override
public Validations register(Credentials credentials, Duration timeout) {
public Establishment register(Credentials credentials, Duration timeout) {
if (metrics != null) {
var serializedSize = credentials.getSerializedSize();
metrics.outboundBandwidth().mark(serializedSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.salesforce.apollo.gorgoneion.client;

import com.google.protobuf.Any;
import com.google.protobuf.ByteString;
import com.salesforce.apollo.archipelago.LocalServer;
import com.salesforce.apollo.archipelago.Router;
import com.salesforce.apollo.archipelago.ServerConnectionCache;
Expand All @@ -25,10 +26,10 @@
import com.salesforce.apollo.stereotomy.mem.MemKERL;
import com.salesforce.apollo.stereotomy.mem.MemKeyStore;
import com.salesforce.apollo.stereotomy.services.proto.ProtoEventObserver;
import com.salesforce.apollo.test.proto.ByteMessage;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import java.security.SecureRandom;
Expand Down Expand Up @@ -60,6 +61,7 @@ public void clientSmoke() throws Exception {
final var prefix = UUID.randomUUID().toString();
var member = new ControlledIdentifierMember(stereotomy.newIdentifier());
var b = DynamicContext.newBuilder();
final var testMessage = ByteMessage.newBuilder().setContents(ByteString.copyFromUtf8("hello world")).build();
b.setCardinality(1);
var context = b.build();
context.activate(member);
Expand All @@ -72,7 +74,8 @@ public void clientSmoke() throws Exception {
var observer = mock(ProtoEventObserver.class);
final var parameters = Parameters.newBuilder().setKerl(kerl).build();
@SuppressWarnings("unused")
var gorgon = new Gorgoneion(true, t -> true, parameters, member, context, observer, gorgonRouter, null);
var gorgon = new Gorgoneion(true, t -> true, (c, v) -> Any.pack(testMessage), parameters, member, context,
observer, gorgonRouter, null);

// The registering client
var client = new ControlledIdentifierMember(stereotomy.newIdentifier());
Expand All @@ -91,18 +94,20 @@ public void clientSmoke() throws Exception {
var admin = clientComminications.connect(member);

assertNotNull(admin);
Function<SignedNonce, Any> attester = sn -> Any.getDefaultInstance();
Function<SignedNonce, Any> attested = _ -> Any.getDefaultInstance();

var gorgoneionClient = new GorgoneionClient(client, attester, parameters.clock(), admin);
var gorgoneionClient = new GorgoneionClient(client, attested, parameters.clock(), admin);

var invitation = gorgoneionClient.apply(Duration.ofSeconds(60));
var establishment = gorgoneionClient.apply(Duration.ofSeconds(60));

gorgonRouter.close(Duration.ofSeconds(0));
clientRouter.close(Duration.ofSeconds(0));

assertNotNull(invitation);
assertNotEquals(Validations.getDefaultInstance(), invitation);
assertEquals(1, invitation.getValidationsCount());
assertNotNull(establishment);
assertNotEquals(Validations.getDefaultInstance(), establishment);
assertEquals(1, establishment.getValidations().getValidationsCount());
assertEquals(testMessage.getContents(),
establishment.getProvisioning().unpack(ByteMessage.class).getContents());

// Verify client KERL published

Expand Down Expand Up @@ -134,11 +139,9 @@ public void multiSmoke() throws Exception {
var countdown = new CountDownLatch(3);
// The kerl observer to publish admitted client KERLs to
var observer = mock(ProtoEventObserver.class);
doAnswer(new Answer<Void>() {
public Void answer(InvocationOnMock invocation) {
countdown.countDown();
return null;
}
doAnswer((Answer<Void>) invocation -> {
countdown.countDown();
return null;
}).when(observer).publish(Mockito.any(), Mockito.anyList());

var b = DynamicContext.newBuilder();
Expand All @@ -147,6 +150,7 @@ public Void answer(InvocationOnMock invocation) {
for (ControlledIdentifierMember member : members) {
context.activate(member);
}
final var testMessage = ByteMessage.newBuilder().setContents(ByteString.copyFromUtf8("hello world")).build();
final var parameters = Parameters.newBuilder().setKerl(kerl).build();
final var gorgoneions = members.stream()
.map(m -> {
Expand All @@ -156,8 +160,9 @@ public Void answer(InvocationOnMock invocation) {
return router;
})
.map(r -> new Gorgoneion(r.getFrom().equals(members.getFirst()), t -> true,
parameters, (ControlledIdentifierMember) r.getFrom(),
context, observer, r, null))
(c, v) -> Any.pack(testMessage), parameters,
(ControlledIdentifierMember) r.getFrom(), context,
observer, r, null))
.toList();

// The registering client
Expand All @@ -177,16 +182,15 @@ public Void answer(InvocationOnMock invocation) {
var admin = clientComminications.connect(members.getFirst());

assertNotNull(admin);
Function<SignedNonce, Any> attester = sn -> {
return Any.getDefaultInstance();
};
Function<SignedNonce, Any> attester = sn -> Any.getDefaultInstance();

var gorgoneionClient = new GorgoneionClient(client, attester, parameters.clock(), admin);
var invitation = gorgoneionClient.apply(Duration.ofSeconds(2_000));
assertNotNull(invitation);
assertNotEquals(Validations.getDefaultInstance(), invitation);
assertTrue(invitation.getValidationsCount() >= context.majority());

var establishment = gorgoneionClient.apply(Duration.ofSeconds(2_000));
assertNotNull(establishment);
assertNotEquals(Validations.getDefaultInstance(), establishment);
assertTrue(establishment.getValidations().getValidationsCount() >= context.majority());
assertEquals(testMessage.getContents(),
establishment.getProvisioning().unpack(ByteMessage.class).getContents());
assertTrue(countdown.await(1, TimeUnit.SECONDS));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.salesforce.apollo.gorgoneion;

import com.codahale.metrics.Timer;
import com.google.protobuf.Any;
import com.google.protobuf.Empty;
import com.google.protobuf.Timestamp;
import com.salesforce.apollo.archipelago.Router;
Expand Down Expand Up @@ -52,6 +53,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.*;
import java.util.function.BiFunction;
import java.util.function.Predicate;

import static com.salesforce.apollo.stereotomy.event.protobuf.ProtobufEventFactory.digestOf;
Expand All @@ -72,14 +74,17 @@ public class Gorgoneion {
private final Predicate<SignedAttestation> verifier;
private final boolean bootstrap;
private final ScheduledExecutorService scheduler;
private final BiFunction<Credentials, Validations, Any> provisioner;

public Gorgoneion(boolean bootstrap, Predicate<SignedAttestation> verifier, Parameters parameters,
public Gorgoneion(boolean bootstrap, Predicate<SignedAttestation> verifier,
BiFunction<Credentials, Validations, Any> provisioner, Parameters parameters,
ControlledIdentifierMember member, Context<Member> context, ProtoEventObserver observer,
Router router, GorgoneionMetrics metrics) {
this(bootstrap, verifier, parameters, member, context, observer, router, metrics, router);
this(bootstrap, verifier, provisioner, parameters, member, context, observer, router, metrics, router);
}

public Gorgoneion(boolean bootstrap, Predicate<SignedAttestation> verifier, Parameters parameters,
public Gorgoneion(boolean bootstrap, Predicate<SignedAttestation> verifier,
BiFunction<Credentials, Validations, Any> provisioner, Parameters parameters,
ControlledIdentifierMember member, Context<Member> context, ProtoEventObserver observer,
Router admissionsRouter, GorgoneionMetrics metrics, Router endorsementRouter) {
this.bootstrap = bootstrap;
Expand All @@ -88,6 +93,7 @@ public Gorgoneion(boolean bootstrap, Predicate<SignedAttestation> verifier, Para
this.context = context;
this.parameters = parameters;
this.observer = observer;
this.provisioner = provisioner;
this.scheduler = Executors.newScheduledThreadPool(1, Thread.ofVirtual().factory());

admissionsComm = admissionsRouter.create(member, context.getId(), new Admit(), ":admissions",
Expand Down Expand Up @@ -141,6 +147,13 @@ private void enroll(Notarization request) {
observer.publish(request.getKerl(), Collections.singletonList(request.getValidations()));
}

private Establishment establish(Credentials credentials, Validations validations) {
return Establishment.newBuilder()
.setValidations(validations)
.setProvisioning(provisioner.apply(credentials, validations))
.buildPartial();
}

private SignedNonce generateNonce(KERL_ application) {
final var identifier = identifier(application);
if (identifier == null) {
Expand Down Expand Up @@ -242,7 +255,7 @@ private CompletableFuture<Validations> notarize(Credentials credentials, Validat
return result;
}

private Validations register(Credentials request) {
private Establishment register(Credentials request) {
final var kerl = request.getAttestation().getAttestation().getKerl();
final var identifier = identifier(kerl);
if (identifier == null) {
Expand Down Expand Up @@ -280,7 +293,7 @@ private Validations register(Credentials request) {
}
}, parameters.frequency());
try {
return validated.thenCompose(v -> notarize(request, v)).get();
return validated.thenCompose(v -> notarize(request, v)).thenApply(v -> establish(request, v)).get();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return null;
Expand Down Expand Up @@ -336,7 +349,7 @@ public void apply(KERL_ request, Digest from, StreamObserver<SignedNonce> respon
}

@Override
public void register(Credentials request, Digest from, StreamObserver<Validations> responseObserver,
public void register(Credentials request, Digest from, StreamObserver<Establishment> responseObserver,
Timer.Context timer) {
if (!validate(request, from)) {
log.warn("Invalid credentials from: {} on: {}", from, member.getId());
Expand All @@ -345,12 +358,12 @@ public void register(Credentials request, Digest from, StreamObserver<Validation
return;
}
try {
Validations invite = Gorgoneion.this.register(request);
if (invite == null) {
var estalishment = Gorgoneion.this.register(request);
if (estalishment == null) {
responseObserver.onError(
new StatusRuntimeException(Status.UNAUTHENTICATED.withDescription("Invalid credentials")));
} else {
responseObserver.onNext(invite);
responseObserver.onNext(estalishment);
responseObserver.onCompleted();
}
} catch (StatusRuntimeException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@
*/
package com.salesforce.apollo.gorgoneion.comm.admissions;

import com.salesforce.apollo.gorgoneion.proto.AdmissionsGrpc.AdmissionsImplBase;
import com.salesforce.apollo.gorgoneion.proto.Credentials;
import com.salesforce.apollo.gorgoneion.proto.SignedNonce;
import com.salesforce.apollo.stereotomy.event.proto.KERL_;
import com.salesforce.apollo.stereotomy.event.proto.Validations;
import com.salesforce.apollo.archipelago.RoutableService;
import com.salesforce.apollo.cryptography.Digest;
import com.salesforce.apollo.gorgoneion.comm.GorgoneionMetrics;
import com.salesforce.apollo.gorgoneion.proto.AdmissionsGrpc.AdmissionsImplBase;
import com.salesforce.apollo.gorgoneion.proto.Credentials;
import com.salesforce.apollo.gorgoneion.proto.Establishment;
import com.salesforce.apollo.gorgoneion.proto.SignedNonce;
import com.salesforce.apollo.protocols.ClientIdentity;

import com.salesforce.apollo.stereotomy.event.proto.KERL_;
import io.grpc.stub.StreamObserver;

/**
Expand Down Expand Up @@ -51,7 +50,7 @@ public void apply(KERL_ application, StreamObserver<SignedNonce> responseObserve
}

@Override
public void register(Credentials request, StreamObserver<Validations> responseObserver) {
public void register(Credentials request, StreamObserver<Establishment> responseObserver) {
var timer = metrics == null ? null : metrics.registerDuration().time();
if (metrics != null) {
var serializedSize = request.getSerializedSize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
package com.salesforce.apollo.gorgoneion.comm.admissions;

import com.codahale.metrics.Timer.Context;
import com.salesforce.apollo.cryptography.Digest;
import com.salesforce.apollo.gorgoneion.proto.Credentials;
import com.salesforce.apollo.gorgoneion.proto.Establishment;
import com.salesforce.apollo.gorgoneion.proto.SignedNonce;
import com.salesforce.apollo.stereotomy.event.proto.KERL_;
import com.salesforce.apollo.stereotomy.event.proto.Validations;
import com.salesforce.apollo.cryptography.Digest;

import io.grpc.stub.StreamObserver;

/**
Expand All @@ -22,6 +21,6 @@ public interface AdmissionsService {

void apply(KERL_ application, Digest from, StreamObserver<SignedNonce> responseObserver, Context timer);

void register(Credentials request, Digest from, StreamObserver<Validations> responseObserver, Context timer);
void register(Credentials request, Digest from, StreamObserver<Establishment> responseObserver, Context timer);

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
*/
package com.salesforce.apollo.gorgoneion;

import com.salesforce.apollo.archipelago.Link;
import com.salesforce.apollo.gorgoneion.proto.Credentials;
import com.salesforce.apollo.gorgoneion.proto.Establishment;
import com.salesforce.apollo.gorgoneion.proto.SignedNonce;
import com.salesforce.apollo.stereotomy.event.proto.KERL_;
import com.salesforce.apollo.stereotomy.event.proto.Validations;
import com.salesforce.apollo.archipelago.Link;
import com.salesforce.apollo.membership.Member;
import com.salesforce.apollo.stereotomy.event.proto.KERL_;

import java.io.IOException;
import java.time.Duration;
Expand Down Expand Up @@ -39,13 +39,13 @@ public Member getMember() {
}

@Override
public Validations register(Credentials credentials, Duration timeout) {
public Establishment register(Credentials credentials, Duration timeout) {
return null;
}
};
}

SignedNonce apply(KERL_ application, Duration timeout);

Validations register(Credentials credentials, Duration timeout);
Establishment register(Credentials credentials, Duration timeout);
}
Loading

0 comments on commit 9347485

Please sign in to comment.