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

https connection with container doesn't work #540

Open
rafakaiber opened this issue May 16, 2023 · 1 comment
Open

https connection with container doesn't work #540

rafakaiber opened this issue May 16, 2023 · 1 comment

Comments

@rafakaiber
Copy link

rafakaiber commented May 16, 2023

I have created a node api that publish messages using a faye client and would like to use the https connection.
In local environment it is working just fine in my faye client I have the certificate and key in place, and it is coming up as https://localhost:4001 and in node api side I have the config pointing to the correct url and using the tls: {ca: certificate.pem}.

It works like a charm, but when I create each service on a container the node api is not able to connect with the faye client.

NODE API
`

export class FayeUtil {
  private static fayeClient;
  static init(appConfig: ApplicationConfig) {
  const tlsConfig = {
    retry: 5,
    timeout: 30,
    tls: {
      rejectUnauthorized: true,
      ca: Environment.getCerficate()
    }
  }
  this.fayeClient = new Faye.Client("https://localhost:4001/prism-faye", tlsConfig);

  this.fayeClient.addExtension({
    outgoing: function (message, callback) {
      AppLogger.debug("Faye Channel Outgoing Message :: " + JSON.stringify(message));
      message.ext = message.ext || {};
      message.ext.password = appConfig["faye"]["secret"];
      message.ext.user = appConfig["faye"]["user"];
      callback(message);
    },
  });

  this.fayeClient.on("transport:down", () => {
    AppLogger.error("Faye Client Down");
  });

  this.fayeClient.on("transport:up", () => {
    AppLogger.info("Faye Client Up");
  });
}

static publish(channel, message) {
  this.fayeClient.publish(channel, message);
}

static subscribe(channel, onMessage) {
  this.fayeClient.subscribe(channel, onMessage);
}

static sendHeartBeat(serverName) {
  this.fayeClient
    .publish("/heartbeat", {
      server: serverName,
      time: Date.now(),
    })
    .then(() => {
      AppLogger.info("HeartBeat published successfully");
    });
  }
}

`

FAYE-PUBSUB

`

  async function start() {
  // load config
  await ConfigService.init("/conf/config.local.json");

  // Create a https server instance 
 const sslOptions = {
   key: fs.readFileSync("/certificate.key", "utf8"),
   cert: fs.readFileSync("/certificate.crt", "utf8")
 }
const httpServerInstance = https.createServer(sslOptions, nonBayeuxHandler);

 // Create a Faye instance
const bayeux = new Faye.NodeAdapter({
   mount: "/prism-faye", // this is the prefix on which faye will be mounted
   timeout: 30
 });

 // attach faye to http server
bayeux.attach(httpServerInstance);

 // Add authentication to restrict publish only to servers
authHandler(bayeux, ConfigService.config());

 // Start Server
httpServerInstance.listen(Environment.getPort(DEFAULT_PORT), Environment.getHost(DEFAULT_HOST), () => {
   AppLogger.info(
     "FAYE HTTP Server Started Successfully: Host = " + Environment.getHost(DEFAULT_HOST) + ", Port = " + 
     Environment.getPort(DEFAULT_PORT)
   );
  });
}

/* Common Code */
start()
  .then(() => {
    AppLogger.info("Faye server started successfully.");
  })
  .catch((err) => {
    AppLogger.error("Error starting Faye server. Send notification.", err);
  });

`

I spent sometime looking for a solution but not able to identify where is the issue, as it works locally, but doesn't work when I bring the code to the container.

the only difference I see in console logs is in the node api
LOCAL -> Faye Client Up
CONTAINER of NODE API -> Faye Client Down

Just as more info: I'm using mac and podman to create the containers

@jcoglan
Copy link
Collaborator

jcoglan commented May 29, 2023

Im not familiar with this container platform so I can't say what's going on here. My gut reaction is that if you got your application working in another environment then it's probably not a problem with the code per se but some environmental difference.

What I would do in this situation is to build understanding of the container platform by first building something simpler on top of it. For example, first write something (anything, just a toy example will do) using just the Node http module and get that working to iron out any networking issues. Then convert that example to https and get your certificates working. Only then would I start building an actual application.

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

2 participants