|
| 1 | +package google |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/base64" |
| 6 | + "encoding/json" |
| 7 | + "errors" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "fmt" |
| 12 | + |
| 13 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 14 | + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + jwtTestSubject = "custom-subject" |
| 19 | + jwtTestFoo = "bar" |
| 20 | + jwtTestComplexFooNested = "baz" |
| 21 | +) |
| 22 | + |
| 23 | +type jwtTestPayload struct { |
| 24 | + Subject string `json:"sub"` |
| 25 | + |
| 26 | + Foo string `json:"foo"` |
| 27 | + |
| 28 | + ComplexFoo struct { |
| 29 | + Nested string `json:"nested"` |
| 30 | + } `json:"complexFoo"` |
| 31 | +} |
| 32 | + |
| 33 | +func testAccCheckServiceAccountJwtValue(name, audience string) resource.TestCheckFunc { |
| 34 | + return func(s *terraform.State) error { |
| 35 | + ms := s.RootModule() |
| 36 | + |
| 37 | + rs, ok := ms.Resources[name] |
| 38 | + |
| 39 | + if !ok { |
| 40 | + return fmt.Errorf("can't find %s in state", name) |
| 41 | + } |
| 42 | + |
| 43 | + jwtString, ok := rs.Primary.Attributes["jwt"] |
| 44 | + |
| 45 | + if !ok { |
| 46 | + return fmt.Errorf("jwt not found") |
| 47 | + } |
| 48 | + |
| 49 | + jwtParts := strings.Split(jwtString, ".") |
| 50 | + |
| 51 | + if len(jwtParts) != 3 { |
| 52 | + return errors.New("jwt does not appear well-formed") |
| 53 | + } |
| 54 | + |
| 55 | + decoded, err := base64.RawURLEncoding.DecodeString(jwtParts[1]) |
| 56 | + |
| 57 | + if err != nil { |
| 58 | + return fmt.Errorf("could not base64 decode jwt body: %w", err) |
| 59 | + } |
| 60 | + |
| 61 | + var payload jwtTestPayload |
| 62 | + |
| 63 | + err = json.NewDecoder(bytes.NewBuffer(decoded)).Decode(&payload) |
| 64 | + |
| 65 | + if err != nil { |
| 66 | + return fmt.Errorf("could not decode jwt payload: %w", err) |
| 67 | + } |
| 68 | + |
| 69 | + if payload.Subject != jwtTestSubject { |
| 70 | + return fmt.Errorf("invalid 'sub', expected '%s', got '%s'", jwtTestSubject, payload.Subject) |
| 71 | + } |
| 72 | + |
| 73 | + if payload.Foo != jwtTestFoo { |
| 74 | + return fmt.Errorf("invalid 'foo', expected '%s', got '%s'", jwtTestFoo, payload.Foo) |
| 75 | + } |
| 76 | + |
| 77 | + if payload.ComplexFoo.Nested != jwtTestComplexFooNested { |
| 78 | + return fmt.Errorf("invalid 'foo', expected '%s', got '%s'", jwtTestComplexFooNested, payload.ComplexFoo.Nested) |
| 79 | + } |
| 80 | + |
| 81 | + return nil |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func TestAccDataSourceGoogleServiceAccountJwt(t *testing.T) { |
| 86 | + t.Parallel() |
| 87 | + |
| 88 | + resourceName := "data.google_service_account_jwt.default" |
| 89 | + serviceAccount := getTestServiceAccountFromEnv(t) |
| 90 | + targetServiceAccountEmail := BootstrapServiceAccount(t, getTestProjectFromEnv(), serviceAccount) |
| 91 | + |
| 92 | + resource.Test(t, resource.TestCase{ |
| 93 | + PreCheck: func() { testAccPreCheck(t) }, |
| 94 | + Providers: testAccProviders, |
| 95 | + Steps: []resource.TestStep{ |
| 96 | + { |
| 97 | + Config: testAccCheckGoogleServiceAccountJwt(targetServiceAccountEmail), |
| 98 | + Check: resource.ComposeTestCheckFunc( |
| 99 | + testAccCheckServiceAccountJwtValue(resourceName, targetAudience), |
| 100 | + ), |
| 101 | + }, |
| 102 | + }, |
| 103 | + }) |
| 104 | +} |
| 105 | + |
| 106 | +func testAccCheckGoogleServiceAccountJwt(targetServiceAccount string) string { |
| 107 | + return fmt.Sprintf(` |
| 108 | +data "google_service_account_jwt" "default" { |
| 109 | + target_service_account = "%s" |
| 110 | +
|
| 111 | + payload = jsonencode({ |
| 112 | + sub: "%s", |
| 113 | + foo: "%s", |
| 114 | + complexFoo: { |
| 115 | + nested: "%s" |
| 116 | + } |
| 117 | + }) |
| 118 | +} |
| 119 | +`, targetServiceAccount, jwtTestSubject, jwtTestFoo, jwtTestComplexFooNested) |
| 120 | +} |
0 commit comments