origin_cert_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package credentials
  2. import (
  3. "fmt"
  4. "io/fs"
  5. "os"
  6. "path"
  7. "testing"
  8. "github.com/rs/zerolog"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. )
  12. const (
  13. originCertFile = "cert.pem"
  14. )
  15. var (
  16. nopLog = zerolog.Nop().With().Logger()
  17. )
  18. func TestLoadOriginCert(t *testing.T) {
  19. cert, err := decodeOriginCert([]byte{})
  20. assert.Equal(t, fmt.Errorf("Cannot decode empty certificate"), err)
  21. assert.Nil(t, cert)
  22. blocks, err := os.ReadFile("test-cert-unknown-block.pem")
  23. assert.NoError(t, err)
  24. cert, err = decodeOriginCert(blocks)
  25. assert.Equal(t, fmt.Errorf("Unknown block RSA PRIVATE KEY in the certificate"), err)
  26. assert.Nil(t, cert)
  27. }
  28. func TestJSONArgoTunnelTokenEmpty(t *testing.T) {
  29. blocks, err := os.ReadFile("test-cert-no-token.pem")
  30. assert.NoError(t, err)
  31. cert, err := decodeOriginCert(blocks)
  32. assert.Equal(t, fmt.Errorf("Missing token in the certificate"), err)
  33. assert.Nil(t, cert)
  34. }
  35. func TestJSONArgoTunnelToken(t *testing.T) {
  36. // The given cert's Argo Tunnel Token was generated by base64 encoding this JSON:
  37. // {
  38. // "zoneID": "7b0a4d77dfb881c1a3b7d61ea9443e19",
  39. // "apiToken": "test-service-key",
  40. // "accountID": "abcdabcdabcdabcd1234567890abcdef"
  41. // }
  42. CloudflareTunnelTokenTest(t, "test-cloudflare-tunnel-cert-json.pem")
  43. }
  44. func CloudflareTunnelTokenTest(t *testing.T, path string) {
  45. blocks, err := os.ReadFile(path)
  46. assert.NoError(t, err)
  47. cert, err := decodeOriginCert(blocks)
  48. assert.NoError(t, err)
  49. assert.NotNil(t, cert)
  50. assert.Equal(t, "7b0a4d77dfb881c1a3b7d61ea9443e19", cert.ZoneID)
  51. key := "test-service-key"
  52. assert.Equal(t, key, cert.APIToken)
  53. }
  54. type mockFile struct {
  55. path string
  56. data []byte
  57. err error
  58. }
  59. type mockFileSystem struct {
  60. files map[string]mockFile
  61. }
  62. func newMockFileSystem(files ...mockFile) *mockFileSystem {
  63. fs := mockFileSystem{map[string]mockFile{}}
  64. for _, f := range files {
  65. fs.files[f.path] = f
  66. }
  67. return &fs
  68. }
  69. func (fs *mockFileSystem) ReadFile(path string) ([]byte, error) {
  70. if f, ok := fs.files[path]; ok {
  71. return f.data, f.err
  72. }
  73. return nil, os.ErrNotExist
  74. }
  75. func (fs *mockFileSystem) ValidFilePath(path string) bool {
  76. _, exists := fs.files[path]
  77. return exists
  78. }
  79. func TestFindOriginCert_Valid(t *testing.T) {
  80. file, err := os.ReadFile("test-cloudflare-tunnel-cert-json.pem")
  81. require.NoError(t, err)
  82. dir := t.TempDir()
  83. certPath := path.Join(dir, originCertFile)
  84. os.WriteFile(certPath, file, fs.ModePerm)
  85. path, err := FindOriginCert(certPath, &nopLog)
  86. require.NoError(t, err)
  87. require.Equal(t, certPath, path)
  88. }
  89. func TestFindOriginCert_Missing(t *testing.T) {
  90. dir := t.TempDir()
  91. certPath := path.Join(dir, originCertFile)
  92. _, err := FindOriginCert(certPath, &nopLog)
  93. require.Error(t, err)
  94. }