From: Nicolas Boisselier Date: Mon, 7 Mar 2016 17:23:31 +0000 (+0100) Subject: credential format X-Git-Url: https://git.nbdom.net/?a=commitdiff_plain;h=a17d70148555edd8c8ada8480870e5e9ac2bcbb2;p=nb.git credential format --- diff --git a/etc/dbs.yaml b/etc/dbs.yaml index be9f6317..f28d23bf 100644 --- a/etc/dbs.yaml +++ b/etc/dbs.yaml @@ -2,8 +2,12 @@ crypt: pdo: 'sqlite:/dev/shm/crypt.db' puppetdb: - pdo: 'pgsql:host=big;dbname=puppetdb;user=puppetdb;password=' + #pdo: 'pgsql:host=big;dbname=puppetdb;user=puppetdb;password=' title: Puppetdb + host: big + type: pgsql + name: puppetdb + user: puppetdb default_table: view_hosts tables: hosts: @@ -24,20 +28,28 @@ puppetdb: type: "int" extra: 1 izi: - pdo: 'mysql:host=admin.izideal.com;port=3306;dbname=izi' - username: izideal + #pdo: 'mysql:host=admin.izideal.com;port=3306;dbname=izi' title: Mysql Izi on Admin + default_table: process + + user: izideal + type: mysql + host: admin.izideal.com + name: izi options: - "PDO::MYSQL_ATTR_INIT_COMMAND: SET NAMES utf8'" - default_table: process izidev: - pdo: 'mysql:host=big.cascais.loc;port=3306;dbname=izi' - username: nico + #pdo: 'mysql:host=big.cascais.loc;port=3306;dbname=izi' title: Mysql Izi on Big + default_table: process + + user: nico + type: mysql + host: admin.izideal.com + name: izi options: - "PDO::MYSQL_ATTR_INIT_COMMAND: SET NAMES utf8'" - default_table: process ui: pdo: 'sqlite:/opt/semantico/product/releases/sem_ui/db/semantico.db' @@ -97,8 +109,12 @@ rent: # NB 10.01.16 template: # NB 10.01.16 View: "''||id||''" postfix: - pdo: 'mysql:host=media.izideal.vpn;port=3306;dbname=postfix' - username: postfix + #pdo: 'mysql:host=media.izideal.vpn;port=3306;dbname=postfix' + user: postfix title: Postfix on Izideal + + type: mysql + host: media.izideal.vpn + name: postfix options: - "PDO::MYSQL_ATTR_INIT_COMMAND: SET NAMES utf8'" diff --git a/lib/php/db.php b/lib/php/db.php index af9e3a95..d8efff59 100644 --- a/lib/php/db.php +++ b/lib/php/db.php @@ -28,10 +28,12 @@ class Db extends nb { # PDO Connection public $conn; public $pdo; + public $pdo_error = PDO::ERRMODE_EXCEPTION; # See: http://php.net/manual/en/pdo.error-handling.php public $options; - public $username = null; + public $host = null; + public $port = null; + public $user = null; public $password = null; - public $pdo_error = PDO::ERRMODE_EXCEPTION; # See: http://php.net/manual/en/pdo.error-handling.php # Database infos public $title; @@ -61,12 +63,6 @@ class Db extends nb { # Args $db = is_scalar($db) ? array('pdo' => $db) : $db; if (is_array($db)) { - -# NB 05.03.16 if (!empty($db->configs)) { -# NB 05.03.16 $this->dbs = $this->config2h($db->dbs); -# NB 05.03.16 unset($db->dbs); -# NB 05.03.16 } - foreach ($db as $k=>$v) $this->$k = $v; } @@ -78,37 +74,68 @@ class Db extends nb { function connect($pdo=null) { if ($pdo !== null) $this->pdo = $pdo; + if (empty($this->pdo) and $this->type) $this->pdo = $this->type.':'; + if (empty($this->type)) $this->type = strtolower(preg_replace('/^([^:]+):.*$/','\1',$this->pdo)); + + # Title + if (empty($this->title)) $this->title = prettyText($this->name); # Name - #if (empty($this->name)) $this->name = preg_replace('/^(?:(?:sqlite:.*(.*?)(\.\w+)?)|(?:.*?dbname=([^;]+).*?))$/','\1',$this->pdo); -# NB 22.12.15 if (empty($this->name) and preg_match('/(?:sqlite:|dbname=)([^;\.]+)/',$this->pdo,$m)) { - if (empty($this->name) and !empty($this->pdo) - and preg_match('/(?:sqlite:(?:.*\/)?|dbname=)([^;\.]+)/',$this->pdo,$m) - ) { + if (empty($this->name) and preg_match('/(?:sqlite:(?:.*\/)?|dbname=)([^;\.]+)/',$this->pdo,$m)) { $this->name = $m[1]; } - # Title - if (empty($this->title)) $this->title = prettyText($this->name); + # User + #preg_match_all('/[:;](user|username|password)=([^;]*)/',$this->pdo,$m); bye($m); + + # Params + if (preg_match_all("/[:;](\w+)=([^;]+)/",$this->pdo,$m,PREG_SET_ORDER)) { + #array_shift($m); + foreach ($m as $param) { + $k = $param[1]; + if (!empty($this->$k)) continue; + $this->$k = $param[2]; + } + #$this->bye($m); + } + #$this->bye($this->pdo); + + if ($this->type != 'sqlite') { + foreach (array( + 'host' => 'host', + 'port' => 'port', + 'user' => 'user', + 'password' => 'password', + 'name' => 'dbname', + ) as $k => $v) { + #self::debug($k); + if (empty($this->$k)) continue; + $this->pdo = preg_replace("/\b$v=[^;]*(;|$)/","",$this->pdo); + $this->pdo .= (preg_match('/[:;]$/',$this->pdo) ? '' : ';') . "$v=".$this->$k; + } + } + #self::bye(">>".$this->pdo); + + # Pdo if (empty($this->pdo)) return false; # Type - $this->type = strtolower(preg_replace('/^([^:]+):.*$/','\1',$this->pdo)); if (!in_array($this->type,array( 'sqlite', 'mysql', 'pgsql', ))) $this->bye("unknow type = `".$this->type."`"); - # User - #preg_match_all('/[:;](user|username|password)=([^;]*)/',$this->pdo,$m); bye($m); - # Connect if (preg_match('/^sqlite:(.*)$/',$this->pdo,$m) and !is_readable($m[1])) { $this->bye("Can't connect to database `".$m[1]."` type=$this->type",false); } else { - $this->conn = new PDO($this->pdo,$this->username,$this->password,$this->options); + $this->conn = new PDO($this->pdo,$this->user,$this->password,$this->options); + + if ($this->type == 'pgsql') { + if (isset($this->password)) $this->pdo = preg_replace('/\bpassword=([^;]+;?)/','',$this->pdo).''; + } if (isset($this->pdo_error)) $this->conn->setAttribute(PDO::ATTR_ERRMODE, $this->pdo_error); } @@ -564,11 +591,12 @@ EOF; if (!empty($files)) { - if (empty($h)) $h = array(); + $h = array(); foreach ($files as $file) { if (is_readable($file) and ($yaml = @yaml_parse_file($file))) { #foreach ($yaml as $k=>$v) { $yaml[$k]['_config2h'] = $file; } + #debug($file); $h = array_replace_recursive($h,$yaml); } #debug($file); @@ -581,6 +609,7 @@ EOF; #debug($h); } if (!$h) return false; + #self::bye($h['puppetdb']); #if ($first !== false) debug ( self::ar_first($h) ); if ($first !== false) $first = self::ar_first($h); return $h; @@ -597,9 +626,8 @@ EOF; $Db = new self(); #bye($_SERVER["PHP_SELF"]); bye($_SERVER); - if (!array_key_exists(nb::$root_dir.'/etc/dbs.yaml',$conf)) $conf[] = nb::$root_dir.'/etc/dbs.yaml'; - if (!array_key_exists('/etc/rent/db.yaml',$conf)) $conf[] = '/etc/rent/db.yaml'; - if (!array_key_exists('/etc/dbs.yaml',$conf)) $conf[] = '/etc/dbs.yaml'; + if (!in_array(nb::$root_dir.'/etc/dbs.yaml',$conf)) $conf[] = nb::$root_dir.'/etc/dbs.yaml'; + if (!in_array('/etc/dbs.yaml',$conf)) $conf[] = '/etc/dbs.yaml'; # Load databases if (! ($dbs = $Db->config2h($conf)) ) return false; @@ -607,7 +635,7 @@ EOF; # NB 05.03.16 #$ar[] = 'b'; # NB 05.03.16 array_unshift($ar,'b'); # NB 05.03.16 bye($ar); -# NB 05.03.16 bye($dbs['puppetdb']); + #bye($dbs['izi']); # Param - Extract dbname from table if (preg_match('/^(\w+)\.(.*?)$/',$Db->p('table'),$m)) { @@ -630,13 +658,11 @@ EOF; #bye($Db->p('db')); if (!isset($dbs[$Db->p('db')])) $Db->bye("Can't find db: `".$Db->p('db')."` in `".join(",",array_keys($dbs))."`",false); $db = array_merge($dbs[$Db->p('db')],array('dbs'=>array_keys($dbs))); + unset($dbs); - if (empty($db['pdo'])) $Db->bye("No pdo for db: `".$Db->p('db')."`",false); - - #bye($db['pdo']); $Db->__construct($db); - unset($dbs); + if (empty($Db->pdo)) $Db->bye("No pdo for db: `".$Db->p('db')."`",false); /* Table