How do I make FreeRADIUS run an SQL query in default site? - freeradius

What I want to do is to make FreeRADIUS run an SQL statement in a site file:
if(such and such condition is met){
run_some_sql_query
}
How do I do that?

I Assume that you are trying to run the SQL on Unlang. (This file is located on sites-enabled/default or sites-available/default)
First thing first, you should read about freeradius SQL https://wiki.freeradius.org/guide/sql-howto and https://wiki.freeradius.org/modules/Rlm_sql
Later, you can use the SQL Xlat as written there
Assumed that your unlang and sql module configuration is all set (your SQL module name is 'sql'), you can do something like this:
if(such and such condition is met){
"%{sql:SELECT * FROM radcheck WHERE username = '%{User-Name}'}"
"%{sql:INSERT INTO radcheck (username, attribute, op, value) VALUES ('%{User-Name}', 'Cleartext-Password', ':=', 'dummyPassword');}"
}

Related

Circle CI replace appsettings.json

I am starting to look at Circle CI to build my projects. At the moment we are using octopus deploy, but want to use something new.
Today we have a appsettings file eg. "Appsettings.json"
Here we have structure eg:
"ConnectionStrings": {
"DatabaseConnectionString": "MyLocalConnectionString",
"MessageBusConnectionString": "MyLocalConnectionString2"
},
"MessageBus": {
"Sqs": {
"DefaultQueu" : "LocalTestQueu",
"ErrorQueu": "LocalErrorQueu"
}
},
...
I want to replace all values with new ones.
Eg: DefaultQueu is the name of the key and I want LocalTestQueu value to be changed to "MyProductionQueu"
For example key in CircleCi would be something like:
MessageBus.Sqs.DefaultQueu = MyProductionQueu
and
ConnectionStrings.DatabaseConnectionString = MyProductionDatabaseConnectionString
How would I do that?
I know there is environment variables where I can do something like:
"DatabaseConnectionString": "$MyConnectionString"
where simply string replace $MyConnectionString with the real connection string. But that is not what I am looking for.
We have all our local connectionstrings stored in source control. So we need key / value replacement as described as before.
Octopus let's us do something like this:
There is no support for this at the moment.

Grails 3.3 execute H2 script command

I'm running a small, trivial Grails 3.3.0 application using a H2 file based database. For simple backup reasons I would like to dump the current database state to a file using the H2 specific SCRIPT command:
SCRIPT TO /path/to/backup/dir/tempoDb.sql;
Currently I am trying to execute the native SQL command like this.
User.withSession { session ->
NativeSQLQuerySpecification nativeSQLQuerySpecification = new NativeSQLQuerySpecification("SCRIPT TO /path/to/backup/dir/tempoDb.sql;", null, null)
session.executeNativeUpdate(nativeSQLQuerySpecification, new QueryParameters())
}
but this does not work.
You can autowire the dataSource and try to run your sql query using the connection obtained from datasource Without going through Hibernate. The dataSource bean is registered in the Grails Spring context and it is an instance of javax.sql.DataSource.
Here is an example of a Grails service that backup the current H2 database to the file system.
#ReadOnly
class BackupService {
DataSource dataSource
def backup() {
def sql = "SCRIPT DROP TO '${System.properties['java.io.tmpdir']}/backup.sql'"
Statement statement = dataSource.connection.createStatement()
boolean result = statement.execute(sql)
}
}

Mongotemplate can't rename a collection

I have a collection named collection_One. I want to rename the collection, but failed. mongotemplate showed me:
executing the 'renameCollection' command on the admin database"
If I run:
db.collection_one.renameCollection("collection_one","collection_two");
in the mongo shell, it works fine.
how to rename a collection by mongotemplate ?
This is the code where I'm trying it:
BasicDBObject basicObject = new BasicDBObject();
basicObject.append("renameCollection","collection_one");
basicObject.append("to","collection_two");
mongoTemplate.executeCommand(basicObject);
String dbName = default;//mongo db name
MongoNamespace mongoNamespace=new MongoNamespace(dbName,"collection_two");
mongoTemplate.getCollection("collection_one").renameCollection(mongoNamespace);
String boot version:2.1.4
If you are using spring's mongo template then try this:
mongoTemplate.getCollection(currName).rename(newName);

Jira custom script validator: check if input textbox exists

I am totally new to Jira. In fact I don't even know where to start. I went to the jira atlassian website but got nothing solid enough to help me. I would like to validate if the information entered into a textbox already exists. I clicked around jira and ended up on the screen below:
Now I would like to find out the following:
Which programming language should be used for validation ? Is it Java
If the name of the custom field(of type Textbox) is XYZ and I wanna if check if value entered into XYZ already exist, how do I go about doing that ? Can I just write conditional statements in Java ?
I wrote some stuff and nothing worked.
That's a screenshot from the Script Runner add-on.
There are some documentation and examples for custom validators here.
You can also find an example here that shows how to query the JIRA (or an external) database from a groovy script. Ie.:
import com.atlassian.jira.component.ComponentAccessor
import groovy.sql.Sql
import org.ofbiz.core.entity.ConnectionFactory
import org.ofbiz.core.entity.DelegatorInterface
import java.sql.Connection
def delegator = (DelegatorInterface) ComponentAccessor.getComponent(DelegatorInterface)
String helperName = delegator.getGroupHelperName("default");
def sqlStmt = """
SELECT project.pname, COUNT(*) AS kount
FROM project
INNER JOIN jiraissue ON project.ID = jiraissue.PROJECT
GROUP BY project.pname
ORDER BY kount DESC
"""
Connection conn = ConnectionFactory.getConnection(helperName);
Sql sql = new Sql(conn)
try {
StringBuffer sb = new StringBuffer()
sql.eachRow(sqlStmt) {
sb << "${it.pname}\t${it.kount}\n"
}
log.debug sb.toString()
}
finally {
sql.close()
}
For anything that gets a bit complex it's easier to implement your script in a groovy file and make it available to Script Runner via the file system. That also allows you use a vcs like git to easily push/pull your changes. More info about how to go about that, is here.

redis-rb multi only increment if key set

I want to store a count in redis. I want to increment the count only if the key exists. What am I doing wrong? exists is returning false and the incr is being executed.
key = "blah"
result = REDIS_DB.multi do
exists = REDIS_DB.exists(key)
REDIS_DB.incr(key) if exists
end
# result: [false, 1]
I am new to redis. I just read the redis transactions doc. From what I understand, the commands in multi should execute one after the other?
Rails 4.0.2, Redis 3.0.1, redis-rb (A Ruby client library for Redis)
As of redis 2.6 lua scripting is supported and it is transactional by definition so following code can be used as well.
redis_script = <<SCRIPT
local exists = redis.call('exists', KEYS[1])
if exists == 1 then
return redis.call('incr', KEYS[1])
end
SCRIPT
# returns incremented value if key exists otherwise nil
REDIS_DB.eval(redis_script, ['testkey'])
Read more about scripting and eval command
Coming to why incr in your actual code executed was because
Each REDIS_DB function call in multi block will return a Redis::Future object not an actual value, as redis-rb caches the commands in multi block. e.g.
REDIS_DB.multi do
return_value = REDIS_DB.exists('tesstt')
puts return_value.inspect
end
will print
<Redis::Future [:exists, "tesstt"]>
Now, If we got through the block in your example
exists = REDIS_DB.exists(key) # returns Redis::Future object
REDIS_DB.incr(key) if exists # evaluates to true as value of exists is an object
So where does result: [false, 1] comes from.
This is because REDIS_DB.multi after yielding your block (and caching the commands) finally converts it into redis query and sends it to redis which eventually runs it and returns the result. so your code is actually converted into following query.
MULTI
exists 'blah'
incr 'blah'
EXEC
and submitted together in a single call to redis which returns 0 for exists ( which redis-rb converts into boolean false) and 1 for incr.
Point to be noted is that this behavior is understandable as if you send each command individually to redis then redis itself will queue everything after MULTI and process it when call to exec is received
This might be what I was looking for:
result = REDIS_DB.watch(key) do
if REDIS_DB.exists(key)
REDIS_DB.incr(key)
else
REDIS_DB.unwatch
end
end

Resources