-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
rfc2136: add support for tsig-keygen generated file #2330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6db8efa
rfc2136: add support for tsig-keygen generated file
ldez b5abda2
chore: generate
ldez 3ec6afc
chore: open file instead of Readfile
ldez f22e147
fix: parsing
ldez 542c49f
chore: simplify
ldez f063de3
chore: simplify
ldez 3a1b0a9
docs: more realistic example
ldez cbd1922
chore: generate
ldez 02065e5
tests: windows...
ldez ed3792a
review: simplify
ldez 7d7cd86
review: apply
ldez 1355f75
review: simplify
ldez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
key "example.com" { | ||
algorithm; | ||
secret "TCG5A6/lOHUGbW0e/9RYYbzWDFMlj1pIxCvybLBayBg="; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
key { | ||
algorithm hmac-sha256; | ||
secret "TCG5A6/lOHUGbW0e/9RYYbzWDFMlj1pIxCvybLBayBg="; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
key "example.com" { | ||
secret "TCG5A6/lOHUGbW0e/9RYYbzWDFMlj1pIxCvybLBayBg="; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
key "example.com" { | ||
algorithm hmac-sha256; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
key "example.com" { | ||
algorithm hmac-sha256; | ||
secret "TCG5A6/lOHUGbW0e/9RYYbzWDFMlj1pIxCvybLBayBg="; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
key "example.com" { | ||
algorithm hmac-sha256; | ||
secret "TCG5A6/lOHUGbW0e/9RYYbzWDFMlj1pIxCvybLBayBg="; | ||
}; | ||
|
||
key "example.org" { | ||
algorithm hmac-sha512; | ||
secret "v6CkK3gop6HXj4+dcWiLXLGSYKVY5J1cTMjDsdl/Ah9B8aWfTgjwFBoHHyiHWSyvwWPDuEIRs2Pqm8nedca4+g=="; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
foo { | ||
bar example; | ||
}; | ||
|
||
key "example.com" { | ||
algorithm hmac-sha256; | ||
secret "TCG5A6/lOHUGbW0e/9RYYbzWDFMlj1pIxCvybLBayBg="; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# TSIG Key File | ||
|
||
How to generate example: | ||
|
||
```console | ||
$ docker run --rm -it -v $(pwd):/app -w /app alpine sh | ||
/app # apk add bind | ||
/app # tsig-keygen example.com > sample1.conf | ||
/app # tsig-keygen -a hmac-sha512 example.com > sample2.conf | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package internal | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/go-viper/mapstructure/v2" | ||
) | ||
|
||
type Key struct { | ||
Name string `mapstructure:"name"` | ||
Algorithm string `mapstructure:"algorithm"` | ||
Secret string `mapstructure:"secret"` | ||
} | ||
|
||
// ReadTSIGFile reads TSIG key file generated with `tsig-keygen`. | ||
func ReadTSIGFile(filename string) (*Key, error) { | ||
file, err := os.Open(filename) | ||
if err != nil { | ||
return nil, fmt.Errorf("open file: %w", err) | ||
} | ||
|
||
defer func() { _ = file.Close() }() | ||
|
||
data := make(map[string]string) | ||
|
||
var read bool | ||
|
||
scanner := bufio.NewScanner(file) | ||
for scanner.Scan() { | ||
line := strings.TrimSpace(strings.TrimSuffix(scanner.Text(), ";")) | ||
|
||
if line == "" { | ||
continue | ||
} | ||
|
||
if read && line == "}" { | ||
break | ||
} | ||
|
||
fields := strings.Fields(line) | ||
|
||
switch { | ||
case fields[0] == "key": | ||
read = true | ||
|
||
if len(fields) != 3 { | ||
return nil, fmt.Errorf("invalid key line: %s", line) | ||
} | ||
|
||
data["name"] = safeUnquote(fields[1]) | ||
|
||
case !read: | ||
continue | ||
|
||
default: | ||
if len(fields) != 2 { | ||
continue | ||
} | ||
|
||
data[safeUnquote(fields[0])] = safeUnquote(fields[1]) | ||
} | ||
} | ||
|
||
key := &Key{} | ||
err = mapstructure.Decode(data, key) | ||
if err != nil { | ||
return nil, fmt.Errorf("decode key: %w", err) | ||
} | ||
|
||
return key, nil | ||
} | ||
|
||
func safeUnquote(v string) string { | ||
if v == "" { | ||
return v | ||
} | ||
|
||
if len(v)-1 != 0 && v[0] == '"' && v[len(v)-1] == '"' { | ||
return v[1 : len(v)-1] | ||
} | ||
ldez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return v | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package internal | ||
|
||
import ( | ||
"path/filepath" | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestReadTSIGFile(t *testing.T) { | ||
testCases := []struct { | ||
desc string | ||
filename string | ||
expected *Key | ||
}{ | ||
{ | ||
desc: "basic", | ||
filename: "sample.conf", | ||
expected: &Key{Name: "example.com", Algorithm: "hmac-sha256", Secret: "TCG5A6/lOHUGbW0e/9RYYbzWDFMlj1pIxCvybLBayBg="}, | ||
}, | ||
{ | ||
desc: "data before the key", | ||
filename: "text_before.conf", | ||
expected: &Key{Name: "example.com", Algorithm: "hmac-sha256", Secret: "TCG5A6/lOHUGbW0e/9RYYbzWDFMlj1pIxCvybLBayBg="}, | ||
}, | ||
{ | ||
desc: "data after the key", | ||
filename: "text_after.conf", | ||
expected: &Key{Name: "example.com", Algorithm: "hmac-sha256", Secret: "TCG5A6/lOHUGbW0e/9RYYbzWDFMlj1pIxCvybLBayBg="}, | ||
}, | ||
{ | ||
desc: "ignore missing secret", | ||
filename: "missing_secret.conf", | ||
expected: &Key{Name: "example.com", Algorithm: "hmac-sha256"}, | ||
}, | ||
{ | ||
desc: "ignore missing algorithm", | ||
filename: "mising_algo.conf", | ||
expected: &Key{Name: "example.com", Secret: "TCG5A6/lOHUGbW0e/9RYYbzWDFMlj1pIxCvybLBayBg="}, | ||
}, | ||
{ | ||
desc: "ignore invalid field format", | ||
filename: "invalid_field.conf", | ||
expected: &Key{Name: "example.com", Secret: "TCG5A6/lOHUGbW0e/9RYYbzWDFMlj1pIxCvybLBayBg="}, | ||
}, | ||
} | ||
|
||
for _, test := range testCases { | ||
t.Run(test.desc, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
key, err := ReadTSIGFile(filepath.Join("fixtures", test.filename)) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, test.expected, key) | ||
}) | ||
} | ||
} | ||
|
||
func TestReadTSIGFile_error(t *testing.T) { | ||
if runtime.GOOS != "linux" { | ||
// Because error messages are different on Windows. | ||
t.Skip("only for UNIX systems") | ||
} | ||
|
||
testCases := []struct { | ||
desc string | ||
filename string | ||
expected string | ||
}{ | ||
{ | ||
desc: "missing file", | ||
filename: "missing.conf", | ||
expected: "open file: open fixtures/missing.conf: no such file or directory", | ||
}, | ||
{ | ||
desc: "invalid key format", | ||
filename: "invalid_key.conf", | ||
expected: "invalid key line: key {", | ||
}, | ||
} | ||
|
||
for _, test := range testCases { | ||
t.Run(test.desc, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
_, err := ReadTSIGFile(filepath.Join("fixtures", test.filename)) | ||
require.Error(t, err) | ||
|
||
require.EqualError(t, err, test.expected) | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.