rsa_keys.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2021 EMQ Technologies Co., Ltd.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package jwt
  15. import (
  16. "crypto/rsa"
  17. "fmt"
  18. "os"
  19. "path"
  20. "strings"
  21. "sync"
  22. "github.com/golang-jwt/jwt"
  23. "github.com/lf-edge/ekuiper/internal/conf"
  24. )
  25. var (
  26. privateKeyRepository = make(map[string]*rsa.PrivateKey)
  27. repositoryLock sync.Mutex
  28. )
  29. const RSAKeyDir = "mgmt"
  30. func GetPrivateKeyWithKeyName(keyName string) (*rsa.PrivateKey, error) {
  31. repositoryLock.Lock()
  32. defer repositoryLock.Unlock()
  33. key, ok := privateKeyRepository[keyName]
  34. if ok {
  35. return key, nil
  36. }
  37. privateKey, err := privateKeyFromFile(keyName)
  38. if err != nil {
  39. return nil, err
  40. }
  41. privateKeyRepository[keyName] = privateKey
  42. return privateKey, nil
  43. }
  44. func GetPublicKey(keyName string) (*rsa.PublicKey, error) {
  45. publicKey, err := publicKeyFromFile(keyName)
  46. if err != nil {
  47. return nil, err
  48. }
  49. return publicKey, nil
  50. }
  51. func insensitiveGetFilePath(prikeyName string) (string, error) {
  52. confDir, err := conf.GetConfLoc()
  53. if nil != err {
  54. return "", err
  55. }
  56. dir := path.Join(confDir, RSAKeyDir)
  57. dirEntries, err := os.ReadDir(dir)
  58. if nil != err {
  59. return "", err
  60. }
  61. for _, entry := range dirEntries {
  62. fileName := entry.Name()
  63. if strings.EqualFold(fileName, prikeyName) {
  64. filePath := path.Join(dir, fileName)
  65. return filePath, nil
  66. }
  67. }
  68. return "", fmt.Errorf("not found target key file %s in /etc/%s", prikeyName, RSAKeyDir)
  69. }
  70. func privateKeyFromFile(keyName string) (*rsa.PrivateKey, error) {
  71. keyPath, err := insensitiveGetFilePath(keyName)
  72. if err != nil {
  73. return nil, err
  74. }
  75. keyBytes, err := os.ReadFile(keyPath)
  76. if err != nil {
  77. return nil, err
  78. }
  79. signKey, err := jwt.ParseRSAPrivateKeyFromPEM(keyBytes)
  80. if err != nil {
  81. return nil, err
  82. }
  83. return signKey, nil
  84. }
  85. func publicKeyFromFile(keyName string) (*rsa.PublicKey, error) {
  86. keyPath, err := insensitiveGetFilePath(keyName)
  87. if err != nil {
  88. return nil, err
  89. }
  90. keyBytes, err := os.ReadFile(keyPath)
  91. if err != nil {
  92. return nil, err
  93. }
  94. pubKey, err := jwt.ParseRSAPublicKeyFromPEM(keyBytes)
  95. if err != nil {
  96. return nil, err
  97. }
  98. return pubKey, nil
  99. }