Moving class to avoid circular references.

Rename type parameters.
Improve readability.

Signed-off-by: Max Lambrecht <maxlambrecht@gmail.com>
This commit is contained in:
Max Lambrecht 2020-06-08 14:37:02 -03:00
parent 28ad4c0c0c
commit d68c17caaa
4 changed files with 15 additions and 28 deletions

View File

@ -1,4 +1,4 @@
package spiffe.workloadapi.internal; package spiffe.workloadapi;
import com.google.protobuf.ByteString; import com.google.protobuf.ByteString;
import lombok.val; import lombok.val;
@ -11,7 +11,6 @@ import spiffe.exception.X509SvidException;
import spiffe.spiffeid.SpiffeId; import spiffe.spiffeid.SpiffeId;
import spiffe.spiffeid.TrustDomain; import spiffe.spiffeid.TrustDomain;
import spiffe.svid.x509svid.X509Svid; import spiffe.svid.x509svid.X509Svid;
import spiffe.workloadapi.X509Context;
import spiffe.workloadapi.grpc.Workload; import spiffe.workloadapi.grpc.Workload;
import java.security.KeyException; import java.security.KeyException;
@ -23,16 +22,16 @@ import java.util.Map;
/** /**
* Utility methods for converting GRPC objects to JAVA-SPIFFE domain objects. * Utility methods for converting GRPC objects to JAVA-SPIFFE domain objects.
*/ */
public class GrpcConversionUtils { class GrpcConversionUtils {
public static X509Context toX509Context(Workload.X509SVIDResponse x509SVIDResponse) throws CertificateException, X509SvidException { static X509Context toX509Context(Workload.X509SVIDResponse x509SVIDResponse) throws CertificateException, X509SvidException {
List<X509Svid> x509SvidList = getListOfX509Svid(x509SVIDResponse); List<X509Svid> x509SvidList = getListOfX509Svid(x509SVIDResponse);
List<X509Bundle> x509BundleList = getListOfX509Bundles(x509SVIDResponse); List<X509Bundle> x509BundleList = getListOfX509Bundles(x509SVIDResponse);
X509BundleSet bundleSet = X509BundleSet.of(x509BundleList); X509BundleSet bundleSet = X509BundleSet.of(x509BundleList);
return new X509Context(x509SvidList, bundleSet); return new X509Context(x509SvidList, bundleSet);
} }
private static List<X509Bundle> getListOfX509Bundles(Workload.X509SVIDResponse x509SVIDResponse) throws CertificateException { static List<X509Bundle> getListOfX509Bundles(Workload.X509SVIDResponse x509SVIDResponse) throws CertificateException {
List<X509Bundle> x509BundleList = new ArrayList<>(); List<X509Bundle> x509BundleList = new ArrayList<>();
for (Workload.X509SVID x509SVID : x509SVIDResponse.getSvidsList()) { for (Workload.X509SVID x509SVID : x509SVIDResponse.getSvidsList()) {
SpiffeId spiffeId = SpiffeId.parse(x509SVID.getSpiffeId()); SpiffeId spiffeId = SpiffeId.parse(x509SVID.getSpiffeId());

View File

@ -17,7 +17,6 @@ import spiffe.workloadapi.grpc.SpiffeWorkloadAPIGrpc;
import spiffe.workloadapi.grpc.SpiffeWorkloadAPIGrpc.SpiffeWorkloadAPIBlockingStub; import spiffe.workloadapi.grpc.SpiffeWorkloadAPIGrpc.SpiffeWorkloadAPIBlockingStub;
import spiffe.workloadapi.grpc.SpiffeWorkloadAPIGrpc.SpiffeWorkloadAPIStub; import spiffe.workloadapi.grpc.SpiffeWorkloadAPIGrpc.SpiffeWorkloadAPIStub;
import spiffe.workloadapi.grpc.Workload; import spiffe.workloadapi.grpc.Workload;
import spiffe.workloadapi.internal.GrpcConversionUtils;
import spiffe.workloadapi.internal.GrpcManagedChannelFactory; import spiffe.workloadapi.internal.GrpcManagedChannelFactory;
import spiffe.workloadapi.internal.ManagedChannelWrapper; import spiffe.workloadapi.internal.ManagedChannelWrapper;
import spiffe.workloadapi.internal.SecurityHeaderInterceptor; import spiffe.workloadapi.internal.SecurityHeaderInterceptor;

View File

@ -10,13 +10,13 @@ public class SecurityHeaderInterceptor implements ClientInterceptor {
* Intercepts the call to the WorkloadAPI and add the required security header * Intercepts the call to the WorkloadAPI and add the required security header
*/ */
@Override @Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) { public <R,S> ClientCall<R,S> interceptCall(MethodDescriptor<R,S> method, CallOptions callOptions, Channel next) {
return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) { return new ForwardingClientCall.SimpleForwardingClientCall<R,S>(next.newCall(method, callOptions)) {
@Override @Override
public void start(Listener<RespT> responseListener, Metadata headers) { public void start(Listener<S> responseListener, Metadata headers) {
Metadata.Key<String> headerKey = Metadata.Key.of(SECURITY_HEADER, Metadata.ASCII_STRING_MARSHALLER); Metadata.Key<String> headerKey = Metadata.Key.of(SECURITY_HEADER, Metadata.ASCII_STRING_MARSHALLER);
headers.put(headerKey, "true"); headers.put(headerKey, "true");
super.start(new ForwardingClientCallListener.SimpleForwardingClientCallListener<RespT>(responseListener) {}, headers); super.start(new ForwardingClientCallListener.SimpleForwardingClientCallListener<S>(responseListener) {}, headers);
} }
}; };
} }

View File

@ -43,13 +43,9 @@ public class SpiffeTrustManagerFactory extends TrustManagerFactorySpi {
*/ */
@Override @Override
public TrustManager[] engineGetTrustManagers() { public TrustManager[] engineGetTrustManagers() {
SpiffeTrustManager spiffeTrustManager = SpiffeTrustManager spiffeTrustManager = null;
null;
try { try {
spiffeTrustManager = new SpiffeTrustManager( spiffeTrustManager = new SpiffeTrustManager(X509SourceManager.getX509Source(), this::getAcceptedSpiffeIds);
X509SourceManager.getX509Source(),
this::getAcceptedSpiffeIds
);
} catch (X509SourceException e) { } catch (X509SourceException e) {
throw new SpiffeProviderException("The X509 source could not be created", e); throw new SpiffeProviderException("The X509 source could not be created", e);
} catch (SocketEndpointAddressException e) { } catch (SocketEndpointAddressException e) {
@ -79,7 +75,7 @@ public class SpiffeTrustManagerFactory extends TrustManagerFactorySpi {
* with a function verify a chain of certificates using a to validate the SPIFFE IDs * with a function verify a chain of certificates using a to validate the SPIFFE IDs
* of the peer's certificates, and a supplier of accepted SPIFFE IDs. * of the peer's certificates, and a supplier of accepted SPIFFE IDs.
* *
* @param x509BundleSource a {@link X509BundleSource} to provide the X.509-Bundles * @param x509BundleSource a {@link X509BundleSource} to provide the X.509-Bundles
* @param acceptedSpiffeIdsSupplier a Supplier to provide a List of SPIFFE IDs that are accepted * @param acceptedSpiffeIdsSupplier a Supplier to provide a List of SPIFFE IDs that are accepted
* @return a TrustManager array with an initialized TrustManager. * @return a TrustManager array with an initialized TrustManager.
*/ */
@ -87,17 +83,10 @@ public class SpiffeTrustManagerFactory extends TrustManagerFactorySpi {
X509BundleSource x509BundleSource, X509BundleSource x509BundleSource,
Supplier<List<SpiffeId>> acceptedSpiffeIdsSupplier) { Supplier<List<SpiffeId>> acceptedSpiffeIdsSupplier) {
Supplier<List<SpiffeId>> spiffeIdsSupplier; final Supplier<List<SpiffeId>> spiffeIdsSupplier =
if (acceptedSpiffeIdsSupplier != null) { acceptedSpiffeIdsSupplier != null ? acceptedSpiffeIdsSupplier : this::getAcceptedSpiffeIds;
spiffeIdsSupplier = acceptedSpiffeIdsSupplier;
} else { val spiffeTrustManager = new SpiffeTrustManager(x509BundleSource, spiffeIdsSupplier);
spiffeIdsSupplier = this::getAcceptedSpiffeIds;
}
val spiffeTrustManager =
new SpiffeTrustManager(
x509BundleSource,
spiffeIdsSupplier
);
return new TrustManager[]{spiffeTrustManager}; return new TrustManager[]{spiffeTrustManager};
} }