Mitigation Strategy: Principle of Least Privilege (API Scope Configuration)
-
Mitigation Strategy: Grant the application only the necessary API scopes using the
setScopes()
method. -
Description:
- Identify Required Operations: List all the specific actions your application needs to perform with Google Cloud APIs.
- Find Granular Scopes: For each operation, identify the most specific OAuth 2.0 scopes required. Consult the Google API documentation for each service. Avoid broad scopes.
- Configure Scopes in Code: Use the
$client->setScopes()
method to explicitly set the required scopes. Pass an array of scope strings or a single space-separated string.$client = new Google\Client(); $client->setScopes([ 'https://www.googleapis.com/auth/cloud-storage.read_only', 'https://www.googleapis.com/auth/bigquery.readonly' ]); // OR $client->setScopes('https://www.googleapis.com/auth/cloud-storage.read_only https://www.googleapis.com/auth/bigquery.readonly');
- Avoid Default Scopes: Be aware that some API services might have default scopes if you don't explicitly set them. Always explicitly set the scopes you need.
- Regularly Review: Periodically review the scopes and remove any that are no longer needed.
-
Threats Mitigated:
- Unauthorized Access (Severity: High): Limits the impact of a compromised credential.
- Data Breach (Severity: High): Limits the amount of data accessible.
- Privilege Escalation (Severity: High): Prevents broader access.
- Accidental Misuse (Severity: Medium): Reduces unintended actions.
-
Impact:
- Unauthorized Access: Risk significantly reduced.
- Data Breach: Potential data loss significantly reduced.
- Privilege Escalation: Highly effective prevention.
- Accidental Misuse: Reduces likelihood and impact.
-
Currently Implemented:
- Scopes are set for Cloud Storage and BigQuery access.
-
Missing Implementation:
- Missing for Gmail API; currently using a broad scope. Needs to be refined using
$client->setScopes()
.
- Missing for Gmail API; currently using a broad scope. Needs to be refined using
Mitigation Strategy: Secure Credential Handling (Library Configuration)
-
Mitigation Strategy: Use
setAuthConfig()
with environment variables oruseApplicationDefaultCredentials()
for secure credential loading. -
Description:
- Avoid Hardcoding: Never hardcode credentials directly in your code.
- Environment Variables:
- Set the
GOOGLE_APPLICATION_CREDENTIALS
environment variable to the path of your service account key file. - Use
$client->setAuthConfig(getenv('GOOGLE_APPLICATION_CREDENTIALS'));
- Set the
- Application Default Credentials (ADC):
- Use
$client->useApplicationDefaultCredentials();
The library will automatically find credentials based on the environment (GCE metadata, environment variables, etc.). This is the preferred method when possible.
- Use
- Workload Identity (GKE): If running on GKE, configure Workload Identity. The library will automatically use these credentials (via ADC).
- Do not commit credentials: Ensure that credential files are not committed to version control.
-
Threats Mitigated:
- Credential Exposure (Severity: High): Prevents credentials from being exposed.
- Source Code Compromise (Severity: High): Protects credentials if the source code is compromised.
- Accidental Disclosure (Severity: Medium): Reduces accidental sharing.
-
Impact:
- Credential Exposure: Risk dramatically reduced.
- Source Code Compromise: Highly effective.
- Accidental Disclosure: Significantly reduces the chance.
-
Currently Implemented:
- Using
getenv('GOOGLE_APPLICATION_CREDENTIALS')
in development. - Using
$client->useApplicationDefaultCredentials()
in production (with Google Cloud Secret Manager).
- Using
-
Missing Implementation:
- None
Mitigation Strategy: Token Refresh and Error Handling (with Library Methods)
-
Mitigation Strategy: Implement robust error handling, including handling token expiration and using retry mechanisms provided by the library.
-
Description:
- Wrap API Calls: Wrap API calls in
try-catch
blocks to handle potential exceptions. - Handle
Google\Service\Exception
: Specifically catchGoogle\Service\Exception
to handle errors related to the API, including authentication and authorization failures. - Implement Retry Logic: Use the library's built-in retry mechanisms:
setBackoff()
: Configure exponential backoff on the request object.
$request = $service->objects->get($bucket, $object); $request->setBackoff(new Google\Http\BackoffStrategy()); // Use default backoff // OR customize: $backoff = new Google\Http\BackoffStrategy( null, // Use default delay function Google\Http\BackoffStrategy::MAX_RETRIES, // Max retries [503, 429] // HTTP status codes to retry on ); $request->setBackoff($backoff); try { $response = $request->execute(); } catch (Google\Service\Exception $e) { // Handle the exception (log, report, etc.) }
- Global Retry Configuration: Configure retry settings globally for the client using
setClientConfig()
.
$client = new Google\Client(); $client->setClientConfig([ 'retry' => [ 'retries' => 3, // Number of retries 'http_codes' => [503, 429], // Status codes to retry // ... other retry options ] ]);
- Handle Token Expiration: The library automatically handles token refresh. However, be prepared to handle cases where the refresh token itself is invalid or revoked.
- Log Errors: Log any errors encountered, but never log the access token or refresh token.
- Wrap API Calls: Wrap API calls in
-
Threats Mitigated:
- Token Expiration/Revocation (Severity: Medium): Ensures the application can gracefully handle token issues.
- Transient API Errors (Severity: Low): Improves application resilience by retrying temporary errors.
- Denial of Service (DoS) (Severity: Medium): Backoff helps prevent overwhelming the API.
-
Impact:
- Token Expiration/Revocation: Improves application stability.
- Transient API Errors: Reduces application errors.
- Denial of Service: Helps prevent DoS.
-
Currently Implemented:
try-catch
blocks are used around API calls.
-
Missing Implementation:
setBackoff()
or global retry configuration is not currently implemented. Need to add retry logic with exponential backoff.
Mitigation Strategy: Data Minimization (Using fields
Parameter)
-
Mitigation Strategy: Request only the necessary data fields using the
fields
parameter in API requests. -
Description:
- Identify Required Fields: Determine the minimum set of data fields your application needs from the API response.
- Use the
fields
Parameter: Many Google APIs support afields
parameter that allows you to specify which fields to return. Use this parameter in your API requests.$optParams = [ 'fields' => 'items(id,name,email)' // Only retrieve id, name, and email ]; $results = $service->users->listUsers($optParams);
- Consult API Documentation: Refer to the specific API documentation to understand the syntax and supported fields for the
fields
parameter. - Avoid
*
: Avoid usingfields=*
(which retrieves all fields) unless absolutely necessary.
-
Threats Mitigated:
- Data Exposure (Severity: Medium): Reduces the amount of data retrieved, minimizing the potential impact of a data breach.
- Performance Issues (Severity: Low): Retrieving only necessary data can improve API response times and reduce bandwidth usage.
-
Impact:
- Data Exposure: Reduces the risk of exposing unnecessary data.
- Performance Issues: Can improve performance.
-
Currently Implemented:
- Not consistently implemented. Some API calls retrieve all fields.
-
Missing Implementation:
- Need to review all API calls and add the
fields
parameter where appropriate to limit the data retrieved.
- Need to review all API calls and add the