@digigov-oss/gsis-audit-record-db

JSON file storage database for use with audit mechanism of GSIS

Usage no npm install needed!

<script type="module">
  import digigovOssGsisAuditRecordDb from 'https://cdn.skypack.dev/@digigov-oss/gsis-audit-record-db';
</script>

README

AuditRecordDB

According to Common Guide for Client Applications of GSIS, (https://www.gsis.gr/dimosia-dioikisi/ked/koinos-odigos) all online services are required to have the addition of tracking data (auditRecord) when calling them.

This module implements a JSON file storage database for use with the audit mechanism of GSIS. It follows the interoperability_specsv1.4 and simplifies the integration with Nextjs/Nodejs projects.

It also provides a way for automating protocol numbers and transactions Id's, if your app does not provide them.

Usage

import auditRecordDB from 'AuditRecordDB';

const main = () =>{
   //returns generated AuditRecord, that record stored by default in `/tmp` folder using `FileEngine` SOULD change this for production
   console.log(auditRecordDB({auditUnit:'DigiGov'}))
   /*
   {
    auditUnit: 'DigiGov',
    auditTransactionId: '1',
    auditProtocol: '1/2022-01-01',
    auditTransactionDate: '2022-01-01T00:00:01Z',
    auditUserIp: '127.0.0.1',
    auditUserId: 'system'
   }
   */
}

//you can change the FileEngine storage path
import auditRecordDB,{FileEngine} from 'AuditRecordDB';
const main = () =>{
console.log(auditRecordDB({},new FileEngine('/tmp/auditRecords')))
}

Prostgresql

To work with Postgresql, you need to use PostgresqlEngine instead of FileEngine Moreover, have to install the native libpg library for Postgresql.

On macOS: brew install libpq On Ubuntu/Debian: apt-get install libpq-dev g++ make On RHEL/CentOS: yum install postgresql-devel

//you can use the PostgresSqlEngine via enviroment variables
import auditRecordDB,{PostgresSqlEngine} from 'AuditRecordDB';
const main = () =>{
process.env.PGHOST='localhost'
process.env.PGUSER='dbuser'
process.env.PGPASSWORD='secretpassword'
process.env.PGDATABASE='audit'
process.env.PGPORT='5432'

console.log(auditRecordDB({},new PostgresSqlEngine()))
}

//or via connection string
import auditRecordDB,{PostgresSqlEngine} from 'AuditRecordDB';
const connectionString = 'postgresql://dbuser:secretpassword@localhost:5432/audit'
const main = () =>{
console.log(auditRecordDB({},new PostgresSqlEngine()))
}
//The postgresql engine asumes that you have already create the table `audit_records` in the database, and that the table has the following columns:
//auditUnit: text
//auditTransactionId: text
//auditProtocol: text
//auditTransactionDate: text
//auditUserIp: text
//auditUserId: text
//
//if you have already a table on Postgresql you can use it by mapping the columns to the AuditRecord
import auditRecordDB,{PostgresSqlEngine} from 'AuditRecordDB';
const connectionString = 'postgresql://dbuser:secretpassword@localhost:5432/audit'
const main = () =>{
console.log(auditRecordDB({},new PostgresSqlEngine({
tableName:'audit_records',
columns:{
  auditUnit:'audit_unit',
  auditTransactionId:'audit_transaction_id',
  auditProtocol:'audit_protocol',
  auditTransactionDate:'audit_transaction_date',
  auditUserIp:'audit_user_ip',
  auditUserId:'audit_user_id'
  }
})))
}

AuditEngine

By default, the file storage engine FileEngine is used. Please keep in mind to change the storage path to app needs.

The app can also use PostgreSqlEngine.

To make it work, pass environment variables according to the documentation of the LIBPGSQL library. https://www.postgresql.org/docs/9.1/libpq-envars.html

Please remember that protocol and/or transaction id sequences will be created in the database if not provided. Each day a new sequence will be created for protocol needs as protyyyymmdd_seq. So if you do not need to keep tracking on application, you may need to clean up those sequences from time to time using a cron job.

Look at FileEngine.ts and PostgreSqlEngine.ts for examples to extend the store to another 'real' database.