npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

kube-service-bindings

v3.0.0

Published

helper for consuming kubernetes service bindings

Downloads

95,323

Readme

kube-service-bindings

Service bindings is kubernetes spec on how to communicate service secrets to applications in an automated way. The spec is available here.

The goal of this package is to make it easy for Node.js applications to consume these secrets, without requiring developers to be familiar with service bindings.

CI Coverage Status License NPM version

Install

npm install kube-service-bindings --save

Supported Node.js Versions

kube-service-bindings supports and is tested only on the current, maintenance and active Node.js LTS versions. We will bump the major version in a release of kube-service-bindings soon after an LTS version of Node.js goes EOL.

Usage

The package provides the getBinding method which does roughly the following:

  • Looks for the $SERVICE_BINDING_ROOT variable in order to determine if bindings are available.
  • Reads the info from the files.
  • Maps the names of the files to the options names needed by the Node.js clients that will connect to the service.

getBinding(type, client, bindingOptions)

This is an example of how kube-service-bindings might be used:

const Kafka = require('node-rdkafka');
const serviceBindings = require('kube-service-bindings');

try {
  // check if the deployment has been bound to a kafka instance through
  // service bindings. If so use that connect info
  kafkaConnectionBindings = serviceBindings.getBinding('KAFKA', 'node-rdkafka');
} catch (err) { // proper error handling here
};

const stream = Kafka.KafkaConsumer.createReadStream(
  Object.assign({
    'group.id': 'consumer-test', // identifier to use to help trace activity in Kafka
    'socket.keepalive.enable': true, // Enable TCP keep-alives on broker sockets
    'enable.auto.commit': false // Automatically and periodically commit offsets in the background.
  }, kafkaConnectionBindings),
  {},
  {
    topics: 'countries'
  }
);

The parameters for getBinding include:

| Parameter | Type | | --------------------------------- | -------- | | type | String | | client | String | | bindingOptions | Object |

type

The type of service for which a binding is being requested. Currently the supported types are:

  • 'KAFKA'
  • 'POSTGRESQL'
  • 'REDIS'
  • 'MONGODB'
  • 'AMQP'
  • 'MYSQL'

client

The package the application is using to connect to the service. kube-service-bindings is aware of a subset of possible packages. For those that it is aware of, it can map the service bindings into the form required by the client.

Currently the following clients are recognized based on the supported types:

  • KAFKA
    • node-rdkafka
    • kafkajs
  • POSTGRESQL
    • pg
    • odbc
  • REDIS
    • redis
    • ioredis
  • MONGODB
    • mongodb
    • mongoose
  • AMQP
    • rhea
  • MYSQL
    • mysql
    • mysql2
    • odbc

(Deprecated) If you don't specify a client, the object returned will be a direct map from the bindings, with the keys corresponding to the name of each file provided by the binding.

Example on mongoDB client

const serviceBindings = require('kube-service-bindings');
const { MongoClient } = require('mongodb');

const { url, connectionOptions } = serviceBindings.getBinding(
  'MONGODB',
  'mongodb'
);

const mongoClient = new MongoClient(url, connectionOptions);

bindingOptions

An object which provides additional control over how binding data is parsed.

| Attribute | type | default Value | | --------------------------------- | --------- | ------------- | | id | String | undefined | | removeUnmapped | Boolean | as set by client, or true if not set by client | | allowCopy | Boolean | false | | bindingData | Object | undefined |

id

Id used to filter the available bindings. For example, if you have two Kafka services bound to your application an id can be specified to identify which one should be used. If there are multiple bindings that satisfy a request and no id is specified, the first one found will be used.

removeUnmapped

Removes all binding data which is not mapped for the client. If false any binding data which is not mapped remains in it's raw form on the binding object returned. The default depends on the client.

allowCopy

Enables setting proper permissions for some of the binding data, where the system has not provided them correctly. It allows binding files content to be copied/stored in a new file and directory. This has to be enabled by the user in order to be aware of the security risk, as some files might include sensitive material. For example, connecting to postgresql with the odbc client, the following error is thrown for the tls.key file if copies are not allowed: permissions should be u=rw (0600) or less.

bindingData

An optional object for passing binding data to kube-service-bindings. This is useful especially in local dev environtment or as a fallback in case of binding data are not available.

Example 1 mongodb client:

const serviceBindings = require('kube-service-bindings');

let url;
let connectionOptions;

try {
  ({ url, connectionOptions } = serviceBindings.getBinding(
    'MONGODB',
    'mongodb'
  ));
} catch (err) {
  ({ url, connectionOptions } = serviceBindings.getBinding(
    'MONGODB',
    'mongodb',
    {
      host: 'mongodb.host.com',
      password: 'password',
      port: 27017,
      username: 'user1'
    }
  ));
}

Example 2 kafkajs client:

const serviceBindings = require('kube-service-bindings');

let kafkaConnectionBindings;

try {
  kafkaConnectionBindings = serviceBindings.getBinding('KAFKA', 'kafkajs');
} catch (err) {
  kafkaConnectionBindings = serviceBindings.getBinding('KAFKA', 'kafkajs', {
    bootstrapServers: 'test-boostrap:443',
    clientId: 'client1',
    clientSecret: 'pass1',
    password: 'pass1',
    provider: 'rhoas',
    saslMechanism: 'PLAIN',
    securityProtocol: 'SASL_SSL',
    type: 'kafka',
    user: 'user1'
  });
}

getBinding()

If you don't specify any parameters the getBinding function will return binding data in raw format.

Example of fetching binding data in raw format

$ tree $SERVICE_BINDING_ROOT

/bindings
├── kafka-bindings
│   ├── bootstrapServers
│   ├── clientId
│   ├── clientSecret
│   ├── password
│   ├── provider
│   ├── saslMechanism
│   ├── securityProtocol
│   ├── type
│   └── user
└── kafka-bindings-another
    ├── bootstrapServers
    ├── clientId
    ├── clientSecret
    ├── password
    ├── provider
    ├── saslMechanism
    ├── securityProtocol
    ├── type
    └── user

By executing getBinding in the above environment.

const serviceBindings = require('kube-service-bindings');

const rawBindingData = getBinding();

console.log(rawBindingData);

Will result in below output:

[
  {
    "bootstrapServers": "test-boostrap:443",
    "clientId": "client1",
    "clientSecret": "pass1",
    "password": "pass1",
    "provider": "rhoas",
    "saslMechanism": "PLAIN",
    "securityProtocol": "SASL_SSL",
    "type": "kafka",
    "user": "user1"
  },
  {
    "bootstrapServers": "another-test-boostrap:443",
    "clientId": "another-client1",
    "clientSecret": "another-pass1",
    "password": "another-pass1",
    "provider": "another-rhoas",
    "saslMechanism": "another-PLAIN",
    "securityProtocol": "another-SASL_SSL",
    "type": "another-kafka",
    "user": "another-user1"
  }
]