-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added custom gRPC resolver to support envoy proxy Signed-off-by: Pradeep Mishra <pradeepbbl@gmail.com>
- Loading branch information
1 parent
8c6e0ad
commit fb0f789
Showing
10 changed files
with
272 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
.../java/dev/openfeature/contrib/providers/flagd/resolver/common/GenericConfigException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package dev.openfeature.contrib.providers.flagd.resolver.common; | ||
|
||
/** | ||
* Custom exception for invalid gRPC configurations. | ||
*/ | ||
|
||
public class GenericConfigException extends RuntimeException { | ||
public GenericConfigException(String message) { | ||
super(message); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
.../dev/openfeature/contrib/providers/flagd/resolver/common/nameresolvers/EnvoyResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package dev.openfeature.contrib.providers.flagd.resolver.common.nameresolvers; | ||
|
||
import io.grpc.EquivalentAddressGroup; | ||
import io.grpc.NameResolver; | ||
import java.net.InetSocketAddress; | ||
import io.grpc.Attributes; | ||
import io.grpc.Status; | ||
import java.net.URI; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
/** | ||
* Envoy NameResolver, will always override the authority with the specified authority and | ||
* use the socketAddress to connect. | ||
* <p> | ||
* Custom URI Scheme: | ||
* <p> | ||
* envoy://[proxy-agent-host]:[proxy-agent-port]/[service-name] | ||
* <p> | ||
* `service-name` is used as authority instead host | ||
*/ | ||
public class EnvoyResolver extends NameResolver { | ||
private final URI uri; | ||
private final String authority; | ||
private Listener2 listener; | ||
|
||
public EnvoyResolver(URI targetUri) { | ||
this.uri = targetUri; | ||
this.authority = targetUri.getPath().substring(1); | ||
} | ||
|
||
@Override | ||
public String getServiceAuthority() { | ||
return authority; | ||
} | ||
|
||
@Override | ||
public void shutdown() { | ||
} | ||
|
||
@Override | ||
public void start(Listener2 listener) { | ||
this.listener = listener; | ||
this.resolve(); | ||
} | ||
|
||
@Override | ||
public void refresh() { | ||
this.resolve(); | ||
} | ||
|
||
private void resolve() { | ||
try { | ||
InetSocketAddress address = new InetSocketAddress(this.uri.getHost(), this.uri.getPort()); | ||
Attributes addressGroupAttributes = Attributes.newBuilder() | ||
.set(EquivalentAddressGroup.ATTR_AUTHORITY_OVERRIDE, this.authority) | ||
.build(); | ||
List<EquivalentAddressGroup> equivalentAddressGroup = Collections.singletonList( | ||
new EquivalentAddressGroup(address, addressGroupAttributes) | ||
); | ||
ResolutionResult resolutionResult = ResolutionResult.newBuilder() | ||
.setAddresses(equivalentAddressGroup) | ||
.build(); | ||
this.listener.onResult(resolutionResult); | ||
} catch (Exception e) { | ||
this.listener.onError(Status.UNAVAILABLE.withDescription("Unable to resolve host ").withCause(e)); | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...nfeature/contrib/providers/flagd/resolver/common/nameresolvers/EnvoyResolverProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package dev.openfeature.contrib.providers.flagd.resolver.common.nameresolvers; | ||
|
||
import io.grpc.NameResolver; | ||
import io.grpc.NameResolverProvider; | ||
import java.net.URI; | ||
|
||
public class EnvoyResolverProvider extends NameResolverProvider { | ||
static final String ENVOY_SCHEME = "envoy"; | ||
static final String DEFAULT_SERVICE_NAME = "undefined"; | ||
@Override | ||
protected boolean isAvailable() { | ||
return true; | ||
} | ||
|
||
@Override | ||
protected int priority() { | ||
return 6; | ||
} | ||
|
||
@Override | ||
public NameResolver newNameResolver(URI targetUri, NameResolver.Args args) { | ||
if (!ENVOY_SCHEME.equals(targetUri.getScheme())) { | ||
return null; | ||
} | ||
|
||
if (!isValidPath(targetUri.getPath()) || targetUri.getHost() == null || targetUri.getPort() == -1 ) { | ||
throw new IllegalArgumentException("Incorrectly formatted target uri; " | ||
+ "expected: '" + ENVOY_SCHEME + ":[//]<proxy-agent-host>:<proxy-agent-port>/<service-name>';" | ||
+ "but was '" + targetUri + "'"); | ||
} | ||
|
||
return new EnvoyResolver(targetUri); | ||
} | ||
|
||
@Override | ||
public String getDefaultScheme() { | ||
return ENVOY_SCHEME; | ||
} | ||
|
||
private static boolean isValidPath(String path) { | ||
return !path.isEmpty() && !path.substring(1).isEmpty() | ||
&& !path.substring(1).contains("/"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...ture/contrib/providers/flagd/resolver/common/nameresolvers/EnvoyResolverProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package dev.openfeature.contrib.providers.flagd.resolver.common.nameresolvers; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.stream.Stream; | ||
|
||
class EnvoyResolverProviderTest { | ||
private final EnvoyResolverProvider provider = new EnvoyResolverProvider(); | ||
|
||
@Test | ||
void envoyProviderTestScheme() { | ||
Assertions.assertTrue(provider.isAvailable()); | ||
Assertions.assertNotNull(provider.newNameResolver(URI.create("envoy://localhost:1234/foo.service"), | ||
null)); | ||
Assertions.assertNull(provider.newNameResolver(URI.create("invalid-scheme://localhost:1234/foo.service"), | ||
null)); | ||
} | ||
|
||
|
||
@ParameterizedTest | ||
@MethodSource("getInvalidPaths") | ||
void invalidTargetUriTests(String mockUri) { | ||
Exception exception = Assertions.assertThrows(IllegalArgumentException.class, () -> { | ||
provider.newNameResolver(URI.create(mockUri), null); | ||
}); | ||
|
||
Assertions.assertTrue(exception.toString().contains("Incorrectly formatted target uri")); | ||
} | ||
|
||
private static Stream<Arguments> getInvalidPaths() { | ||
return Stream.of( | ||
Arguments.of("envoy://localhost:1234/test.service/test"), | ||
Arguments.of("envoy://localhost:1234/"), | ||
Arguments.of("envoy://localhost:1234"), | ||
Arguments.of("envoy://localhost/test.service"), | ||
Arguments.of("envoy:///test.service") | ||
); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
.../openfeature/contrib/providers/flagd/resolver/common/nameresolvers/EnvoyResolverTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package dev.openfeature.contrib.providers.flagd.resolver.common.nameresolvers; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import java.net.URI; | ||
|
||
class EnvoyResolverTest { | ||
@Test | ||
void envoyResolverTest() { | ||
// given | ||
EnvoyResolver envoyResolver = new EnvoyResolver(URI.create("envoy://localhost:1234/foo.service")); | ||
|
||
// then | ||
Assertions.assertEquals("foo.service", envoyResolver.getServiceAuthority()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters