Skip to content

Commit 2f8eddb

Browse files
feat(HAProxyFrontendAddress): allow interface references as extaddr value #947
1 parent af53b6f commit 2f8eddb

4 files changed

Lines changed: 378 additions & 5 deletions

File tree

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
namespace RESTAPI\Fields;
4+
5+
require_once 'RESTAPI/autoloader.inc';
6+
7+
use RESTAPI\Core\Model;
8+
use RESTAPI\Core\ModelSet;
9+
use RESTAPI\Models\NetworkInterface;
10+
11+
/**
12+
* Defines an interface address reference with an `_ipv4` or `_ipv6` suffix. Interface names follow
13+
* `represent_interfaces_as`, while configuration stores interface IDs. The inherited `allow_custom`
14+
* argument accepts additional literal values that are never converted or treated as references.
15+
*/
16+
class InterfaceAddressField extends InterfaceField {
17+
/**
18+
* Enumerates interface address choices and any allowed literal values.
19+
* @return array The represented choices mapped to their verbose names.
20+
*/
21+
public function get_interface_choices(): array {
22+
$choices = [];
23+
24+
foreach (parent::get_interface_choices() as $interface => $description) {
25+
if (in_array($interface, $this->allow_custom, true)) {
26+
continue;
27+
}
28+
29+
$choices[$interface . '_ipv4'] = "$description (IPv4)";
30+
$choices[$interface . '_ipv6'] = "$description (IPv6)";
31+
}
32+
33+
foreach ($this->allow_custom as $keyword) {
34+
$choices[$keyword] = $keyword;
35+
}
36+
37+
if ($this->allow_empty) {
38+
$choices[''] = 'None';
39+
}
40+
41+
return $choices;
42+
}
43+
44+
/**
45+
* Converts the represented interface to its internal ID, retaining the address family suffix.
46+
* @param mixed $representation_value The represented interface address or literal value.
47+
* @return string|null The value suitable for storing in configuration.
48+
*/
49+
public function _to_internal(mixed $representation_value): string|null {
50+
$interface = $this->get_interface_name($representation_value);
51+
if ($interface === null) {
52+
return $representation_value;
53+
}
54+
55+
return parent::_to_internal($interface) . substr($representation_value, -5);
56+
}
57+
58+
/**
59+
* Represents an internal interface ID according to the API settings, retaining its suffix.
60+
* @param mixed $internal_value The configured interface address or literal value.
61+
* @return string The represented value.
62+
*/
63+
public function _from_internal(mixed $internal_value): string {
64+
$interface = $this->get_interface_name($internal_value);
65+
if ($interface === null) {
66+
return $internal_value;
67+
}
68+
69+
return parent::_from_internal($interface) . substr($internal_value, -5);
70+
}
71+
72+
/**
73+
* Obtains the interface referenced by a single address for HATEOAS discovery.
74+
* @return Model|null The referenced interface, or null for literal or unset values.
75+
*/
76+
public function get_related_model(): Model|null {
77+
return $this->many ? null : $this->get_interface_model($this->value);
78+
}
79+
80+
/**
81+
* Obtains the interfaces referenced by a many-valued field for HATEOAS discovery.
82+
* @return ModelSet The referenced interfaces, excluding literal values.
83+
*/
84+
public function get_related_models(): ModelSet {
85+
$models = new ModelSet(model_objects: []);
86+
foreach ($this->many ? $this->value ?? [] : [] as $value) {
87+
$interface = $this->get_interface_model($value);
88+
if ($interface !== null) {
89+
$models->model_objects[] = $interface;
90+
}
91+
}
92+
93+
return $models;
94+
}
95+
96+
/**
97+
* Removes only a trailing address family suffix, leaving allowed literal values untouched.
98+
* @param mixed $value The internal or represented value.
99+
* @return string|null The interface name without its suffix, or null for non-reference values.
100+
*/
101+
private function get_interface_name(mixed $value): string|null {
102+
if (!is_string($value) or in_array($value, $this->allow_custom, true)) {
103+
return null;
104+
}
105+
106+
if (str_ends_with($value, '_ipv4') or str_ends_with($value, '_ipv6')) {
107+
return substr($value, 0, -5);
108+
}
109+
110+
return null;
111+
}
112+
113+
/**
114+
* Looks up an assigned interface using a represented address reference.
115+
* @param mixed $value The represented address reference.
116+
* @return Model|null The referenced interface, or null when no interface matches.
117+
*/
118+
private function get_interface_model(mixed $value): Model|null {
119+
$interface = $this->get_interface_name($value);
120+
if ($interface === null) {
121+
return null;
122+
}
123+
124+
$query = NetworkInterface::query(
125+
query_params: array_merge($this->interface_query, [$this->represented_as => $interface]),
126+
);
127+
return $query->exists() ? $query->first() : null;
128+
}
129+
}

‎pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Models/HAProxyFrontendAddress.inc‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,33 @@
22

33
namespace RESTAPI\Models;
44

5+
require_once 'RESTAPI/autoloader.inc';
6+
57
use RESTAPI\Core\Model;
68
use RESTAPI\Dispatchers\HAProxyApplyDispatcher;
79
use RESTAPI\Fields\BooleanField;
10+
use RESTAPI\Fields\InterfaceAddressField;
811
use RESTAPI\Fields\PortField;
912
use RESTAPI\Fields\StringField;
1013

1114
/**
1215
* Defines a Model for HAProxy Frontend Addresses.
1316
*/
1417
class HAProxyFrontendAddress extends Model {
15-
public StringField $extaddr;
18+
public InterfaceAddressField $extaddr;
1619
public StringField $extaddr_custom;
1720
public PortField $extaddr_port;
1821
public BooleanField $extaddr_ssl;
1922
public StringField $exaddr_advanced;
2023

21-
public function __construct(mixed $id = null, mixed $parent_id = null, mixed $data = [], ...$options) {
24+
/**
25+
* Defines the HAProxy frontend address fields and configuration location.
26+
* @param mixed $id The address ID.
27+
* @param mixed $parent_id The frontend ID.
28+
* @param mixed $data The address data.
29+
* @param mixed ...$options Additional Model options.
30+
*/
31+
public function __construct(mixed $id = null, mixed $parent_id = null, mixed $data = [], mixed ...$options) {
2232
# Set model attributes
2333
$this->parent_model_class = 'HAProxyFrontend';
2434
$this->config_path = 'a_extaddr/item';
@@ -28,11 +38,13 @@ class HAProxyFrontendAddress extends Model {
2838
$this->verbose_name = 'HAProxy Frontend Address';
2939

3040
# Set model fields
31-
$this->extaddr = new StringField(
41+
$this->extaddr = new InterfaceAddressField(
3242
required: true,
33-
choices: ['custom', 'any_ipv4', 'any_ipv6', 'localhost_ipv4', 'localhost_ipv6'],
43+
allow_custom: ['custom', 'any_ipv4', 'any_ipv6', 'localhost_ipv4', 'localhost_ipv6'],
3444
verbose_name: 'External Address',
35-
help_text: 'The external address to use.',
45+
help_text: 'The external address to use. Interface references must match the format set as the REST APIs ' .
46+
'interface representation format in the REST API settings, and must end with `_ipv4` or `_ipv6` to ' .
47+
'select the address family.',
3648
);
3749
$this->extaddr_custom = new StringField(
3850
required: true,
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
3+
namespace RESTAPI\Tests;
4+
5+
require_once 'RESTAPI/autoloader.inc';
6+
7+
use RESTAPI\Core\TestCase;
8+
use RESTAPI\Fields\InterfaceAddressField;
9+
use RESTAPI\Models\NetworkInterface;
10+
use RESTAPI\Models\RESTAPISettings;
11+
12+
/**
13+
* Tests represented interface address references and literal values.
14+
*/
15+
class APIFieldsInterfaceAddressFieldTestCase extends TestCase {
16+
/**
17+
* Checks choices, validation, round-trip conversion, and relations in every representation mode.
18+
*/
19+
public function test_interface_representations(): void {
20+
$settings = new RESTAPISettings();
21+
foreach (['id', 'if', 'descr'] as $mode) {
22+
$settings->represent_interfaces_as->value = $mode;
23+
$settings->update();
24+
25+
foreach (['wan', 'lan', 'opt1'] as $id) {
26+
$interface = NetworkInterface::query(query_params: ['id' => $id])->first();
27+
$represented = $mode === 'id' ? $id : $interface->$mode->value;
28+
foreach (['_ipv4', '_ipv6'] as $suffix) {
29+
$field = $this->make_field();
30+
$field->value = $represented . $suffix;
31+
$this->assert_is_true(in_array($field->value, $field->choices, true));
32+
$this->assert_is_true($field->validate());
33+
$this->assert_equals($field->to_internal(), $id . $suffix);
34+
$field->from_internal($id . $suffix);
35+
$this->assert_equals($field->value, $represented . $suffix);
36+
$this->assert_equals($field->get_related_model()->id, $id);
37+
$this->assert_is_empty($field->get_related_models()->model_objects);
38+
}
39+
}
40+
}
41+
}
42+
43+
/**
44+
* Checks that literal values are preserved and never identify related interfaces.
45+
*/
46+
public function test_static_choices(): void {
47+
$settings = new RESTAPISettings();
48+
foreach (['id', 'if', 'descr'] as $mode) {
49+
$settings->represent_interfaces_as->value = $mode;
50+
$settings->update();
51+
$keywords = ['custom', 'any_ipv4', 'any_ipv6', 'localhost_ipv4', 'localhost_ipv6', 'WAN_ipv4'];
52+
$field = $this->make_field(allow_custom: $keywords);
53+
foreach ($keywords as $keyword) {
54+
$field->value = $keyword;
55+
$this->assert_is_true($field->validate());
56+
$this->assert_equals($field->to_internal(), $keyword);
57+
$field->from_internal($keyword);
58+
$this->assert_equals($field->value, $keyword);
59+
$this->assert_equals($field->get_related_model(), null);
60+
$this->assert_is_false(in_array($keyword . '_ipv4', $field->choices, true));
61+
}
62+
}
63+
}
64+
65+
/**
66+
* Checks that bare, unknown, and malformed interface references are rejected.
67+
*/
68+
public function test_invalid_references(): void {
69+
$settings = new RESTAPISettings();
70+
$settings->represent_interfaces_as->value = 'descr';
71+
$settings->update();
72+
$field = $this->make_field();
73+
74+
foreach (['WAN', 'wan_ipv4', 'missing_ipv6', 'WAN_ipv5', 'WAN:ip', 'WAN_ipv4_extra', 'any_ipv4'] as $value) {
75+
$field->value = $value;
76+
$this->assert_throws_response(
77+
response_id: 'FIELD_INVALID_CHOICE',
78+
code: 400,
79+
callable: fn() => $field->validate(),
80+
);
81+
}
82+
}
83+
84+
/**
85+
* Checks that interface queries constrain both address families and related models.
86+
*/
87+
public function test_interface_query(): void {
88+
$settings = new RESTAPISettings();
89+
$settings->represent_interfaces_as->value = 'id';
90+
$settings->update();
91+
$field = $this->make_field(interface_query: ['id' => 'lan']);
92+
$this->assert_equals(array_keys($field->get_interface_choices()), ['lan_ipv4', 'lan_ipv6']);
93+
$field->value = 'wan_ipv4';
94+
$this->assert_equals($field->get_related_model(), null);
95+
$this->assert_throws_response(
96+
response_id: 'FIELD_INVALID_CHOICE',
97+
code: 400,
98+
callable: fn() => $field->validate(),
99+
);
100+
}
101+
102+
/**
103+
* Checks many-valued conversion and HATEOAS discovery without treating static values as interfaces.
104+
*/
105+
public function test_many_values(): void {
106+
$settings = new RESTAPISettings();
107+
$settings->represent_interfaces_as->value = 'descr';
108+
$settings->update();
109+
110+
foreach ([',', null] as $delimiter) {
111+
$field = $this->make_field(many: true, delimiter: $delimiter, allow_custom: ['any_ipv4']);
112+
$field->value = ['WAN_ipv4', 'LAN_ipv6', 'any_ipv4'];
113+
$internal = ['wan_ipv4', 'lan_ipv6', 'any_ipv4'];
114+
$internal = $delimiter === null ? $internal : implode($delimiter, $internal);
115+
$this->assert_is_true($field->validate());
116+
$this->assert_equals($field->to_internal(), $internal);
117+
$field->from_internal($internal);
118+
$this->assert_equals($field->value, ['WAN_ipv4', 'LAN_ipv6', 'any_ipv4']);
119+
$this->assert_equals($field->get_related_model(), null);
120+
$related = $field->get_related_models()->model_objects;
121+
$this->assert_equals(count($related), 2);
122+
$this->assert_equals($related[0]->id, 'wan');
123+
$this->assert_equals($related[1]->id, 'lan');
124+
}
125+
}
126+
127+
/**
128+
* Checks that optional null and empty values do not create interface references.
129+
*/
130+
public function test_optional_values(): void {
131+
$field = $this->make_field(allow_null: true, allow_empty: true);
132+
foreach ([null, ''] as $value) {
133+
$field->value = $value;
134+
$this->assert_is_true($field->validate());
135+
$this->assert_equals($field->to_internal(), $value);
136+
$field->from_internal($value);
137+
$this->assert_equals($field->value, $value);
138+
$this->assert_equals($field->get_related_model(), null);
139+
}
140+
}
141+
142+
/**
143+
* Constructs a field with the context and dynamic choices required for validation.
144+
* @param mixed ...$options Additional field options.
145+
* @return InterfaceAddressField The field to test.
146+
*/
147+
private function make_field(mixed ...$options): InterfaceAddressField {
148+
$field = new InterfaceAddressField(...$options);
149+
$field->name = 'extaddr';
150+
$field->context = new RESTAPISettings();
151+
$field->set_choices_from_callable();
152+
return $field;
153+
}
154+
}

0 commit comments

Comments
 (0)