The CMS machine-translates English pages into Kannada and Hindi with Cloud Translation, Basic edition (v2), authenticated with an API key an Admin pastes into the CMS. This page creates that key, locks it down, tests it, and saves it.
#How the code uses the key
#What is called, and from where
| Fact | Value (from the code) |
|---|---|
| Edition | Basic, v2. Advanced (v3) does not accept API keys, which is why v2 is used |
| Endpoint | https://translation.googleapis.com/language/translate/v2 |
| Calls | GET .../v2/languages?key=... and POST .../v2?key=... with JSON {"q": [...], "source": "en", "target": "<lang>", "format": "html"} |
| Who calls it | The CMS pod (when an Admin saves the key: the test) and the worker pods (translation jobs). The translation-tick CronJob only queues work |
| Where the key is kept | PostgreSQL table secrets, row google.translation.api-key, sealed with AES-256-GCM under SECRETS_KEY |
| What the screen shows | The key's last four characters, who saved it and when |
| Who may save it | CMS Admin (and Superadmin) only: permission translation.manage |
#What happens when Google refuses
The worker sorts each refusal into a kind, and the kind decides what happens:
| Kind | Google's reason, for example | What the CMS does |
|---|---|---|
key |
keyInvalid, API_KEY_INVALID, accessNotConfigured, SERVICE_DISABLED, API_KEY_SERVICE_BLOCKED, ipRefererBlocked, PERMISSION_DENIED, HTTP 401 |
Every job stops until a working key is saved. Saving one clears the block |
quota |
dailyLimitExceeded, quotaExceeded, billingNotEnabled, BILLING_DISABLED |
The whole job stops until the next day or until billing is fixed, then resumes by itself |
rate |
HTTP 429, rateLimitExceeded |
Slows down |
transient |
HTTP 5xx | That page waits and is tried again |
language |
HTTP 400 naming the target language | That language stops |
#Before you start
- The Google Cloud project exists, with billing linked and the Cloud Translation API enabled (google/cloud-project).
- You have API Keys Admin (or Editor/Owner) on the project.
- You know the public IP addresses the EKS pods leave through (see Find the pods' egress IP addresses).
- An Admin can sign in to the CMS (deploy/people), for the last step.
#Create the key
#Console: create an API key
Make one key per environment: kims-translation-uat and kims-translation-production.
- In the Google Cloud console, select the KIMS project in the project picker.
- Open the navigation menu > APIs & Services > Credentials.
- Click Create credentials > API key.
- If the dialog offers a name, enter
kims-translation-uat. If it offers Authenticate API calls through a service account, leave it unticked: the CMS needs a standard key. - If the console asks for a restriction before creating (newer consoles do), choose API restrictions > Restrict key > Cloud Translation API.
- Click Create.
- The API key created dialog shows the key. Copy it straight into KIMS's password vault. Do not paste it into chat, email, tickets or a file in Git.
You should see the key in the API Keys list on the Credentials page, with a warning icon until it is restricted.
An API key is a secret. Anyone with it can spend KIMS's money on translation. Never put it in a values file, a Jenkins job, a shell command line or this site.
#Rename the key
- On Credentials, click the key's name.
- Set Name to
kims-translation-uat(or-production). - Click Save.
#Restrict the key
#API restriction: Cloud Translation API only
- On Credentials, click the key's name.
- Under API restrictions, choose Restrict key.
- In the drop-down, tick Cloud Translation API only. Untick everything else.
- Click Save.
You should see Cloud Translation API listed under the key's restrictions. A refusal from any other API with this key is then expected.
#Find the pods' egress IP addresses
An IP restriction only helps if it lists the public addresses Google sees, which are those of the VPC's NAT gateways (or KIMS's outbound proxy or firewall). Internal (private) addresses are not supported by Google's IP restriction.
Ask from inside a pod, which also proves the pod can reach the internet:
kubectl --context <KUBE_CONTEXT> -n <NAMESPACE> exec deploy/kims-worker -- node -e "fetch('https://checkip.amazonaws.com').then(r=>r.text()).then(t=>console.log(t.trim()))"kubectl --context <KUBE_CONTEXT> -n <NAMESPACE> exec deploy/kims-worker -- node -e "fetch('https://checkip.amazonaws.com').then(r=>r.text()).then(t=>console.log(t.trim()))"kubectl --context <KUBE_CONTEXT> -n <NAMESPACE> exec deploy/kims-worker -- node -e "fetch('https://checkip.amazonaws.com').then(r=>r.text()).then(t=>console.log(t.trim()))"Expected: one public IPv4 address, for example 13.234.10.20. Run it a few times and on the CMS too (deploy/kims-cms): with several NAT gateways (one per Availability Zone) you will see different addresses. Cross-check with AWS:
aws ec2 describe-nat-gateways --region <AWS_REGION> --filter Name=vpc-id,Values=<VPC_ID> Name=state,Values=available --query "NatGateways[].NatGatewayAddresses[].PublicIp" --output textaws ec2 describe-nat-gateways --region <AWS_REGION> --filter Name=vpc-id,Values=<VPC_ID> Name=state,Values=available --query "NatGateways[].NatGatewayAddresses[].PublicIp" --output textaws ec2 describe-nat-gateways --region <AWS_REGION> --filter Name=vpc-id,Values=<VPC_ID> Name=state,Values=available --query "NatGateways[].NatGatewayAddresses[].PublicIp" --output textExpected: every NAT gateway's Elastic IP, separated by tabs. The list from the pods must be inside this list (or be KIMS's proxy address). If the command needs the VPC ID, aws/networking has it.
The pods do not exist until the first deploy (deploy/first-deploy). It is fine to create the key with only the API restriction first, then add the IP restriction once the pods are running.
#Application restriction: IP addresses
- On Credentials, click the key's name.
- Under Application restrictions (on some consoles, Set an application restriction), choose IP addresses.
- Click Add, enter one egress address (for example
13.234.10.20, or a range such as13.234.10.0/28), click Done. Repeat for each address. - Click Save.
Changes can take up to five minutes to apply.
Once the IP restriction is on, the key works only from KIMS's network. A test from your laptop then fails with HTTP 403. That is expected. Test from your laptop before adding it, or rely on the CMS's own test (it runs in the CMS pod). If KIMS's NAT addresses change (a NAT gateway is recreated), translation stops with a key block: update the restriction.
#Quotas, budget and pricing
#Pricing
Cloud Translation Basic is billed per character sent, including HTML tags and characters that are not translated. At the time of writing, Google lists USD 20 per million characters, with the first 500,000 characters a month covered by a USD 10 monthly credit shared across Basic and Advanced. Always check the live pricing page (Sources).
To estimate: (characters of English text to translate) x (number of languages). Example: 5 million characters of page text into Kannada and Hindi is 10 million characters, roughly USD 190 after the credit. The CMS translates only what changed after the first run.
#Budget and alerts
- Open the navigation menu > Billing > Budgets & alerts.
- Click Create budget.
- Name:
KIMS translation. Projects: the KIMS project. Services: Cloud Translation API (or all services). Click Next. - Budget type: Specified amount. Target amount: what KIMS agrees, for example the monthly estimate plus 50 percent. Click Next.
- Keep the thresholds at 50, 90 and 100 percent of actual spend. Tick Email alerts to billing admins and users, and add KIMS's monitoring email through a notification channel if they want.
- Click Finish.
You should see the budget in the list with its current spend. A budget only alerts; it does not stop spending.
#Cap usage with a quota (optional)
To put a hard ceiling on spend, lower the Translation API's characters-per-day quota:
- Open IAM & Admin > Quotas & System Limits.
- Filter by Service: Cloud Translation API.
- Tick the characters-per-day quota (the exact name differs between console versions), click Edit quotas (or the pencil), enter a new value, and submit.
When the cap is reached, Google refuses with a daily-limit reason, the CMS marks the job quota and resumes it the next day by itself. Set the cap well above a normal day's work, or the first full translation of the site will take several days.
#Test the key from a terminal
Run this before adding the IP restriction, or from a machine whose address is in the restriction. The commands read the key without echoing it and send it in a header, so it does not end up in shell history or in a URL.
#Test: list the languages
read -rs -p "API key: " GKEY; echo
curl -s -H "X-goog-api-key: $GKEY" "https://translation.googleapis.com/language/translate/v2/languages" | head -c 200; echoread -rs "GKEY?API key: "; echo # zsh, the macOS default shell
# in bash instead: read -rs -p "API key: " GKEY; echo
curl -s -H "X-goog-api-key: $GKEY" "https://translation.googleapis.com/language/translate/v2/languages" | head -c 200; echo$sec = Read-Host "API key" -AsSecureString
$GKEY = [Runtime.InteropServices.Marshal]::PtrToStringBSTR([Runtime.InteropServices.Marshal]::SecureStringToBSTR($sec))
$r = Invoke-RestMethod -Uri "https://translation.googleapis.com/language/translate/v2/languages" -Headers @{ "X-goog-api-key" = $GKEY }
$r.data.languages.CountExpected on Linux and macOS: JSON starting { "data": { "languages": [ { "language": "af" }, .... On Windows: a number above 100 (the count of languages).
#Test: translate one word into Hindi
curl -s -X POST -H "X-goog-api-key: $GKEY" -H "Content-Type: application/json" \
-d '{"q":["Hello"],"source":"en","target":"hi","format":"html"}' \
"https://translation.googleapis.com/language/translate/v2"; echo
unset GKEYcurl -s -X POST -H "X-goog-api-key: $GKEY" -H "Content-Type: application/json" \
-d '{"q":["Hello"],"source":"en","target":"hi","format":"html"}' \
"https://translation.googleapis.com/language/translate/v2"; echo
unset GKEY$body = @{ q = @("Hello"); source = "en"; target = "hi"; format = "html" } | ConvertTo-Json
$r = Invoke-RestMethod -Method Post -Uri "https://translation.googleapis.com/language/translate/v2" -Headers @{ "X-goog-api-key" = $GKEY } -ContentType "application/json; charset=utf-8" -Body $body
$r.data.translations[0].translatedText
Remove-Variable GKEY, secExpected: {"data":{"translations":[{"translatedText":"नमस्ते"}]}} (formatting may differ), or on Windows the Hindi word on its own line. The Windows console may show it as boxes if its font lacks Devanagari; the answer is still right.
#What a failed test means
| Answer | Cause | Fix |
|---|---|---|
400, API key not valid |
Key mistyped or deleted | Copy it again from Credentials > the key > Show key |
403, SERVICE_DISABLED or accessNotConfigured |
Cloud Translation API not enabled | google/cloud-project |
403, API_KEY_SERVICE_BLOCKED |
The key's API restriction does not include Cloud Translation API | API restriction |
403 naming the IP address or PERMISSION_DENIED from your laptop |
The IP restriction is working | Expected; test from a pod via the CMS instead |
403, BILLING_DISABLED or billingNotEnabled |
No billing account | Link a billing account |
| Hangs, then times out | No outbound HTTPS to Google | google/outbound-access |
#Save the key in the CMS
#Steps on the Translation screen
- Sign in to the CMS as an Admin:
https://<CMS_HOST>. - Open Translation in the left navigation.
- Paste the key into the key field and click the save button.
- Wait a few seconds: the CMS calls Google from the CMS pod (the language list, then "Hello" into Hindi) before it stores anything.
You should see the key's last four characters, your email and the time. Nothing else of the key is ever shown again.
#Messages the screen can give
| Message | Meaning |
|---|---|
That does not look like a Google API key. Copy the whole key from Google Cloud. |
It is not 30 to 60 letters, digits, - or _. Usually a partial copy |
Google refused this key. followed by Google's words |
The key, its restrictions, the API or the IP restriction: Google's words say which |
The key could not be checked. followed by the error |
Usually the pod cannot reach translation.googleapis.com (google/outbound-access) |
Only an Admin can manage machine translation. |
Sign in as an Admin |
#Check a translation end to end
- Open any short English page in the CMS.
- Use its translation controls to machine-translate it into Kannada or Hindi.
- Within about five minutes (the
translation-tickCronJob runs every five minutes and the worker picks it up), a Kannada or Hindi draft appears for that page.
If nothing happens, read the worker's log:
kubectl --context <KUBE_CONTEXT> -n <NAMESPACE> logs deploy/kims-worker --since=15m | grep -i translatkubectl --context <KUBE_CONTEXT> -n <NAMESPACE> logs deploy/kims-worker --since=15m | grep -i translatkubectl --context <KUBE_CONTEXT> -n <NAMESPACE> logs deploy/kims-worker --since=15m | Select-String -Pattern translat#Rotate or remove the key
#Rotate
- Create a new key with the same restrictions (Create the key, Restrict the key).
- Save it on the CMS Translation screen. Saving replaces the old one.
- Run a small translation to check it.
- In the console, Credentials > the old key > Delete.
#Remove
On the Translation screen, remove the key. Translation jobs then stop with a key block until a new key is saved.
#If SECRETS_KEY changed
If the secrets-key in the kims-app Secret was ever replaced, the stored key cannot be decrypted. Save the key again on the Translation screen (deploy/secrets).
#UAT and production
| Step | UAT | Production |
|---|---|---|
| Key | kims-translation-uat |
kims-translation-production, a separate key |
| IP restriction | UAT cluster's NAT addresses | Production cluster's NAT addresses (the same if both share a VPC) |
| Saved in | UAT CMS, Translation screen | Production CMS, Translation screen (it is a different database) |
| Data bundle | The key does not travel in the bundle: it was encrypted with the old environment's SECRETS_KEY |
Same |
#Sources
kims-v2/packages/translate/src/google.tskims-v2/apps/cms/src/lib/translation/key.tskims-v2/apps/cms/src/lib/translation/key-test.tskims-v2/apps/cms/src/app/(app)/translation/actions.tskims-v2/apps/cms/src/lib/auth/policy.tskims-v2/packages/secrets/src/index.tskims-v2/apps/worker/src/schedule.tskims-v2/deploy/helm/kims/values.yaml- https://docs.cloud.google.com/docs/authentication/api-keys
- https://cloud.google.com/translate/docs/basic/translating-text
- https://cloud.google.com/translate/pricing
- https://cloud.google.com/translate/quotas
- https://cloud.google.com/billing/docs/how-to/budgets