cmd

package
v0.0.0-...-fcebd9b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 8, 2025 License: BSD-2-Clause Imports: 32 Imported by: 0

Documentation

Overview

* Copyright (c) 2024 Johan Stenstam, johan.stenstam@internetstiftelsen.se

* Copyright (c) 2024 Johan Stenstam, johan.stenstam@internetstiftelsen.se

Index

Constants

View Source
const (
	FLAG_ENROLL_CREDENTIALS   = "enroll-credentials" // #nosec G101 -- only used as flag name in CLI
	FLAG_ENROLL_CONFDIR       = "confdir"
	FLAG_ENROLL_CERTDIR       = "certdir"
	FLAG_RENEW_DATAKEY        = "renew-datakey"
	FLAG_RENEW_CLIENTKEY      = "renew-clientkey"
	FLAG_RENEW_CACERT_OUT     = "renew-cacert-out"
	FLAG_RENEW_CLIENTCERT_OUT = "renew-clientcert-out"
)
View Source
const (
	DIRNAME_DEFAULT_CONFDIR  = "/etc/dnstapir/"
	DIRNAME_DEFAULT_CERTDIR  = "/etc/dnstapir/certs/"
	FILENAME_DATAKEY_PRIV    = "datakey-priv.json"
	FILENAME_TLS_CRT         = "tls.crt"
	FILENAME_TLS_KEY         = "tls.key"
	FILENAME_CA_CRT          = "ca.crt"
	FILENAME_VALKEY_STORE    = "validation-keys.json"
	FILENAME_POP_SOURCES     = "pop-sources.yaml"
	FILENAME_POP_OUTPUTS     = "pop-outputs.yaml"
	FILENAME_POP_POLICY      = "pop-policy.yaml"
	FILENAME_TAPIR_POP       = "tapir-pop.yaml"
	FILENAME_TAPIR_EDM       = "tapir-edm.toml"
	URL_NODEMAN_API_PATH     = "api/v1/node"
	CONTENT_TYPE_NODEMAN_API = "application/json"
	JWK_KEY_ISS              = "iss"
)
View Source
const CFG_TML_POP_OUTPUTS = `` /* 131-byte string literal not displayed */
View Source
const CFG_TML_POP_POLICY = `` /* 333-byte string literal not displayed */
View Source
const CFG_TML_POP_SOURCES = `` /* 373-byte string literal not displayed */
View Source
const CFG_TML_TAPIR_EDM = `` /* 835-byte string literal not displayed */
View Source
const CFG_TML_TAPIR_POP = `` /* 1526-byte string literal not displayed */

Variables

View Source
var ApiCmd = &cobra.Command{
	Use:   "api",
	Short: "request a TAPIR-POP api summary",
	Long:  `Query TAPIR-POP for the provided API endpoints and print that out in a (hopefully) comprehensible fashion.`,
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) != 0 {
			log.Fatal("api must have no arguments")
		}
		tapir.GlobalCF.Api.ShowApi()
	},
}
View Source
var BumpCmd = &cobra.Command{
	Use:   "bump",
	Short: "Instruct TAPIR-POP to bump the SOA serial of the RPZ zone",
	Run: func(cmd *cobra.Command, args []string) {
		resp := SendCommandCmd(tapir.CommandPost{
			Command: "bump",
			Zone:    dns.Fqdn(tapir.GlobalCF.Zone),
		})
		if resp.Error {
			fmt.Printf("%s\n", resp.ErrorMsg)
		}

		fmt.Printf("%s\n", resp.Msg)
	},
}
View Source
var DawgCmd = &cobra.Command{
	Use:   "dawg",
	Short: "Generate or interact with data stored in a DAWG file; only useable via sub-commands",
}
View Source
var DebugCmd = &cobra.Command{
	Use:   "debug",
	Short: "Prefix command to various debug tools; do not use in production",
}
View Source
var EnrollCmd = &cobra.Command{
	Use:   "enroll",
	Short: "Enroll the node into DNSTAPIR",
	Run: func(cmd *cobra.Command, args []string) {
		enroll()
	},
}
View Source
var FilterlistsCmd = &cobra.Command{
	Use:   "filterlists",
	Short: "Return the allow/deny/doubtlists from the current data structures",
	Run: func(cmd *cobra.Command, args []string) {
		resp := SendDebugCmd(tapir.DebugPost{
			Command: "filterlists",
		})
		if resp.Error {
			fmt.Printf("%s\n", resp.ErrorMsg)
		}
		fmtstring := "%-75s|%-20s|%-20s|%-10s|%-10s\n"

		fmt.Printf(fmtstring, "Domain", "Source", "Src Fmt", "Filter", "Flags")
		fmt.Println(strings.Repeat("-", 135))

		for _, l := range resp.Lists["allowlist"] {
			for _, n := range l.Names {
				fmt.Printf(fmtstring, n.Name, l.Name, "-", "allow", "-")
			}
		}
		for _, l := range resp.Lists["denylist"] {
			for _, n := range l.Names {
				fmt.Printf(fmtstring, n.Name, l.Name, "-", "deny", "-")
			}
		}
		for _, l := range resp.Lists["doubtlist"] {
			for _, n := range l.Names {
				fmt.Printf(fmtstring, n.Name, l.Name, l.SrcFormat, "doubt", strconv.Itoa(int(n.TagMask)))
			}
		}
	},
}
View Source
var Listname string
View Source
var PopCmd = &cobra.Command{
	Use:   "pop",
	Short: "Prefix command, only usable via sub-commands",
}
View Source
var PopMqttCmd = &cobra.Command{
	Use:   "mqtt",
	Short: "Prefix command, only usable via sub-commands",
}
View Source
var PopMqttRestartCmd = &cobra.Command{
	Use:   "restart",
	Short: "Instruct TAPIR-POP MQTT Engine to restart",
	Run: func(cmd *cobra.Command, args []string) {
		resp := SendCommandCmd(tapir.CommandPost{
			Command: "mqtt-restart",
		})
		if resp.Error {
			fmt.Printf("%s\n", resp.ErrorMsg)
		}

		fmt.Printf("%s\n", resp.Msg)
	},
}
View Source
var PopMqttStartCmd = &cobra.Command{
	Use:   "start",
	Short: "Instruct TAPIR-POP MQTT Engine to start",
	Run: func(cmd *cobra.Command, args []string) {
		resp := SendCommandCmd(tapir.CommandPost{
			Command: "mqtt-start",
		})
		if resp.Error {
			fmt.Printf("%s\n", resp.ErrorMsg)
		}

		fmt.Printf("%s\n", resp.Msg)
	},
}
View Source
var PopMqttStopCmd = &cobra.Command{
	Use:   "stop",
	Short: "Instruct TAPIR-POP MQTT Engine to stop",
	Run: func(cmd *cobra.Command, args []string) {
		resp := SendCommandCmd(tapir.CommandPost{
			Command: "mqtt-stop",
		})
		if resp.Error {
			fmt.Printf("%s\n", resp.ErrorMsg)
		}

		fmt.Printf("%s\n", resp.Msg)
	},
}
View Source
var PopPingCmd = &cobra.Command{
	Use:   "ping",
	Short: "Send an API ping request to TAPIR-POP and present the response",
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) != 0 {
			log.Fatal("ping must have no arguments")
		}

		pr, err := tapir.GlobalCF.Api.SendPing(tapir.GlobalCF.PingCount, false)
		if err != nil {
			log.Fatalf("Error from SendPing: %v", err)
		}

		uptime := time.Now().Sub(pr.BootTime).Round(time.Second)
		if tapir.GlobalCF.Verbose {
			fmt.Printf("%s from %s @ %s (version %s): pings: %d, pongs: %d, uptime: %v time: %s, client: %s\n",
				pr.Msg, pr.Daemon, pr.ServerHost, pr.Version, pr.Pings,
				pr.Pongs, uptime, pr.Time.Format(timelayout), pr.Client)
		} else {
			fmt.Printf("%s: pings: %d, pongs: %d, uptime: %v, time: %s\n",
				pr.Msg, pr.Pings, pr.Pongs, uptime, pr.Time.Format(timelayout))
		}
	},
}
View Source
var PopStatusCmd = &cobra.Command{
	Use:   "status",
	Short: "Get the status of TAPIR-POP",
	Run: func(cmd *cobra.Command, args []string) {
		resp := SendCommandCmd(tapir.CommandPost{
			Command: "status",
		})
		if resp.Error {
			fmt.Printf("%s\n", resp.ErrorMsg)
		}

		fmt.Printf("%s\n", resp.Msg)

		if len(resp.TapirFunctionStatus.ComponentStatus) != 0 {
			tfs := resp.TapirFunctionStatus
			fmt.Printf("TAPIR-POP Status. Reported components: %d Total errors (since last start): %d\n", len(tfs.ComponentStatus), tfs.NumFailures)
			out := []string{"Component|Status|Error msg|# Fails|# Warns|LastFailure|LastSuccess"}
			for k, v := range tfs.ComponentStatus {
				out = append(out, fmt.Sprintf("%s|%s|%s|%d|%d|%v|%v", k, tapir.StatusToString[v.Status], v.ErrorMsg, v.NumFails, v.NumWarnings, v.LastFail.Format(tapir.TimeLayout), v.LastSuccess.Format(tapir.TimeLayout)))
			}
			fmt.Printf("%s\n", columnize.SimpleFormat(out))
		}
	},
}
View Source
var PopStopCmd = &cobra.Command{
	Use:   "stop",
	Short: "Instruct TAPIR-POP to stop",
	Run: func(cmd *cobra.Command, args []string) {
		resp := SendCommandCmd(tapir.CommandPost{
			Command: "stop",
		})
		if resp.Error {
			fmt.Printf("%s\n", resp.ErrorMsg)
		}

		fmt.Printf("%s\n", resp.Msg)
	},
}
View Source
var RenewCmd = &cobra.Command{
	Use:   "renew",
	Short: "Renew TLS certs used to secure the MQTT connection",
	Run: func(cmd *cobra.Command, args []string) {
		renew()
	},
}
View Source
var RpzAddCmd = &cobra.Command{
	Use:   "add",
	Short: "Instruct TAPIR-POP to add a new rule to the RPZ zone",
	Run: func(cmd *cobra.Command, args []string) {
		if rpzname == "" {
			fmt.Printf("Error: domain name for which to add new RPZ rule for not specified.\n")
			os.Exit(1)
		}

		if rpztype == "" {
			fmt.Printf("Error: RPZ list type for domain name \"%s\" not specified.\n", rpzname)
			fmt.Printf("Error: must be one of: allowlist, doubtlist or denylist.\n")
			os.Exit(1)
		}

		if rpzpolicy == "" {
			fmt.Printf("Error: desired RPZ policy for domain name \"%s\" not specified.\n", rpzname)
			os.Exit(1)
		}

		resp := SendCommandCmd(tapir.CommandPost{
			Command:  "rpz-add",
			Name:     dns.Fqdn(rpzname),
			ListType: rpztype,
			Action:   rpzaction,
			Policy:   rpzpolicy,
			Zone:     dns.Fqdn(tapir.GlobalCF.Zone),
		})
		if resp.Error {
			fmt.Printf("%s\n", resp.ErrorMsg)
		}

		if resp.Msg != "" {
			fmt.Printf("%s\n", resp.Msg)
		}
	},
}
View Source
var RpzCmd = &cobra.Command{
	Use:   "rpz",
	Short: "Instruct TAPIR-POP to modify the RPZ zone; must use sub-command",
	Long: `Known actions are:
drop	       send no response at all
nxdomain       return an NXDOMAIN response
nodata	       return a NODATA response`,
}
View Source
var RpzListCmd = &cobra.Command{
	Use:   "list",
	Short: "Instruct TAPIR-POP to remove a rule from the RPZ zone",
	Run: func(cmd *cobra.Command, args []string) {
		resp := SendCommandCmd(tapir.CommandPost{
			Command: "rpz-list-sources",
		})
		if resp.Error {
			fmt.Printf("%s\n", resp.ErrorMsg)
		}

		fmt.Printf("%s\n", resp.Msg)
	},
}
View Source
var RpzLookupCmd = &cobra.Command{
	Use:   "lookup",
	Short: "Instruct TAPIR-POP to remove a rule from the RPZ zone",
	Run: func(cmd *cobra.Command, args []string) {
		if rpzname == "" {
			fmt.Printf("Error: domain name look up not specified.\n")
			os.Exit(1)
		}

		resp := SendCommandCmd(tapir.CommandPost{
			Command: "rpz-lookup",
			Name:    dns.Fqdn(rpzname),
			Zone:    dns.Fqdn(tapir.GlobalCF.Zone),
		})
		if resp.Error {
			fmt.Printf("%s\n", resp.ErrorMsg)
		}

		fmt.Printf("%s\n", resp.Msg)
	},
}
View Source
var RpzRemoveCmd = &cobra.Command{
	Use:   "remove",
	Short: "Instruct TAPIR-POP to remove a rule from the RPZ zone",
	Run: func(cmd *cobra.Command, args []string) {
		if rpzname == "" {
			fmt.Printf("Error: domain name to add rule for not specified.\n")
			os.Exit(1)
		}

		resp := SendCommandCmd(tapir.CommandPost{
			Command: "rpz-remove",
			Name:    dns.Fqdn(rpzname),
			Zone:    dns.Fqdn(tapir.GlobalCF.Zone),
		})
		if resp.Error {
			fmt.Printf("%s\n", resp.ErrorMsg)
		}

		fmt.Printf("%s\n", resp.Msg)
	},
}
View Source
var SloggerCmd = &cobra.Command{
	Use:   "slogger",
	Short: "Prefix command to TAPIR-Slogger, only usable in TAPIR Core, not in TAPIR Edge",
}
View Source
var SloggerEdmCmd = &cobra.Command{
	Use:   "edm",
	Short: "Prefix command, only usable via sub-commands",
}
View Source
var SloggerEdmStatusCmd = &cobra.Command{
	Use:   "status",
	Short: "Get the TAPIR-EDM status report from TAPIR-Slogger",
	Run: func(cmd *cobra.Command, args []string) {
		resp := SendSloggerCommand(tapir.SloggerCmdPost{
			Command: "status",
		})
		if resp.Error {
			fmt.Printf("%s\n", resp.ErrorMsg)
		}

		fmt.Printf("%s\n", resp.Msg)

		if len(resp.EdmStatus) == 0 {
			fmt.Printf("No Status reports from any TAPIR-EDM received\n")
			os.Exit(0)
		}

		showfails := ""
		if onlyfails {
			showfails = " (only fails)"
		}

		var out []string
		for functionid, ps := range resp.EdmStatus {
			fmt.Printf("Status for TAPIR-EDM%s: %s\n", showfails, functionid)
			out = []string{"Component|Status|Error msg|NumFailures|LastFailure|LastSuccess"}
			for comp, v := range ps.ComponentStatus {
				if !onlyfails || v.Status == tapir.StatusFail {
					out = append(out, fmt.Sprintf("%s|%s|%s|%d|%s|%s", comp, tapir.StatusToString[v.Status], v.ErrorMsg, v.NumFails,
						v.LastFail.Format(tapir.TimeLayout), v.LastSuccess.Format(tapir.TimeLayout)))
				}
			}
		}

		fmt.Printf("%s\n", columnize.SimpleFormat(out))
	},
}
View Source
var SloggerPingCmd = &cobra.Command{
	Use:   "ping",
	Short: "Send an API ping request to TAPIR-Slogger and present the response",
	Run: func(cmd *cobra.Command, args []string) {
		if len(args) != 0 {
			log.Fatal("ping must have no arguments")
		}

		api, err := SloggerApi()
		if err != nil {
			log.Fatalf("Error: Could not set up API client to TAPIR-SLOGGER: %v", err)
		}

		pr, err := api.SendPing(tapir.GlobalCF.PingCount, false)
		if err != nil {
			log.Fatalf("Error from SendPing: %v", err)
		}

		uptime := time.Now().Sub(pr.BootTime).Round(time.Second)
		if tapir.GlobalCF.Verbose {
			fmt.Printf("%s from %s @ %s (version %s): pings: %d, pongs: %d, uptime: %v time: %s, client: %s\n",
				pr.Msg, pr.Daemon, pr.ServerHost, pr.Version, pr.Pings,
				pr.Pongs, uptime, pr.Time.Format(timelayout), pr.Client)
		} else {
			fmt.Printf("%s: pings: %d, pongs: %d, uptime: %v, time: %s\n",
				pr.Msg, pr.Pings, pr.Pongs, uptime, pr.Time.Format(timelayout))
		}
	},
}
View Source
var SloggerPopCmd = &cobra.Command{
	Use:   "pop",
	Short: "Prefix command, only usable via sub-commands",
}
View Source
var SloggerPopStatusCmd = &cobra.Command{
	Use:   "status",
	Short: "Get the TAPIR-POP status report from TAPIR-Slogger",
	Run: func(cmd *cobra.Command, args []string) {
		resp := SendSloggerCommand(tapir.SloggerCmdPost{
			Command: "status",
		})
		if resp.Error {
			fmt.Printf("%s\n", resp.ErrorMsg)
		}

		fmt.Printf("%s\n", resp.Msg)

		if len(resp.PopStatus) == 0 {
			fmt.Printf("No Status reports from any TAPIR-POP received\n")
			os.Exit(0)
		}

		showfails := ""
		if onlyfails {
			showfails = " (only fails)"
		}

		var out []string
		for functionid, ps := range resp.PopStatus {
			fmt.Printf("Status for TAPIR-POP%s: %s\n", showfails, functionid)
			out = []string{"Component|Status|Error msg|NumFailures|LastFailure|LastSuccess"}
			for comp, v := range ps.ComponentStatus {
				if !onlyfails || v.Status == tapir.StatusFail {
					out = append(out, fmt.Sprintf("%s|%s|%s|%d|%s|%s", comp, tapir.StatusToString[v.Status], v.ErrorMsg, v.NumFails,
						v.LastFail.Format(tapir.TimeLayout), v.LastSuccess.Format(tapir.TimeLayout)))
				}
			}
		}

		fmt.Printf("%s\n", columnize.SimpleFormat(out))
	},
}

Functions

func CompileDawgFromCSV

func CompileDawgFromCSV(srcfile, outfile string)

func CompileDawgFromText

func CompileDawgFromText(srcfile, outfile string)

func SendCommandCmd

func SendCommandCmd(data tapir.CommandPost) tapir.CommandResponse

func SendDebugCmd

func SendDebugCmd(data tapir.DebugPost) tapir.DebugResponse

func SendSloggerCommand

func SendSloggerCommand(data tapir.SloggerCmdPost) tapir.SloggerCmdResponse

func SloggerApi

func SloggerApi() (*tapir.ApiClient, error)

Types

type ConfigData

type ConfigData struct {
	CertdirPath        string
	CaCertPath         string
	ClientCertPath     string
	ClientKeyPath      string
	ConfigTopic        string
	StatusTopic        string
	ObservationsTopic  string
	SignkeyPath        string
	ValidationKeysPath string
	MqttBroker         string
	AggrecUrl          string
}

type DebugResponse

type DebugResponse struct {
	Msg      string
	Data     interface{}
	Error    bool
	ErrorMsg string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL