ecr.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 ecr
  13. import (
  14. "context"
  15. "fmt"
  16. "strconv"
  17. "strings"
  18. "encoding/base64"
  19. "github.com/aws/aws-sdk-go/service/ecr"
  20. genv1alpha1 "github.com/external-secrets/external-secrets/apis/generators/v1alpha1"
  21. awsauth "github.com/external-secrets/external-secrets/pkg/provider/aws/auth"
  22. apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
  23. "k8s.io/apimachinery/pkg/util/json"
  24. "sigs.k8s.io/controller-runtime/pkg/client"
  25. )
  26. type Generator struct{}
  27. const (
  28. errNoSpec = "no config spec provided"
  29. errParseSpec = "unable to parse spec: %w"
  30. errCreateSess = "unable to create aws session: %w"
  31. errGetToken = "unable to get authorization token: %w"
  32. )
  33. func (g *Generator) Generate(ctx context.Context, jsonSpec *apiextensions.JSON, kube client.Client, namespace string) (map[string][]byte, error) {
  34. if jsonSpec == nil {
  35. return nil, fmt.Errorf(errNoSpec)
  36. }
  37. res, err := parseSpec(jsonSpec.Raw)
  38. if err != nil {
  39. return nil, fmt.Errorf(errParseSpec, err)
  40. }
  41. sess, err := awsauth.NewGeneratorSession(
  42. ctx,
  43. res.Spec.Auth,
  44. res.Spec.Role,
  45. res.Spec.Region,
  46. kube,
  47. namespace,
  48. awsauth.DefaultSTSProvider,
  49. awsauth.DefaultJWTProvider)
  50. if err != nil {
  51. return nil, fmt.Errorf(errCreateSess, err)
  52. }
  53. client := ecr.New(sess)
  54. out, err := client.GetAuthorizationToken(&ecr.GetAuthorizationTokenInput{})
  55. if err != nil {
  56. return nil, fmt.Errorf(errGetToken, err)
  57. }
  58. if len(out.AuthorizationData) != 1 {
  59. return nil, fmt.Errorf("unexpected number of authorization tokens. expected 1, found %d", len(out.AuthorizationData))
  60. }
  61. // AuthorizationToken is base64 encoded {username}:{password} string
  62. decodedToken, err := base64.StdEncoding.DecodeString(*out.AuthorizationData[0].AuthorizationToken)
  63. if err != nil {
  64. return nil, err
  65. }
  66. parts := strings.Split(string(decodedToken), ":")
  67. if len(parts) != 2 {
  68. return nil, fmt.Errorf("unexpected token format")
  69. }
  70. exp := out.AuthorizationData[0].ExpiresAt.UTC().Unix()
  71. return map[string][]byte{
  72. "username": []byte(parts[0]),
  73. "password": []byte(parts[1]),
  74. "proxy_endpoint": []byte(*out.AuthorizationData[0].ProxyEndpoint),
  75. "expires_at": []byte(strconv.FormatInt(exp, 10)),
  76. }, nil
  77. }
  78. func parseSpec(data []byte) (*genv1alpha1.ECRAuthorizationToken, error) {
  79. var spec genv1alpha1.ECRAuthorizationToken
  80. err := json.Unmarshal(data, &spec)
  81. return &spec, err
  82. }
  83. func init() {
  84. genv1alpha1.Register(genv1alpha1.ECRAuthorizationTokenKind, &Generator{})
  85. }