Socket
Socket
Sign inDemoInstall

kube-service-bindings

Package Overview
Dependencies
0
Maintainers
9
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    kube-service-bindings

helper for consuming kubernetes service bindings


Version published
Weekly downloads
18K
decreased by-3.57%
Maintainers
9
Install size
37.7 kB
Created
Weekly downloads
 

Changelog

Source

3.0.0 (2023-06-01)

⚠ BREAKING CHANGES

  • removal of node 14

Features

Bug Fixes

  • improve test structure (a9539be)
  • package.json & package-lock.json to reduce vulnerabilities (d151da8)
  • upgrade eslint from 8.23.0 to 8.23.1 (c5e7678)
  • upgrade eslint from 8.23.1 to 8.24.0 (71caa7f)
  • upgrade eslint from 8.24.0 to 8.25.0 (c3fabe7)
  • upgrade eslint from 8.25.0 to 8.26.0 (e375e3f)
  • upgrade eslint from 8.26.0 to 8.27.0 (ed8ccc2)
  • upgrade eslint from 8.27.0 to 8.28.0 (2bd2549)
  • upgrade eslint from 8.28.0 to 8.29.0 (a574437)
  • upgrade eslint from 8.29.0 to 8.30.0 (dfcd065)
  • upgrade eslint from 8.30.0 to 8.31.0 (001f486)
  • upgrade eslint from 8.31.0 to 8.32.0 (df3aa7c)
  • upgrade eslint from 8.32.0 to 8.33.0 (b583263)
  • upgrade eslint from 8.33.0 to 8.34.0 (35767b5)
  • upgrade eslint from 8.34.0 to 8.35.0 (af74885)
  • upgrade eslint from 8.35.0 to 8.36.0 (2801623)
  • upgrade eslint from 8.36.0 to 8.37.0 (06d7081)
  • upgrade eslint from 8.37.0 to 8.38.0 (9db8cff)
  • upgrade eslint from 8.38.0 to 8.39.0 (f11c447)
  • upgrade eslint from 8.39.0 to 8.40.0 (e425f82)
  • upgrade eslint-plugin-import from 2.26.0 to 2.27.4 (afe7c5e)
  • upgrade eslint-plugin-import from 2.27.4 to 2.27.5 (996fff2)
  • upgrade eslint-plugin-n from 15.2.5 to 15.3.0 (7f926e0)
  • upgrade eslint-plugin-n from 15.3.0 to 15.4.0 (9347386)
  • upgrade eslint-plugin-n from 15.4.0 to 15.5.1 (30dd19c)
  • upgrade eslint-plugin-n from 15.5.1 to 15.6.0 (9410e8d)
  • upgrade eslint-plugin-n from 15.6.0 to 15.6.1 (2aa25a0)
  • upgrade eslint-plugin-n from 15.6.1 to 15.7.0 (dfc5ede)
  • upgrade eslint-plugin-promise from 6.0.1 to 6.1.0 (29f6a98)
  • upgrade eslint-plugin-promise from 6.1.0 to 6.1.1 (80fa8f3)
  • upgrade mocha from 10.1.0 to 10.2.0 (f852df5)

Readme

Source

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:

ParameterType
typeString
clientString
bindingOptionsObject

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.

Attributetypedefault Value
idStringundefined
removeUnmappedBooleanas set by client, or true if not set by client
allowCopyBooleanfalse
bindingDataObjectundefined
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"
  }
]

Keywords

FAQs

Last updated on 01 Jun 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc