|
| 1 | +# App Reverse Proxy with OIDC Authentication Middleware |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +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 implementation for storing session which is the preferred usage. See [store-implementations](https://github.com/gorilla/sessions#store-implementations) for other options. |
| 6 | + |
| 7 | +This sample demonstrates how to: |
| 8 | + |
| 9 | +- Create a development Namespace in the Kyma runtime. |
| 10 | +- Consume the SCP service XSUAA |
| 11 | +- Deploy the following Kubernetes resources: |
| 12 | + - API deployment written in GO |
| 13 | + - API Rule |
| 14 | + - Service |
| 15 | + - Configmap |
| 16 | + - ServiceBinding |
| 17 | + - ServiceBindingUsage |
| 18 | + |
| 19 | +## Prerequisites |
| 20 | + |
| 21 | +- SAP BTP, Kyma runtime instance |
| 22 | +- [Docker](https://www.docker.com/) |
| 23 | +- [Go](https://golang.org/doc/install) |
| 24 | +- [kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/) configured to use the `KUBECONFIG` file downloaded from the Kyma runtime |
| 25 | + |
| 26 | +## Steps |
| 27 | + |
| 28 | +### Create XSUAA Service Instance |
| 29 | + |
| 30 | +1. Create a new `dev` Namespace: |
| 31 | + |
| 32 | + ```shell script |
| 33 | + kubectl create namespace dev |
| 34 | + kubectl label namespaces dev istio-injection=enabled |
| 35 | + ``` |
| 36 | + |
| 37 | +2. Open the file `k8s/xsuaa-instance.yaml` and adjust the value `<cluster domain>` and then apply the file |
| 38 | + |
| 39 | + ```shell script |
| 40 | + kubectl -n dev apply -f ./k8s/xsuaa-instance.yaml |
| 41 | + ``` |
| 42 | + |
| 43 | + > > For a complete list of parameters visit [Application Security Descriptor Configuration Syntax](https://help.sap.com/viewer/4505d0bdaf4948449b7f7379d24d0f0d/2.0.04/en-US/6d3ed64092f748cbac691abc5fe52985.html) |
| 44 | +
|
| 45 | +3. Once the instance is provisioned choose the menu option `Service Management -> BTP Service Bindings` within the `dev` namespace. |
| 46 | +4. 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. |
| 47 | + |
| 48 | +### Run the API locally |
| 49 | + |
| 50 | +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: |
| 51 | + |
| 52 | + ```shell script |
| 53 | + export IDP_clientid='<instance clientid>' |
| 54 | + export IDP_clientsecret=<instance clientsecret> |
| 55 | + export IDP_url=<instance url> |
| 56 | + export IDP_xsappname=<xsappname> |
| 57 | + ``` |
| 58 | + |
| 59 | +2. Adjust the config.json which contains the following properties. The provided `config.json` is configured to use the examples |
| 60 | + |
| 61 | + - [React frontend MS SQL](../frontend-react-mssql/README.md) |
| 62 | + - Requires the configmap API_URL to point to `https://app-auth-proxy.<cluster domain>` |
| 63 | + - [Golang MS SQL database API](../api-mssql-go/README.md) |
| 64 | + |
| 65 | + | Property | Description | Remarks | |
| 66 | + | ------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------ | |
| 67 | + | routes | An array of routes to be proxied | | |
| 68 | + | routes.path | The incoming path | | |
| 69 | + | routes.priority | The priority of the path with 1 be the highest | | |
| 70 | + | routes.protected | If the auth middleware is required on the path | | |
| 71 | + | routes.remove_from_path | If assigned, this value will be removed from routes.path before the call is proxied | | |
| 72 | + | routes.target | The target of the proxied route which can be a service url | | |
| 73 | + | routes.http_method_scopes | An array containing HTTP methods and thier associated user scopes | For no restrictions this can be obmitted or assigned: http\-method: "\*", "scope": "\*" | |
| 74 | + | routes.http_method_scopes.http_method | An HTTP methods for example GET | | |
| 75 | + | routes.http_method_scopes.scope | A scope which is allowed the call the given http_method on the route path | Use $XSAPPNAME for the application name, for example using a Kyma scopes - $XSAPPNAME.runtimeDeveloper | |
| 76 | + | idp_config | Optionally set IDP config if not using a service binding | | |
| 77 | + | idp_config.url | The IDP url | If this value is not set, the environment variables will be used | |
| 78 | + | idp_config.clientsecret | The IDP client secret | | |
| 79 | + | idp_config.clientid | The IDP client ID | | |
| 80 | + | idp_config.token_endpoint_auth_method | The htttp method used to during authentication | For XSUAA use client_secret_post, for SAPIAS us client_secret_basic | |
| 81 | + | redirect_uri | The registered redirect_uri to be called | | |
| 82 | + | debug | Toggle debug on or off | | |
| 83 | + | redis_store | When configure app will you redis to store the sessions, otherwise a memory store is used which should only be used for evaluation. | | |
| 84 | + | redis_store.addr | The service address of the Redis database | If this value is not set, memory storage will be used to store the session | |
| 85 | + | redis_store.password | The password of the Redis database | | |
| 86 | + | redis_store.db | The database index | | |
| 87 | + | cookie.session_name | The name of the session cookie | | |
| 88 | + | cookie.max_age_seconds | The max age of the session cookie | | |
| 89 | + | cookie.key | The key used to encrypt the session cookie | | |
| 90 | + | cookie.httponly | If the cookie can be accessed with Javascript or only http | | |
| 91 | + |
| 92 | +3. Run the application: |
| 93 | + |
| 94 | + ```shell script |
| 95 | + go run ./cmd/proxy |
| 96 | + ``` |
| 97 | + |
| 98 | +4. Accessible endpoints include |
| 99 | + - http://localhost:8000/ |
| 100 | + - http://localhost:8000/auth/user |
| 101 | + - http://localhost:8000/auth/groups |
| 102 | + |
| 103 | +### Build the Docker image |
| 104 | + |
| 105 | +1. Build and push the image to your Docker repository: |
| 106 | + |
| 107 | + ```shell script |
| 108 | + docker build -t {your-docker-account}/app-auth-proxy -f docker/Dockerfile . |
| 109 | + docker push {your-docker-account}/app-auth-proxy |
| 110 | + ``` |
| 111 | + |
| 112 | +2. To run the image locally adjust the config.json and either set the env variables individually, or copy them from your environment: |
| 113 | + |
| 114 | + ```shell script |
| 115 | + docker run -p 8000:8000 --env-file ./env.list --mount type=bind,source=$(pwd)/config/config.json,target=/app/config/config.json -d jcawley5/app-auth-proxy:latest |
| 116 | + OR |
| 117 | + docker run -p 8000:8000 --env-file <(env | grep IDP) --mount type=bind,source=$(pwd)/config/config.json,target=/app/config/config.json -d jcawley5/app-auth-proxy:latest |
| 118 | + ``` |
| 119 | + |
| 120 | +### Deploy the APP |
| 121 | + |
| 122 | +1. Create a new `dev` Namespace: |
| 123 | + |
| 124 | + ```shell script |
| 125 | + kubectl create namespace dev |
| 126 | + ``` |
| 127 | + |
| 128 | +2. Within `./k8s/configmap.yaml` adjust the values and then apply the ConfigMap: |
| 129 | + |
| 130 | + ```shell script |
| 131 | + kubectl -n dev apply -f ./k8s/configmap.yaml |
| 132 | + ``` |
| 133 | + |
| 134 | +3. Get the name of the ServiceInstance: |
| 135 | + |
| 136 | + ```shell script |
| 137 | + kubectl -n dev get serviceinstances |
| 138 | + ``` |
| 139 | + |
| 140 | + For example: |
| 141 | + |
| 142 | + | NAME | CLASS | PLAN | STATUS | AGE | |
| 143 | + | ---------------------- | ------------------------- | ----------- | ------ | --- | |
| 144 | + | **_xsuaa-showy-yard_** | ClusterServiceClass/xsuaa | application | Ready | 63m | |
| 145 | + |
| 146 | +4. Within `./k8s/deployment.yaml` adjust the value of `<Service Instance Name>` to the XSUAA service instance name and the apply the Deployment: |
| 147 | + |
| 148 | + ```shell script |
| 149 | + kubectl -n dev apply -f ./k8s/deployment.yaml |
| 150 | + ``` |
| 151 | + |
| 152 | +5. Apply the APIRule: |
| 153 | + |
| 154 | + ```shell script |
| 155 | + kubectl -n dev apply -f ./k8s/apirule.yaml |
| 156 | + ``` |
| 157 | + |
| 158 | +6. Verify that the Deployment is up and running: |
| 159 | + |
| 160 | + ```shell script |
| 161 | + kubectl -n dev get deployment app-auth-proxy |
| 162 | + ``` |
| 163 | + |
| 164 | +7. Use the APIRule `https://app-auth-proxy.{cluster-domain}` |
0 commit comments