password.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package password
  13. import (
  14. "context"
  15. "errors"
  16. "fmt"
  17. "github.com/sethvargo/go-password/password"
  18. apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  19. "sigs.k8s.io/controller-runtime/pkg/client"
  20. "sigs.k8s.io/yaml"
  21. genv1alpha1 "github.com/external-secrets/external-secrets/apis/generators/v1alpha1"
  22. )
  23. type Generator struct{}
  24. const (
  25. defaultLength = 24
  26. defaultSymbolChars = "~!@#$%^&*()_+`-={}|[]\\:\"<>?,./"
  27. digitFactor = 0.25
  28. symbolFactor = 0.25
  29. errNoSpec = "no config spec provided"
  30. errParseSpec = "unable to parse spec: %w"
  31. errGetToken = "unable to get authorization token: %w"
  32. )
  33. type generateFunc func(
  34. len int,
  35. symbols int,
  36. symbolCharacters string,
  37. digits int,
  38. noUpper bool,
  39. allowRepeat bool,
  40. ) (string, error)
  41. func (g *Generator) Generate(_ context.Context, jsonSpec *apiextensions.JSON, _ client.Client, _ string) (map[string][]byte, error) {
  42. return g.generate(
  43. jsonSpec,
  44. generateSafePassword,
  45. )
  46. }
  47. func (g *Generator) generate(jsonSpec *apiextensions.JSON, passGen generateFunc) (map[string][]byte, error) {
  48. if jsonSpec == nil {
  49. return nil, errors.New(errNoSpec)
  50. }
  51. res, err := parseSpec(jsonSpec.Raw)
  52. if err != nil {
  53. return nil, fmt.Errorf(errParseSpec, err)
  54. }
  55. symbolCharacters := defaultSymbolChars
  56. if res.Spec.SymbolCharacters != nil {
  57. symbolCharacters = *res.Spec.SymbolCharacters
  58. }
  59. passLen := defaultLength
  60. if res.Spec.Length > 0 {
  61. passLen = res.Spec.Length
  62. }
  63. digits := int(float32(passLen) * digitFactor)
  64. if res.Spec.Digits != nil {
  65. digits = *res.Spec.Digits
  66. }
  67. symbols := int(float32(passLen) * symbolFactor)
  68. if res.Spec.Symbols != nil {
  69. symbols = *res.Spec.Symbols
  70. }
  71. pass, err := passGen(
  72. passLen,
  73. symbols,
  74. symbolCharacters,
  75. digits,
  76. res.Spec.NoUpper,
  77. res.Spec.AllowRepeat,
  78. )
  79. if err != nil {
  80. return nil, err
  81. }
  82. return map[string][]byte{
  83. "password": []byte(pass),
  84. }, nil
  85. }
  86. func generateSafePassword(
  87. passLen int,
  88. symbols int,
  89. symbolCharacters string,
  90. digits int,
  91. noUpper bool,
  92. allowRepeat bool,
  93. ) (string, error) {
  94. gen, err := password.NewGenerator(&password.GeneratorInput{
  95. Symbols: symbolCharacters,
  96. })
  97. if err != nil {
  98. return "", err
  99. }
  100. return gen.Generate(
  101. passLen,
  102. digits,
  103. symbols,
  104. noUpper,
  105. allowRepeat,
  106. )
  107. }
  108. func parseSpec(data []byte) (*genv1alpha1.Password, error) {
  109. var spec genv1alpha1.Password
  110. err := yaml.Unmarshal(data, &spec)
  111. return &spec, err
  112. }
  113. func init() {
  114. genv1alpha1.Register(genv1alpha1.PasswordKind, &Generator{})
  115. }