rsa_keys.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. "github.com/golang-jwt/jwt"
  19. "github.com/lf-edge/ekuiper/internal/conf"
  20. "io/ioutil"
  21. "path"
  22. "strings"
  23. "sync"
  24. )
  25. var privateKeyRepository = make(map[string]*rsa.PrivateKey)
  26. var repositoryLock sync.Mutex
  27. const RSAKeyDir = "mgmt"
  28. func GetPrivateKeyWithKeyName(keyName string) (*rsa.PrivateKey, error) {
  29. repositoryLock.Lock()
  30. defer repositoryLock.Unlock()
  31. key, ok := privateKeyRepository[keyName]
  32. if ok {
  33. return key, nil
  34. }
  35. privateKey, err := privateKeyFromFile(keyName)
  36. if err != nil {
  37. return nil, err
  38. }
  39. privateKeyRepository[keyName] = privateKey
  40. return privateKey, nil
  41. }
  42. func GetPublicKey(keyName string) (*rsa.PublicKey, error) {
  43. publicKey, err := publicKeyFromFile(keyName)
  44. if err != nil {
  45. return nil, err
  46. }
  47. return publicKey, nil
  48. }
  49. func insensitiveGetFilePath(prikeyName string) (string, error) {
  50. confDir, err := conf.GetConfLoc()
  51. if nil != err {
  52. return "", err
  53. }
  54. dir := path.Join(confDir, RSAKeyDir)
  55. infos, err := ioutil.ReadDir(dir)
  56. if nil != err {
  57. return "", err
  58. }
  59. for _, info := range infos {
  60. fileName := info.Name()
  61. if strings.EqualFold(fileName, prikeyName) {
  62. filePath := path.Join(dir, fileName)
  63. return filePath, nil
  64. }
  65. }
  66. return "", fmt.Errorf("not found target key file %s in /etc/%s", prikeyName, RSAKeyDir)
  67. }
  68. func privateKeyFromFile(keyName string) (*rsa.PrivateKey, error) {
  69. keyPath, err := insensitiveGetFilePath(keyName)
  70. if err != nil {
  71. return nil, err
  72. }
  73. keyBytes, err := ioutil.ReadFile(keyPath)
  74. if err != nil {
  75. return nil, err
  76. }
  77. signKey, err := jwt.ParseRSAPrivateKeyFromPEM(keyBytes)
  78. if err != nil {
  79. return nil, err
  80. }
  81. return signKey, nil
  82. }
  83. func publicKeyFromFile(keyName string) (*rsa.PublicKey, error) {
  84. keyPath, err := insensitiveGetFilePath(keyName)
  85. if err != nil {
  86. return nil, err
  87. }
  88. keyBytes, err := ioutil.ReadFile(keyPath)
  89. if err != nil {
  90. return nil, err
  91. }
  92. pubKey, err := jwt.ParseRSAPublicKeyFromPEM(keyBytes)
  93. if err != nil {
  94. return nil, err
  95. }
  96. return pubKey, nil
  97. }