FirewallRules.js 15.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
Ext.define('PVE.form.FWMacroSelector', {
    extend: 'PVE.form.ComboGrid',
    alias: 'widget.pveFWMacroSelector',

    initComponent: function() {
	var me = this;

	var store = Ext.create('Ext.data.Store', {
	    autoLoad: true,
	    fields: [ 'macro', 'descr' ],
	    idProperty: 'macro',
	    proxy: {
		type: 'pve',
		url: "/api2/json/cluster/firewall/macros"
	    },
	    sorters: {
		property: 'macro',
		order: 'DESC'
	    }
	});

	Ext.apply(me, {
	    store: store,
	    allowBlank: true,
	    autoSelect: false,
	    valueField: 'macro',
	    displayField: 'macro',
            listConfig: {
		columns: [
		    {
			header: gettext('Macro'),
			dataIndex: 'macro',
			hideable: false,
			width: 100
		    },
		    {
			header: gettext('Description'),
			flex: 1,
			dataIndex: 'descr'
		    }
		]
	    }
	});

	me.callParent();
    }
});

Ext.define('PVE.FirewallRulePanel', {
    extend: 'PVE.panel.InputPanel',

    allow_iface: false,

54
    list_refs_url: undefined,
55

56 57 58 59
    onGetValues: function(values) {
	var me = this;

	// hack: editable ComboGrid returns nothing when empty, so we need to set ''
60
	// Also, disabled text fields return nothing, so we need to set ''
61

62 63 64 65 66
	Ext.Array.each(['source', 'dest', 'proto', 'sport', 'dport'], function(key) {
	    if (values[key] === undefined) {
		values[key] = '';
	    }
	});
67

68 69
	delete values.modified_marker;
 
70 71 72
	return values;
    },

73 74 75
    initComponent : function() {
	var me = this;

76 77
	if (!me.list_refs_url) {
	    throw "no list_refs_url specified";
78 79
	}

80
	me.column1 = [
81 82 83 84 85 86 87 88
	    {
		// hack: we use this field to mark the form 'dirty' when the
		// record has errors- so that the user can safe the unmodified 
		// form again.
		xtype: 'hiddenfield',
		name: 'modified_marker',
		value: '',
	    },
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
	    {
		xtype: 'pveKVComboBox',
		name: 'type',
		value: 'in',
		data: [['in', 'in'], ['out', 'out']],
		fieldLabel: gettext('Direction'),
		allowBlank: false
	    },
	    {
		xtype: 'pveKVComboBox',
		name: 'action',
		value: 'ACCEPT',
		data: [['ACCEPT', 'ACCEPT'], ['DROP', 'DROP'], ['REJECT', 'REJECT']],
		fieldLabel: gettext('Action'),
		allowBlank: false
104 105 106 107 108 109 110
	    }
        ];

	if (me.allow_iface) {
	    me.column1.push({
		xtype: 'pvetextfield',
		name: 'iface',
111
		deleteEmpty: !me.create,
112 113 114 115 116 117 118 119 120 121 122 123 124
		value: '',
		fieldLabel: gettext('Interface')
	    });
	} else {
	    me.column1.push({
		xtype: 'displayfield',
		fieldLabel: '',
		height: 22, // hack: set same height as text fields
		value: ''
	    });
	}

	me.column1.push([
125 126 127 128 129 130 131
	    {
		xtype: 'displayfield',
		fieldLabel: '',
		height: 7,
		value: ''
	    },
	    {
132
		xtype: 'pveIPRefSelector',
133 134 135
		name: 'source',
		autoSelect: false,
		editable: true,
136
		base_url: me.list_refs_url,
137
		value: '',
138 139
		fieldLabel: gettext('Source')

140 141
	    },
	    {
142
		xtype: 'pveIPRefSelector',
143 144 145
		name: 'dest',
		autoSelect: false,
		editable: true,
146
		base_url: me.list_refs_url,
147
		value: '',
148
		fieldLabel: gettext('Destination')
149
	    }
150
	]);
151

152
	
153 154 155 156 157 158 159 160 161 162 163 164 165 166
	me.column2 = [
	    {
		xtype: 'pvecheckbox',
		name: 'enable',
		checked: false,
		height: 22, // hack: set same height as text fields
		uncheckedValue: 0,
		fieldLabel: gettext('Enable')
	    },
	    {
		xtype: 'pveFWMacroSelector',
		name: 'macro',
		value: '',
		fieldLabel: gettext('Macro'),
167 168 169 170 171 172 173 174 175
		allowBlank: true,
		listeners: {
		    change: function(f, value) {
                        if (value === '') {
			    me.down('field[name=proto]').setDisabled(false);
			    me.down('field[name=sport]').setDisabled(false);
			    me.down('field[name=dport]').setDisabled(false);
                        } else {
			    me.down('field[name=proto]').setDisabled(true);
176
			    me.down('field[name=proto]').setValue('');
177
			    me.down('field[name=sport]').setDisabled(true);
178
			    me.down('field[name=sport]').setValue('');
179
			    me.down('field[name=dport]').setDisabled(true);
180 181
 			    me.down('field[name=dport]').setValue('');
                       }
182 183
                    }
                }
184 185
	    },
	    {
186
		xtype: 'pveIPProtocolSelector',
187
		name: 'proto',
188 189
		autoSelect: false,
		editable: true,
190
		value: '',
191
		fieldLabel: gettext('Protocol')
192
	    },
193 194 195 196 197 198 199
	    {
		xtype: 'displayfield',
		fieldLabel: '',
		height: 7,
		value: ''
	    },
	    {
200
		xtype: 'textfield',
201 202 203 204 205
		name: 'sport',
		value: '',
		fieldLabel: gettext('Source port')
	    },
	    {
206
		xtype: 'textfield',
207 208 209 210 211
		name: 'dport',
		height: 22, // hack: set same height as text fields
		value: '',
		fieldLabel: gettext('Dest. port')
	    }
212
	];
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
	
	me.columnB = [
	    {
		xtype: 'textfield',
		name: 'comment',
		value: '',
		fieldLabel: gettext('Comment')
	    }
	];

	me.callParent();
    }
});

Ext.define('PVE.FirewallRuleEdit', {
    extend: 'PVE.window.Edit',

    base_url: undefined,
231
    list_refs_url: undefined,
232 233 234 235 236 237 238

    allow_iface: false,

    initComponent : function() {
	/*jslint confusion: true */
	var me = this;

239 240 241
	if (!me.base_url) {
	    throw "no base_url specified";
	}
242 243
	if (!me.list_refs_url) {
	    throw "no list_refs_url specified";
244 245
	}

246 247 248 249 250 251 252 253 254 255 256 257
	me.create = (me.rule_pos === undefined);

	if (me.create) {
            me.url = '/api2/extjs' + me.base_url;
            me.method = 'POST';
        } else {
            me.url = '/api2/extjs' + me.base_url + '/' + me.rule_pos.toString();
            me.method = 'PUT';
        }

	var ipanel = Ext.create('PVE.FirewallRulePanel', {
	    create: me.create,
258
	    list_refs_url: me.list_refs_url,
259 260 261 262 263 264 265 266 267 268 269 270 271 272
	    allow_iface: me.allow_iface,
	    rule_pos: me.rule_pos
	});

	Ext.apply(me, {
            subject: gettext('Rule'),
	    isAdd: true,
	    items: [ ipanel ]
	});

	me.callParent();

	if (!me.create) {
	    me.load({
273
		success: function(response, options) {
274 275
		    var values = response.result.data;
		    ipanel.setValues(values);
276
		    if (values.errors) {
277 278
			var field = me.query('[isFormField][name=modified_marker]')[0];
			field.setValue(1);
279 280 281 282 283
			Ext.Function.defer(function() {
			    var form = ipanel.up('form').getForm();
			    form.markInvalid(values.errors)
			}, 100);
		    }
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
		}
	    });
	}
    }
});

Ext.define('PVE.FirewallGroupRuleEdit', {
    extend: 'PVE.window.Edit',

    base_url: undefined,

    allow_iface: false,

    initComponent : function() {
	/*jslint confusion: true */
	var me = this;

	me.create = (me.rule_pos === undefined);

	if (me.create) {
            me.url = '/api2/extjs' + me.base_url;
            me.method = 'POST';
        } else {
            me.url = '/api2/extjs' + me.base_url + '/' + me.rule_pos.toString();
            me.method = 'PUT';
        }

	var column1 = [
	    {
		xtype: 'hiddenfield',
		name: 'type',
		value: 'group'
	    },
	    {
		xtype: 'pveSecurityGroupsSelector',
		name: 'action',
		value: '',
		fieldLabel: gettext('Security Group'),
		allowBlank: false
	    }
	];

	if (me.allow_iface) {
	    column1.push({
		xtype: 'pvetextfield',
		name: 'iface',
		deleteEmpty: !me.create,
		value: '',
		fieldLabel: gettext('Interface')
	    });
	}

	var ipanel = Ext.create('PVE.panel.InputPanel', {
	    create: me.create,
	    column1: column1,
	    column2: [
		{
		    xtype: 'pvecheckbox',
		    name: 'enable',
		    checked: false,
		    height: 22, // hack: set same height as text fields
		    uncheckedValue: 0,
		    fieldLabel: gettext('Enable')
		}
	    ],
	    columnB: [
		{
		    xtype: 'textfield',
		    name: 'comment',
		    value: '',
		    fieldLabel: gettext('Comment')
		}
	    ]
	});

	Ext.apply(me, {
            subject: gettext('Rule'),
	    isAdd: true,
	    items: [ ipanel ]
	});

	me.callParent();

	if (!me.create) {
	    me.load({
		success:  function(response, options) {
		    var values = response.result.data;
		    ipanel.setValues(values);
		}
	    });
	}
    }
});

Ext.define('PVE.FirewallRules', {
    extend: 'Ext.grid.Panel',
    alias: 'widget.pveFirewallRules',

    base_url: undefined,
383
    list_refs_url: undefined,
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440

    addBtn: undefined,
    removeBtn: undefined,
    editBtn: undefined,
    groupBtn: undefined,

    tbar_prefix: undefined,

    allow_groups: true,
    allow_iface: false,

    setBaseUrl: function(url) {
        var me = this;

	me.base_url = url;

	if (url === undefined) {
	    me.addBtn.setDisabled(true);
	    if (me.groupBtn) {
		me.groupBtn.setDisabled(true);
	    }
	    me.store.removeAll();
	} else {
	    me.addBtn.setDisabled(false);
	    if (me.groupBtn) {
		me.groupBtn.setDisabled(false);
	    }
	    me.store.setProxy({
		type: 'pve',
		url: '/api2/json' + url
	    });

	    me.store.load();
	}
    },

    moveRule: function(from, to) {
        var me = this;

	if (!me.base_url) { 
	    return;
	}

	PVE.Utils.API2Request({
	    url: me.base_url + "/" + from,
	    method: 'PUT',
	    params: { moveto: to },
	    waitMsgTarget: me,
	    failure: function(response, options) {
		Ext.Msg.alert(gettext('Error'), response.htmlStatus);
	    },
	    callback: function() {
		me.store.load();
	    }
	});
    },

441
    updateRule: function(rule) {
442 443 444 445 446 447 448 449 450 451
        var me = this;

	if (!me.base_url) { 
	    return;
	}

	rule.enable = rule.enable ? 1 : 0;

	var pos = rule.pos;
	delete rule.pos;
452
	delete rule.errors;
453 454 455 456 457 458 459

	PVE.Utils.API2Request({
	    url: me.base_url + '/' + pos.toString(),
	    method: 'PUT',
	    params: rule,
	    waitMsgTarget: me,
	    failure: function(response, options) {
460
		Ext.Msg.alert(gettext('Error'), response.htmlStatus);
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
	    },
	    callback: function() {
		me.store.load();
	    }
	});
    },

    deleteRule: function(rule) {
        var me = this;

	if (!me.base_url) {
	    return;
	}

	PVE.Utils.API2Request({
	    url: me.base_url + '/' + rule.pos.toString() +
		'?digest=' + encodeURIComponent(rule.digest),
	    method: 'DELETE',
	    waitMsgTarget: me,
	    failure: function(response, options) {
		Ext.Msg.alert(gettext('Error'), response.htmlStatus);
	    },
	    callback: function() {
		me.store.load();
	    }
	});
    },

    initComponent: function() {
	/*jslint confusion: true */
        var me = this;

493 494
	if (!me.list_refs_url) {
	    throw "no list_refs_url specified";
495 496
	}

497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
	var store = new Ext.data.Store({
	    model: 'pve-fw-rule'
	});

	var reload = function() {
	    store.load();
	};

	var sm = Ext.create('Ext.selection.RowModel', {});

	var run_editor = function() {
	    var rec = sm.getSelection()[0];
	    if (!rec) {
		return;
	    }
	    var type = rec.data.type;

	    var editor;
	    if (type === 'in' || type === 'out') {
		editor = 'PVE.FirewallRuleEdit';
	    } else if (type === 'group') {
		editor = 'PVE.FirewallGroupRuleEdit';
	    } else {
		return;
	    }

	    var win = Ext.create(editor, {
		digest: rec.data.digest,
		allow_iface: me.allow_iface,
		base_url: me.base_url,
527
		list_refs_url: me.list_refs_url,
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
		rule_pos: rec.data.pos
	    });

	    win.show();
	    win.on('destroy', reload);
	};

	me.editBtn = new PVE.button.Button({
	    text: gettext('Edit'),
	    disabled: true,
	    selModel: sm,
	    handler: run_editor
	});

	me.addBtn =  Ext.create('Ext.Button', {
	    text: gettext('Add'),
	    disabled: true,
	    handler: function() {
		var win = Ext.create('PVE.FirewallRuleEdit', {
		    allow_iface: me.allow_iface,
548
		    base_url: me.base_url,
549
		    list_refs_url: me.list_refs_url
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
		});
		win.on('destroy', reload);
		win.show();
	    }
	});

	if (me.allow_groups) {
	    me.groupBtn =  Ext.create('Ext.Button', {
		text: gettext('Insert') + ': ' + gettext('Security Group'),
		disabled: true,
		handler: function() {
		    var win = Ext.create('PVE.FirewallGroupRuleEdit', {
			allow_iface: me.allow_iface,
			base_url: me.base_url
		    });
		    win.on('destroy', reload);
		    win.show();
		}
	    });
	}

	me.removeBtn = new PVE.button.Button({
	    text: gettext('Remove'),
	    selModel: sm,
	    disabled: true,
	    handler: function() {
		var rec = sm.getSelection()[0];
		if (!rec) {
		    return;
		}
		me.deleteRule(rec.data);
	    }
	});

	var tbar = me.tbar_prefix ? [ me.tbar_prefix ] : [];
	tbar.push(me.addBtn);
	if (me.groupBtn) {
	    tbar.push(me.groupBtn);
	}
	tbar.push([ me.removeBtn, me.editBtn ]);

591 592 593 594 595 596 597 598 599 600 601
	var render_errors = function(name, value, metaData, record) {
	    var errors = record.data.errors;
	    if (errors && errors[name]) {
		metaData.tdCls = 'x-form-invalid-field';
		var html = '<p>' +  Ext.htmlEncode(errors[name]) + '</p>';
		metaData.tdAttr = 'data-qwidth=600 data-qtitle="ERROR" data-qtip="' + 
		    html.replace(/\"/g,'&quot;') + '"';
	    }
	    return value;
	};

602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
	var columns = [
	    {
		// similar to xtype: 'rownumberer',
		dataIndex: 'pos',
		resizable: false,
		width: 23,
		sortable: false,
		align: 'right',
		hideable: false,
		menuDisabled: true,
		renderer: function(value, metaData, record, rowIdx, colIdx, store) {
		    metaData.tdCls = Ext.baseCSSPrefix + 'grid-cell-special';
		    if (value >= 0) {
			return value;
		    }
		    return '';
		}
	    },
	    {
		xtype: 'checkcolumn',
		header: gettext('Enable'),
		dataIndex: 'enable',
		listeners: {
		    checkchange: function(column, record, checked) {
			record.commit();
			var data = {};
			record.fields.each(function(field) {
			    data[field.name] = record.get(field.name);
			});
			if (!me.allow_iface || !data.iface) {
			    delete data.iface;
			}
634
			me.updateRule(data);
635 636 637 638 639 640 641
		    }
		},
		width: 50
	    },
	    {
		header: gettext('Type'),
		dataIndex: 'type',
642 643 644
		renderer: function(value, metaData, record) {
		    return render_errors('type', value, metaData, record);
		},
645 646 647 648 649
		width: 50
	    },
	    {
		header: gettext('Action'),
		dataIndex: 'action',
650 651 652
		renderer: function(value, metaData, record) {
		    return render_errors('action', value, metaData, record);
		},
653 654 655 656 657
		width: 80
	    },
	    {
		header: gettext('Macro'),
		dataIndex: 'macro',
658 659 660
		renderer: function(value, metaData, record) {
		    return render_errors('macro', value, metaData, record);
		},
661 662 663 664 665 666 667 668
		width: 80
	    }
	];

	if (me.allow_iface) {
	    columns.push({
		header: gettext('Interface'),
		dataIndex: 'iface',
669 670 671
		renderer: function(value, metaData, record) {
		    return render_errors('iface', value, metaData, record);
		},
672 673 674 675 676 677 678 679
		width: 80
	    });
	}

	columns.push([
	    {
		header: gettext('Source'),
		dataIndex: 'source',
680 681 682
		renderer: function(value, metaData, record) {
		    return render_errors('source', value, metaData, record);
		},
683 684 685 686 687
		width: 100
	    },
	    {
		header: gettext('Destination'),
		dataIndex: 'dest',
688 689 690
		renderer: function(value, metaData, record) {
		    return render_errors('dest', value, metaData, record);
		},
691 692 693 694 695
		width: 100
	    },
	    {
		header: gettext('Protocol'),
		dataIndex: 'proto',
696 697 698
		renderer: function(value, metaData, record) {
		    return render_errors('proto', value, metaData, record);
		},
699 700 701 702 703
		width: 100
	    },
	    {
		header: gettext('Dest. port'),
		dataIndex: 'dport',
704 705 706
		renderer: function(value, metaData, record) {
		    return render_errors('dport', value, metaData, record);
		},
707 708 709 710 711
		width: 100
	    },
	    {
		header: gettext('Source port'),
		dataIndex: 'sport',
712 713 714
		renderer: function(value, metaData, record) {
		    return render_errors('sport', value, metaData, record);
		},
715 716 717 718 719 720
		width: 100
	    },
	    {
		header: gettext('Comment'),
		dataIndex: 'comment',
		flex: 1,
721 722
		renderer: function(value, metaData, record) {
		    return render_errors('comment', Ext.util.Format.htmlEncode(value), metaData, record);
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
		}
	    }
	]);

	Ext.apply(me, {
	    store: store,
	    selModel: sm,
	    tbar: tbar,
            viewConfig: {
		plugins: [
		    {
			ptype: 'gridviewdragdrop',
			dragGroup: 'FWRuleDDGroup',
			dropGroup: 'FWRuleDDGroup'
		    }
		],
		listeners: {
                    beforedrop: function(node, data, dropRec, dropPosition) {
			if (!dropRec) {
			    return false; // empty view
			}
			var moveto = dropRec.get('pos');
			if (dropPosition === 'after') {
			    moveto++;
			}
			var pos = data.records[0].get('pos');
			me.moveRule(pos, moveto);
			return 0;
                    },
		    itemdblclick: run_editor
		}
	    },
755
	    sortableColumns: false,
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
	    columns: columns
	});

	me.callParent();

	if (me.base_url) {
	    me.setBaseUrl(me.base_url); // load
	}
    }
}, function() {

    Ext.define('pve-fw-rule', {
	extend: 'Ext.data.Model',
	fields: [ { name: 'enable', type: 'boolean' },
		  'type', 'action', 'macro', 'source', 'dest', 'proto', 'iface',
771
		  'dport', 'sport', 'comment', 'pos', 'digest', 'errors' ],
772 773 774 775
	idProperty: 'pos'
    });

});