session_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 session
  13. import (
  14. "testing"
  15. "time"
  16. "github.com/aws/aws-sdk-go/aws"
  17. "github.com/aws/aws-sdk-go/aws/credentials/stscreds"
  18. "github.com/aws/aws-sdk-go/aws/session"
  19. "github.com/aws/aws-sdk-go/service/sts"
  20. "github.com/stretchr/testify/assert"
  21. fakesess "github.com/external-secrets/external-secrets/pkg/provider/aws/session/fake"
  22. )
  23. func TestSession(t *testing.T) {
  24. tbl := []struct {
  25. test string
  26. aks string
  27. sak string
  28. region string
  29. role string
  30. sts STSProvider
  31. expectedKeyID string
  32. expectedSecretKey string
  33. }{
  34. {
  35. test: "test default role provider",
  36. aks: "2222",
  37. sak: "1111",
  38. region: "xxxxx",
  39. role: "",
  40. sts: DefaultSTSProvider,
  41. expectedSecretKey: "1111",
  42. expectedKeyID: "2222",
  43. },
  44. {
  45. test: "test custom sts provider",
  46. aks: "1111",
  47. sak: "2222",
  48. region: "xxxxx",
  49. role: "zzzzz",
  50. sts: func(*session.Session) stscreds.AssumeRoler {
  51. return &fakesess.AssumeRoler{
  52. AssumeRoleFunc: func(input *sts.AssumeRoleInput) (*sts.AssumeRoleOutput, error) {
  53. assert.Equal(t, *input.RoleArn, "zzzzz")
  54. return &sts.AssumeRoleOutput{
  55. AssumedRoleUser: &sts.AssumedRoleUser{
  56. Arn: aws.String("1123132"),
  57. AssumedRoleId: aws.String("xxxxx"),
  58. },
  59. Credentials: &sts.Credentials{
  60. SecretAccessKey: aws.String("3333"),
  61. AccessKeyId: aws.String("4444"),
  62. Expiration: aws.Time(time.Now().Add(time.Hour)),
  63. SessionToken: aws.String("6666"),
  64. },
  65. }, nil
  66. },
  67. }
  68. },
  69. expectedSecretKey: "3333",
  70. expectedKeyID: "4444",
  71. },
  72. }
  73. for i := range tbl {
  74. row := tbl[i]
  75. t.Run(row.test, func(t *testing.T) {
  76. sess, err := New(row.sak, row.aks, row.region, row.role, row.sts)
  77. assert.Nil(t, err)
  78. creds, err := sess.Config.Credentials.Get()
  79. assert.Nil(t, err)
  80. assert.Equal(t, row.expectedKeyID, creds.AccessKeyID)
  81. assert.Equal(t, row.expectedSecretKey, creds.SecretAccessKey)
  82. })
  83. }
  84. }