Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for service authority #1

Open
wants to merge 3 commits into
base: booking-main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libs/providers/flagd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Options can be defined in the constructor or as environment variables. Construct
| selector | FLAGD_SOURCE_SELECTOR | string | - | |
| cache | FLAGD_CACHE | string | lru | lru, disabled |
| maxCacheSize | FLAGD_MAX_CACHE_SIZE | int | 1000 | |
| serviceAuthority | FLAGD_SERVICE_AUTHORITY | string | - | rpc, in-process |

#### Resolver type-specific Defaults

Expand Down
4 changes: 4 additions & 0 deletions libs/providers/flagd/src/lib/configuration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('Configuration', () => {
const resolverType = 'in-process';
const selector = 'app=weather';
const offlineFlagSourcePath = '/tmp/flags.json';
const serviceAuthority = 'test-service2';

process.env['FLAGD_HOST'] = host;
process.env['FLAGD_PORT'] = `${port}`;
Expand All @@ -41,6 +42,7 @@ describe('Configuration', () => {
process.env['FLAGD_SOURCE_SELECTOR'] = `${selector}`;
process.env['FLAGD_RESOLVER'] = `${resolverType}`;
process.env['FLAGD_OFFLINE_FLAG_SOURCE_PATH'] = offlineFlagSourcePath;
process.env['FLAGD_SERVICE_AUTHORITY'] = serviceAuthority;

expect(getConfig()).toStrictEqual({
host,
Expand All @@ -52,6 +54,7 @@ describe('Configuration', () => {
resolverType,
selector,
offlineFlagSourcePath,
serviceAuthority,
});
});

Expand All @@ -64,6 +67,7 @@ describe('Configuration', () => {
cache: 'lru',
resolverType: 'rpc',
selector: '',
serviceAuthority: '',
};

process.env['FLAGD_HOST'] = 'override';
Expand Down
11 changes: 11 additions & 0 deletions libs/providers/flagd/src/lib/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ export interface Config {
* @default 1000
*/
maxCacheSize?: number;

/**
* The target host (authority) when routing requests through a proxy (e.g. Envoy)
*
* @default ''
*/
serviceAuthority?: string;
}

export type FlagdProviderOptions = Partial<Config>;
Expand All @@ -94,6 +101,7 @@ enum ENV_VAR {
FLAGD_SOURCE_SELECTOR = 'FLAGD_SOURCE_SELECTOR',
FLAGD_RESOLVER = 'FLAGD_RESOLVER',
FLAGD_OFFLINE_FLAG_SOURCE_PATH = 'FLAGD_OFFLINE_FLAG_SOURCE_PATH',
FLAGD_SERVICE_AUTHORITY = 'FLAGD_SERVICE_AUTHORITY',
}

const getEnvVarConfig = (): Partial<Config> => ({
Expand Down Expand Up @@ -124,6 +132,9 @@ const getEnvVarConfig = (): Partial<Config> => ({
...(process.env[ENV_VAR.FLAGD_OFFLINE_FLAG_SOURCE_PATH] && {
offlineFlagSourcePath: process.env[ENV_VAR.FLAGD_OFFLINE_FLAG_SOURCE_PATH],
}),
...(process.env[ENV_VAR.FLAGD_SERVICE_AUTHORITY] && {
serviceAuthority: process.env[ENV_VAR.FLAGD_SERVICE_AUTHORITY],
}),
});

export function getConfig(options: FlagdProviderOptions = {}) {
Expand Down
12 changes: 10 additions & 2 deletions libs/providers/flagd/src/lib/service/in-process/grpc/grpc-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ClientReadableStream, ServiceError, credentials } from '@grpc/grpc-js';
import { ClientReadableStream, ServiceError, credentials, ClientOptions } from '@grpc/grpc-js';
import { Logger } from '@openfeature/core';
import { GeneralError } from '@openfeature/server-sdk';
import { FlagSyncServiceClient, SyncFlagsRequest, SyncFlagsResponse } from '../../../../proto/ts/flagd/sync/v1/sync';
Expand Down Expand Up @@ -28,13 +28,21 @@ export class GrpcFetch implements DataFetch {
private _isConnected = false;

constructor(config: Config, syncServiceClient?: FlagSyncServiceClient, logger?: Logger) {
const { host, port, tls, socketPath, selector } = config;
const { host, port, tls, socketPath, selector, serviceAuthority } = config;

let clientOptions: ClientOptions | undefined;
if (serviceAuthority) {
clientOptions = {
'grpc.default_authority': serviceAuthority,
};
}

this._syncClient = syncServiceClient
? syncServiceClient
: new FlagSyncServiceClient(
socketPath ? `unix://${socketPath}` : `${host}:${port}`,
tls ? credentials.createSsl() : credentials.createInsecure(),
clientOptions,
);

this._logger = logger;
Expand Down