Skip to content
This repository was archived by the owner on Sep 19, 2022. It is now read-only.

Commit 9e12729

Browse files
authored
Merge pull request #23 from pajavyskocil/IdPListServiceDB
IdPListsServiceDB
2 parents 7e97dfe + afca193 commit 9e12729

File tree

11 files changed

+524
-143
lines changed

11 files changed

+524
-143
lines changed

config-templates/module_perun.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939
*/
4040
//'disco.disableWhitelisting' => true,
4141

42+
/**
43+
* specify which type of IdPListService will be used
44+
* Expected values: csv, db
45+
*/
46+
'idpListServiceType' => '',
47+
4248
/**
4349
* Specify prefix for filtering AuthnContextClassRef
4450
* All AuthnContextClassRef values starts with this prefix will be removed before the request will be send to IdP
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/**
3+
* This is example configuration for connection to database with whitelist/greylist
4+
* Copy this file to default config directory and edit the properties.
5+
*
6+
* @author Pavel Vyskočil <[email protected]>
7+
*/
8+
$config = array(
9+
10+
/*
11+
* Fill the serverName
12+
*/
13+
'serverName' => 'localhost',
14+
15+
/*
16+
* If you want to use the default port, please comment this option
17+
*/
18+
'port' => 3306,
19+
20+
/*
21+
* Fill the user name
22+
*/
23+
'userName' => 'proxy',
24+
25+
/*
26+
* Fill the password
27+
*/
28+
'password' => 'passwd',
29+
30+
/*
31+
* Fill the database name
32+
*/
33+
'databaseName' => 'Proxy',
34+
35+
/*
36+
* Fill the table name for whiteList
37+
*/
38+
'whiteListTableName' => 'whiteList',
39+
40+
/*
41+
* Fill the table name for greyList
42+
*/
43+
'greyListTableName' => 'greyList',
44+
45+
/*
46+
* Fill true, if your SQL Server used encrypted connections. False if not
47+
*/
48+
'encryption' => true/false,
49+
50+
/*
51+
* The path name to the certificate authority file.
52+
*
53+
* If your SQL Server used encrypted connections, you must fill this option.
54+
*/
55+
'ssl_ca' => '/example/ca.pem',
56+
57+
/*
58+
* The path name to the certificate file.
59+
*
60+
* If your SQL Server used encrypted connections, you must fill this option.
61+
*/
62+
'ssl_cert_path' => '/example/cert.pem',
63+
64+
/*
65+
* The path name to the key file.
66+
*
67+
* If your SQL Server used encrypted connections, you must fill this option.
68+
*/
69+
'ssl_key_path' => '/example/key.pem',
70+
71+
/*
72+
* The pathname to a directory that contains trusted SSL CA certificates in PEM format.
73+
*
74+
* If your SQL Server used encrypted connections, you must fill this option.
75+
*/
76+
'ssl_ca_path' => '/etc/ssl',
77+
78+
);

config-templates/tables.sql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- Script for creating tables
2+
CREATE TABLE whiteList (
3+
date DATETIME NOT NULL DEFAULT current_timestamp,
4+
entityId VARCHAR(255) NOT NULL,
5+
reason VARCHAR(255),
6+
INDEX (entityId),
7+
PRIMARY KEY (entityId)
8+
);
9+
10+
CREATE TABLE greyList (
11+
date DATETIME NOT NULL DEFAULT current_timestamp,
12+
entityId VARCHAR(255) NOT NULL,
13+
reason VARCHAR(255),
14+
INDEX (entityId),
15+
PRIMARY KEY (entityId)
16+
);

lib/DatabaseCommand.php

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
<?php
2+
include("DatabaseConnector.php");
3+
/**
4+
* Class for working with Database
5+
*
6+
* @author Pavel Vyskočil <[email protected]>
7+
*/
8+
class DatabaseCommand
9+
{
10+
11+
const WHITELIST = "whiteList";
12+
const GREYLIST = "greyList";
13+
14+
/**
15+
* Function returns array of all IdPs in whitelist/greylist
16+
* @param string $tableName 'whitelist' or 'greylist'
17+
* @return array of all IdPs, every IdP is represents as array
18+
*/
19+
public static function getAllIdps($tableName) {
20+
$databaseConnector = new DatabaseConnector();
21+
$conn = $databaseConnector->getConnection();
22+
$whiteListTableName = $databaseConnector->getWhiteListTableName();
23+
$greyListTableName = $databaseConnector->getGreyListTableName();
24+
$table = null;
25+
$listOfIdPs = array();
26+
assert($conn != NULL);
27+
28+
if ($tableName == self::WHITELIST) {
29+
$table = $whiteListTableName;
30+
} else if ($tableName == self::GREYLIST) {
31+
$table = $greyListTableName;
32+
}
33+
34+
$stmt = $conn->prepare("SELECT * FROM " . $table);
35+
36+
if ($stmt) {
37+
$ex =$stmt->execute();
38+
if ($ex === false) {
39+
SimpleSAML\Logger::error("Error during select all from " . $table);
40+
}
41+
42+
$stmt->bind_result($timestamp, $entityId, $reason);
43+
while ($stmt->fetch()) {
44+
$idp = array();
45+
$idp['timestamp'] = $timestamp;
46+
$idp['entityid'] = $entityId;
47+
$idp['reason'] = $reason;
48+
array_push($listOfIdPs, $idp);
49+
}
50+
51+
$stmt->close();
52+
} else {
53+
SimpleSAML\Logger::error("Error during preparing statement");
54+
}
55+
56+
$conn->close();
57+
return $listOfIdPs;
58+
}
59+
60+
/**
61+
* Function returns array of all entityId in whitelist/greylist
62+
* @param string $tableName 'whitelist' or 'greylist'
63+
* @return array of entityIds
64+
*/
65+
public static function getAllEntityIds($tableName) {
66+
$databaseConnector = new DatabaseConnector();
67+
$conn = $databaseConnector->getConnection();
68+
$whiteListTableName = $databaseConnector->getWhiteListTableName();
69+
$greyListTableName = $databaseConnector->getGreyListTableName();
70+
$table = null;
71+
$listOfIdPs = array();
72+
assert($conn != NULL);
73+
74+
if ($tableName == self::WHITELIST) {
75+
$table = $whiteListTableName;
76+
} else if ($tableName == self::GREYLIST) {
77+
$table = $greyListTableName;
78+
}
79+
80+
$stmt = $conn->prepare("SELECT * FROM " . $table);
81+
82+
if ($stmt) {
83+
$ex =$stmt->execute();
84+
if ($ex === false) {
85+
SimpleSAML\Logger::error("Error during select all entityIds from " . $table);
86+
}
87+
88+
$stmt->bind_result($timestamp, $entityId, $reason);
89+
while ($stmt->fetch()) {
90+
array_push($listOfIdPs, $entityId);
91+
}
92+
93+
$stmt->close();
94+
} else {
95+
SimpleSAML\Logger::error("Error during preparing statement");
96+
}
97+
98+
$conn->close();
99+
return $listOfIdPs;
100+
}
101+
102+
103+
/**
104+
* Function inserts the line into table with $tableName
105+
* @param string $tableName 'whitelist' or 'greylist'
106+
* @param string $entityId
107+
* @param string $reason
108+
*/
109+
public static function insertTolist($tableName, $entityId, $reason) {
110+
$databaseConnector = new DatabaseConnector();
111+
$conn = $databaseConnector->getConnection();
112+
$whiteListTableName = $databaseConnector->getWhiteListTableName();
113+
$greyListTableName = $databaseConnector->getGreyListTableName();
114+
$table = null;
115+
assert($conn != NULL);
116+
117+
if ($tableName == self::WHITELIST) {
118+
$table = $whiteListTableName;
119+
} else if ($tableName == self::GREYLIST) {
120+
$table = $greyListTableName;
121+
}
122+
123+
$stmt = $conn->prepare("INSERT INTO " . $table . " (entityId, reason) VALUES (?, ?)");
124+
125+
if ($stmt) {
126+
$stmt->bind_param("ss", $entityId, $reason);
127+
$ex =$stmt->execute();
128+
if ($ex === false) {
129+
SimpleSAML\Logger::error("Error during inserting entityId " . $entityId . " into " . $table);
130+
}
131+
132+
SimpleSAML\Logger::debug("EntityId " . $entityId . " was inserted into " . $table);
133+
$stmt->close();
134+
} else {
135+
SimpleSAML\Logger::error("Error during preparing statement");
136+
}
137+
138+
$conn->close();
139+
}
140+
141+
/**
142+
* Function deletes the line from table with $tableName and $entityID
143+
* @param string $tableName 'whitelist' or 'greylist'
144+
* @param string $entityId
145+
*/
146+
public static function deleteFromList($tableName, $entityId) {
147+
$databaseConnector = new DatabaseConnector();
148+
$conn = $databaseConnector->getConnection();
149+
$whiteListTableName = $databaseConnector->getWhiteListTableName();
150+
$greyListTableName = $databaseConnector->getGreyListTableName();
151+
$table = null;
152+
assert($conn != NULL);
153+
154+
if ($tableName == self::WHITELIST) {
155+
$table = $whiteListTableName;
156+
} else if ($tableName == self::GREYLIST) {
157+
$table = $greyListTableName;
158+
}
159+
160+
$stmt = $conn->prepare("DELETE FROM " . $table . " WHERE entityId=?");
161+
162+
if ($stmt) {
163+
$stmt->bind_param("s", $entityId);
164+
$ex =$stmt->execute();
165+
if ($ex === false) {
166+
SimpleSAML\Logger::error("Error during deleting entityId " . $entityId . " from " . $table);
167+
}
168+
169+
SimpleSAML\Logger::debug("EntityId " . $entityId . " was deleted from " . $table);
170+
$stmt->close();
171+
} else {
172+
SimpleSAML\Logger::error("Error during preparing statement");
173+
}
174+
175+
$conn->close();
176+
}
177+
}

lib/DatabaseConnector.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
/**
3+
* Class for getting connection to DB
4+
*
5+
* @author Pavel Vyskočil <[email protected]>
6+
*/
7+
class databaseConnector
8+
{
9+
private $serverName;
10+
private $port;
11+
private $username;
12+
private $password;
13+
private $databaseName;
14+
private $whitelistTableName;
15+
private $greyListTableName;
16+
private $encryption;
17+
private $sslCA;
18+
private $sslCert;
19+
private $sslKey;
20+
private $sslCAPath;
21+
22+
const CONFIG_FILE_NAME = 'module_perun_idpListsServiceDB.php';
23+
const SERVER = 'serverName';
24+
const PORT = 'port';
25+
const USER = 'userName';
26+
const PASSWORD = 'password';
27+
const DATABASE = 'databaseName';
28+
const WHITELIST_TABLE_NAME = 'whiteListTableName';
29+
const GREYLIST_TABLE_NAME = 'greyListTableName';
30+
const ENCRYPTION = 'encryption';
31+
const SSL_CA = 'ssl_ca';
32+
const SSL_CERT = 'ssl_cert_path';
33+
const SSL_KEY = 'ssl_key_path';
34+
const SSL_CA_PATH = 'ssl_ca_path';
35+
36+
37+
public function __construct()
38+
{
39+
$conf = SimpleSAML_Configuration::getConfig(self::CONFIG_FILE_NAME);
40+
$this->serverName = $conf->getString(self::SERVER);
41+
$this->port = $conf->getInteger(self::PORT, null);
42+
$this->username = $conf->getString(self::USER);
43+
$this->password = $conf->getString(self::PASSWORD);
44+
$this->databaseName = $conf->getString(self::DATABASE);
45+
$this->whitelistTableName = $conf->getString(self::WHITELIST_TABLE_NAME);
46+
$this->greyListTableName = $conf->getString(self::GREYLIST_TABLE_NAME);
47+
$this->encryption = $conf->getBoolean(self::ENCRYPTION);
48+
$this->sslCA = $conf->getString(self::SSL_CA);
49+
$this->sslCert = $conf->getString(self::SSL_CERT);
50+
$this->sslKey = $conf->getString(self::SSL_KEY);
51+
$this->sslCAPath = $conf->getString(self::SSL_CA_PATH);
52+
}
53+
54+
/**
55+
* Function returns the connection to db
56+
* @return mysqli connection
57+
*/
58+
public function getConnection()
59+
{
60+
$conn = mysqli_init();
61+
if ($this->encryption === true) {
62+
SimpleSAML_Logger::debug("Getting connection with encryption.");
63+
mysqli_ssl_set($conn, $this->sslKey, $this->sslCert, $this->sslCA, $this->sslCAPath, null);
64+
if ($this->port === null) {
65+
mysqli_real_connect($conn, $this->serverName, $this->username, $this->password, $this->databaseName);
66+
} else {
67+
mysqli_real_connect($conn, $this->serverName, $this->username, $this->password, $this->databaseName, $this->port);
68+
}
69+
} else {
70+
if ($this->port === null) {
71+
mysqli_real_connect($conn, $this->serverName, $this->username, $this->password, $this->databaseName);
72+
} else {
73+
mysqli_real_connect($conn, $this->serverName, $this->username, $this->password, $this->databaseName, $this->port);
74+
}
75+
}
76+
return $conn;
77+
}
78+
79+
/**
80+
* Function returns name of table for whitelist
81+
* @return mixed whitelist table name
82+
*/
83+
public function getWhiteListTableName()
84+
{
85+
return $this->whitelistTableName;
86+
}
87+
88+
/**
89+
* Function returns name of table for greylist
90+
* @return mixed whitelist table name
91+
*/
92+
public function getGreyListTableName()
93+
{
94+
return $this->greyListTableName;
95+
}
96+
}

0 commit comments

Comments
 (0)