# Copyright 2020 Google, LLC.
#
# 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.

# [START run_secure_request]
import os
import urllib

import google.auth.transport.requests
import google.oauth2.id_token


def new_request(data):
    """
    new_request creates a new HTTP request with IAM ID Token credential.
    This token is automatically handled by private Cloud Run (fully managed)
    and Cloud Functions.
    """

    url = os.environ.get("EDITOR_UPSTREAM_RENDER_URL")
    if not url:
        raise Exception("EDITOR_UPSTREAM_RENDER_URL missing")

    unauthenticated = os.environ.get("EDITOR_UPSTREAM_UNAUTHENTICATED", False)

    req = urllib.request.Request(url, data=data.encode())

    credentials, project = google.auth.default()

    if not unauthenticated:
        auth_req = google.auth.transport.requests.Request()
        target_audience = url

        id_token = google.oauth2.id_token.fetch_id_token(auth_req, target_audience)
        req.add_header("Authorization", f"Bearer {id_token}")

    response = urllib.request.urlopen(req)
    return response.read()
# [END run_secure_request]

@@ -1046,88 +1046,8 @@
 %22)%0A%0A
-    unauthenticated = os.environ.get(%22EDITOR_UPSTREAM_UNAUTHENTICATED%22, False)%0A%0A
     
@@ -1154,41 +1154,8 @@
 t()%0A
-%0A    if not unauthenticated:%0A    
     
@@ -1206,20 +1206,16 @@
 quest()%0A
-    
     targ
@@ -1237,20 +1237,16 @@
 rl%0A%0A    
- 
-   
 id_token
@@ -1313,20 +1313,16 @@
 dience)%0A
-    
     req.

Update render.py (#4415)