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

@cag-group/utils

v1.0.1

Published

Various helpers, utilities and tools

Downloads

10

Readme

Various helpers, tools and utilities for Node.js

API authorization

Use in a server application to implement basic authentication in it's REST API.

The class BasicAuthChecker is used in code the do authorization. Credentials are read from the local directory secrets which when running locally contains uncommitted credentials for development, and in Kubernetes a ecret containing the credentials are mounted at secrets so the application always reach it by:

const credentials = require('../secrets/api-credentials/api-credentials.json')

disregarding if running on a local laptop, in cloud stage or cloud prod.

Use in code like this:

const {BasicAuthChecker} = require('@cag-group/utils')
const credentials = require('../secrets/api-credentials/api-credentials.json')
...
const checker = new BasicAuthChecker(credentials)
const username = checker.getValidUser(req)
if (!username) {
  console.log('Missing/invalid auth')
  return res.sendStatus(401)
}

Create file api-credentials.json in the folder secrets/api-credentials with accounts for local tests:

[
  { "name": "u1", "pass": "somepass" },
  { "name": "u1", "pass": "changedpass" },
  { "name": "u2", "pass": "anotherpass" }
]

do not commit files in /secrets, these are for local tests.

In the example above user "u1" is present twice with different passwords. basic-auth-checker supports this in order to support change of API-passwords without downtime.

Create api-credentials secret

Create a local file api-credentials.json in the root directory with the intended users and generated passwords (see command below).

Create the secret:

kubectl -n your-namespace create secret generic api-credentials --from-file=api-credentials.json

Use api-credentials secret in Kubernetes

  1. Define a volume in server.yaml on the same level as containers::
      volumes:
        - name: api-credentials
          secret:
            secretName: api-credentials
  1. Mount the secret volume in the server container
        volumeMounts:
        - name: api-credentials
          mountPath: /server/secrets/api-credentials/
          readOnly: true

Update existing usernames/passwords in existing kubernetes secret

  1. Get existing credentials api-credentials.json from your secrets vault and save it in the root folder.
  2. Generate a new password, for example with: dd if=/dev/urandom bs=1 count=32 2>/dev/null | base64 | rev | cut -b 2- | rev | tr -dc _A-Z-a-z-0-9 | head -c15;
  3. Edit the file and add a new row with same username and the generated password
  4. Create a new secret with the updated content:
kubectl -n your-namespace delete secret api-credentials
kubectl -n your-namespace create secret generic api-credentials --from-file=api-credentials.json

Restart the pod in order for it to read the changed secret:

kubectl -n your-namespace delete pod <podname>
  1. Save the new credentials in the secrets vault and delete the local file