<p>I am using the promtail helm chart to connect to a Loki server running on a different stack. I have Loki behind an Nginx ingress secured with basic auth.</p>
<p>I can't find any documentation on this, and it's very possible it's just my admittedly limited understanding of helm.</p>
<p>I'm simply trying to use basic auth to connect to the Loki instance while using a Kubernetes secret instead of plaintext credentials in the helm values</p>
<p>This works perfect:</p>
<pre><code>  snippets:
    extraClientConfigs: |
      basic_auth:
        username: myusername
        password: mypassword
</code></pre>
<p>I created a secret like this:</p>
<pre><code>kubectl create secret generic loki-credentials -n monitoring --from-literal=password=&quot;mypassword&quot; --from-literal=username=&quot;myusername&quot;
</code></pre>
<p>and now I want to use that in the <code>values.yaml</code> file.</p>
<p>This is what I got so far:</p>
<pre><code>extraEnv:
  - name: LOKI_USERNAME
    valueFrom:
      secretKeyRef:
        name: loki-credentials
        key: username
  - name: LOKI_PASSWORD
    valueFrom:
      secretKeyRef:
        name: loki-credentials
        key: password

extraArgs:
  - -client.external-labels=stack=development
  - -config.expand-env

config:
  serverPort: 3101
  lokiAddress: myurl
  snippets:
    extraClientConfigs: |
      basic_auth:
        username: ${LOKI_USERNAME}
        password: ${LOKI_PASSWORD}
</code></pre>
<p>I just get a 401 response.</p>
<pre><code> Chart version: 3.1.0
 Promtail version: 2.1.0
</code></pre>
<p><strong>EDIT</strong></p>
<p>Here is my ingress yaml:</p>
<pre><code>controller:
  replicaCount: 1

  config:
    force-ssl-redirect: &quot;true&quot;
    use-forwarded-headers: &quot;true&quot;

  service:
    targetPorts:
      http: http
      https: http
    annotations:
      service.beta.kubernetes.io/aws-load-balancer-ssl-cert: &quot;arn:aws:acm:us-west-2:123456:certificate/123456&quot;
      service.beta.kubernetes.io/aws-load-balancer-backend-protocol: &quot;http&quot;
      service.beta.kubernetes.io/aws-load-balancer-ssl-ports: &quot;https&quot;

    enableHttp: false
    enableHttps: true
    type: LoadBalancer
    loadBalancerSourceRanges:
      - &quot;0.0.0.0/0&quot;

   ## Name of the ingress class to route through this controller
  ingressClass: nginx-external
</code></pre>
<p>...and my loki <code>values.yaml</code></p>
<pre><code>ingress:
  enabled: true
  annotations:
    kubernetes.io/ingress.class: &quot;nginx-external&quot;
    nginx.ingress.kubernetes.io/auth-type: basic
    nginx.ingress.kubernetes.io/auth-secret: loki-ingress-auth
    nginx.ingress.kubernetes.io/auth-realm: &quot;Authentication Required&quot;
  hosts:
    - host: loki.mydomain.com
      paths: 
        - &quot;/&quot;
  tls: []

config:
  auth_enabled: false
  ingester:
    chunk_idle_period: 3m
    chunk_block_size: 262144
    chunk_retain_period: 1m
    max_transfer_retries: 3
    lifecycler:
      ring:
        kvstore:
          store: inmemory
        replication_factor: 1
  
  schema_config:
    configs:
    - from: 2021-03-06
      store: boltdb-shipper
      object_store: aws
      schema: v11
      index:
        prefix: loki_index_
        period: 24h
        
  server:
    http_listen_port: 3100

  storage_config:
    aws:
      bucketnames: my-bucket-name
      region: us-west-2
      s3forcepathstyle: true

    boltdb_shipper:
      active_index_directory: /data/loki/boltdb-shipper-active
      cache_location: /data/loki/boltdb-shipper-cache
      shared_store: s3

  chunk_store_config:
    max_look_back_period: 0s
  
  table_manager:
    retention_deletes_enabled: false
    retention_period: 0s

  compactor:
    working_directory: /data/loki/boltdb-shipper-compactor
    shared_store: aws

replicas: 1

podAnnotations:
  iam.amazonaws.com/role: &quot;arn:aws:iam::123456:role/my-loki-role&quot;

resources:
  limits:
    cpu: 500m
    memory: 2G
  requests:
    cpu: 250m
    memory: 1G

# The values to set in the PodDisruptionBudget spec
# If not set then a PodDisruptionBudget will not be created
podDisruptionBudget:
  minAvailable: 1
</code></pre>
<p>My logs from the nginx pod that Loki is sitting behind:</p>
<pre><code>2021/03/09 04:23:44 [error] 37#37: *925 user &quot;myusername&quot;: password mismatch, client: xxx.xx.xxx.xxx, server: loki.mydomain.com, request: &quot;POST /loki/api/v1/push HTTP/1.1&quot;, host: &quot;loki.mydomain.com&quot;
2021/03/09 04:23:44 [error] 37#37: *921 user &quot;myusername&quot;: password mismatch, client: xxx.xx.xxx.xxx, server: loki.mydomain.com, request: &quot;POST /loki/api/v1/push HTTP/1.1&quot;, host: &quot;loki.mydomain.com&quot;
xx.xxx.xxx.xx - myusername [09/Mar/2021:04:23:44 +0000] &quot;POST /loki/api/v1/push HTTP/1.1&quot; 401 172 &quot;-&quot; &quot;promtail/2.1.0&quot; 326 0.000 [monitoring-loki-3100] [] - - - - 63294b16fe010a8c9ec1d4684f0472f5
xxx.xx.xxx.xxx: - myusername [09/Mar/2021:04:23:44 +0000] &quot;POST /loki/api/v1/push HTTP/1.1&quot; 204 0 &quot;-&quot; &quot;promtail/2.1.0&quot; 2744 0.003 [monitoring-loki-3100] [] xxx.xx.xxx.xxx:3100 0 0.004 204 029e0a9d1ee6242cad8b9a6d2ee50940
2021/03/09 04:23:44 [error] 37#37: *925 user &quot;myusername&quot;: password mismatch, client: xx.xxx.xxx.xx, server: loki.mydomain.com, request: &quot;POST /loki/api/v1/push HTTP/1.1&quot;, host: &quot;loki.mydomain.com&quot;
xxx.xx.xxx.xxx - myusername [09/Mar/2021:04:23:44 +0000] &quot;POST /loki/api/v1/push HTTP/1.1&quot; 401 172 &quot;-&quot; &quot;promtail/2.1.0&quot; 325 0.000 [monitoring-loki-3100] [] - - - - b75a2cfcf6c62b81953dd4fb26f1a844
xxx.xx.xxx.xxx - myusername [09/Mar/2021:04:23:44 +0000] &quot;POST /loki/api/v1/push HTTP/1.1&quot; 204 0 &quot;-&quot; &quot;promtail/2.1.0&quot; 1513 0.014 [monitoring-loki-3100] [] xxx.xx.xxx.xxx:3100 0 0.016 204 0049965a49877cb5d336ac6ec869feb4
2021/03/09 04:23:45 [error] 36#36: *941 user &quot;myusername&quot;: password mismatch, client: xxx.xx.xxx.xxx, server: loki.mydomain.com, request: &quot;POST /loki/api/v1/push HTTP/1.1&quot;, host: &quot;loki.mydomain.com&quot;
xxx.xx.xxx.xxx - myusername [09/Mar/2021:04:23:45 +0000] &quot;POST /loki/api/v1/push HTTP/1.1&quot; 401 172 &quot;-&quot; &quot;promtail/2.1.0&quot; 326 0.000 [monitoring-loki-3100] [] - - - - e5954bd055db5b3e9bd3227f57651847
</code></pre>

<p>To give a bit of background to anyone new to Loki, as stated in the <a href="https://grafana.com/docs/loki/latest/operations/authentication/" rel="nofollow noreferrer">documentation</a>: <em>Loki does not come with any included authentication layer. Operators are expected to run an authenticating reverse proxy in front of your services, such as NGINX using basic auth or an OAuth2 proxy.</em></p>
<p>This basically means that you'll have to place something in between the client(s) and Loki to enforce e.g. basic authentication. In this case there's a Ingress (Nginx) acting as a reverse proxy with basic authentication.</p>
<p>To troubleshoot problems with authentication using Nginx there's a number of things to check:</p>
<ul>
<li>Logs of the Nginx Ingress Pod, check for authentication errors.</li>
<li>That the added Kubernetes Secret contains what you expect.</li>
<li>That you have configured the Ingress object with the needed <code>annotations</code>.</li>
</ul>
<p>When it comes to using Nginx as Ingress and adding basic authentication <a href="https://kubernetes.github.io/ingress-nginx/examples/auth/basic/" rel="nofollow noreferrer">this</a> resource from the official docs is really helpful.</p>
<p>In general when creating Kubernetes secrets, especially from the command line using <code>kubectl</code> you'll have to single quote the password if it contains special characters. This is so that the special characters wont be interpreted by your shell. More info <a href="https://kubernetes.io/docs/tasks/configmap-secret/managing-secret-using-kubectl/#create-a-secret" rel="nofollow noreferrer">here</a>.</p>
