Improving validations to channel address
This commit is contained in:
parent
218b8dc079
commit
618cc0e022
|
|
@ -1,5 +1,6 @@
|
||||||
package spiffe.api.svid;
|
package spiffe.api.svid;
|
||||||
|
|
||||||
|
import com.google.common.base.Preconditions;
|
||||||
import io.grpc.*;
|
import io.grpc.*;
|
||||||
import io.grpc.netty.NegotiationType;
|
import io.grpc.netty.NegotiationType;
|
||||||
import io.grpc.netty.NettyChannelBuilder;
|
import io.grpc.netty.NettyChannelBuilder;
|
||||||
|
|
@ -14,6 +15,9 @@ import org.apache.commons.lang3.SystemUtils;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
|
|
||||||
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
import static com.google.common.base.Preconditions.checkState;
|
||||||
|
import static java.lang.String.format;
|
||||||
import static org.apache.commons.lang3.StringUtils.isBlank;
|
import static org.apache.commons.lang3.StringUtils.isBlank;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -40,10 +44,10 @@ class SpiffeEndpointChannelBuilder {
|
||||||
URI parsedAddress = parseAddress(spiffeEndpointAddress);
|
URI parsedAddress = parseAddress(spiffeEndpointAddress);
|
||||||
if (isTcp(parsedAddress)) {
|
if (isTcp(parsedAddress)) {
|
||||||
return createTcpChannel(parsedAddress);
|
return createTcpChannel(parsedAddress);
|
||||||
} else {
|
|
||||||
validateUDSAddress(parsedAddress);
|
|
||||||
return createNativeSocketChannel(parsedAddress);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
validateUDSAddress(parsedAddress);
|
||||||
|
return createNativeSocketChannel(parsedAddress);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -54,7 +58,7 @@ class SpiffeEndpointChannelBuilder {
|
||||||
private static String getAddressFromEnv() {
|
private static String getAddressFromEnv() {
|
||||||
String address = System.getenv(ENV_ADDRESS_VAR);
|
String address = System.getenv(ENV_ADDRESS_VAR);
|
||||||
if (isBlank(address)) {
|
if (isBlank(address)) {
|
||||||
throw new IllegalStateException(ENV_ADDRESS_VAR + " env var is not defined");
|
throw new IllegalStateException(format("%s env var is not defined", ENV_ADDRESS_VAR ));
|
||||||
}
|
}
|
||||||
return address;
|
return address;
|
||||||
}
|
}
|
||||||
|
|
@ -66,6 +70,7 @@ class SpiffeEndpointChannelBuilder {
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private static ManagedChannel createTcpChannel(URI parsedAddress) {
|
private static ManagedChannel createTcpChannel(URI parsedAddress) {
|
||||||
|
checkNotNull(parsedAddress, "UDS address is null" );
|
||||||
return NettyChannelBuilder.forAddress(parsedAddress.getHost(), parsedAddress.getPort())
|
return NettyChannelBuilder.forAddress(parsedAddress.getHost(), parsedAddress.getPort())
|
||||||
.negotiationType(NegotiationType.PLAINTEXT)
|
.negotiationType(NegotiationType.PLAINTEXT)
|
||||||
.build();
|
.build();
|
||||||
|
|
@ -77,6 +82,7 @@ class SpiffeEndpointChannelBuilder {
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private static ManagedChannel createNativeSocketChannel(URI spiffeEndpointAddress) {
|
private static ManagedChannel createNativeSocketChannel(URI spiffeEndpointAddress) {
|
||||||
|
checkNotNull(spiffeEndpointAddress, "UDS address is null" );
|
||||||
NettyChannelBuilder channelBuilder = NettyChannelBuilder.
|
NettyChannelBuilder channelBuilder = NettyChannelBuilder.
|
||||||
forAddress(new DomainSocketAddress(spiffeEndpointAddress.getPath()));
|
forAddress(new DomainSocketAddress(spiffeEndpointAddress.getPath()));
|
||||||
configureNativeSocketChannel(channelBuilder);
|
configureNativeSocketChannel(channelBuilder);
|
||||||
|
|
@ -113,15 +119,11 @@ class SpiffeEndpointChannelBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void validateUDSAddress(URI address) {
|
private static void validateUDSAddress(URI address) {
|
||||||
if (!isBlank(address.getHost())) {
|
checkNotNull(address, "UDS address is null" );
|
||||||
throw new IllegalArgumentException("Unexpected Authority component in Unix uri: " + address.getHost());
|
checkState(isBlank(address.getHost()),
|
||||||
}
|
format("Unexpected Authority component in Unix uri: %s", address.getHost() ) );
|
||||||
if (isBlank(address.getPath())) {
|
checkState(!isBlank(address.getPath()), "No Path defined for Unix uri");
|
||||||
throw new IllegalArgumentException("No Path defined for Unix uri");
|
checkState(address.getPath().startsWith("/"), "Unix Socket Path not absolute");
|
||||||
}
|
|
||||||
if (!address.getPath().startsWith("/")) {
|
|
||||||
throw new IllegalArgumentException("Unix Socket Path not absolute");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isTcp(URI spiffeEndpointAddress) {
|
private static boolean isTcp(URI spiffeEndpointAddress) {
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,8 @@ class SpiffeWorkloadStub {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
* @param spiffeEndpointAddress where the WorkloadAPI is listening
|
* @param spiffeEndpointAddress where the WorkloadAPI is listening. It's validated by
|
||||||
|
* the channel builder to ensure that it's a correct UDS or TCP address
|
||||||
*/
|
*/
|
||||||
SpiffeWorkloadStub(String spiffeEndpointAddress) {
|
SpiffeWorkloadStub(String spiffeEndpointAddress) {
|
||||||
ManagedChannel managedChannel = SpiffeEndpointChannelBuilder.newChannel(spiffeEndpointAddress);
|
ManagedChannel managedChannel = SpiffeEndpointChannelBuilder.newChannel(spiffeEndpointAddress);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue