working configuration for reservations

- Make ipv4 reservations in kea by directly writing to the database
- uses same configuration file as database configuration under the
heading reservationV4
This commit is contained in:
Matthew Stobbs 2024-09-24 14:43:54 -06:00
parent 003f9fca01
commit a78f001a41
9 changed files with 85 additions and 170 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
kea-manage.yaml

View File

@ -4,14 +4,15 @@ Copyright © 2024 NAME HERE <EMAIL ADDRESS>
package cmd
import (
"context"
"errors"
"log/slog"
"os"
"git.staur.ca/stobbsm/kea-manage/ipv4/reservation"
"git.staur.ca/stobbsm/kea-manage/lib/database"
"git.staur.ca/stobbsm/kea-manage/lib/database/postgres"
"github.com/jackc/pgx/v5"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// rootCmd represents the base command when called without any subcommands
@ -49,53 +50,39 @@ func init() {
// This will only work with hw-address identifiers. If I need support for more, I'll add it
func runMain(cmd *cobra.Command, args []string) {
reslist := []*reservation.ReservationV4{
reservation.ReserveV4MacAddr(
"90:e2:ba:19:25:70",
"10.0.255.2",
"mars",
100,
),
reservation.ReserveV4MacAddr(
"90:e2:ba:19:25:70",
"10.1.255.2",
"mars",
101,
),
reservation.ReserveV4MacAddr(
"90:e2:ba:19:25:70",
"10.2.255.2",
"mars",
102,
),
reservation.ReserveV4MacAddr(
"90:e2:ba:19:25:70",
"10.3.255.2",
"mars",
103,
),
reservation.ReserveV4MacAddr(
"BC:24:11:E5:C5:AF",
"10.0.0.4",
"ns01",
100,
),
reservation.ReserveV4MacAddr(
"BC:24:11:B1:1C:AD",
"10.1.0.6",
"db01",
101,
),
reslist := []*reservation.ReservationV4{}
vlist := viper.Get("reservationV4").([]any)
for _, v := range vlist {
v := v.(map[string]any)
r := reservation.ReserveV4MacAddr(
v["type"].(string),
v["identifier"].(string),
v["ipv4"].(string),
v["hostname"].(string),
v["subnetid"].(int),
)
reslist = append(
reslist,
r,
)
}
pg := postgres.Open("10.1.0.6", 5432, "kea", "xGq42ZMeMUIWRK", "kea")
slog.Debug("reslist", slog.Any("slice", reslist))
dbconf := viper.GetStringMapString("database")
pg := postgres.Open(dbconf["host"], dbconf["port"], dbconf["user"], dbconf["password"], dbconf["name"])
for _, v := range reslist {
v := v
if err := pg.InsertResV4(v); err != nil {
slog.Error(err.Error())
if errors.As(err, &database.Exists) {
slog.Info("exists", slog.Any("reservation", *v))
} else {
slog.Error(err.Error())
}
continue
}
slog.Info("inserted reservation", *v)
slog.Info("inserted", slog.Any("reservation", *v))
}
}

View File

@ -4,31 +4,6 @@ import (
"git.staur.ca/stobbsm/kea-manage/lib/types"
)
const insertHostReservation = `INSERT INTO hosts (dhcp_identifier,
dhcp_identifier_type,
dhcp4_subnet_id,
ipv4_address,
hostname)
VALUES (DECODE(REPLACE($1, ':', ''), 'hex'), $2, $3, (SELECT ($4::inet - '0.0.0.0'::inet)), $5);`
const getHostIDWithMacAddr = `SELECT host_id
FROM hosts
WHERE dhcp4_subnet_id = $1
AND dhcp_identifier_type = $2
AND dhcp_identifier = (DECODE(REPLACE($3, ':', ''), 'hex'));`
const insertIfNotExists = `INSERT INTO hosts (dhcp_identifier,
dhcp_identifier_type,
dhcp4_subnet_id,
ipv4_address,
hostname)
SELECT DECODE(REPLACE($1, ':', ''), 'hex'), $2, $3, (SELECT ($4::inet - '0.0.0.0'::inet)), $5
WHERE NOT EXISTS (
SELECT host_id FROM hosts
WHERE dhcp4_subnet_id = $3
AND dhcp_identifier_type = $2
AND dhcp_identifier = DECODE(REPLACE($1, ':', ''), 'hex'));`
type ReservationV4 struct {
Type int
SubnetID int
@ -37,10 +12,10 @@ type ReservationV4 struct {
Hostname string
}
func ReserveV4MacAddr(mac string, addr string, hostname string, subnet int) *ReservationV4 {
func ReserveV4MacAddr(rtype string, mac string, addr string, hostname string, subnet int) *ReservationV4 {
r := &ReservationV4{
MacAddr: mac,
Type: int(types.MacAddress),
Type: types.Lookup(rtype),
SubnetID: subnet,
Ipv4: addr,
Hostname: hostname,

View File

@ -1,91 +0,0 @@
database:
type: pgx
host: 10.1.0.6
port: 5432
user: kea
password: xGq42ZMeMUIWRK
database: kea
reservationV4:
- type: macaddr
identifer: "90:e2:ba:19:25:70"
subnetid: 100
hostname: mars
ipv4: 10.0.255.2
- type: macaddr
identifer: "90:e2:ba:19:25:70"
subnetid: 101
hostname: mars
ipv4: 10.1.255.2
- type: macaddr
identifer: "90:e2:ba:19:25:70"
subnetid: 102
hostname: mars
ipv4: 10.2.255.2
- type: macaddr
identifer: "90:e2:ba:19:25:70"
subnetid: 103
hostname: mars
ipv4: 10.3.255.2
- type: macaddr
identifer: "BC:24:11:E5:C5:AF"
subnetid: 100
hostname: ns01
ipv4: 10.0.0.4
- type: macaddr
identifer: "BC:24:11:2F:4B:23"
subnetid: 101
hostname: ns01
ipv4: 10.1.0.4
- type: macaddr
identifer: "BC:24:11:99:68:40"
subnetid: 102
hostname: ns01
ipv4: 10.2.0.4
- type: macaddr
identifer: "BC:24:11:79:BF:B4"
subnetid: 103
hostname: ns01
ipv4: 10.3.0.4
- type: macaddr
identifer: "BC:24:11:B1:1C:AD"
subnetid: 101
hostname: db01
ipv4: 10.1.0.6
- type: macaddr
identifier: "BC:21:11:52:FA:73"
subnetid: 100
hostname: dhcp01
ipv4: 10.0.0.5
- type: macaddr
identifier: "BC:21:11:E6:45:0B"
subnetid: 101
hostname: dhcp01
ipv4: 10.1.0.5
- type: macaddr
identifier: "BC:24:11:D2:6D:38"
subnetid: 102
hostname: dhcp01
ipv4: 10.2.0.5
- type: macaddr
identifier: "BC:24:11:A2:C5:94"
subnetid: 103
hostname: dhcp01
ipv4: 10.3.0.5
- type: macaddr
identifier: "BC:24:11:B8:BC:CF"
subnetid: 100
hostname: jellyfin
ipv4: 10.0.0.20

View File

@ -7,3 +7,11 @@ import (
type StoreV4 interface {
InsertResV4(*reservation.ReservationV4) error
}
type ExistsError struct{}
func (m *ExistsError) Error() string {
return "exists"
}
var Exists = &ExistsError{}

View File

@ -5,6 +5,7 @@ import (
"fmt"
"git.staur.ca/stobbsm/kea-manage/ipv4/reservation"
"git.staur.ca/stobbsm/kea-manage/lib/database"
"github.com/jackc/pgx/v5"
)
@ -15,9 +16,9 @@ type Postgres struct {
// Open receives the host, port, username, password and database name of a postgresql
// database, and manages connections to it.
func Open(host string, port int, user string, password string, name string) *Postgres {
func Open(host, port, user, password, name string) *Postgres {
var p = &Postgres{
connString: fmt.Sprintf("postgres://%s:%s@%s:%d/%s", user, password, host, port, name),
connString: fmt.Sprintf("postgres://%s:%s@%s:%s/%s", user, password, host, port, name),
}
return p
}
@ -32,6 +33,7 @@ func (p *Postgres) close() {
p.conn.Close(context.Background())
}
// InsertResV4 adds IPv4 reservations to the kea database
func (p *Postgres) InsertResV4(r *reservation.ReservationV4) error {
if err := p.connect(context.Background()); err != nil {
return err
@ -44,12 +46,18 @@ func (p *Postgres) InsertResV4(r *reservation.ReservationV4) error {
}
defer tx.Rollback(context.Background())
if _, err = tx.Exec(context.Background(), insertIfNotExists, r.MacAddr, r.Type, r.SubnetID, r.Ipv4, r.Hostname); err != nil {
ct, err := tx.Exec(context.Background(), insertIfNotExists, r.MacAddr, r.Type, r.SubnetID, r.Ipv4, r.Hostname)
if err != nil {
return err
}
if err = tx.Commit(context.Background()); err != nil {
return err
}
if ct.RowsAffected() == 0 {
return database.Exists
}
return nil
}

View File

@ -1,5 +1,18 @@
package postgres
const insertHostReservation = `INSERT INTO hosts (dhcp_identifier,
dhcp_identifier_type,
dhcp4_subnet_id,
ipv4_address,
hostname)
VALUES (DECODE(REPLACE($1, ':', ''), 'hex'), $2, $3, (SELECT ($4::inet - '0.0.0.0'::inet)), $5);`
const getHostIDWithMacAddr = `SELECT host_id
FROM hosts
WHERE dhcp4_subnet_id = $1
AND dhcp_identifier_type = $2
AND dhcp_identifier = (DECODE(REPLACE($3, ':', ''), 'hex'));`
const insertIfNotExists = `INSERT INTO hosts (dhcp_identifier,
dhcp_identifier_type,
dhcp4_subnet_id,

View File

@ -1,8 +1,17 @@
package types
type IdentifierType int
import "strings"
const (
HWAddress = 0
MacAddress = 0
)
var types = map[string]int{
"macaddr": 0,
"hwaddr": 0,
"invalid": 255,
}
func Lookup(rtype string) int {
rtype = strings.ToLower(rtype)
if t, ok := types[rtype]; ok == true {
return t
}
return types["invalid"]
}

View File

@ -1,6 +1,7 @@
package main
import (
"log/slog"
"os"
"git.staur.ca/stobbsm/kea-manage/cmd"
@ -8,10 +9,14 @@ import (
)
func main() {
slog.SetLogLoggerLevel(slog.LevelDebug)
viper.SetConfigName("kea-manage")
viper.AddConfigPath("/etc")
viper.AddConfigPath(os.Getenv("HOME") + "/.config/kea-manage")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
slog.Error("error while reading configuration", err)
os.Exit(1)
}
cmd.Execute()
}