Basic external auth policy

Authenticate requests with a basic dictionary of usernames and passwords.

Basic authentication sends encoded user credentials in a standard header within the request. Then, Gloo Mesh authenticates the request against a dictionary of usernames and passwords that are written in the external auth policy. If the credentials in the request header match the policy, the request is sent to the destination. If not, Gloo Mesh Gateway returns a 401 response.

You might use basic auth for testing environments, such as when you release a new API method or version to a small number of known users.

Before you begin

This guide assumes that you use the same names for components like clusters, workspaces, and namespaces as in the getting started. If you have different names, make sure to update the sample configuration files in this guide.
  1. Complete the multicluster getting started guide to set up the following testing environment.
    • Three clusters along with environment variables for the clusters and their Kubernetes contexts.
    • The Gloo Platform CLI, meshctl, along with other CLI tools such as kubectl and istioctl.
    • The Gloo management server in the management cluster, and the Gloo agents in the workload clusters.
    • Istio installed in the workload clusters.
    • A simple Gloo workspace setup.
  2. Install Bookinfo and other sample apps.
  3. Make sure that you have the following CLI tools, or something comparable:
    • htpasswd to generate hashed, salted passwords.
    • base64 to encode strings.

Configure basic external auth policies

You can apply a basic external auth policy at the route or destination level. For more information, see Applying policies.

  1. Generate a salt and hashed password for your user credentials. The following example uses the htpasswd tool for a user named user.
    htpasswd -nbm user password
    

    Example output:

    user:$apr1$TYiryv0/$8BvzLUO9IfGPGGsPnAgSu1
    
  2. Retrieve the salt and hashed password from the output of the previous step.
    • Salt: TYiryv0/
    • Hashed password: 8BvzLUO9IfGPGGsPnAgSu1
  3. Review the following sample configuration file.
    apiVersion: security.policy.gloo.solo.io/v2
    kind: ExtAuthPolicy
    metadata:
      name: basic-auth
      namespace: bookinfo
    spec:
      applyToDestinations:
      - port:
          number: 9080
        selector:
          labels:
            app: reviews
      config:
        glooAuth:
          configs:
          - basicAuth:
              apr:
                users:
                  user:
                    hashedPassword: 8BvzLUO9IfGPGGsPnAgSu1
                    salt: TYiryv0/
        server:
          name: default-server
    

Review the following table to understand this configuration. For more information, see the API docs.

Setting Description
applyToDestinations Configure which destinations to apply the policy to, by using labels. Destinations can be a Kubernetes service, VirtualDestination, or ExternalService. If you do not specify any destinations or routes, the rate limit policy applies to all destinations in the workspace by default. If you do not specify any destinations but you do specify a route, the rate limit applies to the route but to no destinations.
config.glooAuth.configs.basicAuth Configure the basic auth credentials to use to authenticate requests. The example sets up user credentials for a user named user in the required APR1 format. For more information, see the API reference.
apr.users.user.hashedPassword The hashed password that you generated in the previous step. The example sets 8BvzLUO9IfGPGGsPnAgSu1.
apr.users.user.salt The salt, or random data that hashes the password, that you generated in the previous step. The example sets TYiryv0/.
server The external auth server for the policy to use. In this example, only the name is specified. Gloo attempts to find the server in the same namespace and cluster as the policy, or you can add namespace and cluster fields. If you omit the server section, Gloo uses the external auth server that is created for you.

Apply external auth to external services

The following example is for an external auth policy that applies to an external service. Note that this policy requires different routing table and external auth server resources, as well as an external service.

apiVersion: security.policy.gloo.solo.io/v2
kind: ExtAuthPolicy
metadata:
  name: httpbin-basic-auth
  namespace: bookinfo
spec:
  applyToRoutes:
  - route:
      labels:
        route: external-service
  config:
    glooAuth:
      configs:
      - basicAuth:
          apr:
            users:
              user:
                hashedPassword: 8BvzLUO9IfGPGGsPnAgSu1
                salt: TYiryv0/
    server:
      name: default-server
apiVersion: admin.gloo.solo.io/v2
kind: ExtAuthServer
metadata:
  name: default-server
  namespace: bookinfo
spec:
  destinationServer:
    port:
      number: 8083
    ref:
      cluster: cluster-1
      name: ext-auth-service
      namespace: gloo-mesh-addons
apiVersion: networking.gloo.solo.io/v2
kind: ExternalService
metadata:
  name: external-service
  namespace: bookinfo
spec:
  hosts:
  - httpbin.org
  ports:
  - name: http
    number: 80
    protocol: HTTP
apiVersion: networking.gloo.solo.io/v2
kind: RouteTable
metadata:
  name: external-service-northsouth
  namespace: bookinfo
spec:
  defaultDestination:
    kind: EXTERNAL_SERVICE
    port:
      number: 80
    ref:
      name: external-service
      namespace: bookinfo
  hosts:
  - www.example.com
  http:
  - forwardTo: {}
    labels:
      "no": auth
    matchers:
    - headers:
      - name: noauth
        value: "true"
    name: external-service-no-auth
  - forwardTo: {}
    labels:
      route: external-service
    name: external-service-northsouth
  virtualGateways:
  - name: istio-ingressgateway

Verify basic external auth policies

  1. Make sure that the external auth service is installed and running. If not, install the external auth service.

    kubectl get pods --context ${REMOTE_CONTEXT1} -A -l app=ext-auth-service
    
  2. Apply the example basic external auth policy and server in the workload cluster.

    kubectl apply --context ${REMOTE_CONTEXT1} -f - << EOF
    apiVersion: security.policy.gloo.solo.io/v2
    kind: ExtAuthPolicy
    metadata:
      name: basic-auth
      namespace: bookinfo
    spec:
      applyToDestinations:
      - port:
          number: 9080
        selector:
          labels:
            app: reviews
      config:
        glooAuth:
          configs:
          - basicAuth:
              apr:
                users:
                  user:
                    hashedPassword: 8BvzLUO9IfGPGGsPnAgSu1
                    salt: TYiryv0/
        server:
          name: default-server
    ---
    apiVersion: admin.gloo.solo.io/v2
    kind: ExtAuthServer
    metadata:
      name: default-server
      namespace: bookinfo
    spec:
      destinationServer:
        port:
          number: 8083
        ref:
          cluster: cluster-1
          name: ext-auth-service
          namespace: gloo-mesh-addons
    EOF
       

  3. View the ext-auth-service deployment logs to verify that the policy was accepted. Look for a message similar to the following: "logger":"extauth","caller":"runner/run.go:179","msg":"got new config". If you don't see this message, check the management server logs for errors.

    kubectl logs -n gloo-mesh-addons deploy/ext-auth-service -c ext-auth-service -f --context $REMOTE_CONTEXT1
    
  4. Send an unauthenticated request to the reviews app.

    Create a temporary curl pod in the bookinfo namespace, so that you can test the app setup. You can also use this method in Kubernetes 1.23 or later, but an ephemeral container might be simpler, as shown in the other tab.

    1. Create the curl pod.
      kubectl run -it -n bookinfo --context $REMOTE_CONTEXT1 curl \
        --image=curlimages/curl:7.73.0 --rm  -- sh
      
    2. Send a request to the reviews app.
      curl http://reviews:9080/reviews/1 -v
      

    Use the kubernetes debug command to create an ephemeral curl container in the deployment. This way, the curl container inherits any permissions from the app that you want to test. If you don't run Kubernetes 1.23 or later, you can deploy a separate curl pod or manually add the curl container as shown in the other tab.

    kubectl --context ${REMOTE_CONTEXT1} -n bookinfo debug -i pods/$(kubectl get pod --context ${REMOTE_CONTEXT1} -l app=reviews -A -o jsonpath='{.items[0].metadata.name}') --image=curlimages/curl -- curl -v http://reviews:9080/reviews/1
    

    If the output has an error about EphemeralContainers, see Ephemeral containers don’t work when testing Bookinfo.

    Example output: Notice that the request is denied with a 401 Unauthorized response.

    HTTP/1.1 401 Unauthorized
    
  5. Encode the expected user credentials in base64 format.

    echo -n "user:password" | base64
    

    Example output:

    dXNlcjpwYXNzd29yZA==
    
  6. Repeat the request to the ratings app, including the authorization header with the user credentials. This time, the request succeeds.

    Create a temporary curl pod in the bookinfo namespace, so that you can test the app setup. You can also use this method in Kubernetes 1.23 or later, but an ephemeral container might be simpler, as shown in the other tab.

    1. Create the curl pod.
      kubectl run -it -n bookinfo --context $REMOTE_CONTEXT1 curl \
        --image=curlimages/curl:7.73.0 --rm  -- sh
      
    2. Send a request to the reviews app.
      curl -H "Authorization: basic dXNlcjpwYXNzd29yZA==" http://reviews:9080/reviews/1 -v
      
    3. Exit the temporary pod. The pod deletes itself.
      exit
      

    Use the kubernetes debug command to create an ephemeral curl container in the deployment. This way, the curl container inherits any permissions from the app that you want to test. If you don't run Kubernetes 1.23 or later, you can deploy a separate curl pod or manually add the curl container as shown in the other tab.

    kubectl --context ${REMOTE_CONTEXT1} -n bookinfo debug -i pods/$(kubectl get pod --context ${REMOTE_CONTEXT1} -l app=reviews -A -o jsonpath='{.items[0].metadata.name}') --image=curlimages/curl -- curl -v -H "Authorization: basic dXNlcjpwYXNzd29yZA==" http://reviews:9080/reviews/1
    

    Example output:

    HTTP/1.1 200 OK
    
  7. Optional: Clean up the resources that you created.

    kubectl --context $REMOTE_CONTEXT1 -n bookinfo delete ExtAuthPolicy basic-auth
    kubectl --context $REMOTE_CONTEXT1 -n bookinfo delete ExtAuthServer default-server