This commit adds complete Terraform infrastructure as code for deploying code-server on both EC2 and EKS platforms with enterprise-grade security and SAML/OIDC authentication. Features: - EC2 deployment with Auto Scaling Groups and Application Load Balancer - EKS deployment with managed node groups and AWS Load Balancer Controller - Private network setup with VPC, private subnets, and NAT gateways - SAML/OIDC authentication using OAuth2 Proxy - Security hardening: - KMS encryption for data at rest - TLS encryption in transit - IAM roles with least privilege - Security groups with minimal access - VPC Flow Logs - IMDSv2 enforcement - Auto-scaling capabilities for both EC2 and EKS - CloudWatch logging and monitoring - Automated deployment scripts Terraform Modules: - modules/vpc: VPC with public/private subnets, NAT, and VPC endpoints - modules/security: Security groups, IAM roles, and KMS keys - modules/ec2: EC2 Auto Scaling deployment with ALB - modules/eks: EKS cluster with managed node groups and addons Deployments: - deployments/ec2: EC2 deployment configuration - deployments/eks: EKS deployment configuration with Kubernetes manifests Documentation: - README.md: Comprehensive deployment and operations guide - QUICK-START.md: Quick reference for fast deployment - SAML-SETUP-GUIDE.md: Step-by-step IdP configuration guide Scripts: - scripts/deploy-ec2.sh: Automated EC2 deployment - scripts/deploy-eks.sh: Automated EKS deployment - scripts/destroy-ec2.sh: EC2 cleanup - scripts/destroy-eks.sh: EKS cleanup
12 KiB
SAML/OIDC Authentication Setup Guide
This guide provides step-by-step instructions for configuring various identity providers with code-server deployment.
Table of Contents
- Overview
- Okta Setup
- Azure Active Directory Setup
- Google Workspace Setup
- AWS IAM Identity Center (SSO) Setup
- Generic OIDC Provider
- Testing Authentication
- Troubleshooting
Overview
The code-server deployment uses OAuth2 Proxy to provide authentication via SAML/OIDC. This acts as a reverse proxy that handles authentication before requests reach code-server.
Key Concepts
- OIDC Discovery URL: Endpoint that provides IdP configuration
- Client ID: Unique identifier for your application
- Client Secret: Secret key for authentication
- Redirect URI: URL where users return after authentication
- Cookie Secret: Secret for encrypting session cookies
Okta Setup
1. Create Application in Okta
- Log in to your Okta admin console
- Navigate to Applications → Applications
- Click Create App Integration
- Select:
- Sign-in method: OIDC - OpenID Connect
- Application type: Web Application
- Click Next
2. Configure Application
General Settings:
- App integration name: Code-Server
- Logo: (optional) Upload code-server logo
Sign-in redirect URIs:
https://code-server.example.com/oauth2/callback
Sign-out redirect URIs:
https://code-server.example.com
Assignments:
- Controlled access: Choose who can access (Everyone, specific groups, etc.)
Click Save
3. Get Configuration Values
After creating the application:
- Copy Client ID
- Copy Client Secret (click "Show" if hidden)
- Note your Okta domain (e.g.,
dev-12345.okta.com)
4. Configure Terraform Variables
# terraform.tfvars
oauth2_issuer_url = "https://dev-12345.okta.com/.well-known/openid-configuration"
oauth2_client_id = "<YOUR_CLIENT_ID>"
oauth2_client_secret = "<YOUR_CLIENT_SECRET>"
oauth2_redirect_url = "https://code-server.example.com/oauth2/callback"
# Generate cookie secret
# python -c 'import os,base64; print(base64.urlsafe_b64encode(os.urandom(32)).decode())'
oauth2_cookie_secret = "<GENERATED_SECRET>"
# Optional: Restrict to specific users
oauth2_allowed_emails = [
"user1@company.com",
"user2@company.com"
]
5. Assign Users
In Okta admin console:
- Go to Applications → Code-Server
- Click Assignments tab
- Click Assign → Assign to People or Assign to Groups
- Add users/groups who should have access
Azure Active Directory Setup
1. Register Application
- Log in to Azure Portal
- Navigate to Azure Active Directory → App registrations
- Click New registration
2. Configure Application
Name: Code-Server Supported account types:
- Single tenant (most common)
- Or Multi-tenant if needed
Redirect URI:
- Platform: Web
- URL:
https://code-server.example.com/oauth2/callback
Click Register
3. Configure Authentication
- Go to Authentication in left menu
- Under Implicit grant and hybrid flows, check:
- ✅ ID tokens (used for implicit and hybrid flows)
- Under Advanced settings:
- Allow public client flows: No
- Click Save
4. Create Client Secret
- Go to Certificates & secrets in left menu
- Click New client secret
- Description: Code-Server
- Expires: Choose duration (24 months recommended)
- Click Add
- Copy the secret value immediately (it won't be shown again)
5. API Permissions
- Go to API permissions in left menu
- Verify these permissions exist:
- Microsoft Graph →
openid - Microsoft Graph →
profile - Microsoft Graph →
email
- Microsoft Graph →
- Click Grant admin consent (if you have admin rights)
6. Get Configuration Values
From Overview page:
- Application (client) ID: Copy this
- Directory (tenant) ID: Copy this
7. Configure Terraform Variables
# terraform.tfvars
oauth2_issuer_url = "https://login.microsoftonline.com/<TENANT_ID>/v2.0/.well-known/openid-configuration"
oauth2_client_id = "<APPLICATION_CLIENT_ID>"
oauth2_client_secret = "<CLIENT_SECRET>"
oauth2_redirect_url = "https://code-server.example.com/oauth2/callback"
oauth2_cookie_secret = "<GENERATED_SECRET>"
# Optional: Restrict by email
oauth2_allowed_emails = [
"user1@company.com",
"user2@company.com"
]
8. Restrict Access (Optional)
To limit access to specific users/groups:
- Go to Enterprise applications
- Find your Code-Server application
- Go to Properties
- Set User assignment required? to Yes
- Go to Users and groups
- Click Add user/group
- Select users or groups
Google Workspace Setup
1. Create OAuth Client
- Go to Google Cloud Console
- Select or create a project
- Navigate to APIs & Services → Credentials
- Click Create Credentials → OAuth client ID
2. Configure OAuth Consent Screen
If prompted:
- Click Configure Consent Screen
- User Type: Internal (for Google Workspace) or External
- Fill in application information:
- App name: Code-Server
- User support email: Your email
- Developer contact: Your email
- Scopes: Add
openid,email,profile - Click Save and Continue
3. Create OAuth Client ID
Application type: Web application
Name: Code-Server
Authorized redirect URIs:
https://code-server.example.com/oauth2/callback
Click Create
4. Get Configuration Values
After creation:
- Copy Client ID
- Copy Client Secret
5. Configure Terraform Variables
# terraform.tfvars
oauth2_issuer_url = "https://accounts.google.com/.well-known/openid-configuration"
oauth2_client_id = "<YOUR_CLIENT_ID>.apps.googleusercontent.com"
oauth2_client_secret = "<YOUR_CLIENT_SECRET>"
oauth2_redirect_url = "https://code-server.example.com/oauth2/callback"
oauth2_cookie_secret = "<GENERATED_SECRET>"
# Restrict to your domain
oauth2_allowed_emails = [
"user1@company.com",
"user2@company.com"
]
6. Domain Restriction (Google Workspace)
To restrict to your entire domain:
# For EKS: k8s/oauth2-proxy.yaml
# Add to ConfigMap:
email_domains = ["company.com"]
AWS IAM Identity Center (SSO) Setup
1. Enable IAM Identity Center
- Go to IAM Identity Center
- Enable IAM Identity Center if not already enabled
- Note your AWS access portal URL
2. Register Application
- In IAM Identity Center, go to Applications
- Click Add application
- Select I have an application I want to set up
- Click Next
3. Configure Application
Display name: Code-Server
Description: Code-Server IDE
Application start URL: https://code-server.example.com
Application metadata:
- Choose Manual entry
- Application ACS URL:
https://code-server.example.com/oauth2/callback - Application SAML audience:
https://code-server.example.com
Click Submit
4. Get Configuration Values
- Download the IAM Identity Center SAML metadata file
- Note the Client ID (from application details)
- Create a Client Secret (in application settings)
5. Configure Terraform Variables
# terraform.tfvars
# Use OIDC endpoint for your region
oauth2_issuer_url = "https://portal.sso.<region>.amazonaws.com/.well-known/openid-configuration"
oauth2_client_id = "<YOUR_CLIENT_ID>"
oauth2_client_secret = "<YOUR_CLIENT_SECRET>"
oauth2_redirect_url = "https://code-server.example.com/oauth2/callback"
oauth2_cookie_secret = "<GENERATED_SECRET>"
6. Assign Users
- Go to Assigned users tab
- Click Assign users
- Select users or groups
- Click Assign users
Generic OIDC Provider
For any OIDC-compliant provider:
1. Required Information
Obtain from your IdP:
- OIDC Discovery URL (usually
https://idp.example.com/.well-known/openid-configuration) - Client ID
- Client Secret
- Supported scopes (typically
openid,profile,email)
2. Register Redirect URI
In your IdP, register:
https://code-server.example.com/oauth2/callback
3. Configure Terraform Variables
# terraform.tfvars
oauth2_issuer_url = "<OIDC_DISCOVERY_URL>"
oauth2_client_id = "<CLIENT_ID>"
oauth2_client_secret = "<CLIENT_SECRET>"
oauth2_redirect_url = "https://code-server.example.com/oauth2/callback"
oauth2_cookie_secret = "<GENERATED_SECRET>"
Testing Authentication
1. Deploy Application
# EC2
cd deployments/ec2
terraform apply
# EKS
cd deployments/eks
terraform apply
kubectl apply -f k8s/oauth2-proxy.yaml
2. Access Application
Navigate to your code-server URL (e.g., https://code-server.example.com)
3. Expected Flow
- Browser redirects to IdP login page
- Enter credentials and authenticate
- IdP redirects back to code-server with authorization code
- OAuth2 Proxy exchanges code for tokens
- Session cookie is set
- Request is proxied to code-server
- Code-server interface loads
4. Verify Session
After successful login:
- Check browser cookies for
_oauth2_proxycookie - Cookie should be HttpOnly, Secure, and SameSite
5. Test Logout
Navigate to: https://code-server.example.com/oauth2/sign_out
You should be logged out and redirected to IdP
Troubleshooting
Common Issues
Redirect URI Mismatch
Error: redirect_uri_mismatch or similar
Solution:
- Verify redirect URI in IdP exactly matches Terraform configuration
- Check for trailing slashes (should not have them)
- Ensure HTTPS (not HTTP)
Invalid Client
Error: invalid_client
Solution:
- Verify Client ID is correct
- Verify Client Secret is correct and not expired
- Check that client is enabled in IdP
Cookie Errors
Error: Authentication succeeds but session is not maintained
Solution:
- Ensure
oauth2_cookie_secretis set and is 32 bytes (base64 encoded) - Verify domain in cookie matches your URL
- Check browser is accepting cookies
- Ensure HTTPS is configured (cookies may not work over HTTP)
Access Denied
Error: User authenticates but gets "Access Denied"
Solution:
- Check
oauth2_allowed_emailslist - Verify user is assigned to application in IdP
- Check OAuth2 Proxy logs:
# EC2 aws logs tail /aws/ec2/<prefix>-code-server --filter-pattern oauth2-proxy # EKS kubectl logs -n code-server -l app=oauth2-proxy
Issuer URL Not Accessible
Error: error fetching OIDC discovery
Solution:
- Verify issuer URL is accessible from your network
- Check security groups allow outbound HTTPS
- Verify URL is correct (test in browser)
- Check for typos in URL
Debug Mode
Enable debug logging:
EC2:
Edit modules/ec2/user-data.sh and add to oauth2-proxy args:
- --log-level=debug
EKS:
Edit deployments/eks/k8s/oauth2-proxy.yaml:
args:
- --log-level=debug
Testing Connectivity
Test OIDC Discovery:
curl -s https://your-idp.com/.well-known/openid-configuration | jq .
Test Redirect:
# Should show OAuth2 login page
curl -I https://code-server.example.com
Check OAuth2 Proxy Health:
# EC2
curl http://<instance-ip>:4180/ping
# EKS
kubectl port-forward -n code-server svc/oauth2-proxy 4180:4180
curl http://localhost:4180/ping
Security Best Practices
- Use HTTPS: Always use HTTPS for production
- Rotate Secrets: Regularly rotate client secrets and cookie secrets
- Limit Scope: Request only necessary OIDC scopes
- Session Timeout: Configure appropriate session expiry
- Restrict Emails: Use
oauth2_allowed_emailsto limit access - Monitor Logs: Regularly review authentication logs
- Use Groups: Manage access via IdP groups rather than individual users