Add subnet support for client secrets
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Steve McMaster 2022-10-16 20:48:49 +00:00
parent 91389e72d1
commit ba0d96eda7
3 changed files with 57 additions and 4 deletions

View File

@ -4,7 +4,7 @@ assign:
"BE39.34DD.76AC": 30 "BE39.34DD.76AC": 30
"C919.2AC9.7E20": 40 "C919.2AC9.7E20": 40
clients: clients:
127.0.0.1: 070c58eb-f79d-4cbb-8482-3aaab089cc88 127.0.0.0/8: 0f5c6a8b-bb18-46fc-8fea-2c2032cb0754
192.168.200.123: 1231fa06-593b-4f37-a4a1-646e22c34928 192.168.200.123: 1231fa06-593b-4f37-a4a1-646e22c34928
10.244.0.1: 83f271a8-285d-4830-b4ae-2c91d460b7e1 10.244.0.1: 83f271a8-285d-4830-b4ae-2c91d460b7e1
default_vlan: 100 default_vlan: 100

19
test.py
View File

@ -1,12 +1,27 @@
import tempfile
import pyrad.packet import pyrad.packet
import yaml
from pyrad.client import Client from pyrad.client import Client
from pyrad.dictionary import Dictionary from pyrad.dictionary import Dictionary
with open("config.sample.yaml", "r", encoding="utf-8") as f:
config = yaml.load(f, Loader=yaml.SafeLoader)
with tempfile.NamedTemporaryFile() as tmpfile:
with open(tmpfile.name, "w", encoding="utf-8") as f:
for attribute in config["dictionary"]:
f.write(f"ATTRIBUTE {attribute['name']} {attribute['id']} {attribute['type']}\n")
dictionary = Dictionary(tmpfile.name)
srv = Client( srv = Client(
server="127.0.0.1", server="127.0.0.1",
secret=b"070c58eb-f79d-4cbb-8482-3aaab089cc88", secret=b"0f5c6a8b-bb18-46fc-8fea-2c2032cb0754z",
dict=Dictionary("dictionary") dict=dictionary
) )
req = srv.CreateAuthPacket( req = srv.CreateAuthPacket(

View File

@ -1,8 +1,10 @@
import argparse import argparse
import logging import logging
import ipaddress
import os import os
import re import re
import tempfile import tempfile
from collections import UserDict
import asyncio import asyncio
import yaml import yaml
@ -21,6 +23,39 @@ except ImportError:
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class IpaddressDict(UserDict):
def __getitem__(self, key):
# If `key` exists as a key, just return that
if super().__contains__(key):
return super().__getitem__(key)
# Test that `key` is actually an ip_address
try:
key = ipaddress.ip_address(key)
except ValueError as e:
raise KeyError(key) from e
# Check each key we have as an ip_network
for (test, value) in self.items():
try:
test = ipaddress.ip_network(test)
except ValueError:
continue
if key in test:
return value
# No dice
raise KeyError(key)
def __contains__(self, key):
try:
self[key]
except KeyError:
return False
return True
class VlanAssign(ServerAsync): class VlanAssign(ServerAsync):
def __init__(self, usermap, *args, default_vlan=1, **kwargs): def __init__(self, usermap, *args, default_vlan=1, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
@ -84,7 +119,7 @@ def main():
config["assign"] = {normalize_mac(k): v for (k, v) in config["assign"].items()} config["assign"] = {normalize_mac(k): v for (k, v) in config["assign"].items()}
with tempfile.NamedTemporaryFile() as tmpfile: with tempfile.NamedTemporaryFile() as tmpfile:
with open(tmpfile.name, "w") as f: with open(tmpfile.name, "w", encoding="utf-8") as f:
for attribute in config["dictionary"]: for attribute in config["dictionary"]:
f.write(f"ATTRIBUTE {attribute['name']} {attribute['id']} {attribute['type']}\n") f.write(f"ATTRIBUTE {attribute['name']} {attribute['id']} {attribute['type']}\n")
@ -95,6 +130,7 @@ def main():
server = VlanAssign( server = VlanAssign(
loop=loop, loop=loop,
dictionary=dictionary, dictionary=dictionary,
enable_pkt_verify=True,
usermap=config["assign"] usermap=config["assign"]
) )
@ -105,6 +141,8 @@ def main():
address address
) )
server.hosts = IpaddressDict(server.hosts)
try: try:
# Initialize transports # Initialize transports
loop.run_until_complete( loop.run_until_complete(