sshgen_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //go:build !windows
  2. package sshgen
  3. import (
  4. "crypto/rand"
  5. "crypto/rsa"
  6. "encoding/json"
  7. "fmt"
  8. "io"
  9. "net/http"
  10. "net/http/httptest"
  11. "net/url"
  12. "os"
  13. "strings"
  14. "testing"
  15. "time"
  16. "github.com/go-jose/go-jose/v4"
  17. "github.com/go-jose/go-jose/v4/jwt"
  18. "github.com/stretchr/testify/assert"
  19. "github.com/cloudflare/cloudflared/config"
  20. cfpath "github.com/cloudflare/cloudflared/token"
  21. )
  22. const (
  23. audTest = "cf-test-aud"
  24. nonceTest = "asfd"
  25. )
  26. type signingArguments struct {
  27. Principals []string `json:"principals"`
  28. ClientPubKey string `json:"public_key"`
  29. Duration string `json:"duration"`
  30. }
  31. func TestCertGenSuccess(t *testing.T) {
  32. url, _ := url.Parse("https://cf-test-access.com/testpath")
  33. token := tokenGenerator()
  34. fullName, err := cfpath.GenerateSSHCertFilePathFromURL(url, keyName)
  35. assert.NoError(t, err)
  36. assert.True(t, strings.HasSuffix(fullName, "/cf-test-access.com-testpath-cf_key"))
  37. pubKeyName := fullName + ".pub"
  38. certKeyName := fullName + "-cert.pub"
  39. defer func() {
  40. os.Remove(fullName)
  41. os.Remove(pubKeyName)
  42. os.Remove(certKeyName)
  43. }()
  44. resp := signingArguments{
  45. Principals: []string{"dalton"},
  46. ClientPubKey: "ecdsa-sha2-nistp256-cert-v01@openssh.com AAAAKGVjZHNhLXNoYTItbmlzdHAyNTYtY2VydC12MDFAb3BlbnNzaC5jb20AAAAg+0rYq4mNGIAHiH1xPOJXfmOpTEwFIcyXzGJieTOhRs8AAAAIbmlzdHAyNTYAAABBBJIcsq02e8ZaofJXOZKp7yQdKW/JIouJ90lybr76hHIRrZBL1t4JEimfLvNDphPrTW9VDQaIcBSKNaxRqHOS8jezoJbhFGWhqQAAAAEAAAAgZWU5OTliNGRkZmFmNjgxNDEwMTVhMDJiY2ZhMTdiN2UAAAAKAAAABmF1c3RpbgAAAABc1KFoAAAAAFzUohwAAAAAAAAARwAAABdwZXJtaXQtYWdlbnQtZm9yd2FyZGluZwAAAAAAAAAKcGVybWl0LXB0eQAAAAAAAAAOcGVybWl0LXVzZXItcmMAAAAAAAAAAAAAAGgAAAATZWNkc2Etc2hhMi1uaXN0cDI1NgAAAAhuaXN0cDI1NgAAAEEEeAuYR56XaxvH5Z1p0hDCTQ7wC4dbj0Gc+LOKu1f94og2ilZTv9tutg8cZrqAT97REmGH6j9KIOVLGsPVjajSKAAAAGQAAAATZWNkc2Etc2hhMi1uaXN0cDI1NgAAAEkAAAAhAORY9ZO3TQsrUm6ajnVW+arbnVfTkxYBYFlVoeOEXKZuAAAAIG96A8nQnTuprWXLSemWL68RXC1NVKnBOIPD2Z7UIOB1",
  47. Duration: "3m",
  48. }
  49. w := httptest.NewRecorder()
  50. respJson, err := json.Marshal(resp)
  51. assert.NoError(t, err)
  52. w.Write(respJson)
  53. mockRequest = func(url, contentType string, body io.Reader) (*http.Response, error) {
  54. assert.Contains(t, "/cdn-cgi/access/cert_sign", url)
  55. assert.Equal(t, "application/json", contentType)
  56. buf, err := io.ReadAll(body)
  57. assert.NoError(t, err)
  58. assert.NotEmpty(t, buf)
  59. return w.Result(), nil
  60. }
  61. err = GenerateShortLivedCertificate(url, token)
  62. assert.NoError(t, err)
  63. exist, err := config.FileExists(fullName)
  64. assert.NoError(t, err)
  65. if !exist {
  66. assert.FailNow(t, fmt.Sprintf("key should exist at: %s", fullName), fullName)
  67. return
  68. }
  69. exist, err = config.FileExists(pubKeyName)
  70. assert.NoError(t, err)
  71. if !exist {
  72. assert.FailNow(t, fmt.Sprintf("key should exist at: %s", pubKeyName), pubKeyName)
  73. return
  74. }
  75. exist, err = config.FileExists(certKeyName)
  76. assert.NoError(t, err)
  77. if !exist {
  78. assert.FailNow(t, fmt.Sprintf("key should exist at: %s", certKeyName), certKeyName)
  79. return
  80. }
  81. }
  82. func tokenGenerator() string {
  83. iat := time.Now()
  84. exp := time.Now().Add(time.Minute * 5)
  85. claims := jwt.Claims{
  86. Audience: jwt.Audience{audTest},
  87. IssuedAt: jwt.NewNumericDate(iat),
  88. Expiry: jwt.NewNumericDate(exp),
  89. }
  90. key, err := rsa.GenerateKey(rand.Reader, 4096)
  91. if err != nil {
  92. panic(err)
  93. }
  94. signer, err := jose.NewSigner(jose.SigningKey{Algorithm: jose.RS256, Key: key}, (&jose.SignerOptions{}).WithType("JWT"))
  95. if err != nil {
  96. panic(err)
  97. }
  98. signedToken, err := jwt.Signed(signer).Claims(claims).Serialize()
  99. if err != nil {
  100. panic(err)
  101. }
  102. return signedToken
  103. }