2020年3月29日日曜日

Rocky linux 8にpostgres14をインストール

【Rocky linux9 Postgres clieant インストール 2024/1/10追記】
# dnf -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-$(rpm -E %{centos})-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# dnf install libpq-devel
or
# dnf install postgresql15-devel


【2023/12/15追記】
■Postgres client インストール
# dnf install libpq-devel
# which pg_config
/usr/bin/pg_config

ERROR
 gcc: エラー: /usr/lib/rpm/redhat/redhat-hardened-cc1: そのようなファイルやディレクトリはありません
→dnf install redhat-rpm-config


【2023/04/20更新】
■Postgre client インストール(Django)
# dnf install postgresql14-devel
# find / -print |grep pg_config
/usr/pgsql-14/bin/pg_config
$ . ./env
$ PATH=/usr/pgsql-14/bin:$PATH;export PATH
$ pip3 install psycopg2
$ ./env freeze
$ vi Config/settings.py << __EOF__
DATABASES = { 
   'default': {
     'ENGINE'       :  'django.db.backends.postgresql_psycopg2',
     'NAME'           :  'office_pg',
     'USER'            :  'admin',
     'PASSWORD':  'casio00',
     'HOST'            :  '192.168.254.201',
     'PORT'            :  '5432',
   },
}  
$ psql -U postgres -h 192.168.254.201 

■Postgre Server インストール
$  cat /etc/redhat-release
Rocky Linux release 8.7 (Green Obsidian)
# dnf -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-$(rpm -E %{centos})-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# dnf update
# dnf module list postgresql
# dnf module disable postgresql
# dnf search postgresql*-server
# dnf install -y postgresql14-server
# passwd postgres
# PGSETUP_INITDB_OPTIONS='--encoding=UTF-8 --no-locale'
# /usr/pgsql-14/bin/postgresql-14-setup initdb
# vi /var/lib/pgsql/14/data/pg_hba.conf 
#local   all             all                                   peer
local     all             all                                    trust
#host    all             all             127.0.0.1/32            ident
host all all 192.168.0.0/16                     password
# vi /var/lib/pgsql/14/data/postgresql.conf
listen_addresses = '*'          
port = 5432
# firewall-cmd --add-port=5432/tcp --zone=public --permanent
# firewall-cmd --reload
# mkdir  -p /data/postgres/data
# chown  -R postgres:postgres /data/postgres/data
# semanage fcontext -a -t postgresql_db_t "/data/postgres(/.*)?"
# grep -i postgresql /etc/selinux/targeted/contexts/files/file_contexts.local
# restorecon -R -v /data/postgres
# ls -lZR /data/postgres

(確認)
#  ls -l /var/lib/pgsql/14/data
 # vi /usr/lib/systemd/system/postgresql-14.service
#  grep -v -E "^#|^$" /var/lib/pgsql/14/data/pg_hba.conf
# grep listen_addresses /var/lib/pgsql/14/data/postgresql.conf
# ls -lZ /var/lib/pgsql
 drwx------. 4 postgres postgres system_u:object_r:postgresql_db_t:s0 51  4月 20 14:26 14

 
# systemctl start postgresql-14
# systemctl enable  postgresql-14

$ su - postgres

$ psql -U postgres
ALTER USER postgres PASSWORD 'password';

$ psql 
create user admin with password 'password' superuser;
create database ARCSDBMS owner admin;



-------------------------------------old----------------------------------------------------
# useradd admin
# su - postgres
$ postgres --version
postgres (PostgreSQL) 12.1
$ createuser admin
$ createdb testdb -O admin
$ psql  -c "select usename from pg_user;"
$ psql -l testdb
$ psql testdb
testdb=>\du
testdb=>\l
testdb=> create table test_table (no int, name text);
testdb=> insert into test_table (no,name) values (01,'CentOS');
testdb=> select * from test_table;
testdb=> drop table test_table;
testdb=>\dt
testdb=>\q
$ dropdb testdb
$ psql -l

【パスワード変更】
# vi /var/lib/pgsql/data/pg_hba.conf
local   all             all                                    peer/md5 → trust
# systemctl  restart postgresql
# su - postgres
$ psql -U postgres
postgres=# alter role postgres with password 'newpassword';

pg_hba.conf を戻す。







2020年3月5日木曜日

AES 暗号化

JWT debugger : https://jwt.io/

#!/usr/bin/env python36
# coding=utf-8
import base64
import string, random
import json
from Crypto import Random
from Crypto.Cipher import AES


def generateNewSeed( n=32 ):
  return(''.join([random.choice(string.ascii_letters + string.digits) for i in range(n)]))

class AESCipher(object):
  def __init__(self, seed, block_size=32):
    self.bs = block_size
    if len(seed) >= block_size:
      self.key = seed[:block_size]
    else:
      self.key = self._pad(seed)

  def encrypt(self, raw):
    raw = self._pad(raw)
    iv = Random.new().read(AES.block_size)
    cipher = AES.new(self.key, AES.MODE_CBC, iv)
    return base64.b64encode(iv + cipher.encrypt(raw))

  def decrypt(self, enc):
    enc = base64.b64decode(enc)
    iv = enc[:AES.block_size]
    cipher = AES.new(self.key, AES.MODE_CBC, iv)
    return self._unpad(cipher.decrypt(enc[AES.block_size:]))

  def _pad(self, s):
    return s + (self.bs - len(s) % self.bs) * chr(self.bs - len(s) % self.bs)

  def _unpad(self, s):
    return s[:-ord(s[len(s)-1:])]

  def _getkey( self ):
    return self.key

  def encryptGCSRToken( self, gcode, ccode, scode, rcode, token ):
    data = { 'gcode':gcode, 'ccode':ccode, 'scode':scode, 'rcode':rcode, 'token':token }
    raw  = json.dumps( data )
    return( self.encrypt( raw ))

  def decryptGCSRToken( self, cipher  ):
    raw = self.decrypt( cipher )
    data = json.loads( raw )
    return data['gcode'], data['ccode'],data['scode'],data['rcode'],data['token']

def main():
  seed = generateNewSeed()

  CIPHER = AESCipher( seed )

  print( "seed=", seed )
  print( "key(getkey)=", CIPHER._getkey() )
  print( "block size=",AES.block_size )

  # 暗号化
  encryption = CIPHER.encrypt("hogefuga")
  print( 'ENCRYPTION =',encryption)

  encryption1 = CIPHER.encrypt("hogefuga")
  print( 'ENCRYPTION1=',encryption1)

  # 復号化
  print(CIPHER.decrypt(encryption))
  print(CIPHER.decrypt(encryption))


  gcode = '901'
  ccode = '1003'
  scode = '0002'
  rcode = '77xxxxxxxxxx1'
  token = 'tokenxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxtoken'


  cipher = CIPHER.encryptGCSRToken( gcode, ccode, scode, rcode, token )

  print( 'cipher=', cipher )                 # GCSR +TOKEN 端末内に格納

  gcode, ccode, scode, rcode, token = CIPHER.decryptGCSRToken( cipher )

  print( gcode )
  print( ccode )
  print( scode )
  print( rcode )
  print( token )


if __name__ == "__main__":
    main()

2020年3月2日月曜日

apache child pid exit signal Segmentation fault (11)

$ gcb /usr/sbin/httpd

  use: debuginfo-install httpd-2.4.6-90.el7.centos.x86_64

$ debuginfo-install httpd-2.4.6-90.el7.centos.x86_64
$ gcb /usr/sbin/httpd
(gdb) run -X
(gdb) list
(gdb) where

quit

# ulimit -a | grep 'core file size'
# ulimit -S -c unlimited
# ulimit -a | grep 'core file size'

zabbix7 amazon linux2023 インストール postgres15

【postgres】 dnf -y install postgresql15-server postgresql15-server-devel postgresql-setup initdb passwd postgres vi `find / -name pg_hba.con...