password.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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, genv1alpha1.GeneratorProviderState, error) {
  42. return g.generate(
  43. jsonSpec,
  44. generateSafePassword,
  45. )
  46. }
  47. func (g *Generator) Cleanup(_ context.Context, jsonSpec *apiextensions.JSON, state genv1alpha1.GeneratorProviderState, _ client.Client, _ string) error {
  48. return nil
  49. }
  50. func (g *Generator) generate(jsonSpec *apiextensions.JSON, passGen generateFunc) (map[string][]byte, genv1alpha1.GeneratorProviderState, error) {
  51. if jsonSpec == nil {
  52. return nil, nil, errors.New(errNoSpec)
  53. }
  54. res, err := parseSpec(jsonSpec.Raw)
  55. if err != nil {
  56. return nil, nil, fmt.Errorf(errParseSpec, err)
  57. }
  58. symbolCharacters := defaultSymbolChars
  59. if res.Spec.SymbolCharacters != nil {
  60. symbolCharacters = *res.Spec.SymbolCharacters
  61. }
  62. passLen := defaultLength
  63. if res.Spec.Length > 0 {
  64. passLen = res.Spec.Length
  65. }
  66. digits := int(float32(passLen) * digitFactor)
  67. if res.Spec.Digits != nil {
  68. digits = *res.Spec.Digits
  69. }
  70. symbols := int(float32(passLen) * symbolFactor)
  71. if res.Spec.Symbols != nil {
  72. symbols = *res.Spec.Symbols
  73. }
  74. pass, err := passGen(
  75. passLen,
  76. symbols,
  77. symbolCharacters,
  78. digits,
  79. res.Spec.NoUpper,
  80. res.Spec.AllowRepeat,
  81. )
  82. if err != nil {
  83. return nil, nil, err
  84. }
  85. return map[string][]byte{
  86. "password": []byte(pass),
  87. }, nil, nil
  88. }
  89. func generateSafePassword(
  90. passLen int,
  91. symbols int,
  92. symbolCharacters string,
  93. digits int,
  94. noUpper bool,
  95. allowRepeat bool,
  96. ) (string, error) {
  97. gen, err := password.NewGenerator(&password.GeneratorInput{
  98. Symbols: symbolCharacters,
  99. })
  100. if err != nil {
  101. return "", err
  102. }
  103. return gen.Generate(
  104. passLen,
  105. digits,
  106. symbols,
  107. noUpper,
  108. allowRepeat,
  109. )
  110. }
  111. func parseSpec(data []byte) (*genv1alpha1.Password, error) {
  112. var spec genv1alpha1.Password
  113. err := yaml.Unmarshal(data, &spec)
  114. return &spec, err
  115. }
  116. func init() {
  117. genv1alpha1.Register(genv1alpha1.PasswordKind, &Generator{})
  118. }