A service account is a special non-human Google Account that applications, automated processes, or compute workloads, such as a Compute Engine instance, use to make authorized API calls. Unlike user accounts, a service account is identified solely by its unique email address.
Applications use service accounts to authenticate and gain access to Google Cloud resources. When an application authenticates by using a service account, it gains access to the resources that the service account has been assigned explicit Identity and Access Management permissions for. This provides a secure, auditable, and centralized method for machine-to-machine authorization.
For Secure Web Proxy, service accounts play a crucial role in authorization and policy enforcement. By using a service account to identify the traffic source of a workload, you can create granular Secure Web Proxy policies that specifically allow or deny web traffic based on the application's identity, rather than just its IP address.
This page shows you how to do the following:
- Create a Secure Web Proxy instance with an empty policy
- Create a service account and grant IAM roles
- Create Secure Web Proxy rules
- Test connectivity of your entire Secure Web Proxy configuration
Before you begin
Complete the initial setup steps.
Request an organization administrator to grant you access to a service account.
Verify that you have the Google Cloud CLI version 406.0.0 or later installed:
gcloud version | head -n1If you have an earlier gcloud CLI version installed, update the version:
gcloud components update --version=406.0.0
Create a Secure Web Proxy instance with an empty policy
To create a Secure Web Proxy instance, you must first create an empty security policy and then create a web proxy that uses the policy.
Create an empty security policy
Console
In the Google Cloud console, go to the SWP Policies page.
Click Create a policy.
In the Name field, enter a name for the policy, such as
myswppolicy.In the Description field, enter a description for the policy, such as
My new swp policy.For Regions, select the region where you want to create the policy, such as
us-central1.Click Create.
Cloud Shell
Use your preferred text editor to create a
policy.yamlfile.Add the following to the
policy.yamlfile that you created:name: projects/PROJECT_NAME/locations/REGION/gatewaySecurityPolicies/POLICY_NAME description: POLICY_DESCRIPTIONReplace the following:
PROJECT_NAME: name of your projectREGION: region where your policy is created, such asus-central1POLICY_NAME: name of your policyPOLICY_DESCRIPTION: description for your policy
Import the security policy by using the
gcloud network-security gateway-security-policies importcommand:gcloud network-security gateway-security-policies import POLICY_NAME \ --source=POLICY_FILE.yaml \ --location=REGION
Create a web proxy
Console
In the Google Cloud console, go to the Web Proxies page.
Click Create a secure web proxy.
In the Name field, enter a name for the web proxy, such as
myswp.In the Description field, enter a description for the web proxy, such as
My new swp.For Routing mode, select the Explicit option.
For Regions, select the region where you want to create the web proxy, such as
us-central1.For Network, select the network where you want to create the web proxy.
For Subnetwork, select the VPC subnet that you previously created as part of the initial setup steps.
Optional: In the Web proxy IP address field, enter the Secure Web Proxy IP address.
You can enter an IP address from the range of Secure Web Proxy IP addresses that reside in the subnetwork you created in the previous step. If you don't enter the IP address, then your Secure Web Proxy instance automatically chooses an IP address from the selected subnetwork.
For Certificate, select the certificate that you want to use to create the web proxy.
For Policy, select the policy that you created to associate the web proxy with.
Click Create.
Cloud Shell
Use your preferred text editor to create a
gateway.yamlfile.Add the following to the
gateway.yamlfile:name: projects/PROJECT_NAME/locations/REGION/gateways/GATEWAY_NAME type: SECURE_WEB_GATEWAY ports: [GATEWAY_PORT_NUMBERS] certificateUrls: [CERTIFICATE_URLS] gatewaySecurityPolicy: projects/PROJECT_NAME/locations/REGION/gatewaySecurityPolicies/POLICY_NAME network: projects/PROJECT_NAME/global/networks/NETWORK_NAME subnetwork: projects/PROJECT_NAME/regions/REGION/subnetworks/SUBNETWORK addresses: [GATEWAY_IP_ADDRESS] scope: samplescopeReplace the following:
GATEWAY_NAME: name of this Secure Web Proxy instanceGATEWAY_PORT_NUMBERS: list of port numbers for this gateway, such as[80,443]CERTIFICATE_URLS: list of SSL certificate URLsSUBNETWORK: VPC subnet that you previously created as part of the initial setup stepsGATEWAY_IP_ADDRESS: optional list of IP addresses for your Secure Web Proxy instances within the proxy subnets that you previously created in the initial setup stepsIf you choose not to list IP addresses, omit the field to let the web proxy choose an IP address for you.
Create the Secure Web Proxy instance by using the
gcloud network-services gateways importcommand:gcloud network-services gateways import GATEWAY_NAME \ --source=gateway.yaml \ --location=REGION
Test connectivity
To test connectivity, use the curl command from any virtual machine (VM)
instance within your Virtual Private Cloud (VPC) network:
curl -x https://GATEWAY_IP_ADDRESS:PORT_NUMBER https://www.example.com --proxy-insecure
If everything is working correctly, then your Secure Web Proxy instance
returns a 403 Forbidden status code. This error confirms the following:
The Secure Web Proxy instance has been successfully deployed and is actively receiving traffic.
The Secure Web Proxy policy is correctly enforcing the default security posture of rejecting all traffic until you define specific
allowrules in the next sections.
Create a service account and grant IAM roles
Create a service account: use the
gcloud iam service-accounts createcommand to create a service account namedmy-swp-sain your Google Cloud project.gcloud iam service-accounts create my-swp-sa \ --display-name="Secure Web Proxy service account" \ --description="Service account for Secure Web Proxy instances"
Grant IAM roles to the service account: use the
gcloud projects add-iam-policy-bindingcommand to grant the necessary IAM roles to the service account so that it can manage or use resources in your Google Cloud project.For example, to allow the service account to use network resources, grant it the Compute Network User role (
roles/compute.networkUser).gcloud projects add-iam-policy-binding PROJECT_ID \ --member="serviceAccount:my-swp-sa@PROJECT_ID.iam.gserviceaccount.com" \ --role="roles/compute.networkUser"
Replace
PROJECT_IDwith the ID of your Google Cloud project.
Create a Secure Web Proxy rule
Console
In the Google Cloud console, go to the SWP Policies page.
Click the name of your policy that you previously created, such as
myswppolicy.Click Add rule.
For each rule, do the following:
In the Priority field, enter a numeric evaluation order for the rule. Rules are evaluated from highest to lowest priority, where
0is the highest priority.In the Name field, enter a name for the rule.
In the Description field, enter a description for the rule.
For Action, select one of the following options:
- Allow: to allow connection requests that match the rule.
- Deny: to deny connection requests that match the rule.
For the Status field, select one of the following options for the rule enforcement:
- Enabled: to enforce the rule on your Secure Web Proxy instance.
- Disabled: to not enforce the rule on your Secure Web Proxy instance.
In the Session Match section, specify the email address of the service account that you previously created. For example:
sessionMatcher: "source.matchServiceAccount(SERVICE_ACCOUNT) && host() == 'example.com'"For more information about the syntax for
SessionMatcher, see CEL matcher language reference.In the Application Match section, specify the criteria for matching the request.
Click Add rule.
To add another rule, click Add rule.
Cloud Shell
Use your preferred text editor to create the
rule.yamlfile.To allow access to a URL from the appropriate service account, add the following to the
rule.yamlfile that you created:name: projects/PROJECT_NAME/locations/REGION/gatewaySecurityPolicies/POLICY_NAME/rules/RULE_NAME description: RULE_DESCRIPTION enabled: true priority: RULE_PRIORITY sessionMatcher: CEL_EXPRESSION basicProfile: ALLOWReplace the following:
RULE_NAME: name of this ruleRULE_DESCRIPTION: description for the ruleRULE_PRIORITY: priority of this rule; a lower number corresponds to a higher priorityCEL_EXPRESSION: a Common Expression Language (CEL) expression. For more information, see CEL matcher language reference.
For example, to allow access to
example.comfrom the resource with the required service account, add the following to the YAML file that you created for thesessionMatcher:```yaml sessionMatcher: "source.matchServiceAccount('SERVICE_ACCOUNT') && host() == 'example.com'" ```Replace
SERVICE_ACCOUNTwith the service account that you want to allow. This must be the service account's email address.Import the rules that you created by using the
gcloud network-security gateway-security-policies rules importcommand:gcloud network-security gateway-security-policies rules import RULE_NAME \ --source=RULE_FILE.yaml \ --location=REGION \ --gateway-security-policy=POLICY_NAME
Test connectivity
You can test connectivity again to validate that your entire Secure Web Proxy configuration—including the service account, security policy rules, and the gateway—is working as intended.
To test connectivity, use the curl command from the resource—like a VM
instance—with the attached SERVICE_ACCOUNT:
curl -x https://IPv4_ADDRESS:443 http://example.com
--proxy-insecure
Replace IPv4_ADDRESS with the IPv4 address of your
Secure Web Proxy instance.
If everything works correctly, then you receive a 200 OK status code. This
status code confirms the following key outcomes:
Successful authorization: it proves that the traffic originating from the resource is correctly identified by the assigned service account, and that this identity successfully matches the
allowrule you added to the security policy.End-to-end functionality: it validates that the traffic is flowing correctly through the configured path—from the resource > to the Secure Web Proxy instance > passing the security policy > and reaching the external destination.