origin_cert.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package credentials
  2. import (
  3. "encoding/json"
  4. "encoding/pem"
  5. "fmt"
  6. "os"
  7. "path/filepath"
  8. "github.com/mitchellh/go-homedir"
  9. "github.com/rs/zerolog"
  10. "github.com/cloudflare/cloudflared/config"
  11. )
  12. const (
  13. DefaultCredentialFile = "cert.pem"
  14. )
  15. type namedTunnelToken struct {
  16. ZoneID string `json:"zoneID"`
  17. AccountID string `json:"accountID"`
  18. APIToken string `json:"apiToken"`
  19. }
  20. type OriginCert struct {
  21. ZoneID string
  22. APIToken string
  23. AccountID string
  24. }
  25. // FindDefaultOriginCertPath returns the first path that contains a cert.pem file. If none of the
  26. // DefaultConfigSearchDirectories contains a cert.pem file, return empty string
  27. func FindDefaultOriginCertPath() string {
  28. for _, defaultConfigDir := range config.DefaultConfigSearchDirectories() {
  29. originCertPath, _ := homedir.Expand(filepath.Join(defaultConfigDir, DefaultCredentialFile))
  30. if ok := fileExists(originCertPath); ok {
  31. return originCertPath
  32. }
  33. }
  34. return ""
  35. }
  36. func decodeOriginCert(blocks []byte) (*OriginCert, error) {
  37. if len(blocks) == 0 {
  38. return nil, fmt.Errorf("Cannot decode empty certificate")
  39. }
  40. originCert := OriginCert{}
  41. block, rest := pem.Decode(blocks)
  42. for {
  43. if block == nil {
  44. break
  45. }
  46. switch block.Type {
  47. case "PRIVATE KEY", "CERTIFICATE":
  48. // this is for legacy purposes.
  49. break
  50. case "ARGO TUNNEL TOKEN":
  51. if originCert.ZoneID != "" || originCert.APIToken != "" {
  52. return nil, fmt.Errorf("Found multiple tokens in the certificate")
  53. }
  54. // The token is a string,
  55. // Try the newer JSON format
  56. ntt := namedTunnelToken{}
  57. if err := json.Unmarshal(block.Bytes, &ntt); err == nil {
  58. originCert.ZoneID = ntt.ZoneID
  59. originCert.APIToken = ntt.APIToken
  60. originCert.AccountID = ntt.AccountID
  61. }
  62. default:
  63. return nil, fmt.Errorf("Unknown block %s in the certificate", block.Type)
  64. }
  65. block, rest = pem.Decode(rest)
  66. }
  67. if originCert.ZoneID == "" || originCert.APIToken == "" {
  68. return nil, fmt.Errorf("Missing token in the certificate")
  69. }
  70. return &originCert, nil
  71. }
  72. func readOriginCert(originCertPath string) ([]byte, error) {
  73. originCert, err := os.ReadFile(originCertPath)
  74. if err != nil {
  75. return nil, fmt.Errorf("cannot read %s to load origin certificate", originCertPath)
  76. }
  77. return originCert, nil
  78. }
  79. // FindOriginCert will check to make sure that the certificate exists at the specified file path.
  80. func FindOriginCert(originCertPath string, log *zerolog.Logger) (string, error) {
  81. if originCertPath == "" {
  82. log.Error().Msgf("Cannot determine default origin certificate path. No file %s in %v. You need to specify the origin certificate path by specifying the origincert option in the configuration file, or set TUNNEL_ORIGIN_CERT environment variable", DefaultCredentialFile, config.DefaultConfigSearchDirectories())
  83. return "", fmt.Errorf("client didn't specify origincert path")
  84. }
  85. var err error
  86. originCertPath, err = homedir.Expand(originCertPath)
  87. if err != nil {
  88. log.Err(err).Msgf("Cannot resolve origin certificate path")
  89. return "", fmt.Errorf("cannot resolve path %s", originCertPath)
  90. }
  91. // Check that the user has acquired a certificate using the login command
  92. ok := fileExists(originCertPath)
  93. if !ok {
  94. log.Error().Msgf(`Cannot find a valid certificate for your origin at the path:
  95. %s
  96. If the path above is wrong, specify the path with the -origincert option.
  97. If you don't have a certificate signed by Cloudflare, run the command:
  98. cloudflared login
  99. `, originCertPath)
  100. return "", fmt.Errorf("cannot find a valid certificate at the path %s", originCertPath)
  101. }
  102. return originCertPath, nil
  103. }
  104. // FileExists checks to see if a file exist at the provided path.
  105. func fileExists(path string) bool {
  106. fileStat, err := os.Stat(path)
  107. if err != nil {
  108. return false
  109. }
  110. return !fileStat.IsDir()
  111. }