mfa.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 provides functionality for generating multi-factor authentication tokens.
  14. package mfa
  15. import (
  16. "context"
  17. "errors"
  18. "fmt"
  19. corev1 "k8s.io/api/core/v1"
  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 MFA token generation functionality.
  26. type Generator struct{}
  27. const (
  28. errNoSpec = "no config spec provided"
  29. errParseSpec = "unable to parse spec: %w"
  30. )
  31. // Generate creates an MFA token based on the provided configuration.
  32. // It retrieves the seed from a Kubernetes secret and generates a time-based one-time password.
  33. func (g *Generator) Generate(ctx context.Context, jsonSpec *apiextensions.JSON, c client.Client, namespace string) (map[string][]byte, genv1alpha1.GeneratorProviderState, error) {
  34. if jsonSpec == nil {
  35. return nil, nil, errors.New(errNoSpec)
  36. }
  37. res, err := parseSpec(jsonSpec.Raw)
  38. if err != nil {
  39. return nil, nil, fmt.Errorf(errParseSpec, err)
  40. }
  41. var opts []GeneratorOptionsFunc
  42. if res.Spec.Length > 0 {
  43. opts = append(opts, WithLength(res.Spec.Length))
  44. }
  45. if res.Spec.TimePeriod > 0 {
  46. opts = append(opts, WithTimePeriod(int64(res.Spec.TimePeriod)))
  47. }
  48. if res.Spec.When != nil {
  49. opts = append(opts, WithWhen(res.Spec.When.Time))
  50. }
  51. secret := &corev1.Secret{}
  52. if err := c.Get(ctx, client.ObjectKey{Namespace: namespace, Name: res.Spec.Secret.Name}, secret); err != nil {
  53. return nil, nil, fmt.Errorf("failed to find secret for token key: %w", err)
  54. }
  55. seed, ok := secret.Data[res.Spec.Secret.Key]
  56. if !ok {
  57. return nil, nil, fmt.Errorf("secret key %s does not exist in secret data map", res.Spec.Secret.Key)
  58. }
  59. opts = append(opts, WithToken(string(seed)))
  60. token, timeLeft, err := generateCode(opts...)
  61. if err != nil {
  62. return nil, nil, fmt.Errorf("failed to generate code for token key: %w", err)
  63. }
  64. return map[string][]byte{
  65. "token": []byte(token),
  66. "timeLeft": []byte(timeLeft),
  67. }, nil, nil
  68. }
  69. // Cleanup performs any necessary cleanup after token generation.
  70. func (g *Generator) Cleanup(_ context.Context, _ *apiextensions.JSON, _ genv1alpha1.GeneratorProviderState, _ client.Client, _ string) error {
  71. return nil
  72. }
  73. func parseSpec(data []byte) (*genv1alpha1.MFA, error) {
  74. var spec genv1alpha1.MFA
  75. err := yaml.Unmarshal(data, &spec)
  76. return &spec, err
  77. }
  78. // NewGenerator creates a new Generator instance.
  79. func NewGenerator() genv1alpha1.Generator {
  80. return &Generator{}
  81. }
  82. // Kind returns the generator kind.
  83. func Kind() string {
  84. return string(genv1alpha1.GeneratorKindMFA)
  85. }