Skip to content

Commit

Permalink
add azuread groupfilter
Browse files Browse the repository at this point in the history
  • Loading branch information
vcmirko committed Nov 22, 2023
1 parent 4b4273e commit 212dc40
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 5 deletions.
15 changes: 14 additions & 1 deletion client/src/lib/Helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ var Helpers = {
var i = size == 0 ? 0 : Math.floor(Math.log(size) / Math.log(1024));
return (size / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i];
},
deepClone(o){
if(o===undefined){
return o
}
try{
return (JSON.parse(JSON.stringify(o)))
}catch(e){
console.error("Failed deepcloning - ",e)
return undefined
}

},
evalSandbox(expression){
// local autonumbering
function fnGetNumberedName(names,pattern,value,fillgap=false){
Expand Down Expand Up @@ -177,7 +189,8 @@ var Helpers = {
return o
})
}
}
}
if(expression)
return eval(expression)
}

Expand Down
4 changes: 3 additions & 1 deletion client/src/views/AzureAd.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<div class="mt-2">
<BulmaInput :disabled="!azuread.enable" icon="user-tag" v-model="azuread.client_id" label="Client Id" placeholder="" :required="true" :hasError="$v.azuread.client_id.$invalid" :errors="[]" />
<BulmaInput :disabled="!azuread.enable" icon="user-secret" v-model="azuread.secret_id" type="password" label="Secret Id" placeholder="" :required="true" :hasError="$v.azuread.secret_id.$invalid" :errors="[]" />
<BulmaInput :disabled="!azuread.enable" icon="filter" v-model="azuread.groupfilter" label="Groupname Regex" placeholder="A regular expression to match groups" :required="false" :errors="[]" />
<div class="notification is-info-light content">
<strong>Required API Permissions</strong><br>
<ul>
Expand Down Expand Up @@ -67,7 +68,8 @@
azuread:{
client_id:"",
secret_id:"",
enable:true
enable:true,
groupfilter:""
},
settings:{
url:""
Expand Down
15 changes: 15 additions & 0 deletions client/src/views/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
password: ""
},
azureAdEnabled:false,
azureGroupfilter:"",
azureGraphUrl:"https://graph.microsoft.com"
}
},
Expand All @@ -56,6 +57,7 @@
.then((result)=>{
if(result.data?.status=='success'){
this.azureAdEnabled=!!result.data.data.output.azureAdEnabled
this.azureGroupfilter=result.data.data.output.azureGroupfilter
this.azureGraphUrl=result.data.data.output.azureGraphUrl
if(azuretoken){
this.getGroupsAndLogin(azuretoken)
Expand All @@ -69,6 +71,7 @@
})
},
getGroupsAndLogin(azuretoken, url = `${this.azureGraphUrl}/v1.0/me/transitiveMemberOf`, allGroups = []) {
var ref=this
const config = {
headers: {
Authorization: `Bearer ${azuretoken}`
Expand All @@ -84,6 +87,18 @@
// If there's a nextLink, make a recursive call to get the next page of data
this.getGroupsAndLogin(azuretoken, res.data['@odata.nextLink'], allGroups);
} else {
var validRegex=true
var regex
try{
regex = new RegExp(ref.azureGroupfilter, 'g');
}catch(e){
console.error("MS Entra ID Group filter is not a valid regular expression")
validRegex=false
}
if(validRegex && ref.azureGroupfilter){
allGroups = allGroups.filter(x => x.match(regex))
console.log("Groups have been filtered")
}
// No more nextLink, you have all the groups
axios.post('/api/v1/auth/azureadoauth2/login', { azuretoken, groups:allGroups })
.then((result) => {
Expand Down
2 changes: 2 additions & 0 deletions server/schema/forms_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@
"recipients"
]
},
"hasApproval":{ "type": "boolean"},
"approval": {
"$id": "/approval",
"type": "object",
Expand Down Expand Up @@ -487,6 +488,7 @@
"type": "string",
"enum": ["ansible", "awx", "git","multistep"]
},
"hasApproval":{ "type": "boolean"},
"approval":{
"$ref": "/approval"
},
Expand Down
1 change: 1 addition & 0 deletions server/src/controllers/login.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ exports.settings = async function(req,res){
var settings={}
// console.log(inspect(azure))
settings.azureAdEnabled=azure.enable
settings.azureGroupfilter=azure.groupfilter
settings.azureGraphUrl=authConfig.azureGraphUrl
res.json(new RestResult("success","",settings,""))
})
Expand Down
5 changes: 3 additions & 2 deletions server/src/db/create_azuread_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ DROP TABLE IF EXISTS `azuread`;
CREATE TABLE `azuread` (
`client_id` text DEFAULT NULL,
`secret_id` text DEFAULT NULL,
`enable` tinyint(4) DEFAULT NULL
`enable` tinyint(4) DEFAULT NULL,
`groupfilter` varchar(250) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO AnsibleForms.azuread(client_id,secret_id,enable) VALUES('','',0);
INSERT INTO AnsibleForms.azuread(client_id,secret_id,enable,groupfilter) VALUES('','',0,'');
3 changes: 2 additions & 1 deletion server/src/models/azureAd.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ var AzureAd=function(azuread){
this.client_id = azuread.client_id;
this.secret_id = encrypt(azuread.secret_id);
this.enable = (azuread.enable)?1:0;
this.groupfilter = azuread.groupfilter;
};
AzureAd.update = function (record) {
logger.info(`Updating azuread`)
return mysql.do("UPDATE AnsibleForms.`azuread` set ?", record)
};
AzureAd.isEnabled = function(){
return mysql.do("SELECT enable FROM AnsibleForms.`azuread` limit 1;")
return mysql.do("SELECT enable,groupfilter FROM AnsibleForms.`azuread` limit 1;")
.then((res)=>{
if(res.length>0){
return res[0]
Expand Down
1 change: 1 addition & 0 deletions server/src/models/schema.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ function patchAll(){
buffer = fs.readFileSync(`${__dirname}/../db/create_azuread_table.sql`)
sql = buffer.toString()
tablePromises.push(addTable("azuread",sql)) // add azuread table
tablePromises.push(addColumn("azuread","groupfilter","varchar(250)",true,"NULL")) // add column to limit azuread groups
//tablePromises.push(addRecord("settings",["mail_server","mail_port","mail_secure","mail_username","mail_password","mail_from","url"],["''",25,0,"''","''","''","''"]))
// buffer=fs.readFileSync(`${__dirname}/../db/create_settings_table.sql`)
// sql=buffer.toString();
Expand Down

0 comments on commit 212dc40

Please sign in to comment.