Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rlm_sql_mongo: Backport SQL/Mongo driver from v3.2.x #5154

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions raddb/mods-available/sql
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ sql {
# * oracle
# * postgresql
# * sqlite
# * mongo
#
dialect = "sqlite"

Expand All @@ -68,6 +69,7 @@ sql {
# | freetds | mssql
# | null | any
# | unixodbc | mssql
# | mongodb | mongo
# |===
#
driver = "${dialect}"
Expand Down
46 changes: 46 additions & 0 deletions raddb/mods-config/sql/driver/mongo
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#
# Configuration for Mongo.
#
# Note that the Mongo driver is experimental. The FreeRADIUS developers
# are unable to help with the syntax of the Mongo queries. Please see
# the Mongo documentation for that syntax.
#
# The Mongo driver supports only the following methods:
#
# aggregate
# findAndModify
# findOne
# insert
#
# For examples, see the query files:
#
# raddb/mods-config/sql/main/mongo/queries.conf
# raddb/mods-config/sql/main/mongo/schema-sample.js
#
# In order to use findAndModify with an aggretation pipleline, make
# sure that you are running MongoDB version 5.0 or greater. FreeRADIUS
# assumes that the paramaters passed to the methods are supported by the
# version of MongoDB which it is connected to.
#
mongo {
#
# The TLS parameters here map directly to the Mongo TLS configuration
#

# tls {
# ca_file = "/etc/ssl/certs/my_ca.crt"
# ca_path = "/etc/ssl/certs/"
# certificate_file = "/etc/ssl/certs/private/client.crt"
# private_key_file = "/etc/ssl/certs/private/client.key"
# cipher = "DHE-RSA-AES256-SHA:AES128-SHA"
# }
#
#
# If yes, (or auto and libmongo-c reports warnings are
# available), will retrieve and log additional warnings from
# the server if an error has occured.
#
# Defaults to 'auto'
#
warnings = auto
}
478 changes: 478 additions & 0 deletions raddb/mods-config/sql/main/mongo/queries.conf

Large diffs are not rendered by default.

329 changes: 329 additions & 0 deletions raddb/mods-config/sql/main/mongo/schema-sample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,329 @@
/*
* -*- text -*-
*
* raddb/mods-config/sql/main/mongo/schema-sample.js
*
* Schema for processing radcheck,radreply,radusergroup,radgroupcheck and radgroupreply entries
*/

// Drop
db = db.getSiblingDB("freeradius");
db.dropDatabase();

/*
* Field: authorize_check_query
*/
db.createCollection("radcheck")

// Load
db.radcheck.insert([
{
username: 'bob',
attributes: [
{
_id: 0,
username: 'bob',
attribute: 'User-Name',
value: 'bob',
op: '=='
},
{
_id: 1,
username: 'bob',
attribute: 'NAS-Port',
value: '123',
op: ':='
},
{
_id: 2,
username: 'bob',
attribute: 'Cache-TTL',
value: '456',
op: ':='
}
]
}
])

// Query
db.radcheck.aggregate([
{
$match:{
'username': 'bob'
}
},
{
$unwind: '$attributes'
},
{
$replaceRoot:{
newRoot:'$attributes'
}
}
])


/*
* Field: authorize_reply_query
*/
db.createCollection("radreply")

// Load
db.radreply.insert([
{
username: 'bob',
attributes: [
{
_id: 0,
username: 'bob',
attribute: 'NAS-IP-Address',
value: '192.168.250.1',
op: ':='
},
{
_id: 1,
username: 'bob',
attribute: 'Session-Timeout',
value: '86400',
op: ':='
},
{
_id: 2,
username: 'bob',
attribute: 'Service-Type',
value: 'Framed-User',
op: ':='
}
]
}
])

// Query
db.radreply.aggregate([
{
'$match':{
'username': 'bob'
}
},
{
'$unwind': '$attributes'
},
{
$replaceRoot:{
newRoot:'$attributes'
}
}
])


/*
* Field: group_membership_query
*/
db.createCollection("radusergroup")

// Load
db.radusergroup.insert([
{
username: 'bob',
groupname: 'PLANO_10MB',
priority: 0
}
])

// Query
db.radusergroup.aggregate([
{
"$match": {
"username": "bob"
}
},
{
'$project': {
_id: 0,
'groupname': '$groupname'
}
}
])


/*
* Field: authorize_group_check_query
*/
db.createCollection("radgroupcheck")

// Load
db.radgroupcheck.insert([
{
groupname: 'PLANO_10MB',
attributes: [
{
_id: 0,
groupname: 'PLANO_10MB',
attribute: 'Service-Type',
value: 'Framed-User',
op: ':='
},
{
_id: 1,
groupname: 'PLANO_10MB',
attribute: 'Framed-Protocol',
value: 'PPP',
op: ':='
},
{
_id: 2,
groupname: 'PLANO_10MB',
attribute: 'IP-Pool.Name',
value: 'pool_valido',
op: ':='
},
]
}
])

db.radgroupcheck.aggregate([
{
'$match': {
'groupname': 'PLANO_10MB'
}
},
{
'$unwind': '$attributes'
},
{
'$replaceRoot': {
'newRoot': '$attributes'
}
}
])

/*
* Field: authorize_group_reply_query
*/
db.createCollection("radgroupreply")

// Load
db.radgroupreply.insert([
{
groupname: 'PLANO_10MB',
attributes: [
{
_id: 0,
groupname: 'PLANO_10MB',
attribute: 'Acct-Output-Gigawords',
value: '4096',
op: ':='
},
{
_id: 1,
groupname: 'PLANO_10MB',
attribute: 'Acct-Input-Gigawords',
value: '4096',
op: ':='
},
{
_id: 2,
groupname: 'PLANO_10MB',
attribute: 'Vendor-Specific.WISPr.Redirection-URL',
value: 'http://192.168.1.1/captive',
op: ':='
},
{
_id: 3,
groupname: 'PLANO_10MB',
attribute: 'Vendor-Specific.WISPr.Location-ID',
value: 'CaptiveTapioca',
op: ':='
}
]
}
])

db.radgroupreply.aggregate([
{
'$match': {
'groupname': 'PLANO_10MB'
}
},
{
'$unwind': '$attributes'
},
{
'$replaceRoot': {
'newRoot': '$attributes'
}
}
])

/*
* Accounting
*/
db.createCollection("radacct")
// accounting-on {
// query1
db.radacct.findAndModify({
"query":{
"acctstoptime":null,
"nasipaddress":"127.0.0.1",
"acctstarttime":{
"$lt":100
}
},
"update":{
"$set":{
"acctstoptime":"100",
"acctsessiontime":{
"$subtract":[
"100",
"acctstarttime"
]
},
"acctterminatecause":"NAS-Reboot",
"class":"0xcafecade",
"framedipaddress":"10.153.10.1",
"update_date":{
"$date":{
"$numberLong":"1692128889000"
}
},
"start_time":""
},
"$push":{
"events_data":{
"event_id":"0x930df6959411d02de4a16c0abdc2dbeac0d8d86bb8db39346209837843fe4d41",
"event_type":"Accounting-On",
"event_time":"",
"creation_date":{
"$date":{
"$numberLong":"1692128889000"
}
}
}
},
"$setOnInsert":{
"pool_name":"",
"closed":false,
"update_counter":0,
"creation_date":{
"$date":{
"$numberLong":"1692128889000"
}
}
}
},
"upsert":true
})

//query2
// db.createCollection("nasreload")
// db.nasreload.findAndModify({
// { // find a document with that filter
// 'nasipaddress': %{NAS-IP-Address},
// 'reloadtime': ${....event_timestamp}
// },
// "update": {
// 'nasipaddress': %{NAS-IP-Address},
// 'reloadtime': ${....event_timestamp}
// },
// { upsert: true, new: true} // options
// })