-
Notifications
You must be signed in to change notification settings - Fork 77
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
feat: capture env variables into sigleton and reusable constants #2956
Conversation
Signed-off-by: nikolay <[email protected]>
Signed-off-by: nikolay <[email protected]>
Signed-off-by: nikolay <[email protected]>
Signed-off-by: nikolay <[email protected]>
Signed-off-by: nikolay <[email protected]>
Signed-off-by: nikolay <[email protected]>
Signed-off-by: nikolay <[email protected]>
Signed-off-by: nikolay <[email protected]>
# Conflicts: # packages/relay/src/lib/services/hbarLimitService/index.ts # packages/relay/tests/lib/services/hbarLimitService/hbarLimitService.spec.ts
219bc0d
to
d75063a
Compare
Signed-off-by: nikolay <[email protected]>
Signed-off-by: nikolay <[email protected]>
Signed-off-by: nikolay <[email protected]>
Signed-off-by: nikolay <[email protected]>
Signed-off-by: nikolay <[email protected]>
Signed-off-by: nikolay <[email protected]>
Quality Gate failedFailed conditions |
3ccaf6b
to
3b2c5e8
Compare
Signed-off-by: nikolay <[email protected]>
# Conflicts: # packages/relay/src/lib/constants.ts # packages/relay/src/lib/eth.ts # packages/relay/tests/lib/eth/eth_call.spec.ts # packages/relay/tests/lib/ethGetBlockBy.spec.ts # packages/relay/tests/lib/repositories/hbarLimiter/ethAddressHbarSpendingPlanRepository.spec.ts # packages/relay/tests/lib/repositories/hbarLimiter/hbarSpendingPlanRepository.spec.ts # packages/relay/tests/lib/services/cacheService/cacheService.spec.ts
Signed-off-by: nikolay <[email protected]>
Signed-off-by: nikolay <[email protected]>
Signed-off-by: nikolay <[email protected]>
Signed-off-by: nikolay <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work.
I agree with most of Victors comments
Signed-off-by: nikolay <[email protected]>
Signed-off-by: nikolay <[email protected]>
Signed-off-by: nikolay <[email protected]>
# Conflicts: # packages/relay/src/lib/services/hapiService/hapiService.ts # packages/server/tests/acceptance/hbarLimiter.spec.ts # packages/server/tests/acceptance/index.spec.ts # packages/server/tests/acceptance/rpc_batch3.spec.ts # packages/ws-server/tests/acceptance/index.spec.ts
Signed-off-by: nikolay <[email protected]>
Signed-off-by: nikolay <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for doing this. I think we should also have a --help option that can further explain this environment variables and perhaps include examples. It should probably done in another github issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with all comments from @victor-yanev and @Nana-EC
maybe I was not part of the discussion and design however, would like to express my opinions.
I agree that this should be used by all other packages ( relay, server and ws-server) hence the need to keep it as a separate package itself and not only as an internal service of the server
package.
When I thought about the need of this, did so thinking it as a a Service and calling it ConfigService
that had the following responsibilities:
- Read
.env
file - Make sure all required configurations are present.
- Store defaults and parse override.
- "Know" what configurations are available for what services/packages.
- Print at startup the onto the logs the
FINAL
values for all needed configurations so is easy to debug and troubleshoot, and using the logs an operator is able to see what configurations are being used. (Secrets should be obscured before printing, so the configService provider should also know which ones are secrets) - Provide an interface for getting such values.
I understand that the scope of this PR might be different and as I said, am late to the discussion, however would like my comments to be taken into consideration to see what it makes sense and what not, if anything I would strongly argue against as a just passtrough service to GET
the env values, but to make it a full fledged Configuration Service provider that is aware of the config values, its defaults and exposes an Enum or record with an existing list of configs, this way we minimize the risk of duplications, we fail fast if parsing fails, and we print the values for the envs (except secrets those should be oscured)
example of usage in my mind:
instead of:
const shouldDefaultToConsensus = EnvProvider.get('ETH_CALL_DEFAULT_TO_CONSENSUS_NODE') === 'true';
it would be:
const shouldDefaultToConsensus = ConfigService.get(RelayConfigs.ETH_CALL_DEFAULT_TO_CONSENSUS_NODE);
(where RelayConfigs is a known enum) and the values are guaranteed to be present by either defaults overrides or fast failing at (ServiceConfig startup).
Signed-off-by: nikolay <[email protected]>
# Conflicts: # package-lock.json
Signed-off-by: nikolay <[email protected]>
Signed-off-by: nikolay <[email protected]>
Solid observations and proposals 🙏. Our next actions are defined here #3023 and they cover almost all of your concerns. We'll stick to your proposals and ping you for a PR review, once we're ready. It'll be done in another PR because that one will become enormously bigger. cc: @ebadiere |
|
||
chai.use(chaiAsPromised); | ||
|
||
describe('ConfigService tests', async function () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a test for non-existing env?
# Conflicts: # packages/relay/src/formatters.ts # packages/relay/src/lib/clients/cache/localLRUCache.ts # packages/relay/src/lib/clients/cache/redisCache.ts # packages/relay/src/lib/clients/mirrorNodeClient.ts # packages/relay/src/lib/clients/sdkClient.ts # packages/relay/src/lib/hbarlimiter/index.ts # packages/relay/src/lib/poller.ts # packages/relay/src/lib/relay.ts # packages/relay/src/lib/services/cacheService/cacheService.ts # packages/relay/src/lib/services/debugService/index.ts # packages/relay/src/lib/services/ethService/ethCommonService/index.ts # packages/relay/src/lib/services/ethService/ethFilterService/index.ts # packages/relay/src/lib/services/hapiService/hapiService.ts # packages/relay/src/lib/services/hbarLimitService/index.ts # packages/relay/src/lib/services/metricService/metricService.ts # packages/relay/src/lib/subscriptionController.ts # packages/relay/src/utils.ts # packages/relay/tests/lib/clients/localLRUCache.spec.ts # packages/relay/tests/lib/eth/eth_call.spec.ts # packages/relay/tests/lib/eth/eth_estimateGas.spec.ts # packages/relay/tests/lib/eth/eth_gasPrice.spec.ts # packages/relay/tests/lib/eth/eth_getBlockByNumber.spec.ts # packages/relay/tests/lib/eth/eth_getLogs.spec.ts # packages/relay/tests/lib/eth/eth_getTransactionByBlockHashAndIndex.spec.ts # packages/relay/tests/lib/eth/eth_getTransactionByBlockNumberAndIndex.spec.ts # packages/relay/tests/lib/eth/eth_getTransactionByHash.spec.ts # packages/relay/tests/lib/eth/eth_sendRawTransaction.spec.ts # packages/relay/tests/lib/hbarLimiter.spec.ts # packages/relay/tests/lib/mirrorNodeClient.spec.ts # packages/relay/tests/lib/openrpc.spec.ts # packages/relay/tests/lib/precheck.spec.ts # packages/relay/tests/lib/services/eth/filter.spec.ts # packages/relay/tests/lib/web3.spec.ts # packages/server/src/koaJsonRpc/index.ts # packages/server/src/koaJsonRpc/lib/methodConfiguration.ts # packages/server/src/server.ts # packages/server/tests/acceptance/cacheService.spec.ts # packages/server/tests/acceptance/conformityTests.spec.ts # packages/server/tests/acceptance/estimateGasPrecompile.spec.ts # packages/server/tests/acceptance/hbarLimiter.spec.ts # packages/server/tests/acceptance/htsPrecompile/precompileCalls.spec.ts # packages/server/tests/acceptance/index.spec.ts # packages/server/tests/acceptance/rateLimiter.spec.ts # packages/server/tests/acceptance/rpc_batch1.spec.ts # packages/server/tests/clients/relayClient.ts # packages/server/tests/helpers/utils.ts # packages/ws-server/src/controllers/eth_subscribe.ts # packages/ws-server/src/controllers/index.ts # packages/ws-server/src/metrics/connectionLimiter.ts # packages/ws-server/src/utils/utils.ts # packages/ws-server/src/webSocketServer.ts # packages/ws-server/tests/acceptance/getTransactionByHash.spec.ts # packages/ws-server/tests/acceptance/getTransactionCount.spec.ts # packages/ws-server/tests/acceptance/getTransactionReceipt.spec.ts # packages/ws-server/tests/acceptance/index.spec.ts # packages/ws-server/tests/acceptance/sendRawTransaction.spec.ts # packages/ws-server/tests/acceptance/subscribe.spec.ts # packages/ws-server/tests/acceptance/subscribeNewHeads.spec.ts # packages/ws-server/tests/unit/utils.spec.ts # packages/ws-server/tests/unit/validations.spec.ts
Signed-off-by: nikolay <[email protected]>
Signed-off-by: nikolay <[email protected]>
Quality Gate failedFailed conditions |
Description:
Currently, we are dynamically getting the environment variables and parsing/casting them every time. This is inefficient and also, more importantly, allows env configurations to be dynamically changed, which can modify the behavior of the relay based on the current values of the env vars, which can be a potential exploit.
Solution:
We should change this to only capture them once when the relay is initialized and reuse those as constants
Related issue(s):
Fixes #2686
Notes for reviewer:
env-provider
singletonenv-provider
package is added as a dependency torelay
,server
, andws-server
env-provider
must be immutable and should not provide an interface for overriding or extending once-loaded environment variables but there are many tests where we dynamically override envs in order to test specific cases so we should add these interfaces as wellenv-provider
packageprocess.env.<VAR_NAME>
toEnvProviderService.getInstance().get(<VAR_NAME>)
process.env.CHAIN_ID = '0x160c'
is transformed toEnvProviderService.getInstance().dynamicOverride('CHAIN_ID', '0x160c')
Checklist