mfa.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 mfa
  14. import (
  15. "context"
  16. "errors"
  17. "fmt"
  18. corev1 "k8s.io/api/core/v1"
  19. apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  20. "sigs.k8s.io/controller-runtime/pkg/client"
  21. "sigs.k8s.io/yaml"
  22. genv1alpha1 "github.com/external-secrets/external-secrets/apis/generators/v1alpha1"
  23. )
  24. type Generator struct{}
  25. const (
  26. errNoSpec = "no config spec provided"
  27. errParseSpec = "unable to parse spec: %w"
  28. )
  29. func (g *Generator) Generate(ctx context.Context, jsonSpec *apiextensions.JSON, c client.Client, namespace string) (map[string][]byte, genv1alpha1.GeneratorProviderState, error) {
  30. if jsonSpec == nil {
  31. return nil, nil, errors.New(errNoSpec)
  32. }
  33. res, err := parseSpec(jsonSpec.Raw)
  34. if err != nil {
  35. return nil, nil, fmt.Errorf(errParseSpec, err)
  36. }
  37. var opts []GeneratorOptionsFunc
  38. if res.Spec.Length > 0 {
  39. opts = append(opts, WithLength(res.Spec.Length))
  40. }
  41. if res.Spec.TimePeriod > 0 {
  42. opts = append(opts, WithTimePeriod(int64(res.Spec.TimePeriod)))
  43. }
  44. if res.Spec.When != nil {
  45. opts = append(opts, WithWhen(res.Spec.When.Time))
  46. }
  47. secret := &corev1.Secret{}
  48. if err := c.Get(ctx, client.ObjectKey{Namespace: namespace, Name: res.Spec.Secret.Name}, secret); err != nil {
  49. return nil, nil, fmt.Errorf("failed to find secret for token key: %w", err)
  50. }
  51. seed, ok := secret.Data[res.Spec.Secret.Key]
  52. if !ok {
  53. return nil, nil, fmt.Errorf("secret key %s does not exist in secret data map", res.Spec.Secret.Key)
  54. }
  55. opts = append(opts, WithToken(string(seed)))
  56. token, timeLeft, err := generateCode(opts...)
  57. if err != nil {
  58. return nil, nil, fmt.Errorf("failed to generate code for token key: %w", err)
  59. }
  60. return map[string][]byte{
  61. "token": []byte(token),
  62. "timeLeft": []byte(timeLeft),
  63. }, nil, nil
  64. }
  65. func (g *Generator) Cleanup(_ context.Context, jsonSpec *apiextensions.JSON, state genv1alpha1.GeneratorProviderState, _ client.Client, _ string) error {
  66. return nil
  67. }
  68. func parseSpec(data []byte) (*genv1alpha1.MFA, error) {
  69. var spec genv1alpha1.MFA
  70. err := yaml.Unmarshal(data, &spec)
  71. return &spec, err
  72. }
  73. func init() {
  74. genv1alpha1.Register(genv1alpha1.MFAKind, &Generator{})
  75. }