-
Notifications
You must be signed in to change notification settings - Fork 17
Persist Public-Private keypair used by Agent Manager Service for Token Generation #278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,110 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {{- if and .Values.jwtKeysGeneration.enabled (not .Values.jwtSigning.existingSecret) }} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| apiVersion: batch/v1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| kind: Job | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| metadata: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: amp-jwt-keys-generation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| labels: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {{- include "agent-management-platform.labels" . | nindent 4 }} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| app.kubernetes.io/component: jwt-keys-generation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| annotations: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "helm.sh/hook": pre-install,pre-upgrade | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "helm.sh/hook-weight": "-10" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "helm.sh/hook-delete-policy": before-hook-creation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| spec: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| backoffLimit: {{ .Values.jwtKeysGeneration.backoffLimit }} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ttlSecondsAfterFinished: 300 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| template: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| metadata: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: amp-jwt-keys-generation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| labels: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {{- include "agent-management-platform.selectorLabels" . | nindent 8 }} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| app.kubernetes.io/component: jwt-keys-generation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| spec: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {{- include "agent-management-platform.imagePullSecrets" . | nindent 6 }} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| serviceAccountName: {{ include "agent-management-platform.serviceAccountName" . }} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| restartPolicy: Never | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {{- with .Values.jwtKeysGeneration.podSecurityContext }} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| securityContext: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {{- toYaml . | nindent 8 }} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {{- end }} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| containers: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - name: jwt-keys-generation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {{- with .Values.jwtKeysGeneration.securityContext }} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| securityContext: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {{- toYaml . | nindent 12 }} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {{- end }} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| image: "alpine:3.21" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| imagePullPolicy: IfNotPresent | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| command: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - /bin/sh | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - -c | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| set -euo pipefail | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Install required tools | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| apk add --no-cache openssl kubectl curl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Check if secret already exists | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if kubectl get secret {{ include "agent-management-platform.jwtKeysSecretName" . }} -n {{ .Release.Namespace }} &>/dev/null; then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo "JWT keys Secret already exists, skipping generation" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exit 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo "Generating JWT signing keys..." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mkdir -p /tmp/keys | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cd /tmp/keys | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Generate RSA private key (4096 bits for security) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| openssl genrsa -out private.pem 4096 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Extract public key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| openssl rsa -in private.pem -pubout -out public.pem | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Use configured key ID from agentManagerService config | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| KEY_ID="{{ .Values.agentManagerService.config.jwtSigning.activeKeyId }}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| TIMESTAMP=$(date -u +%Y-%m-%dT%H:%M:%SZ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Create public-keys-config.json matching the format expected by gen_keys.sh | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| cat > public-keys-config.json <<EOF | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "keys": [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "kid": "${KEY_ID}", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "algorithm": "RS256", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "publicKeyPath": "/app/keys/public.pem", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "description": "JWT signing key generated by Helm", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "createdAt": "${TIMESTAMP}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| EOF | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+68
to
+80
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Heredoc indentation will produce malformed JSON. The heredoc content is indented with leading spaces which will be included in the generated JSON file, making it invalid. The JSON keys will have leading whitespace. Proposed fix: Use unindented heredoc or remove leading spaces # Create public-keys-config.json matching the format expected by gen_keys.sh
- cat > public-keys-config.json <<EOF
- {
- "keys": [
- {
- "kid": "${KEY_ID}",
- "algorithm": "RS256",
- "publicKeyPath": "/app/keys/public.pem",
- "description": "JWT signing key generated by Helm",
- "createdAt": "${TIMESTAMP}"
- }
- ]
- }
- EOF
+ cat > public-keys-config.json <<'EOF'
+{
+ "keys": [
+ {
+ "kid": "PLACEHOLDER_KEY_ID",
+ "algorithm": "RS256",
+ "publicKeyPath": "/app/keys/public.pem",
+ "description": "JWT signing key generated by Helm",
+ "createdAt": "PLACEHOLDER_TIMESTAMP"
+ }
+ ]
+}
+EOF
+ # Replace placeholders with actual values
+ sed -i "s/PLACEHOLDER_KEY_ID/${KEY_ID}/g" public-keys-config.json
+ sed -i "s/PLACEHOLDER_TIMESTAMP/${TIMESTAMP}/g" public-keys-config.jsonOr use a simpler approach with # Create public-keys-config.json matching the format expected by gen_keys.sh
- cat > public-keys-config.json <<EOF
- {
- ...
- }
- EOF
+ cat > public-keys-config.json << EOF
+{
+ "keys": [
+ {
+ "kid": "${KEY_ID}",
+ "algorithm": "RS256",
+ "publicKeyPath": "/app/keys/public.pem",
+ "description": "JWT signing key generated by Helm",
+ "createdAt": "${TIMESTAMP}"
+ }
+ ]
+}
+EOFNote: The 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Verify keys were generated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if [[ ! -f private.pem ]] || [[ ! -f public.pem ]] || [[ ! -f public-keys-config.json ]]; then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo "ERROR: Key generation failed - missing key files" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| exit 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+83
to
+86
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Critical: Bash syntax The script uses Proposed fix: Use POSIX-compliant test syntax- if [[ ! -f private.pem ]] || [[ ! -f public.pem ]] || [[ ! -f public-keys-config.json ]]; then
+ if [ ! -f private.pem ] || [ ! -f public.pem ] || [ ! -f public-keys-config.json ]; then📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo "Creating Kubernetes Secret with JWT keys..." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| kubectl create secret generic {{ include "agent-management-platform.jwtKeysSecretName" . }} \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| -n {{ .Release.Namespace }} \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| --from-file=private.pem=private.pem \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| --from-file=public.pem=public.pem \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| --from-file=public-keys-config.json=public-keys-config.json \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| --dry-run=client -o yaml | \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| kubectl apply -f - | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Add annotations for tracking | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| kubectl annotate secret {{ include "agent-management-platform.jwtKeysSecretName" . }} \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| -n {{ .Release.Namespace }} \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| amp.wso2.com/keys-version="1" \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| amp.wso2.com/key-id="${KEY_ID}" \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| amp.wso2.com/generated-at="${TIMESTAMP}" \ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| --overwrite | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| echo "JWT keys Secret created successfully with key ID: ${KEY_ID}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {{- with .Values.jwtKeysGeneration.resources }} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| resources: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {{- toYaml . | nindent 12 }} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {{- end }} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| {{- end }} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove duplicate startup message.
"Starting agent-manager-service..." is printed twice: once at line 24 and again at line 41. Consider removing the first occurrence since the actual startup happens after key handling.
Proposed fix
Or simply remove line 24:
Also applies to: 41-41
🤖 Prompt for AI Agents