-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2575 from hashicorp/pki-colons-to-hyphens
Change storage of PKI entries from colons to hyphens
- Loading branch information
Showing
8 changed files
with
176 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 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,124 @@ | ||
package pki | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"strings" | ||
|
||
"github.com/hashicorp/vault/logical" | ||
) | ||
|
||
func TestPki_FetchCertBySerial(t *testing.T) { | ||
storage := &logical.InmemStorage{} | ||
|
||
cases := map[string]struct { | ||
Req *logical.Request | ||
Prefix string | ||
Serial string | ||
}{ | ||
"valid cert": { | ||
&logical.Request{ | ||
Storage: storage, | ||
}, | ||
"certs/", | ||
"00:00:00:00:00:00:00:00", | ||
}, | ||
"revoked cert": { | ||
&logical.Request{ | ||
Storage: storage, | ||
}, | ||
"revoked/", | ||
"11:11:11:11:11:11:11:11", | ||
}, | ||
} | ||
|
||
// Test for colon-based paths in storage | ||
for name, tc := range cases { | ||
storageKey := fmt.Sprintf("%s%s", tc.Prefix, tc.Serial) | ||
err := storage.Put(&logical.StorageEntry{ | ||
Key: storageKey, | ||
Value: []byte("some data"), | ||
}) | ||
if err != nil { | ||
t.Fatalf("error writing to storage on %s colon-based storage path: %s", name, err) | ||
} | ||
|
||
certEntry, err := fetchCertBySerial(tc.Req, tc.Prefix, tc.Serial) | ||
if err != nil { | ||
t.Fatalf("error on %s for colon-based storage path: %s", name, err) | ||
} | ||
|
||
// Check for non-nil on valid/revoked certs | ||
if certEntry == nil { | ||
t.Fatalf("nil on %s for colon-based storage path", name) | ||
} | ||
|
||
// Ensure that cert serials are converted/updated after fetch | ||
expectedKey := tc.Prefix + normalizeSerial(tc.Serial) | ||
se, err := storage.Get(expectedKey) | ||
if err != nil { | ||
t.Fatalf("error on %s for colon-based storage path:%s", name, err) | ||
} | ||
if strings.Compare(expectedKey, se.Key) != 0 { | ||
t.Fatalf("expected: %s, got: %s", expectedKey, certEntry.Key) | ||
} | ||
} | ||
|
||
// Reset storage | ||
storage = &logical.InmemStorage{} | ||
|
||
// Test for hyphen-base paths in storage | ||
for name, tc := range cases { | ||
storageKey := tc.Prefix + normalizeSerial(tc.Serial) | ||
err := storage.Put(&logical.StorageEntry{ | ||
Key: storageKey, | ||
Value: []byte("some data"), | ||
}) | ||
if err != nil { | ||
t.Fatalf("error writing to storage on %s hyphen-based storage path: %s", name, err) | ||
} | ||
|
||
certEntry, err := fetchCertBySerial(tc.Req, tc.Prefix, tc.Serial) | ||
if err != nil || certEntry == nil { | ||
t.Fatalf("error on %s for hyphen-based storage path: err: %v, entry: %v", name, err, certEntry) | ||
} | ||
} | ||
|
||
noConvCases := map[string]struct { | ||
Req *logical.Request | ||
Prefix string | ||
Serial string | ||
}{ | ||
"ca": { | ||
&logical.Request{ | ||
Storage: storage, | ||
}, | ||
"", | ||
"ca", | ||
}, | ||
"crl": { | ||
&logical.Request{ | ||
Storage: storage, | ||
}, | ||
"", | ||
"crl", | ||
}, | ||
} | ||
|
||
// Test for ca and crl case | ||
for name, tc := range noConvCases { | ||
err := storage.Put(&logical.StorageEntry{ | ||
Key: tc.Serial, | ||
Value: []byte("some data"), | ||
}) | ||
if err != nil { | ||
t.Fatalf("error writing to storage on %s: %s", name, err) | ||
} | ||
|
||
certEntry, err := fetchCertBySerial(tc.Req, tc.Prefix, tc.Serial) | ||
if err != nil || certEntry == nil { | ||
t.Fatalf("error on %s: err: %v, entry: %v", name, err, certEntry) | ||
} | ||
} | ||
} |
This file contains 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 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 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 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 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 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,7 @@ | ||
package pki | ||
|
||
import "strings" | ||
|
||
func normalizeSerial(serial string) string { | ||
return strings.Replace(strings.ToLower(serial), ":", "-", -1) | ||
} |