-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add google_impersonated_credential datasource
Signed-off-by: Modular Magician <[email protected]>
- Loading branch information
1 parent
4d2ade9
commit ef60ecc
Showing
9 changed files
with
1,932 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"strings" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
iamcredentials "google.golang.org/api/iamcredentials/v1" | ||
) | ||
|
||
func dataSourceGoogleServiceAccountAccessToken() *schema.Resource { | ||
|
||
return &schema.Resource{ | ||
Read: dataSourceGoogleServiceAccountAccessTokenRead, | ||
Schema: map[string]*schema.Schema{ | ||
"target_service_account": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validateRegexp("(" + strings.Join(PossibleServiceAccountNames, "|") + ")"), | ||
}, | ||
"access_token": { | ||
Type: schema.TypeString, | ||
Sensitive: true, | ||
Computed: true, | ||
}, | ||
"scopes": { | ||
Type: schema.TypeSet, | ||
Required: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
StateFunc: func(v interface{}) string { | ||
return canonicalizeServiceScope(v.(string)) | ||
}, | ||
}, | ||
// ValidateFunc is not yet supported on lists or sets. | ||
}, | ||
"delegates": { | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
ValidateFunc: validateRegexp(ServiceAccountLinkRegex), | ||
}, | ||
}, | ||
"lifetime": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ValidateFunc: validateDuration(), // duration <=3600s; TODO: support validteDuration(min,max) | ||
Default: "3600s", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceGoogleServiceAccountAccessTokenRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
log.Printf("[INFO] Acquire Service Account AccessToken for %s", d.Get("target_service_account").(string)) | ||
|
||
service := config.clientIamCredentials | ||
|
||
name := fmt.Sprintf("projects/-/serviceAccounts/%s", d.Get("target_service_account").(string)) | ||
tokenRequest := &iamcredentials.GenerateAccessTokenRequest{ | ||
Lifetime: d.Get("lifetime").(string), | ||
Delegates: convertStringSet(d.Get("delegates").(*schema.Set)), | ||
Scope: canonicalizeServiceScopes(convertStringSet(d.Get("scopes").(*schema.Set))), | ||
} | ||
at, err := service.Projects.ServiceAccounts.GenerateAccessToken(name, tokenRequest).Do() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(time.Now().UTC().String()) | ||
d.Set("access_token", at.AccessToken) | ||
|
||
return nil | ||
} |
66 changes: 66 additions & 0 deletions
66
google/data_source_google_service_account_access_token_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package google | ||
|
||
import ( | ||
"testing" | ||
|
||
"fmt" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func testAccCheckServiceAccountAccessTokenValue(name, value string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
ms := s.RootModule() | ||
rs, ok := ms.Outputs[name] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", name) | ||
} | ||
|
||
// TODO: validate the token belongs to the service account | ||
if rs.Value == "" { | ||
return fmt.Errorf("%s Cannot be empty", name) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func TestAccDataSourceGoogleServiceAccountAccessToken_basic(t *testing.T) { | ||
t.Parallel() | ||
|
||
resourceName := "data.google_service_account_access_token.default" | ||
|
||
targetServiceAccountEmail := getTestServiceAccountFromEnv(t) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckGoogleServiceAccountAccessToken_datasource(targetServiceAccountEmail), | ||
Destroy: true, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(resourceName, "target_service_account", targetServiceAccountEmail), | ||
testAccCheckServiceAccountAccessTokenValue("access_token", targetServiceAccountEmail), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckGoogleServiceAccountAccessToken_datasource(targetServiceAccountID string) string { | ||
|
||
return fmt.Sprintf(` | ||
data "google_service_account_access_token" "default" { | ||
target_service_account = "%s" | ||
scopes = ["userinfo-email", "https://www.googleapis.com/auth/cloud-platform"] | ||
lifetime = "30s" | ||
} | ||
output "access_token" { | ||
value = "${data.google_service_account_access_token.default.access_token}" | ||
} | ||
`, targetServiceAccountID) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.