| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- /*
- Copyright © 2025 ESO Maintainer Team
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- https://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package sts
- import (
- "context"
- "errors"
- "reflect"
- "testing"
- "time"
- "github.com/aws/aws-sdk-go-v2/aws"
- "github.com/aws/aws-sdk-go-v2/service/sts"
- ststypes "github.com/aws/aws-sdk-go-v2/service/sts/types"
- v1 "k8s.io/api/core/v1"
- apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "sigs.k8s.io/controller-runtime/pkg/client"
- clientfake "sigs.k8s.io/controller-runtime/pkg/client/fake"
- "github.com/external-secrets/external-secrets/pkg/utils"
- )
- func TestGenerate(t *testing.T) {
- type args struct {
- ctx context.Context
- jsonSpec *apiextensions.JSON
- kube client.Client
- namespace string
- tokenFunc func(context.Context, *sts.GetSessionTokenInput, ...func(*sts.Options)) (*sts.GetSessionTokenOutput, error)
- }
- tests := []struct {
- name string
- g *Generator
- args args
- want map[string][]byte
- wantErr bool
- }{
- {
- name: "nil spec",
- args: args{
- ctx: context.Background(),
- jsonSpec: nil,
- },
- wantErr: true,
- },
- {
- name: "invalid json",
- args: args{
- ctx: context.Background(),
- tokenFunc: func(ctx context.Context, input *sts.GetSessionTokenInput, optFns ...func(*sts.Options)) (*sts.GetSessionTokenOutput, error) {
- return nil, errors.New("boom")
- },
- jsonSpec: &apiextensions.JSON{
- Raw: []byte(``),
- },
- },
- wantErr: true,
- },
- {
- name: "full spec",
- args: args{
- ctx: context.Background(),
- namespace: "foobar",
- kube: clientfake.NewClientBuilder().WithObjects(&v1.Secret{
- ObjectMeta: metav1.ObjectMeta{
- Name: "my-aws-creds",
- Namespace: "foobar",
- },
- Data: map[string][]byte{
- "key-id": []byte("foo"),
- "access-secret": []byte("bar"),
- },
- }).Build(),
- tokenFunc: func(ctx context.Context, input *sts.GetSessionTokenInput, optFns ...func(*sts.Options)) (*sts.GetSessionTokenOutput, error) {
- t := time.Unix(1234, 0)
- return &sts.GetSessionTokenOutput{
- Credentials: &ststypes.Credentials{
- AccessKeyId: utils.Ptr("access-key-id"),
- Expiration: utils.Ptr(t),
- SecretAccessKey: utils.Ptr("secret-access-key"),
- SessionToken: utils.Ptr("session-token"),
- },
- }, nil
- },
- jsonSpec: &apiextensions.JSON{
- Raw: []byte(`apiVersion: generators.external-secrets.io/v1alpha1
- kind: STSSessionToken
- spec:
- region: eu-west-1
- role: "my-role"
- auth:
- secretRef:
- accessKeyIDSecretRef:
- name: "my-aws-creds"
- key: "key-id"
- secretAccessKeySecretRef:
- name: "my-aws-creds"
- key: "access-secret"`),
- },
- },
- want: map[string][]byte{
- "access_key_id": []byte("access-key-id"),
- "expiration": []byte("1234"),
- "secret_access_key": []byte("secret-access-key"),
- "session_token": []byte("session-token"),
- },
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- g := &Generator{}
- got, _, err := g.generate(
- tt.args.ctx,
- tt.args.jsonSpec,
- tt.args.kube,
- tt.args.namespace,
- func(cfg *aws.Config) stsAPI {
- return &FakeSTS{
- getSessionToken: tt.args.tokenFunc,
- }
- },
- )
- if (err != nil) != tt.wantErr {
- t.Errorf("Generator.Generate() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("Generator.Generate() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- type FakeSTS struct {
- getSessionToken func(context.Context, *sts.GetSessionTokenInput, ...func(*sts.Options)) (*sts.GetSessionTokenOutput, error)
- }
- func (e *FakeSTS) GetSessionToken(ctx context.Context, params *sts.GetSessionTokenInput, optFns ...func(*sts.Options)) (*sts.GetSessionTokenOutput, error) {
- return e.getSessionToken(ctx, params, optFns...)
- }
|