Skip to content

Commit

Permalink
fixed config storage schema
Browse files Browse the repository at this point in the history
  • Loading branch information
HenriWahl committed Jun 2, 2023
1 parent 6abab26 commit a841011
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 59 deletions.
6 changes: 3 additions & 3 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
dhcpy6d (1.4.0-1) stable; urgency=medium
dhcpy6d (1.3.1-1) stable; urgency=medium

* New upstream release
+ added prefix_route_link_local
+ added prefix_route_link_local for client config
+ fixed invalid hostname

-- Henri Wahl <[email protected]> Sun, 28 May 2023 21:00:00 +0200
-- Henri Wahl <[email protected]> Sat, 03 Jun 2023 21:00:00 +0200

dhcpy6d (1.2.2-1) stable; urgency=medium

Expand Down
6 changes: 6 additions & 0 deletions dhcpy6d/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,12 @@ def read_config(self, configfile):
if self.STORE_CONFIG.lower() == 'none':
self.STORE_CONFIG = False

# integerify config schema version
try:
self.STORE_CONFIG_SCHEMA_VERSION = int(self.STORE_CONFIG_SCHEMA_VERSION)
except ValueError:
error_exit(f"{msg_prefix} 'storage_config_schema_version' '{self.STORE_CONFIG_SCHEMA_VERSION}' has to be an integer value")

# if no domain search list has been given use DOMAIN
if len(self.DOMAIN_SEARCH_LIST) == 0:
self.DOMAIN_SEARCH_LIST = self.DOMAIN
Expand Down
5 changes: 2 additions & 3 deletions dhcpy6d/storage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
volatile_store)
from ..helpers import error_exit

from .store_schema import StoreSchema
from .mysql import DBMySQL
from .postgresql import DBPostgreSQL
from .sqlite import SQLite
Expand Down Expand Up @@ -72,8 +71,6 @@ def run(self):
# source of configuration of hosts
# use client configuration only if needed
if cfg.STORE_CONFIG:
StoreSchema.set_version(cfg.STORE_CONFIG_SCHEMA_VERSION)

if cfg.STORE_CONFIG == 'file':
config_store = Textfile(config_query_queue, config_answer_queue)
if cfg.STORE_CONFIG == 'mysql':
Expand All @@ -82,6 +79,8 @@ def run(self):
config_store = DBPostgreSQL(config_query_queue, config_answer_queue)
if cfg.STORE_CONFIG == 'sqlite':
config_store = SQLite(config_query_queue, config_answer_queue, storage_type='config')
# set client config schema version after config storage is established
config_store.set_client_config_schema_version(cfg.STORE_CONFIG_SCHEMA_VERSION)
else:
# dummy configstore if no client config is needed
config_store = Store(config_query_queue, config_answer_queue)
Expand Down
41 changes: 28 additions & 13 deletions dhcpy6d/storage/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
convert_prefix_inline)
from .schemas import (legacy_adjustments,
MYSQL_SQLITE)
from .store_schema import StoreSchema


class ClientConfig:
Expand Down Expand Up @@ -95,8 +94,13 @@ class Store:
# if no tables exist they will be created by create_tables()
QUERY_TABLES = ''

# increasing number of SQL schema versions
version = 3
# increasing number of SQL schema versions for storage of volatile data
VOLATILE_SCHEMA_VERSION = 3

# supported versions of client config database schemas
CONFIG_SCHEMA_VERSIONS = [1, 2]
# default host config fields in database, may be extended at least by version 2
config_fields = ['hostname', 'mac', 'duid', 'class', 'address', 'prefix', 'id']

# link to used database module
db_module = None
Expand All @@ -117,6 +121,8 @@ def __init__(self, query_queue, answer_queue):
self.connected = False
# storage of query answers
self.answers = {}
# schema version of client config entries
self.config_schema_version = 1

def query(self, query):
"""
Expand Down Expand Up @@ -187,7 +193,7 @@ def create_tables(self):
query = self.SCHEMAS[table]
self.cursor.execute(query)
# set initial version
self.cursor.execute(f"INSERT INTO meta (item_key, item_value) VALUES ('version', '{self.version}')")
self.cursor.execute(f"INSERT INTO meta (item_key, item_value) VALUES ('version', '{self.VOLATILE_SCHEMA_VERSION}')")

def check_storage(self):
"""
Expand Down Expand Up @@ -712,11 +718,8 @@ def build_config_from_db(self, transaction):
transaction.client_config_dicts = ClientConfigDicts()

if self.config_prefix_support:

# 'mac LIKE' is necessary if multiple MACs are stored in config DB
fields = StoreSchema.get_host_table_fields()

query = f"SELECT {', '.join(fields)} FROM {self.table_hosts} WHERE " \
query = f"SELECT {', '.join(self.config_fields)} FROM {self.table_hosts} WHERE " \
f"hostname = '{transaction.hostname}' OR " \
f"mac LIKE '%{transaction.mac}%' OR " \
f"duid = '{transaction.duid}'"
Expand All @@ -726,11 +729,12 @@ def build_config_from_db(self, transaction):
# a section here is a host
# lowering MAC and DUID information in case they where upper in database
for host in answer:
prefix_route_link_local = 0
hostname, mac, duid, client_class, address, prefix, host_id = host

if 'prefix_route_link_local' in fields:
prefix_route_link_local = host[7]
prefix_route_link_local = False
# config schema version 2 adds prefix_route_link_local
if 'prefix_route_link_local' in self.config_fields:
hostname, mac, duid, client_class, address, prefix, host_id, prefix_route_link_local = host
else:
hostname, mac, duid, client_class, address, prefix, host_id = host

# lower some attributes to comply with values from request
if mac:
Expand Down Expand Up @@ -920,6 +924,17 @@ def collect_macs_from_db(self):
sys.stdout.flush()
return None

def set_client_config_schema_version(self, version):
"""
set client schema version, most probably from settings
"""
if version not in self.CONFIG_SCHEMA_VERSIONS:
raise Exception(f'Unsupported client config schema version {version}.')
self.config_schema_version = version
# extend version 1 fields by version 2
if self.config_schema_version >= 2:
self.config_fields += ['prefix_route_link_local']

def db_query(self, query):
"""
no not execute query on DB - dummy
Expand Down
39 changes: 0 additions & 39 deletions dhcpy6d/storage/store_schema.py

This file was deleted.

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
]

setup(name='dhcpy6d',
version='1.4.0',
version='1.3.1',
license='GNU GPL v2',
description='DHCPv6 server daemon',
long_description='Dhcpy6d delivers IPv6 addresses for DHCPv6 clients, which can be identified by DUID, hostname or MAC address as in the good old IPv4 days. It allows easy dualstack transition, addresses may be generated randomly, by range, by DNS, by arbitrary ID or MAC address. Clients can get more than one address, leases and client configuration can be stored in databases and DNS can be updated dynamically.',
Expand Down

0 comments on commit a841011

Please sign in to comment.