# Basic authentication configuration with database (Postgres)

**URL:** <https://discuss.konghq.com/t/basic-authentication-configuration-with-database-postgres/10566>\
**Category:** General\
**Tags:** kubernetes, kong-gateway\
**Created:** [August 23, 2022, 2:03pm UTC](https://discuss.konghq.com/t/basic-authentication-configuration-with-database-postgres/10566 "2022-08-23T14:03:30Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Heman\_Malve](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/heman_malve/32/3549_2.png) [@Heman\_Malve](https://discuss.konghq.com/u/Heman_Malve)\
**Post date:** [August 23, 2022, 2:03pm UTC](https://discuss.konghq.com/t/basic-authentication-configuration-with-database-postgres/10566/1 "2022-08-23T14:03:30Z")

</div>

Hi Team,

I’m able to configure (service, route, consumer & credentials)the basic-auth using documentation Admin API’s - [Basic Authentication - Plugin | Kong Docs](https://docs.konghq.com/hub/kong-inc/basic-auth/) .  
I want to use of yaml files - plugin enable, KongPlugin resource to service (present in documentation), Create a Consumer & Create a Credential (no sample yaml file/kind exists in documentation)  
Kong gateway + DB (Postgres) deployed in namespace-1  
Service is available in namespace-2  
How to configure this using yaml based?

Regards  
Heman

---

<div class="post-metadata">

**Author:** ![traines](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/traines/32/158_2.png) [@traines](https://discuss.konghq.com/u/traines)\
**Post date:** [August 23, 2022, 7:10pm UTC](https://discuss.konghq.com/t/basic-authentication-configuration-with-database-postgres/10566/2 "2022-08-23T19:10:57Z")

</div>

Are you just looking for instructions to create the consumer and its credential? [Provisioning Consumers and Credentials - v2.5.x | Kong Docs](https://docs.konghq.com/kubernetes-ingress-controller/2.5.x/guides/using-consumer-credential-resource/#provision-a-consumer) is the documentation that shows that. Credential Secrets work the same for all auth plugins: any field you’d set in [Basic Authentication plugin | Kong Docs](https://docs.konghq.com/hub/kong-inc/basic-auth/#create-a-credential) or similar will be a key in the Secret, so something like:

```auto
stringData:
  kongCredType: basic-auth
  username: myuser
  password: mypassword

```

---

<div class="post-metadata">

**Author:** ![Heman\_Malve](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/heman_malve/32/3549_2.png) [@Heman\_Malve](https://discuss.konghq.com/u/Heman_Malve)\
**Post date:** [August 24, 2022, 5:06am UTC](https://discuss.konghq.com/t/basic-authentication-configuration-with-database-postgres/10566/3 "2022-08-24T05:06:45Z")

</div>

here it is,

apiVersion: [configuration.konghq.com/v1](http://configuration.konghq.com/v1)  
kind: KongPlugin  
metadata:  
name: basic-auth-dbmode  
annotations:  
[kubernetes.io/ingress.class:](http://kubernetes.io/ingress.class:) kong  
config:  
hide\_credentials: true  
plugin: basic-auth

## apiVersion: v1 kind: Secret metadata: name: secretconsumer stringData: kongCredType: basic-auth username: username password: xxxxxx

#create a Consumer and configure the credentials to use the secret created  
apiVersion: [configuration.konghq.com/v1](http://configuration.konghq.com/v1)  
kind: KongConsumer  
metadata:  
name: consumer  
annotations:  
[kubernetes.io/ingress.class:](http://kubernetes.io/ingress.class:) kong  
username: username  
credentials:

- secretconsumer

when we applied at Gateway side and entries are not inserting into DB(Postgres), any other insights here?

---

<div class="post-metadata">

**Author:** ![Heman\_Malve](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/heman_malve/32/3549_2.png) [@Heman\_Malve](https://discuss.konghq.com/u/Heman_Malve)\
**Post date:** [August 24, 2022, 8:27am UTC](https://discuss.konghq.com/t/basic-authentication-configuration-with-database-postgres/10566/4 "2022-08-24T08:27:19Z")

</div>

[Basic Authentication plugin | Kong Docs](https://docs.konghq.com/hub/kong-inc/basic-auth/) ==\> Example plugin configuration

Admin API -

```auto
curl -X POST http://localhost:8001/services/SERVICE_NAME|SERVICE_ID/plugins \
    --data "name=basic-auth" \
    --data "config.hide_credentials=true"

```

Replace `SERVICE_NAME|SERVICE_ID` with the `id` or `name` of the service that this plugin configuration will target.

its making the entry in database under plugins and even services too

But its not happening if we use kubernetes based for the below ones -

First, create a [KongPlugin](https://docs.konghq.com/kubernetes-ingress-controller/latest/references/custom-resources/#kongplugin) resource:

```auto
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
  name: basic-auth-example
config: 
  hide_credentials: true
plugin: basic-auth

```

Next, apply the KongPlugin resource to a [service](https://docs.konghq.com/gateway/latest/admin-api/#service-object) by annotating the service as follows:

```auto
apiVersion: v1
kind: Service
metadata:
  name: SERVICE_NAME|SERVICE_ID
  labels:
    app: SERVICE_NAME|SERVICE_ID
  annotations:
    konghq.com/plugins: basic-auth-example
spec:
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
    name: SERVICE_NAME|SERVICE_ID
  selector:
    app: SERVICE_NAME|SERVICE_ID

```

does Admin API flavor is applicable to DB mode (Postgres) and kubernetes/YAML is applicable to non DB modes?

---

<div class="post-metadata">

**Author:** ![traines](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/traines/32/158_2.png) [@traines](https://discuss.konghq.com/u/traines)\
**Post date:** [August 24, 2022, 6:34pm UTC](https://discuss.konghq.com/t/basic-authentication-configuration-with-database-postgres/10566/5 "2022-08-24T18:34:29Z")

</div>

The Kubernetes YAML applies to both. The controller makes API requests appropriate to whichever mode you’re using on your behalf.

Is that Service used by an Ingress? The controller won’t create any configuration for a Service alone, it has be used as the backend for some Ingress rule or similar.

If so, the first troubleshooting step to take when configuration isn’t updating is the review the controller container logs. It can include errors that indicate if that or some other piece of configuration is invalid and preventing it from sending updates to the Kong admin API.

---

<div class="post-metadata">

**Author:** ![Heman\_Malve](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/heman_malve/32/3549_2.png) [@Heman\_Malve](https://discuss.konghq.com/u/Heman_Malve)\
**Post date:** [August 25, 2022, 3:52am UTC](https://discuss.konghq.com/t/basic-authentication-configuration-with-database-postgres/10566/6 "2022-08-25T03:52:22Z")

</div>

![service](https://canada1.discourse-cdn.com/flex036/uploads/konghq/original/2X/4/42919abdb1082061b01c15bd8542f8472065b5c6.png)

1. Referring to attached screen shot, this is creating the new service but need to apply the basic-auth plugin to existing service which has created thru service object([Admin API - v2.8.x | Kong Docs](https://docs.konghq.com/gateway/latest/admin-api/#service-object)).
2. how it will decide internally to use the Admin API port (8001) while deploying through the yaml file
3. [Kong Hub | Plugins and Integrations | Kong Docs](https://docs.konghq.com/hub/) ==\> i’m looking for plugins documentation Basic Authentication, OAuth 2.0 Authentication, Kong Spec Expose & Response Size Limiting  
can we deploy these plugins purely using yaml files or Admin API(using curl) + yaml files?

Note: Cluster is residing on tanzu kubernetes.

---

<div class="post-metadata">

**Author:** ![Heman\_Malve](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/heman_malve/32/3549_2.png) [@Heman\_Malve](https://discuss.konghq.com/u/Heman_Malve)\
**Post date:** [August 25, 2022, 7:58am UTC](https://discuss.konghq.com/t/basic-authentication-configuration-with-database-postgres/10566/7 "2022-08-25T07:58:31Z")

</div>

References ,

> **[Basic Auth - Plugin | Kong Docs](https://developer.konghq.com/plugins/basic-auth/)**
>
> Secure Services and Routes with Basic Authentication

> **[Custom Resource (CRD) API Reference - Kong Ingress Controller | Kong Docs](https://developer.konghq.com/kubernetes-ingress-controller/reference/custom-resources/)**
>
> See the generated CRD structure containing all possible resource fields and descriptions for each property.

here, Admin API command is applying basic auth plugin to service which already exists (which is inserting into database and working fine)  
Admin API:  
curl -X POST [http://localhost:8001/services/SERVICE\_NAME|SERVICE\_ID/plugins](http://localhost:8001/services/SERVICE_NAME%7CSERVICE_ID/plugins)   
–data “name=basic-auth”   
–data “config.hide\_credentials=true”

## here, plugin and service is getting created independantly, plugin is not applying on service level and functionality is not similar to Admin API apiVersion: [configuration.konghq.com/v1](http://configuration.konghq.com/v1) kind: KongPlugin metadata: name: basic-auth-example config: hide\_credentials: true plugin: basic-auth

apiVersion: [configuration.konghq.com/v1](http://configuration.konghq.com/v1)  
kind: KongPlugin  
metadata:  
name: basic-auth-db  
annotations:  
[kubernetes.io/ingress.class:](http://kubernetes.io/ingress.class:) kong  
config:  
hide\_credentials: true  
plugin: basic-auth

=================================================  
apiVersion: v1  
kind: Service  
metadata:  
name: SERVICE\_NAME|SERVICE\_ID  
labels:  
app: SERVICE\_NAME|SERVICE\_ID  
annotations:  
[konghq.com/plugins:](http://konghq.com/plugins:) basic-auth-example  
spec:  
ports:

- port: 80  
targetPort: 80  
protocol: TCP  
name: SERVICE\_NAME|SERVICE\_ID  
selector:  
app: SERVICE\_NAME|SERVICE\_ID

* * *

apiVersion: v1  
kind: Service  
metadata:  
name: httpbin-service  
labels:  
app: httpbin-service  
annotations:  
[konghq.com/plugins:](http://konghq.com/plugins:) basic-auth-db  
spec:  
ports:

- port: 80  
targetPort: 80  
protocol: TCP  
name: httpbin-service  
selector:  
app: httpbin-service

my requirement is to apply basic-auth plugin(using yaml) to existing service and should insert into database as similar to Admin API with curl command.  
looking single yaml file to fulfill the request as similar Admin API with curl command.

---

<div class="post-metadata">

**Author:** ![Heman\_Malve](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/heman_malve/32/3549_2.png) [@Heman\_Malve](https://discuss.konghq.com/u/Heman_Malve)\
**Post date:** [August 25, 2022, 8:09am UTC](https://discuss.konghq.com/t/basic-authentication-configuration-with-database-postgres/10566/8 "2022-08-25T08:09:18Z")

</div>

> [@traines](#):
>
> 1. The controller makes API requests appropriate to whichever mode you’re using on your behalf.

my comments ==\> I’m connecting to cluster using KUBECONFIG file, how it will choose the Admin API mode thru kubernetes(yaml based)

1. Is that Service used by an Ingress? The controller won’t create any configuration for a Service alone, it has be used as the backend for some Ingress rule or similar.  
my comments ==\> defined service & route objects , able to invoke the service thru proxy port( url:kong-proxy of EXTERNAL-IP :proxy port/servicename)

---

<div class="post-metadata">

**Author:** ![traines](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/traines/32/158_2.png) [@traines](https://discuss.konghq.com/u/traines)\
**Post date:** [August 31, 2022, 11:36pm UTC](https://discuss.konghq.com/t/basic-authentication-configuration-with-database-postgres/10566/9 "2022-08-31T23:36:45Z")

</div>

The controller reads a `CONTROLLER_KONG_ADMIN_URL` environment variable to find the admin API. In most installations this is set for you via static configuration (the manifests available in the controller Github repo) or via automation in the Helm chart.

The guides on the plugin pages do show the minimal configuration needed to apply a plugin to a particular resource type because they’re intended as brief reference examples for users already familiar with controller configuration in general. They don’t provide a complete end-to-end set of instructions to create everything you need; that is instead provided by the guides. Again, I’d recommend going through [Provisioning Consumers and Credentials - v2.5.x | Kong Docs](https://docs.konghq.com/kubernetes-ingress-controller/2.5.x/guides/using-consumer-credential-resource/) to get a working example up and to understand the relation between Ingresses and Services first, and then applying that knowledge to the configuration you actually want to create.

---

<div class="post-metadata">

**Author:** ![Heman\_Malve](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/heman_malve/32/3549_2.png) [@Heman\_Malve](https://discuss.konghq.com/u/Heman_Malve)\
**Post date:** [September 2, 2022, 1:38pm UTC](https://discuss.konghq.com/t/basic-authentication-configuration-with-database-postgres/10566/10 "2022-09-02T13:38:30Z")

</div>

I have deployed using yaml files(not used Admin API’s) with Ingress pattern and plugin is working fine

---

<div class="post-metadata">

**Author:** ![Heman\_Malve](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/heman_malve/32/3549_2.png) [@Heman\_Malve](https://discuss.konghq.com/u/Heman_Malve)\
**Post date:** [September 2, 2022, 1:41pm UTC](https://discuss.konghq.com/t/basic-authentication-configuration-with-database-postgres/10566/11 "2022-09-02T13:41:37Z")

</div>

Thank you Travis … 🙂
