-
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 support for oauth and oidc tokens to cloud_scheduler_job (#2161)
Merged PR #2161.
- Loading branch information
1 parent
dcbeb4d
commit e53bda1
Showing
7 changed files
with
156 additions
and
1 deletion.
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
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,50 @@ | ||
// Both oidc and oauth headers cannot be set | ||
func validateAuthHeaders(diff *schema.ResourceDiff, v interface{}) error { | ||
httpBlock := diff.Get("http_target.0").(map[string]interface{}) | ||
|
||
if httpBlock != nil { | ||
oauth := httpBlock["oauth_token"] | ||
oidc := httpBlock["oidc_token"] | ||
|
||
if oauth != nil && oidc != nil { | ||
if len(oidc.([]interface{})) > 0 && len(oauth.([]interface{})) > 0 { | ||
return fmt.Errorf("Error in http_target: only one of oauth_token or oidc_token can be specified, but not both.") | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
|
||
|
||
func authHeaderDiffSuppress(k, old, new string, d *schema.ResourceData) bool { | ||
// If generating an `oauth_token` and `scope` is not provided in the configuration, | ||
// the default "https://www.googleapis.com/auth/cloud-platform" scope will be used. | ||
// Similarly, if generating an `oidc_token` and `audience` is not provided in the | ||
// configuration, the URI specified in target will be used. Although not in the | ||
// configuration, in both cases the default is returned in the object, but is not in. | ||
// state. We suppress the diff if the values are these defaults but are not stored in state. | ||
|
||
b := strings.Split(k, ".") | ||
if b[0] == "http_target" && len(b) > 4 { | ||
block := b[2] | ||
attr := b[4] | ||
|
||
if block == "oauth_token" && attr == "scope" { | ||
if old == canonicalizeServiceScope("cloud-platform") && new == "" { | ||
return true | ||
} | ||
} | ||
|
||
if block == "oidc_token" && attr == "audience" { | ||
uri := d.Get(strings.Join(b[0:2], ".")+".uri") | ||
if old == uri && new == "" { | ||
return true | ||
} | ||
} | ||
|
||
} | ||
|
||
return false | ||
} |
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,18 @@ | ||
data "google_compute_default_service_account" "default" { } | ||
|
||
resource "google_cloud_scheduler_job" "job" { | ||
name = "<%= ctx[:vars]['job_name'] %>" | ||
description = "test http job" | ||
schedule = "*/8 * * * *" | ||
time_zone = "America/New_York" | ||
|
||
http_target { | ||
http_method = "GET" | ||
uri = "https://cloudscheduler.googleapis.com/v1/projects/<%= ctx[:test_env_vars]['project_name'] %>/locations/<%= ctx[:test_env_vars]['region'] %>/jobs" | ||
|
||
oauth_token { | ||
service_account_email = "${data.google_compute_default_service_account.default.email}" | ||
} | ||
} | ||
} | ||
|
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,18 @@ | ||
data "google_compute_default_service_account" "default" { } | ||
|
||
resource "google_cloud_scheduler_job" "job" { | ||
name = "<%= ctx[:vars]['job_name'] %>" | ||
description = "test http job" | ||
schedule = "*/8 * * * *" | ||
time_zone = "America/New_York" | ||
|
||
http_target { | ||
http_method = "GET" | ||
uri = "https://example.com/ping" | ||
|
||
oidc_token { | ||
service_account_email = "${data.google_compute_default_service_account.default.email}" | ||
} | ||
} | ||
} | ||
|
15 changes: 15 additions & 0 deletions
15
templates/terraform/resource_definition/scheduler_auth.erb
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,15 @@ | ||
<%# The license inside this block applies to this file. | ||
# Copyright 2017 Google Inc. | ||
# 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 | ||
# | ||
# http://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. | ||
-%> | ||
CustomizeDiff: validateAuthHeaders, |