How to Secure Your AWS EKS Application with OAuth 2.0 and SSO Integration Using Amazon Cognito and OAuth2-Proxy

akhil_mittal

akhil mittal

Posted on October 14, 2024

How to Secure Your AWS EKS Application with OAuth 2.0 and SSO Integration Using Amazon Cognito and OAuth2-Proxy

To integrate OAuth 2.0 authentication and authorization with Single Sign-On (SSO) for a service deployed in an AWS EKS (Elastic Kubernetes Service) cluster, you can leverage an Identity Provider (IdP) like Amazon Cognito, Okta, or Auth0. This setup typically involves configuring OAuth 2.0 for your application to authenticate users via SSO and authorize access to your resources based on their roles or groups.

Here’s a detailed step-by-step guide for setting up OAuth 2.0-based authentication and authorization for your service in AWS EKS:

Overview:

  1. Identity Provider (IdP) setup for OAuth 2.0 and SSO.
  2. Application configuration to use OAuth 2.0 with the IdP.
  3. Kubernetes Ingress configuration to integrate with OAuth 2.0 for external traffic.
  4. Authorization using role-based access control (RBAC) in your application.

Step 1: Choose and Set Up an Identity Provider (IdP)

You’ll need an Identity Provider that supports OAuth 2.0 and SSO. Common IdPs are AWS Cognito, Okta, or Auth0.

Option 1: Amazon Cognito

  1. Create a User Pool:
    • Go to the Amazon Cognito Console and create a User Pool. This pool will manage users for your application and provide OAuth 2.0 tokens.
  • SSO Configuration: Set up your Cognito User Pool to use SSO by integrating it with external IdPs (such as Google, Facebook, or corporate SAML providers).
  1. Configure App Client:

    • Create an App Client for the application that will use Cognito for OAuth 2.0. This client will receive the OAuth 2.0 tokens.
    • Enable OAuth 2.0 flows such as Authorization Code and Implicit Grant depending on your application.
    • Set the Callback URL (the URL to which Cognito will redirect the user after successful authentication).
  2. Create a Domain Name:

    • Configure a domain name for your user pool where authentication requests will be directed (e.g., https://your-app.auth.region.amazoncognito.com).
  3. Configure OAuth Scopes:

    • In Cognito, define OAuth scopes such as openid, profile, email, and any other custom scopes for access control.

Option 2: Okta or Auth0

  • Sign Up and Create an Application: Create an account in Okta or Auth0 and configure an OAuth 2.0 application. Set the redirect URIs to handle authentication redirection after login.
  • SSO Integration: Okta and Auth0 have built-in support for integrating with corporate SSO providers (like SAML or OpenID Connect).
  • Generate OAuth 2.0 Credentials: These platforms will generate a client ID, client secret, and other credentials necessary to configure OAuth 2.0 for your application.

Step 2: Configure Your Application for OAuth 2.0

In your service (deployed in EKS), you’ll need to integrate with the OAuth 2.0 provider to handle authentication and authorization.

  1. Configure OAuth Middleware in Your Application:

    • Use an OAuth 2.0 middleware library (depending on your language/framework) to handle authentication. Some common libraries are:
      • Node.js: Use passport.js with the OAuth2 strategy.
      • Java (Spring): Use Spring Security OAuth2.
      • Python (Flask/Django): Use Authlib or Flask-OAuthlib.
  2. Handle OAuth Redirects:

    • Configure your application to handle OAuth redirect URIs and callback URLs.
    • The application should send users to the IdP’s login page and handle the authorization code or tokens returned in the redirect.
  3. Retrieve Tokens:

    • After a successful login, the OAuth 2.0 provider will return an access token (and optionally an ID token and refresh token) to the callback URL.
    • Store these tokens securely (in memory or a session) and use them to authenticate subsequent API requests.
  4. Authorize Requests:

    • Use the OAuth 2.0 access token to validate API requests. The token should be included in the Authorization header of each request:
     Authorization: Bearer <access_token>
    
  • Validate Tokens: Validate the token with the IdP to ensure it’s still valid, contains the right scopes, and hasn’t expired.

Step 3: Configure Ingress with OAuth Proxy in EKS

To protect your application and enforce authentication, you can use an OAuth 2.0 Proxy that acts as a gatekeeper in front of your Kubernetes service. This is especially useful for applications exposed via Ingress in Kubernetes.

3.1. Deploy an OAuth 2.0 Proxy (e.g., oauth2-proxy)

  1. Install OAuth2-Proxy:
    • OAuth2-Proxy is a widely used tool that integrates with OAuth 2.0 providers and sits in front of your service to handle authentication.
    • Install it via Helm or YAML manifests into your EKS cluster:
   helm repo add oauth2-proxy https://oauth2-proxy.github.io/manifests
   helm repo update
   helm install oauth2-proxy oauth2-proxy/oauth2-proxy --namespace <namespace>
Enter fullscreen mode Exit fullscreen mode
  1. Configure OAuth2-Proxy:
    • Set up oauth2-proxy to authenticate with your chosen IdP (Cognito, Okta, etc.). You’ll need to pass the client ID, client secret, and redirect URIs.

Example configuration (values.yaml for OAuth2-Proxy):

   config:
     clientID: "<your-client-id>"
     clientSecret: "<your-client-secret>"
     provider: "oidc"
     oidcIssuerURL: "https://your-app.auth.region.amazoncognito.com"
     cookieSecret: "<randomly-generated-secret>"
     redirectURL: "https://your-app.com/oauth2/callback"
     upstreams:
       - "http://<your-service>:<service-port>"
Enter fullscreen mode Exit fullscreen mode
  1. Configure Kubernetes Ingress: Modify your Ingress resource to include the OAuth2 proxy as a reverse proxy for your application.

Example Ingress configuration with annotations for OAuth2-Proxy:

   apiVersion: networking.k8s.io/v1
   kind: Ingress
   metadata:
     name: myapp-ingress
     annotations:
       kubernetes.io/ingress.class: nginx
       nginx.ingress.kubernetes.io/auth-url: "https://<oauth2-proxy-service>/oauth2/auth"
       nginx.ingress.kubernetes.io/auth-signin: "https://<oauth2-proxy-service>/oauth2/start?rd=$scheme://$host$request_uri"
   spec:
     rules:
       - host: myapp.example.com
         http:
           paths:
             - path: /
               pathType: Prefix
               backend:
                 service:
                   name: myapp-service
                   port:
                     number: 80
Enter fullscreen mode Exit fullscreen mode

3.2. Verify OAuth2 Authentication

  • When users try to access the service, they will be redirected to the OAuth provider’s login page (e.g., Amazon Cognito, Okta, or Auth0).
  • After successful authentication, the user will be redirected back to your application, and OAuth2-Proxy will validate the token before allowing access.

Step 4: Role-Based Access Control (RBAC) for Authorization

Once OAuth 2.0 authentication is in place, you can implement authorization using RBAC based on roles or groups from the IdP.

  1. Fetch User Roles/Groups from IdP:

    • When OAuth 2.0 authentication succeeds, fetch the user’s roles or groups from the ID token or a separate user info API provided by the IdP.
  2. Implement RBAC in Your Application:

    • Define roles (e.g., admin, user, viewer) and assign permissions based on the user's role.
    • Use the token to check the user’s role or group before granting access to sensitive resources or features.
  3. Enforce RBAC Rules:

    • In your application, enforce role-based access by checking the roles provided by the IdP:
     if "admin" in user_roles:
         allow_admin_access()
     else:
         deny_access()
    

Summary of Steps:

  1. Set up an Identity Provider (IdP) (e.g., AWS Cognito, Okta, or Auth0) to provide OAuth 2.0 and SSO capabilities.
  2. Configure your application to handle OAuth 2.0 tokens, authentication, and authorization.
  3. Deploy OAuth2-Proxy to enforce authentication at the Ingress level in your Kubernetes cluster.
  4. Implement RBAC for authorization within your application, based on user roles or groups.

This setup ensures that your service deployed in AWS EKS can handle OAuth 2.0 authentication and SSO seamlessly, with strong security controls around access and permissions. Let me know if you need any further clarification!

💖 💪 🙅 🚩
akhil_mittal
akhil mittal

Posted on October 14, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related