How to use the new branch feature with docker development environment

Hi Forest,

I run Forest in local Docker container for development. We don’t use the basic development environment which means we only have remote environment.

Is it compatible with Forest branch feature ?

Should I install and use forest CLI inside the docker container or outside and then install Forest cli locally in the container?

Best regards
Guillaume

From the container I can run npm i forest-cli -g and then follow the process. However, as soon as I create a branch, Forest asks me to reauthenticate with a password but refuses to authenticate me.

I have to switch to another environment and then to come back to my Docker_devlopment environment.

Hello @Guiguijo,

I’m not sure to well understand your second message. Is your problem still there? If so, can you share with us the request that fails from your browser’s network tab?

Kind regards,
Louis

Hello @Guiguijo,

In order to correctly understand your issue. Tell me if I’m correct:

  • You use docker with a remote environment (example “staging”)
  • You do not have a development environment in your docker.
  • You do have a development environment on your local machine.

The Forest branch feature is only supported with a development environment, so if your docker uses a remote environment, it won’t work.

Tell me if I’m correct. With more precision, I will be able to help you :slight_smile:
I suspect the authenticate issue comes from permission rights on some files which are forbidden, but I need more insights :wink:

hi @Guillaume_Cisco ,

We use Docker with a local environment in the docker container.

I first added forest-cli to my package.json in the dependencies to be able to call forest command from inside the Docker container but it did not work.

Then I connected myself directly to the Docker container and run npm i forest-cli -g inside the container. Then Forest cli was available and I could create a branch.

The thing is that I don’t want to connect to the container and run npm i forest-cli -g etc…again, each time and drop and build my container. Moreover, there is this authentication question.

Hello @Guiguijo,

I think your issue with forest-cli is indeed that the documentation shows you how to install it globally. But if you want to install it locally by adding it to your package.json file, you can also call it, but not globally. The binary will be installed in ./node_modules/.bin.
So, you could try something like ./node_modules/.bin/forest-cli instead of forest-cli. You could also add ./node_modules/.bin to your PATH global environment variable, for making this folder accessible.

Regarding the authentication question, my colleague @lclisson is on it :wink:

1 Like

all right guys. Thanks a lot.

@lclisson do you need more info ?

Yes please, I need this info in order to help you :slight_smile:

When I create a branch or switch to a branch nothing happen in my logs:

but I have this screen on Forest UI:

image

when I enter my password and try to connect, I have a 401 in my logs:

api_forest_1  | Your admin panel is available here: https://app.forestadmin.com/projects
api_forest_1  | info:    ┏ OPTIONS /forest/sessions 204 5ms +5m
api_forest_1  | info:    ┃ [ 1] {
api_forest_1  | info:    ┃ [ 2]   http: {
api_forest_1  | info:    ┃ [ 3]     request: {
api_forest_1  | info:    ┃ [ 4]       method: 'OPTIONS',
api_forest_1  | info:    ┃ [ 5]       url: 'http://api-forest.dev.localhost/forest/sessions',
api_forest_1  | info:    ┃ [ 6]       path: '/forest/sessions',
api_forest_1  | info:    ┃ [ 7]       query: {},
api_forest_1  | info:    ┃ [ 8]       headers: {},
api_forest_1  | info:    ┃ [ 9]       source_ip: '172.23.0.1'
api_forest_1  | info:    ┃ [10]     },
api_forest_1  | info:    ┃ [11]     responseTime: 5,
api_forest_1  | info:    ┃ [12]     response: { status: 204, message: 'No Content', type: undefined, headers: {} }
api_forest_1  | info:    ┃ [13]   },
api_forest_1  | info:    ┃ [14]   service: 'api_forest',
api_forest_1  | info:    ┃ [15]   env: 'development',
api_forest_1  | info:    ┃ [16]   version: 'v2.0.4-9-g3b4d2f48',
api_forest_1  | info:    ┃ [17]   context: { message: 'OPTIONS /forest/sessions 204 5ms' },
api_forest_1  | info:    ┃ [18]   timestamp: '2021-06-22T15:29:33.470Z'
api_forest_1  | info:    ┗ [19] }
api_forest_1  | info:    ┏ POST /forest/sessions 401 87ms +100ms
api_forest_1  | info:    ┃ [ 1] {
api_forest_1  | info:    ┃ [ 2]   http: {
api_forest_1  | info:    ┃ [ 3]     request: {
api_forest_1  | info:    ┃ [ 4]       method: 'POST',
api_forest_1  | info:    ┃ [ 5]       url: 'http://api-forest.dev.localhost/forest/sessions',
api_forest_1  | info:    ┃ [ 6]       path: '/forest/sessions',
api_forest_1  | info:    ┃ [ 7]       query: {},
api_forest_1  | info:    ┃ [ 8]       headers: {},
api_forest_1  | info:    ┃ [ 9]       source_ip: '172.23.0.1'
api_forest_1  | info:    ┃ [10]     },
api_forest_1  | info:    ┃ [11]     responseTime: 87,
api_forest_1  | info:    ┃ [12]     response: { status: 401, message: 'Unauthorized', type: 'text/html; charset=utf-8', headers: {} }
api_forest_1  | info:    ┃ [13]   },
api_forest_1  | info:    ┃ [14]   service: 'api_forest',
api_forest_1  | info:    ┃ [15]   env: 'development',
api_forest_1  | info:    ┃ [16]   version: 'v2.0.4-9-g3b4d2f48',
api_forest_1  | info:    ┃ [17]   context: { message: 'POST /forest/sessions 401 87ms' },
api_forest_1  | info:    ┃ [18]   timestamp: '2021-06-22T15:29:33.572Z'
api_forest_1  | info:    ┗ [19] }
api_forest_1  | [object Object]

and here is the console:

Ok thanks @Guiguijo,

I’ll try to reproduce on my end but I’ll need your some more informations:

  • What is your project name?
  • Are you using forest-sequelize / forest-rails or forest-mongoose?
  • Have you been able to connect before?
  • Have you changed something recently that could explain this new behavior?

Thanks a lot,
Louis

Hi @lclisson ,

  • project name is joinly-forest

  • I use “forest-express-mongoose”: “^7.8.1”

  • I am able to connect. The authentication issue appears on branch creation or switch.

  • I have migrated to joinly-forest to V7 following all the doc recommandations

Thanks for the answers,

I’m still trying to reproduce your issue and I have few more questions

  • Are you also disconnected when running these following commands?
  1. forest branch
  2. forest environments
  • Can you please give me the last 5 characters of your FOREST_ENV_SECRET of your environment?

Kind regards,
Louis

  • No I am not disconnected when I run this commands (I did not know forest environments)
  • last 5 characters of FOREST_ENV_SECRET: 545f1

Everything is good regarding your ENV_SECRET. :slight_smile:

I created a mongoose project with the same forest-mongoose version as you and I don’t have any problems when switching from branch to branch creation.

Could you share your app.js file with me so I can have a closer look? Is this the only time you get the connection pop up?

maybe If you want we can make a Google Meet and I share my screen and all necessary files to you

here is my app.js

const express = require('express')
const requireAll = require('require-all')
const path = require('path')
const cookieParser = require('cookie-parser')
const bodyParser = require('body-parser')
const cors = require('cors')
const jwt = require('express-jwt')
const { logger } = require('./services/logger.services')
const expressWinston = require('express-winston')
const { ensureAuthenticated, PUBLIC_ROUTES } = require('forest-express-mongoose')
const config = require('config')
const app = express()

app.use(
  expressWinston.logger({
    winstonInstance: logger,
    metaField: 'http',
    requestField: 'request',
    responseField: 'response',
    requestWhitelist: [],
    responseWhitelist: [],
    headerBlacklist: ['user-agent'],
    expressFormat: true,
    colorize: true,
    dynamicMeta: function(req, res) {
      return {
        request: {
          method: req.method,
          url: `${req.protocol}://${req.hostname}${req.url}`,
          path: req.path,
          query: req.query,
          headers: config.get('log.http_headers') ? req.headers : {},
          source_ip: req.get('x-forwarded-for')
          // user_agent: req.get('user-agent')
        },
        response: {
          status: res.statusCode,
          message: res.statusMessage,
          type: res.get('Content-Type'),
          headers: config.get('log.http_headers') ? res.getHeaders() : {}
        }
      }
    }
  })
)

app.use(bodyParser.json({ limit: '5mb' }))
app.use(bodyParser.urlencoded({ extended: false, limit: '5mb' }))
app.use(cookieParser())
app.use(express.static(path.join(__dirname, 'public')))

let allowedOrigins = [/forestadmin\.com$/, /localhost:\d{4}$/]

if (process.env.CORS_ORIGINS) {
  allowedOrigins = allowedOrigins.concat(process.env.CORS_ORIGINS.split(','))
}

const corsConfig = {
  origin: allowedOrigins,
  allowedHeaders: ['Authorization', 'X-Requested-With', 'Content-Type'],
  maxAge: 86400, // NOTICE: 1 day
  credentials: true
}

app.use(
  '/forest/authentication',
  cors({
    ...corsConfig,
    origin: corsConfig.origin.concat('null')
  })
)

app.use(cors(corsConfig))

app.use(
  jwt({
    secret: config.get('forest.auth_secret'),
    credentialsRequired: false,
    algorithms: ['HS256']
  })
)

app.use('/forest', (request, response, next) => {
  if (PUBLIC_ROUTES.includes(request.url)) {
    return next()
  }
  return ensureAuthenticated(request, response, next)
})

requireAll({
  dirname: path.join(__dirname, 'routes'),
  recursive: true,
  resolve: Module => app.use('/forest', Module)
})

requireAll({
  dirname: path.join(__dirname, 'middlewares'),
  recursive: true,
  resolve: Module => Module(app)
})

module.exports = app

Sorry for my late answer, I was still looking at your project :slight_smile:

After some research it seems that the problem is that all the other environments in your project are still in v6. Finishing the migration to v7 for all environments should make the authentication problem disappear (as v7 uses a different authentication mechanism → OIDC protocol).

After that and if there are still problems with the use of the CLI and the branches we can organise a call to fix the last small problems.

Let me know if it helps,

Kind regards,
Louis

2 Likes

Ok Louis. I will let you know when the migration will be up on all environments.

1 Like

@lclisson

again I have this error of authentication:

As I am still working on my dev branch where I have migrated Forest to V7, I think I must reset my FOREST_CLIENT_ID

1 Like

Hello @Guiguijo,

Nice, thanks, have you done your migrations on your others environments?

Yes for sure, when using a development environment you are running only one instance node so the FOREST_CLIENT_ID is not necessary. Is it working as expected after removing it ?

Best,
Louis