mfa.go 2.6 KB

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