Home > other >  Access denied for user ''@'127.0.0.1' (using password: NO) running node app in J
Access denied for user ''@'127.0.0.1' (using password: NO) running node app in J

Time:01-25

I'm trying to setup a Jenkins pipeline for my Nodejs app. I have an Ubuntu Docker image with Mariadb that works fine, database starts up, I can connect and execute SQL scripts from the Jenkinsfile. Jenkins starts the Docker image as the root user. The node app is using the Mariadb connector (package mariadb)

Outside of Jenkins, the node app is able to connect to a local MariaDB (for testing) or to a remote MariaDB (deployed app)...nothing wrong with connection there. During local integration tests it's alright as well.

When I try to run the tests through Jenkins, I get this error when the node app tries to create a connection to the database: SequelizeAccessDeniedError: (conn=14, no: 1045, SQLState: 28000) Access denied for user ''@'127.0.0.1' (using password: NO)

In the Jenkinsfile, I am checking the creation of the testuser in the Docker's MariaDB database, the users are successfully created in there.

I am printing the connection details to the console just before I connect, and everything is correct...so why is the MariaDB connector trying to connect with the default user?

EDIT: I tried also setting the socketPath in the Database class constructor, but it gets the same error.

Dockerfile:

from mariadb:10.7.7-focal

# Install additional packages
RUN set -eux; \
    apt-get update; \
    DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
        curl \
        wget \
        npm \
        unzip \
        iceweasel \
        vim \
    && wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb \
    && apt-get install -y --no-install-recommends ./google-chrome-stable_current_amd64.deb

# Install NodeJs 18.x
RUN curl -sL https://deb.nodesource.com/setup_18.x -o nodesource_setup.sh \
    && bash nodesource_setup.sh \
    && apt-get install -y --no-install-recommends nodejs

Jenkins:

stage ('Setup test data in database')
{
    steps
    {
        echo "*** Setting up test data in database ***"
        sh "set"
        sh "service mariadb start -v --skip-name-resolve"
        sh "echo password | mariadb -u root -p -e \"CREATE USER 'testuser'@'localhost' IDENTIFIED BY 'password';\""
        sh "echo password | mariadb -u root -p -e \"CREATE USER 'testuser'@'127.0.0.1' IDENTIFIED BY 'password';\""
        sh "echo password | mariadb -u root -p -e \"GRANT ALL PRIVILEGES ON *.* TO 'testuser'@'127.0.0.1';\""
        sh "echo password | mariadb -u root -p -e \"GRANT ALL PRIVILEGES ON *.* TO 'testuser'@'localhost';\""
        sh "echo password | mariadb -u root -p -e \"FLUSH PRIVILEGES;\""
        sh "echo password | mariadb -u root -p -e \"SELECT user,authentication_string,plugin,host,password FROM mysql.user;\""
        sh "echo password | mariadb -u root -p -e \"SOURCE database/create.sql\""
    }
}
// End SETUP DATABASE
stage ('Fetch integration test drivers')
{
    steps
    {
        echo "*** Fetching integration test drivers ***"
        sh "google-chrome --version"
        sh script:'''
          mkdir ./tests/integration/drivers
          wget https://github.com/mozilla/geckodriver/releases/download/v0.32.0/geckodriver-v0.32.0-linux64.tar.gz
          tar -x geckodriver -zf geckodriver-v0.32.0-linux64.tar.gz -O > tests/integration/drivers/geckodriver && chmod  x ./tests/integration/drivers/geckodriver
          wget https://chromedriver.storage.googleapis.com/108.0.5359.71/chromedriver_linux64.zip
          unzip chromedriver_linux64.zip && chmod  x chromedriver && mv chromedriver ./tests/integration/drivers
          rm geckodriver-v0.32.0-linux64.tar.gz && rm chromedriver_linux64.zip
        '''
    }
}
// End CLONING
stage ('Run NPM install')
{
    steps
    {
        echo "*** Running npm install ***"
        sh "npm install"
    }
}
// End NPM INSTALL
stage ('Running Integration Tests')
{
    steps
    {
        echo "*** Starting Integration Tests ***"
        sh '(npm run dev-api & npm run dev-frontend-instance1 & npm run dev-frontend-instance2) && npm run test-integration'
    }
}

Node app database class constructor:

class Database {
    private static _instances: { instance1: Database | undefined, instance2: Database | undefined } = {
        instance1: undefined,
        instance2: undefined
    };
    private _connectionDetails = {
        host: process.env.API_IP ? process.env.API_IP : "localhost",
        user: process.env.DATABASE_USER ? process.env.DATABASE_USER : "testuser",
        password: process.env.DATABASE_PASSWORD ? process.env.DATABASE_PASSWORD : "password",
        port: 3306,
        database: ""
    };
    private _connection: Promise<mariadb.Connection>;
    private _frontendHostname: string;

    constructor(databaseName: string) {
        this._connectionDetails.database = databaseName;
        console.log("CONNECTION DETAILS: ", this._connectionDetails);
        this._connection = mariadb.createConnection(this._connectionDetails);
        this._frontendHostname = databaseName == "instance1" ? "https://url1" : "https://url2";
        if (process.env.API_IP == "localhost") {
            this._frontendHostname = "https://localhost:8080";
        }
    }

Output in Jenkins:

CONNECTION DETAILS:  {
  host: 'localhost',
  user: 'testuser',
  password: 'password',
  port: 3306,
  database: 'instance1'
}
SequelizeAccessDeniedError: (conn=14, no: 1045, SQLState: 28000) Access denied for user ''@'127.0.0.1' (using password: NO)

CodePudding user response:

I ended up creating the user it was looking for and it works now.

  • Related