TimWolla / docker-adminer

Database management in a single PHP file
https://hub.docker.com/_/adminer/
161 stars 70 forks source link

Preconfigured authoriziation #13

Closed kler closed 7 years ago

kler commented 7 years ago

A nice feature would be if it was possible to pass server, username, password and database as parameters to the docker image.

In that way it would be very convenient to bundle adminer in a development stack docker-compose and provide a one-click access to the database.

I'm thinking that docker-compose.yml could look something like this:

adminer:
    image: adminer
    ports: 8080:8080
    environment:
    - ADMINER_SERVER=db
    - ADMINER_USER=root
    - ADMINER_PASSWORD=secret
    - ADMINER_DATABASE=mydatabase

This would make adminer either 1) just pre-fill the values in the form, or 2) auto-login.

I guess this request would also require changes in adminer itself though.

TimWolla commented 7 years ago

I don't want to modify adminer any more than I already did (prefilling db as the database host and that is only because localhost just doesn't make sense). You can do this easily with a custom image and a little adminer plugin though:

<?php
// Add this file as /var/www/html/plugins-enabled/autologin.php in the adminer image
class Autologin {
  // see: https://github.com/vrana/adminer/blob/7af1ee3702420620641d075ebfd54d4b1d220409/adminer/include/adminer.inc.php#L18-L20
  function credentials() {
    if (!empty($_GET['username'])) return array(SERVER, $_GET["username"], get_password());
    return array($_ENV['ADMINER_SERVER'], $_ENV['ADMINER_USER'], $_ENV['ADMINER_PASSWORD']);
  }
}

return new Autologin();
FROM adminer

COPY autologin.php /var/www/html/plugins-enabled/

When no credentials are put in and the form is submitted the credentials from the environment are used, otherwise the credentials from the form are used.

kler commented 7 years ago

Ok, thx for pointing me (or someone else) in the right direction!

bserem commented 2 years ago

When no credentials are put in and the form is submitted the credentials from the environment are used, otherwise the credentials from the form are used.

This seems to have changed in recent years, adminer does not allow you to login without entering credentials, it displays a message:

Adminer does not support accessing a database without a password, more information.

Adding any random credentials works with the plugin provided in this thread, but it is not a solution. Any info on the matter would be nice.

maximelebastard commented 7 months ago

Based on different elements I found on the internet, seems to currently work fine:

version: "3.9"
services:
  postgres:
    image: postgres:latest
    restart: unless-stopped
    ports:
      - 5432:5432
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
      POSTGRES_DB: dev

  adminer:
    image: adminer:latest
    restart: unless-stopped
    environment:
      ADMINER_DEFAULT_DRIVER: pgsql # server=mysql, pgsql, sqlite, sqlite2, oracle, mssql, mongo, elastic
      ADMINER_DEFAULT_DB: dev
      ADMINER_DEFAULT_SERVER: postgres
      ADMINER_DEFAULT_USERNAME: user
      ADMINER_DEFAULT_PASSWORD: password
    ports:
      - "3344:8080"
    depends_on:
      - postgres
    configs:
      - source: adminer-index.php
        target: /var/www/html/index.php

configs:
  adminer-index.php:
    content: |
      <?php
        if(!count($$_GET)) {
          $$_POST['auth'] = [
            'server' => $$_ENV['ADMINER_DEFAULT_SERVER'],
            'username' => $$_ENV['ADMINER_DEFAULT_USERNAME'],
            'password' => $$_ENV['ADMINER_DEFAULT_PASSWORD'],
            'driver' => $$_ENV['ADMINER_DEFAULT_DRIVER'],
            'db'    => $$_ENV['ADMINER_DEFAULT_DB'],
          ];
        }
        include './adminer.php';
      ?>

Note that double dollar ($$) is for docker-compose not interpreting the php vars as env vars