-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for oauth and oidc tokens to cloud_scheduler_job #2161
Merged
modular-magician
merged 4 commits into
GoogleCloudPlatform:master
from
hashicorp:megan_cloudscheduler_job_auth3
Aug 16, 2019
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mine leaving a comment explaining the expected formats and why we are doing this parsing?