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

Inline script is not supported by eventsource yaml. (http.yaml) #794

Closed
pavan-godspeed opened this issue Dec 7, 2023 · 1 comment
Closed
Assignees

Comments

@pavan-godspeed
Copy link
Contributor

pavan-godspeed commented Dec 7, 2023

Problem

  • Inline script isn't working in the http.yaml (src/eventsources/http.yaml). Identified while using 'jwt' and accessing secretKey from config.
  • While debugging the inline script "<%%>" is all considered as a string value to a key.

Solution

  • Need to check the core code for event sources implementation.
  • Identify why the inline script is all alone considered as a string by debugging.
  • compare the event sources core code with the data sources core code and check how the inline script is handled in data source.
  • Fix the missing code in the event sources core and test it.

Implementation

  • Identify the missing code..

datasourceLoader.ts

export default async function (
  pathString: string
): Promise<{ [key: string]: GSDataSource }> {
  let yamlDatasources = await loadYaml(pathString, false);

  const prismaDatasources = await loadPrismaDsFileNames(pathString);
  const datasourcesConfigs = { ...yamlDatasources, ...prismaDatasources };

  if (datasourcesConfigs && !Object.keys(datasourcesConfigs).length) {
    throw new Error(
      `There are no datasources defined in datasource dir: ${pathString}`
    );
  }
  const datasources: { [key: string]: GSDataSource } = {};

  for await (let dsName of Object.keys(datasourcesConfigs)) {
    logger.debug('evaluating datasource %s', dsName);
    datasourcesConfigs[dsName] = expandVariables(datasourcesConfigs[dsName]);
    logger.debug(
      'evaluated datasource %s %o',
      dsName,
      datasourcesConfigs[dsName]
    );

    // let's load the loadFn and executeFn
    // there is an assumption that for each datasource, the type's .ts file should be inside /datasources/types folder
    const fileName = datasourcesConfigs[dsName].type;

eventsourceLoader.ts

export default async function (eventsourcesFolderPath: string, datasources: PlainObject): Promise<{ [key: string]: GSEventSource | GSDataSourceAsEventSource }> {
  const eventsourcesConfigs = await loadYaml(eventsourcesFolderPath, false);
  if (eventsourcesConfigs && !Object.keys(eventsourcesConfigs).length) {
    throw new Error(`There are no event sources defined in eventsource dir: ${eventsourcesFolderPath}`);
  }

  const eventSources: { [key: string]: GSEventSource | GSDataSourceAsEventSource } = {};

  for await (let esName of Object.keys(eventsourcesConfigs)) {
    // let's load the event source
    const eventSourceConfig = eventsourcesConfigs[esName];
    try {
      const Module = await import(path.join(eventsourcesFolderPath, 'types', eventSourceConfig.type));
      const isPureEventSource = 'initClient' in Module.default.prototype;
      // const isPureEventSource = !!Object.hasOwnProperty.call(Module.default.prototype, 'initClient');
      let eventSourceInatance: GSEventSource | GSDataSourceAsEventSource;

      let Constructor = Module.default;

      if (isPureEventSource) {
        eventSourceInatance = new Constructor(eventsourcesConfigs[esName], datasources) as GSEventSource;
        if ('init' in eventSourceInatance) {
          await eventSourceInatance.init();
        }
      } else {
        let correspondingDatasource = datasources[esName]; // By design, datasource and event source need to share the same name.
        if (!correspondingDatasource) {
          throw new Error(`Corresponding data source for event source ${esName} is not defined. Please ensure a data source type exists with the same file name in /datasources directory`);
        } else {
          eventSourceInatance = new Constructor(eventsourcesConfigs[esName], correspondingDatasource.client) as GSDataSourceAsEventSource;
        }
      }

      eventSources[esName] = eventSourceInatance;
    } catch (error) {
      logger.error(error);
    }
  }
  • The expandVariable function is missing in eventsource and its responsibility to identify the inline script in <%%> and replace the data with requested data.

Checks

  • After implementing the missing code check its functionality using jwt in http.yaml.

http.yaml

type: express
port: 3000
jwt: 
  issuer: sample_iss
  audience: sample_aud
  secretOrKey: <% config.jwt_secret %>
  • define jwt secret key in the .env and access this variable from config/custom-environment-variables.
  • Use the jwt token to execute the apis which required AuthorizationBearer. If the jwt token is correct the api executes successfully which means the jwt_secret key is been accessed from config and inline script is working as expected in the eventsource yaml.
@pavan-godspeed
Copy link
Contributor Author

pavan-godspeed commented Dec 7, 2023

The PR #848 resolves this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants