Bookmark this page

Guided Exercise: Allow Application Access to Kubernetes APIs

Configure an application with limited access to Kubernetes API resources.

Outcomes

You should be able to grant Kubernetes API access to an application by using a service account that has a role with the required privileges.

As the student user on the workstation machine, use the lab command to prepare your system for this exercise.

The lab command copies the following files to the lab directory:

  • The deployment manifest to install the Stakater Reloader application, at https://github.com/stakater/Reloader. This application is a controller that watches for changes in configuration maps and does rolling upgrades on associated deployments.

  • The manifests to install the config-app API, which has an endpoint to show its internal configuration. The deployment manifest mounts the API configuration from a configuration map.

In this exercise, you grant permissions on the appsec-api project to the Reloader application, for read access to the configuration map API and edit access to the deployment API.

Warning

Using a controller to update a Kubernetes resource by reacting to changes is an alternative to, and might conflict with, using GitOps.

[student@workstation ~]$ lab start appsec-api

Instructions

  1. Change to the lab directory.

    1. Change to the ~/DO280/labs/appsec-api directory.

      [student@workstation ~]$ cd ~/DO280/labs/appsec-api
  2. Log in as the admin user and change to the configmap-reloader project.

    1. Open a terminal window and log in as the admin user with the redhatocp password.

      [student@workstation appsec-api]$ oc login -u admin -p redhatocp \
        https://api.ocp4.example.com:6443
      Login successful.
      ...output omitted...
    2. Use the oc project command to change to the configmap-reloader namespace.

      [student@workstation appsec-api]$ oc project configmap-reloader
      Now using project "configmap-reloader" on server ...
  3. Create the configmap-reloader service account to hold the permissions for the Reloader application. Then, assign the configmap-reloader service account to the configmap-reloader deployment.

    1. Create the configmap-reloader service account.

      [student@workstation appsec-api]$ oc create sa configmap-reloader-sa
      serviceaccount/configmap-reloader-sa created
    2. Add the configmap-reloader-sa service account to the deployment in the reloader-deployment.yaml file.

      apiVersion: apps/v1
      kind: Deployment
      metadata:
        labels:
          app: configmap-reloader
        name: configmap-reloader
        namespace: configmap-reloader
      spec:
        selector:
          matchLabels:
            app: configmap-reloader
            release: "reloader"
        template:
          metadata:
            labels:
              app: configmap-reloader
          spec:
            serviceAccountName: configmap-reloader-sa
            containers:
      ...output omitted...
    3. Use the oc command to create the configmap-reloader deployment from the reloader-deployment.yaml file.

      [student@workstation appsec-api]$ oc apply -f reloader-deployment.yaml
      deployment.apps/configmap-reloader created
  4. As the developer user, create the appsec-api project.

    1. Log in to the cluster as the developer user with the developer password.

      [student@workstation appsec-api]$ oc login -u developer -p developer \
        https://api.ocp4.example.com:6443
      Login successful.
      ...output omitted...
    2. Use the oc new-project command to create the appsec-api project.

      [student@workstation appsec-api]$ oc new-project appsec-api
      Now using project "appsec-api" on server ...
  5. Grant permissions to the configmap-reloader-sa service account to watch configuration map resources and roll out deployments on the appsec-api project.

    1. Assign the edit`cluster role to the `configmap-reloader-sa service account in the appsec-api project. To assign the cluster role, create a local role binding by using the oc policy add-role-to-user command with the following options:

      • The edit default cluster role.

      • The system:serviceaccount:configmap-reloader:configmap-reloader-sa username to reference the configmap-reloader-sa service account in the configmap-reloader project.

      • The --rolebinding-name option to use the reloader-edit name for the role binding.

      • The -n appsec-api, which is optional because you are already in the appsec-api project.

      [student@workstation appsec-api]$ oc policy add-role-to-user edit \
         system:serviceaccount:configmap-reloader:configmap-reloader-sa \
         --rolebinding-name=reloader-edit \
         -n appsec-api
      clusterrole.rbac.authorization.k8s.io/edit added: "system:serviceaccount:configmap-reloader:configmap-reloader-sa"

    Note

    The edit cluster role with the local role binding allows the configmap-reloader-sa service account to modify most objects in the appsec-api project. In a production scenario, it is best to grant access only to the APIs that your application requires.

  6. Install the config-app API by using the manifest files in the config-app directory.

    1. Use the oc apply command with the -f option to create all the manifests in the config-app directory.

      [student@workstation appsec-api]$ oc apply -f ./config-app
      configmap/config-app created
      deployment.apps/config-app created
      route.route.openshift.io/config-app created
      service/config-app created
    2. Read the config.yaml content from the config-app configuration map by running the oc get command.

      [student@workstation appsec-api]$ oc get configmap config-app \
        --output="jsonpath={.data.config\.yaml}"
      application:
        name: "config-app"
        description: "config-app"
    3. Run the curl command to verify that the exposed route, https://config-app-appsec-api.apps.ocp4.example.com/config, shows the config-app configuration map content.

      [student@workstation appsec-api]$ curl -s \
        https://config-app-appsec-api.apps.ocp4.example.com/config | jq
      {
        "application": {
          "description": "config-app",
          "name": "config-app"
        }
      }
  7. Configure the config-app deployment with the configmap.reloader.stakater.com/reload: "config-app" annotation so that the controller can roll out deployments automatically when the config-app configuration map changes.

    1. Add the configmap.reloader.stakater.com/reload: "config-app" annotation to the deployment in the config-app/deployment.yaml file.

      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: config-app
        namespace: appsec-api
        annotations:
         configmap.reloader.stakater.com/reload: "config-app"
      spec:
      ...output omitted...
    2. Use the oc apply command to update the resource.

      [student@workstation appsec-api]$ oc apply -f config-app/deployment.yaml
      deployment.apps/config-app configured
    3. Verify that the configmap.reloader.stakater.com/reload: "config-app" annotation is present in the config-app deployment object.

      [student@workstation appsec-api]$ oc get deployment config-app -o yaml
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        annotations:
          configmap.reloader.stakater.com/reload: config-app
      spec:
      ...output omitted...
  8. Update the config-app configuration map description key and query /config endpoint to verify that the Reloader controller upgrades the config-app deployment.

    1. Update the description data in the configuration map in the config-app/configmap.yaml file to the API that exposes its configuration value.

      apiVersion: v1
      kind: ConfigMap
      metadata:
        name: config-app
        namespace: appsec-api
      data:
        config.yaml: |
          application:
            name: "config-app"
            description: "API that exposes its configuration"
    2. Use the oc command to apply the changes to the config-app/configmap.yaml file.

      [student@workstation appsec-api]$ oc apply -f config-app/configmap.yaml
      configmap/config-app configured
    3. Use the watch command to query the API /config endpoint by using the curl command to verify that the API configuration changes. Press Ctrl+C to exit.

      [student@workstation appsec-api]$ watch \
        "curl -s https://config-app-appsec-api.apps.ocp4.example.com/config | jq"
      Every 2.0s: curl -s https://config-app-appsec-api.apps.ocp4.example.com/config | jq
      workstation: ...
      
      {
        "application": {
          "description": "API that exposes its configuration",
          "name": "config-app"
        }
      }

      Wait until the controller application upgrades the deployment.

  9. Change to the home directory to complete the exercise.

    1. Change to the home directory.

      [student@workstation appsec-api]$ cd

Finish

On the workstation machine, use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish appsec-api

Revision: do280-4.14-08d11e1