| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779 |
- package policy
- import (
- "net"
- "testing"
- "github.com/google/go-cmp/cmp"
- "github.com/stretchr/testify/assert"
- )
- func Test_normalizeAndValidateCommonName(t *testing.T) {
- tests := []struct {
- name string
- constraint string
- want string
- wantErr bool
- }{
- {
- name: "fail/empty-constraint",
- constraint: "",
- want: "",
- wantErr: true,
- },
- {
- name: "fail/wildcard",
- constraint: "*",
- want: "",
- wantErr: true,
- },
- {
- name: "ok",
- constraint: "step",
- want: "step",
- wantErr: false,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got, err := normalizeAndValidateCommonName(tt.constraint)
- if (err != nil) != tt.wantErr {
- t.Errorf("normalizeAndValidateCommonName() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if got != tt.want {
- t.Errorf("normalizeAndValidateCommonName() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func Test_normalizeAndValidateDNSDomainConstraint(t *testing.T) {
- tests := []struct {
- name string
- constraint string
- want string
- wantErr bool
- }{
- {
- name: "fail/empty-constraint",
- constraint: "",
- want: "",
- wantErr: true,
- },
- {
- name: "fail/wildcard-partial-label",
- constraint: "*xxxx.local",
- want: "",
- wantErr: true,
- },
- {
- name: "fail/wildcard-in-the-middle",
- constraint: "x.*.local",
- want: "",
- wantErr: true,
- },
- {
- name: "fail/empty-label",
- constraint: "..local",
- want: "",
- wantErr: true,
- },
- {
- name: "fail/empty-reverse",
- constraint: ".",
- want: "",
- wantErr: true,
- },
- {
- name: "fail/no-asterisk",
- constraint: ".example.com",
- want: "",
- wantErr: true,
- },
- {
- name: "fail/idna-internationalized-domain-name-lookup",
- constraint: `\00.local`, // invalid IDNA ASCII character
- want: "",
- wantErr: true,
- },
- {
- name: "ok/wildcard",
- constraint: "*.local",
- want: ".local",
- wantErr: false,
- },
- {
- name: "ok/specific-domain",
- constraint: "example.local",
- want: "example.local",
- wantErr: false,
- },
- {
- name: "ok/idna-internationalized-domain-name-punycode",
- constraint: "*.xn--fsq.jp", // Example value from https://www.w3.org/International/articles/idn-and-iri/
- want: ".xn--fsq.jp",
- wantErr: false,
- },
- {
- name: "ok/idna-internationalized-domain-name-lookup-transformed",
- constraint: "*.例.jp", // Example value from https://www.w3.org/International/articles/idn-and-iri/
- want: ".xn--fsq.jp",
- wantErr: false,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got, err := normalizeAndValidateDNSDomainConstraint(tt.constraint)
- if (err != nil) != tt.wantErr {
- t.Errorf("normalizeAndValidateDNSDomainConstraint() error = %v, wantErr %v", err, tt.wantErr)
- }
- if got != tt.want {
- t.Errorf("normalizeAndValidateDNSDomainConstraint() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func Test_normalizeAndValidateEmailConstraint(t *testing.T) {
- tests := []struct {
- name string
- constraint string
- want string
- wantErr bool
- }{
- {
- name: "fail/empty-constraint",
- constraint: "",
- want: "",
- wantErr: true,
- },
- {
- name: "fail/asterisk",
- constraint: "*.local",
- want: "",
- wantErr: true,
- },
- {
- name: "fail/period",
- constraint: ".local",
- want: "",
- wantErr: true,
- },
- {
- name: "fail/@period",
- constraint: "@.local",
- want: "",
- wantErr: true,
- },
- {
- name: "fail/too-many-@s",
- constraint: "@local@example.com",
- want: "",
- wantErr: true,
- },
- {
- name: "fail/parse-mailbox",
- constraint: "mail@example.com" + string(byte(0)),
- want: "",
- wantErr: true,
- },
- {
- name: "fail/idna-internationalized-domain",
- constraint: "mail@xn--bla.local",
- want: "",
- wantErr: true,
- },
- {
- name: "fail/idna-internationalized-domain-name-lookup",
- constraint: `\00local`,
- want: "",
- wantErr: true,
- },
- {
- name: "fail/parse-domain",
- constraint: "x..example.com",
- want: "",
- wantErr: true,
- },
- {
- name: "ok/wildcard",
- constraint: "@local",
- want: "local",
- wantErr: false,
- },
- {
- name: "ok/specific-mail",
- constraint: "mail@local",
- want: "mail@local",
- wantErr: false,
- },
- // TODO(hs): fix the below; doesn't get past parseRFC2821Mailbox; I think it should be allowed.
- // {
- // name: "ok/idna-internationalized-local",
- // constraint: `bücher@local`,
- // want: "bücher@local",
- // wantErr: false,
- // },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got, err := normalizeAndValidateEmailConstraint(tt.constraint)
- if (err != nil) != tt.wantErr {
- t.Errorf("normalizeAndValidateEmailConstraint() error = %v, wantErr %v", err, tt.wantErr)
- }
- if got != tt.want {
- t.Errorf("normalizeAndValidateEmailConstraint() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func TestNew(t *testing.T) {
- type test struct {
- options []NamePolicyOption
- want *NamePolicyEngine
- wantErr bool
- }
- var tests = map[string]func(t *testing.T) test{
- "fail/with-permitted-common-name": func(t *testing.T) test {
- return test{
- options: []NamePolicyOption{
- WithPermittedCommonNames("*"),
- },
- want: nil,
- wantErr: true,
- }
- },
- "fail/with-excluded-common-name": func(t *testing.T) test {
- return test{
- options: []NamePolicyOption{
- WithExcludedCommonNames(""),
- },
- want: nil,
- wantErr: true,
- }
- },
- "fail/with-permitted-dns-domains": func(t *testing.T) test {
- return test{
- options: []NamePolicyOption{
- WithPermittedDNSDomains("**.local"),
- },
- want: nil,
- wantErr: true,
- }
- },
- "fail/with-excluded-dns-domains": func(t *testing.T) test {
- return test{
- options: []NamePolicyOption{
- WithExcludedDNSDomains("**.local"),
- },
- want: nil,
- wantErr: true,
- }
- },
- "fail/with-permitted-cidrs": func(t *testing.T) test {
- return test{
- options: []NamePolicyOption{
- WithPermittedCIDRs("127.0.0.1//24"),
- },
- want: nil,
- wantErr: true,
- }
- },
- "fail/with-excluded-cidrs": func(t *testing.T) test {
- return test{
- options: []NamePolicyOption{
- WithExcludedCIDRs("127.0.0.1//24"),
- },
- want: nil,
- wantErr: true,
- }
- },
- "fail/with-permitted-ipsOrCIDRs-cidr": func(t *testing.T) test {
- return test{
- options: []NamePolicyOption{
- WithPermittedIPsOrCIDRs("127.0.0.1//24"),
- },
- want: nil,
- wantErr: true,
- }
- },
- "fail/with-permitted-ipsOrCIDRs-ip": func(t *testing.T) test {
- return test{
- options: []NamePolicyOption{
- WithPermittedIPsOrCIDRs("127.0.0:1"),
- },
- want: nil,
- wantErr: true,
- }
- },
- "fail/with-excluded-ipsOrCIDRs-cidr": func(t *testing.T) test {
- return test{
- options: []NamePolicyOption{
- WithExcludedIPsOrCIDRs("127.0.0.1//24"),
- },
- want: nil,
- wantErr: true,
- }
- },
- "fail/with-excluded-ipsOrCIDRs-ip": func(t *testing.T) test {
- return test{
- options: []NamePolicyOption{
- WithExcludedIPsOrCIDRs("127.0.0:1"),
- },
- want: nil,
- wantErr: true,
- }
- },
- "fail/with-permitted-emails": func(t *testing.T) test {
- return test{
- options: []NamePolicyOption{
- WithPermittedEmailAddresses("*.local"),
- },
- want: nil,
- wantErr: true,
- }
- },
- "fail/with-excluded-emails": func(t *testing.T) test {
- return test{
- options: []NamePolicyOption{
- WithExcludedEmailAddresses("*.local"),
- },
- want: nil,
- wantErr: true,
- }
- },
- "fail/with-permitted-uris": func(t *testing.T) test {
- return test{
- options: []NamePolicyOption{
- WithPermittedURIDomains("**.local"),
- },
- want: nil,
- wantErr: true,
- }
- },
- "fail/with-excluded-uris": func(t *testing.T) test {
- return test{
- options: []NamePolicyOption{
- WithExcludedURIDomains("**.local"),
- },
- want: nil,
- wantErr: true,
- }
- },
- "ok/default": func(t *testing.T) test {
- return test{
- options: []NamePolicyOption{},
- want: &NamePolicyEngine{},
- wantErr: false,
- }
- },
- "ok/subject-verification": func(t *testing.T) test {
- options := []NamePolicyOption{
- WithSubjectCommonNameVerification(),
- }
- return test{
- options: options,
- want: &NamePolicyEngine{
- verifySubjectCommonName: true,
- },
- wantErr: false,
- }
- },
- "ok/literal-wildcards": func(t *testing.T) test {
- options := []NamePolicyOption{
- WithAllowLiteralWildcardNames(),
- }
- return test{
- options: options,
- want: &NamePolicyEngine{
- allowLiteralWildcardNames: true,
- },
- wantErr: false,
- }
- },
- "ok/with-permitted-dns-wildcard-domains": func(t *testing.T) test {
- options := []NamePolicyOption{
- WithPermittedDNSDomains("*.local", "*.example.com"),
- }
- return test{
- options: options,
- want: &NamePolicyEngine{
- permittedDNSDomains: []string{".local", ".example.com"},
- numberOfDNSDomainConstraints: 2,
- totalNumberOfPermittedConstraints: 2,
- totalNumberOfConstraints: 2,
- },
- wantErr: false,
- }
- },
- "ok/with-excluded-dns-domains": func(t *testing.T) test {
- options := []NamePolicyOption{
- WithExcludedDNSDomains("*.local", "*.example.com"),
- }
- return test{
- options: options,
- want: &NamePolicyEngine{
- excludedDNSDomains: []string{".local", ".example.com"},
- numberOfDNSDomainConstraints: 2,
- totalNumberOfExcludedConstraints: 2,
- totalNumberOfConstraints: 2,
- },
- wantErr: false,
- }
- },
- "ok/with-permitted-ip-ranges": func(t *testing.T) test {
- _, nw1, err := net.ParseCIDR("127.0.0.1/24")
- assert.NoError(t, err)
- _, nw2, err := net.ParseCIDR("192.168.0.1/24")
- assert.NoError(t, err)
- options := []NamePolicyOption{
- WithPermittedIPRanges(nw1, nw2),
- }
- return test{
- options: options,
- want: &NamePolicyEngine{
- permittedIPRanges: []*net.IPNet{
- nw1, nw2,
- },
- numberOfIPRangeConstraints: 2,
- totalNumberOfPermittedConstraints: 2,
- totalNumberOfConstraints: 2,
- },
- wantErr: false,
- }
- },
- "ok/with-excluded-ip-ranges": func(t *testing.T) test {
- _, nw1, err := net.ParseCIDR("127.0.0.1/24")
- assert.NoError(t, err)
- _, nw2, err := net.ParseCIDR("192.168.0.1/24")
- assert.NoError(t, err)
- options := []NamePolicyOption{
- WithExcludedIPRanges(nw1, nw2),
- }
- return test{
- options: options,
- want: &NamePolicyEngine{
- excludedIPRanges: []*net.IPNet{
- nw1, nw2,
- },
- numberOfIPRangeConstraints: 2,
- totalNumberOfExcludedConstraints: 2,
- totalNumberOfConstraints: 2,
- },
- wantErr: false,
- }
- },
- "ok/with-permitted-cidrs": func(t *testing.T) test {
- _, nw1, err := net.ParseCIDR("127.0.0.1/24")
- assert.NoError(t, err)
- _, nw2, err := net.ParseCIDR("192.168.0.1/24")
- assert.NoError(t, err)
- options := []NamePolicyOption{
- WithPermittedCIDRs("127.0.0.1/24", "192.168.0.1/24"),
- }
- return test{
- options: options,
- want: &NamePolicyEngine{
- permittedIPRanges: []*net.IPNet{
- nw1, nw2,
- },
- numberOfIPRangeConstraints: 2,
- totalNumberOfPermittedConstraints: 2,
- totalNumberOfConstraints: 2,
- },
- wantErr: false,
- }
- },
- "ok/with-excluded-cidrs": func(t *testing.T) test {
- _, nw1, err := net.ParseCIDR("127.0.0.1/24")
- assert.NoError(t, err)
- _, nw2, err := net.ParseCIDR("192.168.0.1/24")
- assert.NoError(t, err)
- options := []NamePolicyOption{
- WithExcludedCIDRs("127.0.0.1/24", "192.168.0.1/24"),
- }
- return test{
- options: options,
- want: &NamePolicyEngine{
- excludedIPRanges: []*net.IPNet{
- nw1, nw2,
- },
- numberOfIPRangeConstraints: 2,
- totalNumberOfExcludedConstraints: 2,
- totalNumberOfConstraints: 2,
- },
- wantErr: false,
- }
- },
- "ok/with-permitted-ipsOrCIDRs-cidr": func(t *testing.T) test {
- _, nw1, err := net.ParseCIDR("127.0.0.1/24")
- assert.NoError(t, err)
- _, nw2, err := net.ParseCIDR("192.168.0.31/32")
- assert.NoError(t, err)
- _, nw3, err := net.ParseCIDR("2001:0db8:85a3:0000:0000:8a2e:0370:7334/128")
- assert.NoError(t, err)
- options := []NamePolicyOption{
- WithPermittedIPsOrCIDRs("127.0.0.1/24", "192.168.0.31", "2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
- }
- return test{
- options: options,
- want: &NamePolicyEngine{
- permittedIPRanges: []*net.IPNet{
- nw1, nw2, nw3,
- },
- numberOfIPRangeConstraints: 3,
- totalNumberOfPermittedConstraints: 3,
- totalNumberOfConstraints: 3,
- },
- wantErr: false,
- }
- },
- "ok/with-excluded-ipsOrCIDRs-cidr": func(t *testing.T) test {
- _, nw1, err := net.ParseCIDR("127.0.0.1/24")
- assert.NoError(t, err)
- _, nw2, err := net.ParseCIDR("192.168.0.31/32")
- assert.NoError(t, err)
- _, nw3, err := net.ParseCIDR("2001:0db8:85a3:0000:0000:8a2e:0370:7334/128")
- assert.NoError(t, err)
- options := []NamePolicyOption{
- WithExcludedIPsOrCIDRs("127.0.0.1/24", "192.168.0.31", "2001:0db8:85a3:0000:0000:8a2e:0370:7334"),
- }
- return test{
- options: options,
- want: &NamePolicyEngine{
- excludedIPRanges: []*net.IPNet{
- nw1, nw2, nw3,
- },
- numberOfIPRangeConstraints: 3,
- totalNumberOfExcludedConstraints: 3,
- totalNumberOfConstraints: 3,
- },
- wantErr: false,
- }
- },
- "ok/with-permitted-emails": func(t *testing.T) test {
- options := []NamePolicyOption{
- WithPermittedEmailAddresses("mail@local", "@example.com"),
- }
- return test{
- options: options,
- want: &NamePolicyEngine{
- permittedEmailAddresses: []string{"mail@local", "example.com"},
- numberOfEmailAddressConstraints: 2,
- totalNumberOfPermittedConstraints: 2,
- totalNumberOfConstraints: 2,
- },
- wantErr: false,
- }
- },
- "ok/with-excluded-emails": func(t *testing.T) test {
- options := []NamePolicyOption{
- WithExcludedEmailAddresses("mail@local", "@example.com"),
- }
- return test{
- options: options,
- want: &NamePolicyEngine{
- excludedEmailAddresses: []string{"mail@local", "example.com"},
- numberOfEmailAddressConstraints: 2,
- totalNumberOfExcludedConstraints: 2,
- totalNumberOfConstraints: 2,
- },
- wantErr: false,
- }
- },
- "ok/with-permitted-uris": func(t *testing.T) test {
- options := []NamePolicyOption{
- WithPermittedURIDomains("host.local", "*.example.com"),
- }
- return test{
- options: options,
- want: &NamePolicyEngine{
- permittedURIDomains: []string{"host.local", ".example.com"},
- numberOfURIDomainConstraints: 2,
- totalNumberOfPermittedConstraints: 2,
- totalNumberOfConstraints: 2,
- },
- wantErr: false,
- }
- },
- "ok/with-excluded-uris": func(t *testing.T) test {
- options := []NamePolicyOption{
- WithExcludedURIDomains("host.local", "*.example.com"),
- }
- return test{
- options: options,
- want: &NamePolicyEngine{
- excludedURIDomains: []string{"host.local", ".example.com"},
- numberOfURIDomainConstraints: 2,
- totalNumberOfExcludedConstraints: 2,
- totalNumberOfConstraints: 2,
- },
- wantErr: false,
- }
- },
- "ok/with-permitted-principals": func(t *testing.T) test {
- options := []NamePolicyOption{
- WithPermittedPrincipals("root", "ops"),
- }
- return test{
- options: options,
- want: &NamePolicyEngine{
- permittedPrincipals: []string{"root", "ops"},
- numberOfPrincipalConstraints: 2,
- totalNumberOfPermittedConstraints: 2,
- totalNumberOfConstraints: 2,
- },
- wantErr: false,
- }
- },
- "ok/with-excluded-principals": func(t *testing.T) test {
- options := []NamePolicyOption{
- WithExcludedPrincipals("root", "ops"),
- }
- return test{
- options: options,
- want: &NamePolicyEngine{
- excludedPrincipals: []string{"root", "ops"},
- numberOfPrincipalConstraints: 2,
- totalNumberOfExcludedConstraints: 2,
- totalNumberOfConstraints: 2,
- },
- wantErr: false,
- }
- },
- }
- for name, prep := range tests {
- tc := prep(t)
- t.Run(name, func(t *testing.T) {
- got, err := New(tc.options...)
- if (err != nil) != tc.wantErr {
- t.Errorf("New() error = %v, wantErr %v", err, tc.wantErr)
- return
- }
- if !cmp.Equal(tc.want, got, cmp.AllowUnexported(NamePolicyEngine{})) {
- t.Errorf("New() diff =\n %s", cmp.Diff(tc.want, got, cmp.AllowUnexported(NamePolicyEngine{})))
- }
- })
- }
- }
- func Test_normalizeAndValidateURIDomainConstraint(t *testing.T) {
- tests := []struct {
- name string
- constraint string
- want string
- wantErr bool
- }{
- {
- name: "fail/empty-constraint",
- constraint: "",
- want: "",
- wantErr: true,
- },
- {
- name: "fail/scheme-https",
- constraint: `https://*.local`,
- want: "",
- wantErr: true,
- },
- {
- name: "fail/too-many-asterisks",
- constraint: "**.local",
- want: "",
- wantErr: true,
- },
- {
- name: "fail/empty-label",
- constraint: "..local",
- want: "",
- wantErr: true,
- },
- {
- name: "fail/empty-reverse",
- constraint: ".",
- want: "",
- wantErr: true,
- },
- {
- name: "fail/domain-with-port",
- constraint: "host.local:8443",
- want: "",
- wantErr: true,
- },
- {
- name: "fail/no-asterisk",
- constraint: ".example.com",
- want: "",
- wantErr: true,
- },
- {
- name: "fail/ipv4",
- constraint: "127.0.0.1",
- want: "",
- wantErr: true,
- },
- {
- name: "fail/ipv6-brackets",
- constraint: "[::1]",
- want: "",
- wantErr: true,
- },
- {
- name: "fail/ipv6-no-brackets",
- constraint: "::1",
- want: "",
- wantErr: true,
- },
- {
- name: "fail/ipv6-no-brackets",
- constraint: "[::1",
- want: "",
- wantErr: true,
- },
- {
- name: "fail/idna-internationalized-domain-name-lookup",
- constraint: `\00local`,
- want: "",
- wantErr: true,
- },
- {
- name: "ok/wildcard",
- constraint: "*.local",
- want: ".local",
- wantErr: false,
- },
- {
- name: "ok/specific-domain",
- constraint: "example.local",
- want: "example.local",
- wantErr: false,
- },
- {
- name: "ok/idna-internationalized-domain-name-lookup",
- constraint: `*.bücher.example.com`,
- want: ".xn--bcher-kva.example.com",
- wantErr: false,
- },
- {
- // IDNA2003 vs. 2008 deviation: https://unicode.org/reports/tr46/#Deviations results
- // in a difference between Go 1.18 and lower versions. Go 1.18 expects ".xn--fa-hia.de"; not .fass.de.
- name: "ok/idna-internationalized-domain-name-lookup-deviation",
- constraint: `*.faß.de`,
- want: ".xn--fa-hia.de",
- wantErr: false,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got, err := normalizeAndValidateURIDomainConstraint(tt.constraint)
- if (err != nil) != tt.wantErr {
- t.Errorf("normalizeAndValidateURIDomainConstraint() error = %v, wantErr %v", err, tt.wantErr)
- }
- if got != tt.want {
- t.Errorf("normalizeAndValidateURIDomainConstraint() = %v, want %v", got, tt.want)
- }
- })
- }
- }
|