package
0.0.0-20240221233143-6c4e3efe1a8f
Repository: https://github.com/sap/cloud-robotics.git
Documentation: pkg.go.dev

# README

Remarks

This app is copied from kyma-runtime-extension-samples and includes an additional config option which allows to control if the auth token is forwarded to the upstream service and serveral fixes.

Overview

This sample provides a reverse proxy feature which dispatches requests to other microservices running in Kyma. It includes a middleware to handle authentication which is based on Open ID Connect and can be configured using XSUAA or SAP IAS. The authentication middleware creates a server side session which is referenced by a cookie provided to the client. It also includes a middleware to validate user scopes based on HTTP methods. By default the app will use a memory store for storing user sessions which is meant for development only. It also contains a Redis implemention for storing session which is the preferred usage. See store-implementations for other options.

This sample demonstrates how to:

  • Create a development Namespace in the Kyma runtime.
  • Consume the SCP service XSUAA
  • Deploy the following Kubernetes resources:
    • API deployment written in GO
    • API Rule
    • Service
    • Configmap
    • ServiceBinding
    • ServiceBindingUsage

Prerequisites

  • SAP BTP, Kyma runtime instance
  • Docker
  • Go
  • kubectl configured to use the KUBECONFIG file downloaded from the Kyma runtime

Steps

Create XSUAA Service Instance

  1. Create a new dev Namespace:
kubectl create namespace dev
  1. Within the Kyma console open the namespace dev
  2. Choose Service Management -> Catalog.
  3. Choose the service Authorization & Trust Management
  4. Choose Add
  5. Choose the Plan application
  6. Choose Add parameters and provide the object after adjusting it to your needs.
{
  "oauth2-configuration": {
    "redirect-uris": [
      "https://app-auth-proxy.<cluster domain>/oauth/callback",
      "http://localhost:8000/oauth/callback"
    ]
  },
  "xsappname": "app-auth-proxy"
}

For a complete list of parameters visit Application Security Descriptor Configuration Syntax

  1. Once the instance is provisioned choose the option Create Credentials
  2. Under the Credentials tab choose the Secret which should display the instance secret in a dialog. Choose Decode to view the values. These will be needed if running the sample locally.

Run the API locally

  1. Optionally set the environment variables required to connect with the XSUAA instance which can be found in the Secret generated with the service instance:
export IDP_clientid='<instance clientid>'
export IDP_clientsecret=<instance clientsecret>
export IDP_url=<instance url>
export IDP_xsappname=<xsappname>
  1. Adjust the config.json which contains the following properties. The provided config.json is configured to use the examples
PropertyDescriptionRemarks
routesAn array of routes to be proxied
routes.pathThe incoming path
routes.priorityThe priority of the path with 1 be the highest
routes.protectedIf the auth middleware is required on the path
routes.remove_from_pathIf assigned, this value will be removed from routes.path before the call is proxied
routes.targetThe target of the proxied route which can be a service url
routes.forward_auth_tokenIf the auth token should be forwarded to upstream service
routes.http_method_scopesAn array containing HTTP methods and thier associated user scopesFor no restrictions this can be obmitted or assigned: http-method: "*", "scope": "*"
routes.http_method_scopes.http_methodAn HTTP methods for example GET
routes.http_method_scopes.scopeA scope which is allowed the call the given http_method on the route pathUse $XSAPPNAME for the application name, for example using a Kyma scopes - $XSAPPNAME.runtimeDeveloper
idp_configOptionally set IDP config if not using a service binding
idp_config.urlThe IDP urlIf this value is not set, the environment variables will be used
idp_config.clientsecretThe IDP client secret
idp_config.clientidThe IDP client ID
idp_config.token_endpoint_auth_methodThe htttp method used to during authenticationFor XSUAA use client_secret_post, for SAPIAS us client_secret_basic
redirect_uriThe registered redirect_uri to be called
debugToggle debug on or off
redis_storeWhen configure app will you redis to store the sessions, otherwise a memory store is used which should only be used for evaluation.
redis_store.addrThe service address of the Redis databaseIf this value is not set, memory storage will be used to store the session
redis_store.passwordThe password of the Redis database
redis_store.dbThe database index
cookie.session_nameThe name of the session cookie
cookie.max_age_secondsThe max age of the session cookie
cookie.keyThe key used to encrypt the session cookie
cookie.httponlyIf the cookie can be accessed with Javascript or only http
  1. Run the application:
go run ./cmd/proxy
  1. Accessible endpoints include

Build the Docker image

  1. Build and push the image to your Docker repository:
docker build -t {your-docker-account}/app-auth-proxy -f docker/Dockerfile .
docker push {your-docker-account}/app-auth-proxy
  1. To run the image locally adjust the config.json and either set the env variables individually, or copy them from your environment:
  docker run -p 8000:8000 --env-file ./env.list --mount type=bind,source=$(pwd)/config/config.json,target=/config/config.json -d app-auth-proxy:latest
  OR
  docker run -p 8000:8000 --env-file <(env | grep IDP) --mount type=bind,source=$(pwd)/config/config.json,target=/config/config.json -d app-auth-proxy:latest

Deploy the APP

  1. Create a new dev Namespace:
kubectl create namespace dev
  1. Within ./k8s/configmap.yaml adjust the values and then apply the ConfigMap:
kubectl -n dev apply -f ./k8s/configmap.yaml
  1. Get the name of the ServiceInstance:
kubectl -n dev get serviceinstances

For example:

NAMECLASSPLANSTATUSAGE
xsuaa-showy-yardClusterServiceClass/xsuaaapplicationReady63m
  1. Within ./k8s/deployment.yaml adjust the value of <Service Instance Name> to the XSUAA service instance name and the apply the Deployment:
kubectl -n dev apply -f ./k8s/deployment.yaml
  1. Apply the APIRule:
kubectl -n dev apply -f ./k8s/apirule.yaml
  1. Verify that the Deployment is up and running:
kubectl -n dev get deployment app-auth-proxy
  1. Use the APIRule:
  • https://app-auth-proxy.{cluster-domain}