password.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. "fmt"
  16. "github.com/sethvargo/go-password/password"
  17. apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  18. "sigs.k8s.io/controller-runtime/pkg/client"
  19. "sigs.k8s.io/yaml"
  20. genv1alpha1 "github.com/external-secrets/external-secrets/apis/generators/v1alpha1"
  21. )
  22. type Generator struct{}
  23. const (
  24. defaultLength = 24
  25. defaultSymbolChars = "~!@#$%^&*()_+`-={}|[]\\:\"<>?,./"
  26. digitFactor = 0.25
  27. symbolFactor = 0.25
  28. errNoSpec = "no config spec provided"
  29. errParseSpec = "unable to parse spec: %w"
  30. errGetToken = "unable to get authorization token: %w"
  31. )
  32. type generateFunc func(
  33. len int,
  34. symbols int,
  35. symbolCharacters string,
  36. digits int,
  37. noUpper bool,
  38. allowRepeat bool,
  39. ) (string, error)
  40. func (g *Generator) Generate(ctx context.Context, jsonSpec *apiextensions.JSON, kube client.Client, namespace string) (map[string][]byte, error) {
  41. return g.generate(
  42. jsonSpec,
  43. generateSafePassword,
  44. )
  45. }
  46. func (g *Generator) generate(jsonSpec *apiextensions.JSON, passGen generateFunc) (map[string][]byte, error) {
  47. if jsonSpec == nil {
  48. return nil, fmt.Errorf(errNoSpec)
  49. }
  50. res, err := parseSpec(jsonSpec.Raw)
  51. if err != nil {
  52. return nil, fmt.Errorf(errParseSpec, err)
  53. }
  54. symbolCharacters := defaultSymbolChars
  55. if res.Spec.SymbolCharacters != nil {
  56. symbolCharacters = *res.Spec.SymbolCharacters
  57. }
  58. passLen := defaultLength
  59. if res.Spec.Length > 0 {
  60. passLen = res.Spec.Length
  61. }
  62. digits := int(float32(passLen) * digitFactor)
  63. if res.Spec.Digits != nil {
  64. digits = *res.Spec.Digits
  65. }
  66. symbols := int(float32(passLen) * symbolFactor)
  67. if res.Spec.Symbols != nil {
  68. symbols = *res.Spec.Symbols
  69. }
  70. pass, err := passGen(
  71. passLen,
  72. symbols,
  73. symbolCharacters,
  74. digits,
  75. res.Spec.NoUpper,
  76. res.Spec.AllowRepeat,
  77. )
  78. if err != nil {
  79. return nil, err
  80. }
  81. return map[string][]byte{
  82. "password": []byte(pass),
  83. }, nil
  84. }
  85. func generateSafePassword(
  86. passLen int,
  87. symbols int,
  88. symbolCharacters string,
  89. digits int,
  90. noUpper bool,
  91. allowRepeat bool,
  92. ) (string, error) {
  93. gen, err := password.NewGenerator(&password.GeneratorInput{
  94. Symbols: symbolCharacters,
  95. })
  96. if err != nil {
  97. return "", err
  98. }
  99. return gen.Generate(
  100. passLen,
  101. digits,
  102. symbols,
  103. noUpper,
  104. allowRepeat,
  105. )
  106. }
  107. func parseSpec(data []byte) (*genv1alpha1.Password, error) {
  108. var spec genv1alpha1.Password
  109. err := yaml.Unmarshal(data, &spec)
  110. return &spec, err
  111. }
  112. func init() {
  113. genv1alpha1.Register(genv1alpha1.PasswordKind, &Generator{})
  114. }