Go API Documentation

github.com/TykTechnologies/tyk/storage/kv

No package summary is available.

Package

Files: 3. Third party imports: 2. Imports from organisation: 0. Tests: 0. Benchmarks: 0.

Vars

var (
	// ErrKeyNotFound is an error meant when a key doesn't exists in the
	// storage backend
	ErrKeyNotFound = errors.New("key not found")
)

Types

Consul

Consul is an implementation of a KV store which uses Consul as it's backend

Field name Field type Comment
store

*consulapi.KV

No comment on field.
type Consul struct {
	store *consulapi.KV
}

Store

Store is a standard interface to a KV backend

Field name Field type Comment
type

any

No comment on field.
type Store interface {
	Get(key string) (string, error)
	// Put writes a value to the KV store
	Put(key string, value string) error
}

Vault

Vault is an implementation of a KV store which uses Consul as it's backend

Field name Field type Comment
client

*vaultapi.Client

No comment on field.
kvV2

bool

No comment on field.
type Vault struct {
	client	*vaultapi.Client
	kvV2	bool
}

Functions

func NewConsul

NewConsul returns a configured consul KV store adapter

func NewConsul(conf config.ConsulConfig) (Store, error) {
	return newConsul(conf)
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func NewVault

NewVault returns a configured vault KV store adapter

func NewVault(conf config.VaultConfig) (Store, error) {
	return newVault(conf)
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func (*Consul) Get

func (c *Consul) Get(key string) (string, error) {
	pair, _, err := c.store.Get(key, nil)
	if err != nil {
		return "", err
	}

	if pair == nil {
		return "", ErrKeyNotFound
	}

	return string(pair.Value), nil
}

Cognitive complexity: 4, Cyclomatic complexity: 3

func (*Consul) Put

func (c *Consul) Put(key string, value string) error {
	kv := c.store

	p := &consulapi.KVPair{
		Key:	key,
		Value:	[]byte(value),
	}

	_, err := kv.Put(p, nil)
	return err
}

Cognitive complexity: 1, Cyclomatic complexity: 1

Uses: consulapi.KVPair.

func (*Consul) Store

func (c *Consul) Store() *consulapi.KV {
	return c.store
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func (*Vault) Client

func (v *Vault) Client() *vaultapi.Client {
	return v.client
}

Cognitive complexity: 0, Cyclomatic complexity: 1

func (*Vault) Get

func (v *Vault) Get(key string) (string, error) {
	logicalStore := v.client.Logical()

	if v.kvV2 {
		// Version 2 engine. Make sure to append data in front
		splitKey := strings.Split(key, "/")
		if len(splitKey) > 0 {
			splitKey[0] = splitKey[0] + "/data"
			key = strings.Join(splitKey, "/")
		}
	}

	splitted := strings.Split(key, ".")
	if len(splitted) != 2 {
		return "", errors.New("key should be in form of config.value")
	}

	secret, err := logicalStore.Read(splitted[0])
	if err != nil {
		return "", err
	}

	if secret == nil {
		return "", ErrKeyNotFound
	}

	var val map[string]interface{} = secret.Data

	if v.kvV2 {
		var ok bool
		val, ok = secret.Data["data"].(map[string]interface{})
		if !ok {
			// This is unlikely to happen though
			return "", ErrKeyNotFound
		}
	}

	value, ok := val[splitted[1]]
	if !ok {
		return "", ErrKeyNotFound
	}

	return value.(string), nil
}

Cognitive complexity: 18, Cyclomatic complexity: 9

Uses: errors.New, strings.Join, strings.Split.

func (*Vault) Put

func (v *Vault) Put(key string, value string) error {
	logicalStore := v.client.Logical()

	if v.kvV2 {
		// Version 2 engine. Make sure to append data in front
		splitKey := strings.Split(key, "/")
		if len(splitKey) > 0 {
			splitKey[0] = splitKey[0] + "/data"
			key = strings.Join(splitKey, "/")
		}
	}

	splitted := strings.Split(key, ".")
	if len(splitted) != 2 {
		return errors.New("key should be in form of config.value")
	}

	// For v2, we need to wrap the data in a data object
	var data map[string]interface{}
	if v.kvV2 {
		data = map[string]interface{}{
			"data": map[string]interface{}{
				splitted[1]: value,
			},
		}
	} else {
		data = map[string]interface{}{
			splitted[1]: value,
		}
	}

	_, err := logicalStore.Write(splitted[0], data)
	return err
}

Cognitive complexity: 17, Cyclomatic complexity: 5

Uses: errors.New, strings.Join, strings.Split.

Private functions

func newConsul

newConsul (conf config.ConsulConfig) (Store, error)
References: consulapi.DefaultConfig, consulapi.NewClient.

func newVault

newVault (conf config.VaultConfig) (Store, error)
References: errors.New, vaultapi.DefaultConfig, vaultapi.NewClient.


Tests

Files: 2. Third party imports: 2. Imports from organisation: 0. Tests: 3. Benchmarks: 0.

Vars

var _ Store = (*Consul)(nil)

Test functions

TestConsulPut

References: assert.Equal, assert.Error, assert.NoError, config.ConsulConfig, testing.T.

TestConsul_Get

References: assert.ErrorIs, config.Default, consulapi.KVPair.

TestVaultPut

References: assert.Equal, assert.Error, assert.NoError, assert.True, config.VaultConfig, testing.T.