DependencyTrack / dependency-track

Dependency-Track is an intelligent Component Analysis platform that allows organizations to identify and reduce risk in the software supply chain.
https://dependencytrack.org/
Apache License 2.0
2.72k stars 580 forks source link

Dependency Track can not connect to Postgres database #3262

Open komaldadore-TU opened 11 months ago

komaldadore-TU commented 11 months ago

Current Behavior

Current Behavior I have Postgres db running on the AWS Linux (ubuntu 22.04) instance (Not Dockerised) , on the same instance I am running dependency-track docker compose container. I have tried the ALPINE_DATABASE_URL wirh both hostname and ip_address. the docker-compose.yml contains:

services: dtrack-apiserver: image: dependencytrack/apiserver environment:

The Dependency-Track container can be configured using any of the

available configuration properties defined in:

https://docs.dependencytrack.org/getting-started/configuration/

All properties are upper case with periods replaced by underscores.

#

Database Properties

Steps to Reproduce

1.Start a docker-container with the official image dependencytrack docker compose

Expected Behavior

Docker container runs normally

Dependency-Track Version

4.9.1

Dependency-Track Distribution

Container Image

Database Server

PostgreSQL

Database Server Version

14.2

Browser

Google Chrome

Checklist

hperat commented 8 months ago

I am having the same error. How do you correctly format the ALPINE_DATABASE_URL config for an RDS postgresql resource/endpoint?

jc911 commented 5 months ago

I solved it. The reason is the docker cannot access the host machine.

  1. config in /etc/postgresql/xx/main/postgresql.conf listen_addresses = '*'

  2. config in /etc/postgersql/xx/main/pg_hba.conf

    host dtrack dtrack 172.20.0.0/16 md5
    # need `docker network ls` and `ifconfig` to get the docker compose bridge ip of dt-apiserver
    sudo service postgresql restart
  3. config firewall (docker belongs to the external network)

    sudo ufw allow from 172.20.0.0/16 to any port 5432
    sudo ufw reload
  4. config in docker_compose.yml

    - ALPINE_DATABASE_URL=jdbc:postgresql://172.17.0.1:5432/dtrack
    # the ip is the docker0 in `ifconfig`

Finnally try docker compose up again

Silverbullet069 commented 4 months ago
1. config in /etc/postgresql/xx/main/postgresql.conf
2. config in /etc/postgersql/xx/main/pg_hba.conf

@jc911 Can you point out where are postgresql.conf and pg_hba.conf? are they inside a docker container image?

jc911 commented 4 months ago
1. config in /etc/postgresql/xx/main/postgresql.conf
2. config in /etc/postgersql/xx/main/pg_hba.conf

@jc911 Can you point out where are postgresql.conf and pg_hba.conf? are they inside a docker container image?

My Postgres was installed in the host machaine, so the config file is in the host.

Silverbullet069 commented 4 months ago

My Postgres was installed in the host machaine, so the config file is in the host.

I knew it, there is no postgresql inside the dtrack-apiserver service. I have to manually set up a postgres:16 service and expose itself to dtrack-apiserver. Dependency-Track README file is terrible.

samuvb commented 3 months ago

@Silverbullet069 how did you exactly set this up? Did you create in your Docker-compose file another service as postgres and then did the changes to those files mentioned above with a bind volume ?

Also did you make a network bridge in the docker-compose file ?

Silverbullet069 commented 2 months ago

Did you create in your Docker-compose file another service as postgres and then did the changes to those files mentioned above with a bind volume ?

Also did you make a network bridge in the docker-compose file ?

@samuvb I did. Spending 2 hours to identify the cause. Just because the tool makes a bold assumption that a PostgreSQL database has been installed in my machine before installing Dependency Track, which is not.

Here is the new docker-compose.yml content:

[!IMPORTANT] I've changed the username and password of database's user and web portal account to dtrack and dtrack, respectively.

#####################################################
# This Docker Compose file contains two services
#    Dependency-Track API Server
#    Dependency-Track FrontEnd
#####################################################

# Modified: silverbullet069
# Issue: dtrack-apiserver not connecting to PostgreSQL server
# Cre: https://stackoverflow.com/a/41912295/9122512

volumes:
  dependency-track:

networks:
  dependency-track-network:

services:
  postgres16:
    image: 'postgres:16'
    environment:
      - POSTGRES_USER=dtrack
      - POSTGRES_PASSWORD=dtrack
    command: "-c listen_addresses='*'"
    networks:
      dependency-track-network:
        # app can communicate with the database using this hostname
        aliases:
          - postgres16
    ports:
      - '5432:5432'
    volumes:
      - './postgres-data:/var/lib/postgresql/data'

    # Mount custom config file with a volume, not recommended since you will have to maintain this file from host's side.
    # - ./CUSTOM-POSTGRESQL.conf:/etc/postgresql.conf

    # Add the database files to the host
    # - ./postgres_data:/var/lib/postgresql/data

    # NOTE: Only set if CUSTOM-POSTGRESQL.conf configured Postgres to write log messages to /logs
    # The directory "./logs" is created by run.sh on the host.
    # - ./logs:/logs

    # The container should use the user and group IDs from the host. When we set the owner of /logs to the user "postgres" in the host (via run.sh), the ID of the container's user "postgres" will match.
    # - /etc/passwd:/etc/passwd:ro
    # - /etc/group:/etc/group:ro

  dtrack-apiserver:
    image: dependencytrack/apiserver
    depends_on:
      - postgres16
    environment:
    # The Dependency-Track container can be configured using any of the
    # available configuration properties defined in:
    # https://docs.dependencytrack.org/getting-started/configuration/
    # All properties are upper case with periods replaced by underscores.
    #
    # Database Properties
      - ALPINE_DATABASE_MODE=external
      - ALPINE_DATABASE_URL=jdbc:postgresql://postgres16:5432/dtrack
      - ALPINE_DATABASE_DRIVER=org.postgresql.Driver
      - ALPINE_DATABASE_USERNAME=dtrack
      - ALPINE_DATABASE_PASSWORD=dtrack
      - ALPINE_DATABASE_POOL_ENABLED=true
      - ALPINE_DATABASE_POOL_MAX_SIZE=20
      - ALPINE_DATABASE_POOL_MIN_IDLE=10
      - ALPINE_DATABASE_POOL_IDLE_TIMEOUT=300000
      - ALPINE_DATABASE_POOL_MAX_LIFETIME=600000
    #
    # Optional LDAP Properties
    # - ALPINE_LDAP_ENABLED=true
    # - ALPINE_LDAP_SERVER_URL=ldap://ldap.example.com:389
    # - ALPINE_LDAP_BASEDN=dc=example,dc=com
    # - ALPINE_LDAP_SECURITY_AUTH=simple
    # - ALPINE_LDAP_BIND_USERNAME=
    # - ALPINE_LDAP_BIND_PASSWORD=
    # - ALPINE_LDAP_AUTH_USERNAME_FORMAT=%s@example.com
    # - ALPINE_LDAP_ATTRIBUTE_NAME=userPrincipalName
    # - ALPINE_LDAP_ATTRIBUTE_MAIL=mail
    # - ALPINE_LDAP_GROUPS_FILTER=(&(objectClass=group)(objectCategory=Group))
    # - ALPINE_LDAP_USER_GROUPS_FILTER=(member:1.2.840.113556.1.4.1941:={USER_DN})
    # - ALPINE_LDAP_GROUPS_SEARCH_FILTER=(&(objectClass=group)(objectCategory=Group)(cn=*{SEARCH_TERM}*))
    # - ALPINE_LDAP_USERS_SEARCH_FILTER=(&(objectClass=user)(objectCategory=Person)(cn=*{SEARCH_TERM}*))
    # - ALPINE_LDAP_USER_PROVISIONING=false
    # - ALPINE_LDAP_TEAM_SYNCHRONIZATION=false
    #
    # Optional OpenID Connect (OIDC) Properties
    # - ALPINE_OIDC_ENABLED=true
    # - ALPINE_OIDC_ISSUER=https://auth.example.com/auth/realms/example
    # - ALPINE_OIDC_CLIENT_ID=
    # - ALPINE_OIDC_USERNAME_CLAIM=preferred_username
    # - ALPINE_OIDC_TEAMS_CLAIM=groups
    # - ALPINE_OIDC_USER_PROVISIONING=true
    # - ALPINE_OIDC_TEAM_SYNCHRONIZATION=true
    #
    # Optional HTTP Proxy Settings
    # - ALPINE_HTTP_PROXY_ADDRESS=proxy.example.com
    # - ALPINE_HTTP_PROXY_PORT=8888
    # - ALPINE_HTTP_PROXY_USERNAME=
    # - ALPINE_HTTP_PROXY_PASSWORD=
    # - ALPINE_NO_PROXY=
    #
    # Optional HTTP Outbound Connection Timeout Settings. All values are in seconds.
    # - ALPINE_HTTP_TIMEOUT_CONNECTION=30
    # - ALPINE_HTTP_TIMEOUT_SOCKET=30
    # - ALPINE_HTTP_TIMEOUT_POOL=60
    #
    # Optional Cross-Origin Resource Sharing (CORS) Headers
      - ALPINE_CORS_ENABLED=true
      - ALPINE_CORS_ALLOW_ORIGIN=*
      - ALPINE_CORS_ALLOW_METHODS=GET, POST, PUT, DELETE, OPTIONS
      - ALPINE_CORS_ALLOW_HEADERS=Origin, Content-Type, Authorization, X-Requested-With, Content-Length, Accept, Origin, X-Api-Key, X-Total-Count, *
      - ALPINE_CORS_EXPOSE_HEADERS=Origin, Content-Type, Authorization, X-Requested-With, Content-Length, Accept, Origin, X-Api-Key, X-Total-Count
      - ALPINE_CORS_ALLOW_CREDENTIALS=true
      - ALPINE_CORS_MAX_AGE=3600
    #
    # Optional logging configuration
    # - LOGGING_LEVEL=INFO
    # - LOGGING_CONFIG_PATH=logback.xml
    #
    # Optional metrics properties
    # - ALPINE_METRICS_ENABLED=true
    # - ALPINE_METRICS_AUTH_USERNAME=
    # - ALPINE_METRICS_AUTH_PASSWORD=
    #
    # Optional environmental variables to enable default notification publisher templates override and set the base directory to search for templates
    # - DEFAULT_TEMPLATES_OVERRIDE_ENABLED=false
    # - DEFAULT_TEMPLATES_OVERRIDE_BASE_DIRECTORY=/data
    #
    # Optional configuration for the Snyk analyzer
    # - SNYK_THREAD_BATCH_SIZE=10
    #
    # Optional environmental variables to provide more JVM arguments to the API Server JVM, i.e. "-XX:ActiveProcessorCount=8"
    # - EXTRA_JAVA_OPTIONS=
    deploy:
      resources:
        limits:
          memory: 12288m
        reservations:
          memory: 8192m
      restart_policy:
        condition: on-failure
    ports:
      - '8081:8080'
    volumes:
      - 'dependency-track:/data'
    restart: unless-stopped
    networks:
      - dependency-track-network

  dtrack-frontend:
    image: dependencytrack/frontend
    depends_on:
      - dtrack-apiserver
    environment:
      # The base URL of the API server.
      # NOTE:
      #   * This URL must be reachable by the browsers of your users.
      #   * The frontend container itself does NOT communicate with the API server directly, it just serves static files.
      #   * When deploying to dedicated servers, please use the external IP or domain of the API server.
      - API_BASE_URL=http://localhost:8081
      # - "OIDC_ISSUER="
      # - "OIDC_CLIENT_ID="
      # - "OIDC_SCOPE="
      # - "OIDC_FLOW="
      # - "OIDC_LOGIN_BUTTON_TEXT="
      # volumes:
      # - "/host/path/to/config.json:/app/static/config.json"
    ports:
      - "8080:8080"
    restart: unless-stopped