1

I'm setting up an instance of the express gateway for routing requests to microservices. It works as expected, but I get the following errors when I try to include redis in my system config

0|apigateway-service  | 2020-01-09T18:50:10.118Z [EG:policy] error: Failed to initialize custom express-session store, please ensure you have connect-redis npm package installed
0|apigateway-service  | 2020-01-09T18:50:10.118Z [EG:gateway] error: Could not hot-reload gateway.config.yml. Configuration is invalid. Error: A client must be directly provided to the RedisStore
0|apigateway-service  | 2020-01-09T18:50:10.118Z [EG:gateway] warn: body-parser policy hasn't provided a schema. Validation for this policy will be skipped.
0|apigateway-service  | 2020-01-09T18:50:10.118Z [EG:policy] error: Failed to initialize custom express-session store, please ensure you have connect-redis npm package installed

I have installed the necessary packages

npm install redis connect-redis express-session

and have updated the system.config.yml file like so,

# Core
db:
  redis:
    host: ${REDIS_HOST}
    port: ${REDIS_PORT}
    db: ${REDIS_DB}
    namespace: EG

plugins:
  # express-gateway-plugin-example:
  #   param1: 'param from system.config'
  health-check:
    package: './health-check/manifest.js'
  body-parser:
    package: './body-parser/manifest.js'

crypto:
  cipherKey: sensitiveKey
  algorithm: aes256
  saltRounds: 10

# OAuth2 Settings
session:
  storeProvider: connect-redis
  storeOptions:
    host: ${REDIS_HOST}
    port: ${REDIS_PORT}
    db: ${REDIS_DB}
  secret: keyboard cat # replace with secure key that will be used to sign session cookie
  resave: false
  saveUninitialized: false
accessTokens:
  timeToExpiry: 7200000
refreshTokens:
  timeToExpiry: 7200000
authorizationCodes:
  timeToExpiry: 300000

My gateway.config.yml file looks like this

http:
  port: 8080
admin:
  port: 9876
apiEndpoints:
  accounts:
    paths: '/accounts*'
  billing:
    paths: '/billing*'

serviceEndpoints:
  accounts:
    url: ${ACCOUNTS_URL}
  billing:
    url: ${BILLING_URL}

policies:
  - body-parser
  - basic-auth
  - cors
  - expression
  - key-auth
  - log
  - oauth2
  - proxy
  - rate-limit

pipelines:
  accounts:
    apiEndpoints:
      - accounts
    policies:
    # Uncomment `key-auth:` when instructed to in the Getting Started guide.
    # - key-auth:
      - body-parser:
      - log: # policy name
        - action:    # array of condition/actions objects
        message: ${req.method} ${req.originalUrl} ${JSON.stringify(req.body)} # parameter for log action
      - proxy:
        - action:
            serviceEndpoint: accounts
            changeOrigin: true
            prependPath: true
            ignorePath: false
            stripPath: true
  billing:
    apiEndpoints:
      - billing
    policies:
    # Uncomment `key-auth:` when instructed to in the Getting Started guide.
    # - key-auth:
      - body-parser:
      - log: # policy name
        - action:    # array of condition/actions objects
        message: ${req.method} ${req.originalUrl} ${JSON.stringify(req.body)} # parameter for log action
      - proxy:
        - action:
            serviceEndpoint: billing
            changeOrigin: true
            prependPath: true
            ignorePath: false
            stripPath: true

package.json

{
  "name": "max-apigateway-service",
  "description": "Express Gateway Instance Bootstraped from Command Line",
  "repository": {},
  "license": "UNLICENSED",
  "version": "1.0.0",
  "main": "server.js",
  "dependencies": {
    "connect-redis": "^4.0.3",
    "express-gateway": "^1.16.9",
    "express-gateway-plugin-example": "^1.0.1",
    "express-session": "^1.17.0",
    "redis": "^2.8.0"
  }
}

Am I missing anything?

user3010617
  • 137
  • 1
  • 12

1 Answers1

0

In my case, I used AWS Elasticache for Redis. I tried to run it but I had "A client must be directly provided to the RedisStore" error. I found my problem from the security group setting. EC2(server) should have a proper security group for the port of Elasticache. And Elasticache should have the same security group.

Step1. Create new security group. Set the inbound rule

enter image description here

Step2. Add the security group to the EC2 server. enter image description here

Step3. Add the security group to the Elasticache. enter image description here

Alf Bae
  • 546
  • 6
  • 10