Skip to content

Commit

Permalink
Improve tagging consistency.
Browse files Browse the repository at this point in the history
Fixes #206, makes scope tagging available on both scope and blacklist pages. Tried to make some code cleanup while I was at it, as well as improve other inconsistencies with tags.
  • Loading branch information
0xdade committed Oct 31, 2019
1 parent 9cea247 commit 105ed2d
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 47 deletions.
6 changes: 4 additions & 2 deletions natlas-server/app/admin/routes.py
Expand Up @@ -147,7 +147,8 @@ def blacklist():
current_app.ScopeManager.update()
flash('%s blacklisted!' % newTarget.target, 'success')
return redirect(url_for('admin.blacklist'))
return render_template("admin/blacklist.html", scope=scope, blacklistSize=blacklistSize, delForm=delForm, editForm=editForm, newForm=newForm, importForm=importForm)
return render_template("admin/blacklist.html", scope=scope, blacklistSize=blacklistSize, delForm=delForm, editForm=editForm, \
newForm=newForm, importForm=importForm, addTagForm=addTagForm)



Expand Down Expand Up @@ -427,7 +428,8 @@ def tags():

addForm = AddTagForm()
if addForm.validate_on_submit():
newTag = Tag(name=addForm.tagname.data.lower())
prepared_tag = addForm.tagname.data.strip()
newTag = Tag(name=prepared_tag)
db.session.add(newTag)
db.session.commit()
flash('Successfully added tag %s' % newTag.name, 'success')
Expand Down
2 changes: 1 addition & 1 deletion natlas-server/app/models.py
Expand Up @@ -121,7 +121,7 @@ def addTags(scopeitem, tags):
if existingTag:
scopeitem.addTag(existingTag)
else:
newTag = Tag(name=tag)
newTag = Tag(name=tag.strip())
db.session.add(newTag)
scopeitem.addTag(newTag)

Expand Down
36 changes: 36 additions & 0 deletions natlas-server/app/static/js/natlas-tagging.js
@@ -0,0 +1,36 @@
$('#tagmodal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget) // Button that triggered the modal
var scopeid = button.data('scopeid') // Extract info from data-* attributes
var scopetarget = button.data('scopetarget') // Extract info from data-* attributes
var tagaction = button.data('action')
var tagstr = $('#scopeTags-'+scopeid).text().trim() // Trim whitespace in jinja formatting leaves whitespace
var tags = tagstr.split(', ')
var modal = $(this)
if (tagaction == 'add') {
modal.find('.modal-title').text('Add tag to ' + scopetarget)
modal.find('#tagScopeForm').attr('action', "/admin/scope/"+scopeid+"/tag")
modal.find('#tagScopeSubmit').text('Add Tag')
if (tagstr != ''){
tags.forEach(function(item) {
$("option[value=" + item.trim() + "]").attr('hidden', '') // hide this option because it's already added
$("option[value=" + item.trim() + "]").attr('disabled', '') // disable this option because it's hidden
});
}


} else {
modal.find('.modal-title').text('Remove tag from ' + scopetarget)
modal.find('#tagScopeForm').attr('action', "/admin/scope/"+scopeid+"/untag")
modal.find('#tagScopeSubmit').text('Remove Tag')
$("select[name='tagname']")[0].options.length = 0;

tags.forEach(function(item) {
$("#tagname").append(
$('<option>', {
value: item.trim(),
text: item.trim()
})
);
});
}
})
39 changes: 1 addition & 38 deletions natlas-server/app/templates/admin/_tagmodal.html
Expand Up @@ -20,41 +20,4 @@ <h5 class="modal-title">Add Tag</h5>
</div>
</div>
</div>
<script>
$('#tagmodal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget) // Button that triggered the modal
var scopeid = button.data('scopeid') // Extract info from data-* attributes
var scopetarget = button.data('scopetarget') // Extract info from data-* attributes
var tagaction = button.data('action')
var tagstr = $('#scopeTags-'+scopeid).text().replace(/\s+/g,'')
var tags = tagstr.split(',')
var modal = $(this)
if (tagaction == 'add') {
modal.find('.modal-title').text('Add tag to ' + scopetarget)
modal.find('#tagScopeForm').attr('action', "/admin/scope/"+scopeid+"/tag")
modal.find('#tagScopeSubmit').text('Add Tag')
if (tagstr != ''){
tags.forEach(function(item) {
$("option[value="+item+"]").attr('hidden', '')
$("option[value="+item+"]").attr('disabled', '')
});
}


} else {
modal.find('.modal-title').text('Remove tag from ' + scopetarget)
modal.find('#tagScopeForm').attr('action', "/admin/scope/"+scopeid+"/untag")
modal.find('#tagScopeSubmit').text('Remove Tag')
$("select[name='tagname']")[0].options.length = 0;

tags.forEach(function(item) {
$("#tagname").append(
$('<option>', {
value: item,
text: item
})
);
});
}
})
</script>
<script src="/static/js/natlas-tagging.js"></script>
2 changes: 1 addition & 1 deletion natlas-server/app/templates/admin/blacklist.html
Expand Up @@ -35,7 +35,7 @@ <h3 class="mt-3">Manage Blacklist ({{ blacklistSize }} hosts)</h3>
<tr>
<th scope="row">{{ item.id }}</th>
<td>{{ item.target }}</td>
<td>{% for tag in item.tags %}{{ tag.name }}{% if not loop.last %}, {% endif %}{% endfor %}</td>
<td id="scopeTags-{{item.id}}">{% for tag in item.tags %}{{ tag.name }}{% if not loop.last %}, {% endif %}{% endfor %}</td>
<td style="text-align:center;">
{% include 'admin/_scope_menu.html' %}
</td>
Expand Down
6 changes: 1 addition & 5 deletions natlas-server/app/templates/admin/scope.html
Expand Up @@ -35,11 +35,7 @@ <h3 class="mt-3">Manage Scope ({{ scopeSize }} hosts)</h3>
<tr>
<th scope="row">{{ item.id }}</th>
<td>{{ item.target }}</td>
<td id="scopeTags-{{item.id}}">
{% for tag in item.tags %}
{{ tag.name }}{% if not loop.last %}, {% endif %}
{% endfor %}
</td>
<td id="scopeTags-{{item.id}}">{% for tag in item.tags %}{{ tag.name }}{% if not loop.last %}, {% endif %}{% endfor %}</td>
<td style="text-align:center;">
{% include 'admin/_scope_menu.html' %}
</td>
Expand Down

0 comments on commit 105ed2d

Please sign in to comment.