pubkeypin_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package pubkeypin
  14. import (
  15. "crypto/x509"
  16. "encoding/pem"
  17. "strings"
  18. "testing"
  19. )
  20. // testCertPEM is a simple self-signed test certificate issued with the openssl CLI:
  21. // openssl req -new -newkey rsa:2048 -days 36500 -nodes -x509 -keyout /dev/null -out test.crt
  22. const testCertPEM = `
  23. -----BEGIN CERTIFICATE-----
  24. MIIDRDCCAiygAwIBAgIJAJgVaCXvC6HkMA0GCSqGSIb3DQEBBQUAMB8xHTAbBgNV
  25. BAMTFGt1YmVhZG0ta2V5cGlucy10ZXN0MCAXDTE3MDcwNTE3NDMxMFoYDzIxMTcw
  26. NjExMTc0MzEwWjAfMR0wGwYDVQQDExRrdWJlYWRtLWtleXBpbnMtdGVzdDCCASIw
  27. DQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK0ba8mHU9UtYlzM1Own2Fk/XGjR
  28. J4uJQvSeGLtz1hID1IA0dLwruvgLCPadXEOw/f/IWIWcmT+ZmvIHZKa/woq2iHi5
  29. +HLhXs7aG4tjKGLYhag1hLjBI7icqV7ovkjdGAt9pWkxEzhIYClFMXDjKpMSynu+
  30. YX6nZ9tic1cOkHmx2yiZdMkuriRQnpTOa7bb03OC1VfGl7gHlOAIYaj4539WCOr8
  31. +ACTUMJUFEHcRZ2o8a/v6F9GMK+7SC8SJUI+GuroXqlMAdhEv4lX5Co52enYaClN
  32. +D9FJLRpBv2YfiCQdJRaiTvCBSxEFz6BN+PtP5l2Hs703ZWEkOqCByM6HV8CAwEA
  33. AaOBgDB+MB0GA1UdDgQWBBRQgUX8MhK2rWBWQiPHWcKzoWDH5DBPBgNVHSMESDBG
  34. gBRQgUX8MhK2rWBWQiPHWcKzoWDH5KEjpCEwHzEdMBsGA1UEAxMUa3ViZWFkbS1r
  35. ZXlwaW5zLXRlc3SCCQCYFWgl7wuh5DAMBgNVHRMEBTADAQH/MA0GCSqGSIb3DQEB
  36. BQUAA4IBAQCaAUif7Pfx3X0F08cxhx8/Hdx4jcJw6MCq6iq6rsXM32ge43t8OHKC
  37. pJW08dk58a3O1YQSMMvD6GJDAiAfXzfwcwY6j258b1ZlI9Ag0VokvhMl/XfdCsdh
  38. AWImnL1t4hvU5jLaImUUMlYxMcSfHBGAm7WJIZ2LdEfg6YWfZh+WGbg1W7uxLxk6
  39. y4h5rWdNnzBHWAGf7zJ0oEDV6W6RSwNXtC0JNnLaeIUm/6xdSddJlQPwUv8YH4jX
  40. c1vuFqTnJBPcb7W//R/GI2Paicm1cmns9NLnPR35exHxFTy+D1yxmGokpoPMdife
  41. aH+sfuxT8xeTPb3kjzF9eJTlnEquUDLM
  42. -----END CERTIFICATE-----`
  43. // expectedHash can be verified using the openssl CLI.
  44. const expectedHash = `sha256:345959acb2c3b2feb87d281961c893f62a314207ef02599f1cc4a5fb255480b3`
  45. // testCert2PEM is a second test cert generated the same way as testCertPEM
  46. const testCert2PEM = `
  47. -----BEGIN CERTIFICATE-----
  48. MIID9jCCAt6gAwIBAgIJAN5MXZDic7qYMA0GCSqGSIb3DQEBBQUAMFkxCzAJBgNV
  49. BAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBX
  50. aWRnaXRzIFB0eSBMdGQxEjAQBgNVBAMTCXRl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iju4jeovI1GA38TvH7MgYln6vMJ+FbgOXj7XCpek7dQiY
  67. KGaeSSH218mGNQaWRQw2Sm3W6cFdANoCJUph4w18s7gjtFpfV63s80hXRps+vEyv
  68. jEQMEQpG8Ss7HGJLGLBw/xAmG0e//XS/o2dDonbGbvzToFByz8OGxjMhk6yV6hdd
  69. +iyvsLAw/MYMSA==
  70. -----END CERTIFICATE-----
  71. `
  72. // testCert is a small helper to get a test x509.Certificate from the PEM constants
  73. func testCert(t *testing.T, pemString string) *x509.Certificate {
  74. // Decode the example certificate from a PEM file into a PEM block
  75. pemBlock, _ := pem.Decode([]byte(pemString))
  76. if pemBlock == nil {
  77. t.Fatal("failed to parse test certificate PEM")
  78. return nil
  79. }
  80. // Parse the PEM block into an x509.Certificate
  81. result, err := x509.ParseCertificate(pemBlock.Bytes)
  82. if err != nil {
  83. t.Fatalf("failed to parse test certificate: %v", err)
  84. return nil
  85. }
  86. return result
  87. }
  88. func TestSet(t *testing.T) {
  89. s := NewSet()
  90. if !s.Empty() {
  91. t.Error("expected a new set to be empty")
  92. return
  93. }
  94. err := s.Allow("xyz")
  95. if err == nil || !s.Empty() {
  96. t.Error("expected allowing junk to fail")
  97. return
  98. }
  99. err = s.Allow("0011223344")
  100. if err == nil || !s.Empty() {
  101. t.Error("expected allowing something too short to fail")
  102. return
  103. }
  104. err = s.Allow(expectedHash + expectedHash)
  105. if err == nil || !s.Empty() {
  106. t.Error("expected allowing something too long to fail")
  107. return
  108. }
  109. err = s.CheckAny([]*x509.Certificate{testCert(t, testCertPEM)})
  110. if err == nil {
  111. t.Error("expected test cert to not be allowed (yet)")
  112. return
  113. }
  114. err = s.Allow(strings.ToUpper(expectedHash))
  115. if err != nil || s.Empty() {
  116. t.Error("expected allowing uppercase expectedHash to succeed")
  117. return
  118. }
  119. err = s.CheckAny([]*x509.Certificate{testCert(t, testCertPEM)})
  120. if err != nil {
  121. t.Errorf("expected test cert to be allowed, but got back: %v", err)
  122. return
  123. }
  124. err = s.CheckAny([]*x509.Certificate{testCert(t, testCert2PEM)})
  125. if err == nil {
  126. t.Error("expected the second test cert to be disallowed")
  127. return
  128. }
  129. }
  130. func TestHash(t *testing.T) {
  131. actualHash := Hash(testCert(t, testCertPEM))
  132. if actualHash != expectedHash {
  133. t.Errorf(
  134. "failed to Hash() to the expected value\n\texpected: %q\n\t actual: %q",
  135. expectedHash,
  136. actualHash,
  137. )
  138. }
  139. }