fix(calendar): remove hardcoded api key and inject via env var#623
fix(calendar): remove hardcoded api key and inject via env var#623TineoC wants to merge 2 commits intokubernetes:masterfrom
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: TineoC The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
71e96f0 to
eefacc4
Compare
|
Before merging this changes. Please follow the steps mentioned in "Production Setup Instructions (Netlify)" |
b315ccc to
6b4e71d
Compare
hugo.yaml
Outdated
|
|
||
| # Everything below this are Site Params | ||
| params: | ||
| google_calendar_api_key: 'PLACEHOLDER_VALUE' |
There was a problem hiding this comment.
for allowing devs add their Google Calendar keys for local development. but is simpler to just use them in prod. I agree with you
layouts/calendar/baseof.html
Outdated
| <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 "" }} |
There was a problem hiding this comment.
Can we make the requirement conditional on whether it is a production build?
static/js/calendar.js
Outdated
|
|
||
| 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>'; |
layouts/calendar/baseof.html
Outdated
| <script> | ||
| renderCalendar(); | ||
| {{ if hugo.IsProduction }} | ||
| {{ $apiKey := getenv "HUGO_GOOGLE_CALENDAR_API_KEY" | default "" }} |
There was a problem hiding this comment.
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
errorfto outright fail the build
| document.addEventListener('DOMContentLoaded', function() { | ||
| var calendarEl = document.getElementById('calendar'); | ||
|
|
||
| if (!calendarEl) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
like this?
<html {{ if hugo.IsProduction }}data-isproduction="true"{{ end }}>|
@mrbobbytables @mfahlandt @stmcginnis Who would be the best person to reach out to for providing and setting the |
@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 |
|
Though steering doesn't have access to the Netlify side, just the Google Workspace. |
|
@katcosgrove @aojea @soltysh Hi! we need to set up a Google API Key for our Calendar to remove this public key for being stored in the source code. What needs to be done?
|
|
This is being handled in this slack thread. |
|
/uncc Happy to approve this change once it's ready for that. I recommend adding a hold before asking for approval, because the unhold is easier to secure than approval. |
|
/hold |
|
The Netlify steps are done. The GOOGLE_CALENDAR_API_KEY env var is added and a deploy is triggered |
|
@reylejano could you double check the preview in netlify? Is getting 400 error: |
8ec4e20 to
8ae4064
Compare
8ae4064 to
626f561
Compare
|
#623 (comment) @reylejano my bad! the description was incorrect. The right key is: |
I changed it to the right key |
abb1d95 to
1ccdc9c
Compare
This PR addresses the security issue of having a hardcoded Google Calendar API key in the source code and improves the robustness of the implementation.
Changes
static/js/calendar.js.HUGO_GOOGLE_CALENDAR_API_KEYenvironment variable.google_calendar_api_keysite parameter inhugo.yaml.jsfilter withjsonify | safeJSto correctly handle empty strings (previously it rendered an empty object{}, which was truthy in JS and caused 400 errors).AIza...) in the browser console to verify which key is being used without exposing it entirely.renderCalendarfunction now properly detects missing keys and displays a Bootstrap alert instead of attempting a failed API call.Setup Instructions
1. Google Cloud Console (Obtain API Key)
AIza).kubernetes.devand your Netlify preview domains.2. Netlify Dashboard
HUGO_GOOGLE_CALENDAR_API_KEYAIza...).Local Development
For local development, the calendar will show a placeholder message by default. To test functionality locally:
netlify devif you have the CLI linked.Closes #45