tinykms

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 7, 2024 License: BSD-3-Clause Imports: 4 Imported by: 0

README

tinykms

codecov github

A tiny in-memory key management "system" that helps managing multiple keys inside a key set (e.g. rotating).

Foreword

This project does not claim to be production-ready. It should not be used in critical applications. If you have any feedback and improvement suggestions I am happy to hear your feedback or receive a PR. Use at your own risk.

Usage

import (
  "fmt"

  "github.com/henvo/tinykms"
)

rawJson := `{
  "keys": [
    { "id": "1", "raw": "...", "priority": 40 },
    { "id": "2", "raw": "...", "priority": 20 },
    { "id": "3", "raw": "...", "priority": 10 },
    { "id": "4", "raw": "...", "priority": 0 }
  ]
}`

// Initialize the key set from string.
ks, _ := tinykms.FromString(rawJson)

// Fetch a key by given ID.
key1, _ := ks.GetKeyByID("1")
fmt.Println(key1.ID) // Prints 1

// Fetch the key with highest priority (lowest number).
key2, _ := ks.GetKey()
fmt.Println(key2.ID) // Prints 3

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EmptyKeySetError = errors.New("KeySet is empty")

Functions

This section is empty.

Types

type Key

type Key struct {
	// ID is unique identifier for the given (raw) key.
	ID string `json:"id"`

	// Raw is the key itself.
	Raw string `json:"raw"`

	// Priority allows easier and more graceful key rotation.
	Priority KeyPriority `json:"priority"`

	// CreatedAt represents the date the key was generated.
	CreatedAt time.Time `json:"created_at"`
}

Key is the representation for a single key with its metadata.

func (Key) String added in v0.1.1

func (k Key) String() string

Implement the Stringer interface.

type KeyPriority

type KeyPriority uint8

KeyPriority is an indicator how a key should be handled by the KMS. 0 = no priority 1 = highest priority 255 = lowest priority

type KeySet

type KeySet struct {
	// Keys hold all keys.
	Keys []Key
}

KeySet holds all the keys provided.

func FromBytes

func FromBytes(rawJSON []byte) (*KeySet, error)

FromBytes initializes a KeySet from a JSON byte slice.

func FromString

func FromString(rawJSON string) (ks *KeySet, err error)

FromString initializes a KeySey from a JSON string.

func (*KeySet) GetKey

func (ks *KeySet) GetKey() (*Key, error)

GetKey selects the Key with the highest priority. The priority is defined by the smallest number for priority. Zero is no priority at all. If multiple keys have the same high priority the first key is picked.

func (*KeySet) GetKeyByID

func (ks *KeySet) GetKeyByID(id string) (*Key, error)

GetKey returns a key by given ID.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL