Skip to content

fix(calendar): remove hardcoded api key and inject via env var#623

Open
TineoC wants to merge 6 commits intokubernetes:masterfrom
TineoC:fix/remove-hardcoded-api-key
Open

fix(calendar): remove hardcoded api key and inject via env var#623
TineoC wants to merge 6 commits intokubernetes:masterfrom
TineoC:fix/remove-hardcoded-api-key

Conversation

@TineoC
Copy link
Contributor

@TineoC TineoC commented Jan 16, 2026

This PR addresses the security issue of having a hardcoded Google Calendar API key in the source code.

Changes

  • Removed Hardcoded Key: The API key has been removed from static/js/calendar.js.
  • Environment Variable Injection: The key is now injected into the calendar script via the GOOGLE_CALENDAR_API_KEY environment variable in layouts/calendar/baseof.html.
  • Graceful Degradation: The renderCalendar function now checks if the key is valid. If it's missing or set to the default PLACEHOLDER_VALUE, it displays a user-friendly "Community Calendar is not available" message instead of crashing or showing a console error.
  • Default Configuration: Added google_calendar_api_key: 'PLACEHOLDER_VALUE' to hugo.yaml to serve as a default and documentation.

Production Setup Instructions (Netlify)

To ensure the calendar works in the production environment, follow these steps:

  1. Log in to your Netlify dashboard.
  2. Select the kubernetes-contributor-site (or equivalent) site.
  3. Navigate to Site configuration > Environment variables.
  4. Click Add a variable.
  5. Key: GOOGLE_CALENDAR_API_KEY
  6. Value: Enter the valid Google Calendar API key (restricted to your domain).
  7. Click Create variable.
  8. Trigger a new deploy (e.g., "Retry deploy" > "Deploy site") for the changes to take effect.

Local Development

For local development, the calendar will show a placeholder message by default. To test the calendar functionality locally:

  1. Obtain a valid API key (or use a dev-specific key).
  2. Run the server with the environment variable:
    export GOOGLE_CALENDAR_API_KEY="your-api-key"
    hugo server
    OR add the key in hugo.yaml
params:
  google_calendar_api_key: 'PLACEHOLDER_VALUE'

Closes #45

@k8s-ci-robot k8s-ci-robot added the do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. label Jan 16, 2026
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: TineoC
Once this PR has been reviewed and has the lgtm label, please assign stmcginnis for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Jan 16, 2026
@TineoC TineoC force-pushed the fix/remove-hardcoded-api-key branch from 71e96f0 to eefacc4 Compare January 16, 2026 20:05
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. label Jan 16, 2026
@TineoC
Copy link
Contributor Author

TineoC commented Jan 16, 2026

Before merging this changes. Please follow the steps mentioned in "Production Setup Instructions (Netlify)"

- Removes the hardcoded Google Calendar API key from JS.
- Injects the key via 'GOOGLE_CALENDAR_API_KEY' environment variable.
- Adds a fallback to 'site.Params.google_calendar_api_key' in hugo.yaml.
- Sets a default 'PLACEHOLDER_VALUE' in hugo.yaml.
- Implements graceful degradation: if the key is missing or is the placeholder, the calendar displays a 'not available' message instead of crashing.
@TineoC TineoC force-pushed the fix/remove-hardcoded-api-key branch from b315ccc to 6b4e71d Compare January 16, 2026 20:13
Copy link
Member

@lmktfy lmktfy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Partial feedback

hugo.yaml Outdated

# Everything below this are Site Params
params:
google_calendar_api_key: 'PLACEHOLDER_VALUE'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this addition?

Copy link
Contributor Author

@TineoC TineoC Jan 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for allowing devs add their Google Calendar keys for local development. but is simpler to just use them in prod. I agree with you

<script src='{{ .Site.BaseURL }}/js/calendar.js'></script>
<script>
renderCalendar();
{{ $apiKey := getenv "HUGO_GOOGLE_CALENDAR_API_KEY" | default .Site.Params.google_calendar_api_key | default "" }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make the requirement conditional on whether it is a production build?


if (!apiKey || apiKey === 'PLACEHOLDER_VALUE') {
console.warn('Google Calendar API key is missing. Calendar will not render.');
calendarEl.innerHTML = '<div style="padding: 20px; border: 1px solid #ccc; background: #f9f9f9; text-align: center;">Community Calendar is not available in this environment (missing API Key).</div>';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid embedding CSS here

- Removed the placeholder value for the Google Calendar API key from hugo.yaml.
- Modified the JavaScript in baseof.html to conditionally render the calendar based on the production environment, ensuring that the API key is only used in production and defaults to an empty string otherwise.
- Removed the check for the placeholder value in the Google Calendar API key validation.
@TineoC TineoC requested a review from lmktfy January 18, 2026 16:13
<script>
renderCalendar();
{{ if hugo.IsProduction }}
{{ $apiKey := getenv "HUGO_GOOGLE_CALENDAR_API_KEY" | default "" }}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's how I'd do it

  • Always try to fetch the value
  • If not production, it doesn't matter
  • If it's missing *in production", call errorf to outright fail the build

document.addEventListener('DOMContentLoaded', function() {
var calendarEl = document.getElementById('calendar');

if (!calendarEl) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A trick: we can set eg data-isproduction on the root <html> element. But we only do that for production builds, and then client side we know if a calendar / API key is expected.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like this?

<html {{ if hugo.IsProduction }}data-isproduction="true"{{ end }}>

- Added a check for the presence of the Google Calendar API key in production environments, ensuring that an error is logged if it is missing.
- Updated the JavaScript to differentiate between production and non-production warnings for the missing API key.
@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Jan 19, 2026
@TineoC TineoC requested a review from lmktfy January 29, 2026 02:05
@TineoC
Copy link
Contributor Author

TineoC commented Feb 7, 2026

@mrbobbytables @mfahlandt @stmcginnis Who would be the best person to reach out to for providing and setting the GOOGLE_CALENDAR_API_KEY in the production environment (Netlify) of the webpage?

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Feb 12, 2026
@k8s-ci-robot k8s-ci-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Feb 12, 2026
@jberkus
Copy link
Contributor

jberkus commented Feb 12, 2026

@mrbobbytables @mfahlandt @stmcginnis Who would be the best person to reach out to for providing and setting the GOOGLE_CALENDAR_API_KEY in the production environment (Netlify) of the webpage?

@ameukam ?

@ameukam
Copy link
Member

ameukam commented Feb 12, 2026

@mrbobbytables @mfahlandt @stmcginnis Who would be the best person to reach out to for providing and setting the GOOGLE_CALENDAR_API_KEY in the production environment (Netlify) of the webpage?

@ameukam ?

That will be @kubernetes/steering-committee which is the current admin group of the Google Workspace org we use: https://github.com/kubernetes/steering#google-workspace

@BenTheElder
Copy link
Member

@BenTheElder
Copy link
Member

Though steering doesn't have access to the Netlify side, just the Google Workspace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

google calendar api key

6 participants