password.go 3.5 KB

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