Skip to content

Commit

Permalink
Go API: NewConfigurator takes unbound bin path
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Jan 2, 2021
1 parent 03e29f3 commit 6668d4f
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 32 deletions.
5 changes: 3 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ func _main(ctx context.Context, buildInfo models.BuildInformation, args []string
client := &http.Client{Timeout: clientTimeout}
// Create configurators
dnsCrypto := dnscrypto.New(client, "", "") // TODO checksums for build
const unboundDir = "/unbound"
dnsConf := unbound.NewConfigurator(logger, os.OpenFile, dnsCrypto, unboundDir)
const unboundEtcDir = "/unbound"
const unboundPath = "/unbound/unbound"
dnsConf := unbound.NewConfigurator(logger, os.OpenFile, dnsCrypto, unboundEtcDir, unboundPath)

if len(args) > 1 && args[1] == "build" {
if err := dnsConf.SetupFiles(ctx); err != nil {
Expand Down
8 changes: 3 additions & 5 deletions pkg/unbound/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,19 @@ import (

func (c *configurator) Start(ctx context.Context, verbosityDetailsLevel uint8) (
stdout io.ReadCloser, wait func() error, err error) {
configFilepath := filepath.Join(c.unboundDir, unboundConfigFilename)
configFilepath := filepath.Join(c.unboundEtcDir, unboundConfigFilename)
args := []string{"-d", "-c", configFilepath}
if verbosityDetailsLevel > 0 {
args = append(args, "-"+strings.Repeat("v", int(verbosityDetailsLevel)))
}

binFilepath := filepath.Join(c.unboundDir, unboundBinFilename)
// Only logs to stderr
_, stdout, wait, err = c.commander.Start(ctx, binFilepath, args...)
_, stdout, wait, err = c.commander.Start(ctx, c.unboundPath, args...)
return stdout, wait, err
}

func (c *configurator) Version(ctx context.Context) (version string, err error) {
binFilepath := filepath.Join(c.unboundDir, unboundBinFilename)
output, err := c.commander.Run(ctx, binFilepath, "-V")
output, err := c.commander.Run(ctx, c.unboundPath, "-V")
if err != nil {
return "", fmt.Errorf("unbound version: %w", err)
}
Expand Down
20 changes: 12 additions & 8 deletions pkg/unbound/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import (
func Test_Start(t *testing.T) {
t.Parallel()
mockCtrl := gomock.NewController(t)
const unboundDir = "/unbound"
const unboundEtcDir = "/unbound"
const unboundPath = "/usr/sbin/unbound"
commander := mock_command.NewMockCommander(mockCtrl)
commander.EXPECT().Start(context.Background(), "/unbound/unbound", "-d", "-c", "/unbound/unbound.conf", "-vv").
commander.EXPECT().Start(context.Background(), unboundPath, "-d", "-c", "/unbound/unbound.conf", "-vv").
Return(nil, nil, nil, nil).Times(1)
c := &configurator{
commander: commander,
unboundDir: unboundDir,
commander: commander,
unboundEtcDir: unboundEtcDir,
unboundPath: unboundPath,
}
stdout, waitFn, err := c.Start(context.Background(), 2)
assert.Nil(t, stdout)
Expand Down Expand Up @@ -56,12 +58,14 @@ func Test_Version(t *testing.T) {
commander := mock_command.NewMockCommander(mockCtrl)
ctx := context.Background()

const unboundDir = "/unbound"
commander.EXPECT().Run(ctx, "/unbound/unbound", "-V").
const unboundEtcDir = "/unbound"
const unboundPath = "/usr/sbin/unbound"
commander.EXPECT().Run(ctx, unboundPath, "-V").
Return(tc.runOutput, tc.runErr).Times(1)
c := &configurator{
unboundDir: unboundDir,
commander: commander,
commander: commander,
unboundEtcDir: unboundEtcDir,
unboundPath: unboundPath,
}
version, err := c.Version(ctx)
if tc.err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/unbound/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import (

func (c *configurator) MakeUnboundConf(settings models.Settings,
hostnamesLines, ipsLines []string, username string, puid, pgid int) (err error) {
configFilepath := filepath.Join(c.unboundDir, unboundConfigFilename)
configFilepath := filepath.Join(c.unboundEtcDir, unboundConfigFilename)
file, err := c.openFile(configFilepath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return err
}

lines := generateUnboundConf(settings, hostnamesLines, ipsLines, c.unboundDir, username)
lines := generateUnboundConf(settings, hostnamesLines, ipsLines, c.unboundEtcDir, username)
_, err = file.WriteString(strings.Join(lines, "\n"))
if err != nil {
_ = file.Close()
Expand Down
1 change: 0 additions & 1 deletion pkg/unbound/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

const (
unboundConfigFilename = "unbound.conf"
unboundBinFilename = "unbound"
cacertsFilename = "ca-certificates.crt"
rootHints = "root.hints"
rootKey = "root.key"
Expand Down
24 changes: 13 additions & 11 deletions pkg/unbound/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,22 @@ type Configurator interface {
}

type configurator struct {
openFile os.OpenFileFunc
commander command.Commander
resolver *net.Resolver
dnscrypto dnscrypto.DNSCrypto
unboundDir string
openFile os.OpenFileFunc
commander command.Commander
resolver *net.Resolver
dnscrypto dnscrypto.DNSCrypto
unboundEtcDir string
unboundPath string
}

func NewConfigurator(logger logging.Logger, openFile os.OpenFileFunc,
dnscrypto dnscrypto.DNSCrypto, unboundDir string) Configurator {
dnscrypto dnscrypto.DNSCrypto, unboundEtcDir, unboundPath string) Configurator {
return &configurator{
openFile: openFile,
commander: command.NewCommander(),
resolver: net.DefaultResolver,
dnscrypto: dnscrypto,
unboundDir: unboundDir,
openFile: openFile,
commander: command.NewCommander(),
resolver: net.DefaultResolver,
dnscrypto: dnscrypto,
unboundEtcDir: unboundEtcDir,
unboundPath: unboundPath,
}
}
2 changes: 1 addition & 1 deletion pkg/unbound/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
const includeConfFilename = "include.conf"

func (c *configurator) createEmptyIncludeConf() error {
filepath := filepath.Join(c.unboundDir, includeConfFilename)
filepath := filepath.Join(c.unboundEtcDir, includeConfFilename)
file, err := c.openFile(filepath, os.O_CREATE, 0644)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions pkg/unbound/roots.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (c *configurator) downloadRootHints(ctx context.Context) error {
return err
}

filepath := filepath.Join(c.unboundDir, rootHints)
filepath := filepath.Join(c.unboundEtcDir, rootHints)
file, err := c.openFile(filepath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return err
Expand Down Expand Up @@ -57,7 +57,7 @@ func (c *configurator) downloadRootKeys(ctx context.Context) error {
return err
}

filepath := filepath.Join(c.unboundDir, rootKey)
filepath := filepath.Join(c.unboundEtcDir, rootKey)
file, err := c.openFile(filepath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return err
Expand Down

0 comments on commit 6668d4f

Please sign in to comment.