shareddiscovery

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2021 License: Apache-2.0 Imports: 12 Imported by: 0

README

Test Lint

Shared Discovery

A library for shared code in the Discovery Service.

API Documentation

Contributing

To contribute, you will need:

  • go >= 1.17
  • gopages for generating documentation

Create a feature branch from main and make your changes updates. Be sure to follow documentation standards when writing public functions and types and provide examples where applicable.

Make sure to run make docgen to update the documentation files.

Make tasks
  • make test runs unit tests
  • make mockgen builds mocks needed for testing

Once you feel good about your change and get it merged to main, you'll want to create a new release. Please follow semantic versioning as you release this library.

Installation

Since this is a private repository, you'll need a new environment variable called GOPRIVATE which you'll need to set to this repo. Put this in your .bashrc or .zshrc file:

export GOPRIVATE="github.com/pgdevelopers"

Now you should be able to run go get github.com/pgdevelopers/shareddiscovery to install this package in your project.

Usage

See the docs for complete API documentation.

Basic usage

  import (
    "github.com/pgdevelopers/shareddiscovery"
    "github.com/aws/aws-sdk-go/aws/session"
	  "github.com/aws/aws-sdk-go/service/dynamodb"
  )

  var discovery shareddiscovery.IFace

  func main() {
    query := QueryInput{Workspace: "tableName"}
    discovery.GetConfig(context.Background(), "someApiToken", query)
  }

  func init() {
    // setup AWS Session
    session := session.New()

    // setup DynamoDB
    dynamo := dynamodb.New(session)

    // setup shareddiscovery now
    discovery = shareddiscovery.New(dynamo)
  }
Testing

Provided is an interface that can be used with gomock to generate a mock for testing with. See the discovery service for more examples of testing with this library.

Documentation

Overview

SharedDiscovery is a shared library that supports getting data from the discovery dynamodb tables based on several criteria, including:

  • APIToken
  • Country
  • Workspace (tablename)

Admin Capabilities

This library also supports the idea of admin calls that can get any token needed. This is done using HMAC. For out purposes, the private key is stored in Secrets Manager under the name of the brand/environment.

Example

Typical usage is to setup a variable with the interface type and initialize that variable in your modules init function using the New() function provided

var shareddiscovery IFace

// setup AWS Session
session, _ := session.NewSession()

// setup DynamoDB
dynamo := dynamodb.New(session)

// define a QueryInput
query := QueryInput{Workspace: "tableName"}

// setup shareddiscovery now
shareddiscovery = New(dynamo)

// call functions
_, err := shareddiscovery.GetConfig(context.Background(), "someApiToken", query)
if err != nil {
	// deal with the error
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type IFace

type IFace interface {
	GetConfig(ctx context.Context, apiToken string, query QueryInput) (map[string]interface{}, error)
	AdminGetAPIToken(ctx context.Context, secretKey string, query QueryInput) (string, error)
}

IFace describes what is required for building a SharedDiscovery implementation.

type QueryInput

type QueryInput struct {
	AppName     string
	Brand       string
	Country     string
	Environment string
	Signature   string
	QueryString map[string]string
	Workspace   string
}

QueryInput defines the values used to query dynamo with.

type SharedDiscovery

type SharedDiscovery struct {
	IFace
	DynamodbSvc dynamodbiface.DynamoDBAPI
}

SharedDiscovery is a custom service object for interacting with the global config

func New

New is a constructor that takes a preconfigured dynamodbiface and returns an implementation of SharedDiscoveryIFace Use this in your init function after creating your aws session and initializing dynamo.

func (SharedDiscovery) AdminGetAPIToken

func (service SharedDiscovery) AdminGetAPIToken(ctx context.Context, secretKey string, query QueryInput) (string, error)

AdminGetAPIToken queries the dynamo table using the provided query and returns the correct apiToken for the caller to use to then make a request using the GetConfig call. It first validates the HMAC signature against the provided secretKey/query params to verify the caller is who they say they are.

Example

For making an admin call, a secretKey is required and the query string is needed to regenerate the signature correctly. The callers signature should also be passed via in to verify.

var shareddiscovery IFace

// setup AWS Session
session, _ := session.NewSession()

// setup DynamoDB
dynamo := dynamodb.New(session)

// define a QueryInput
query := QueryInput{
	Workspace:   "discovery_app",
	Signature:   "e52af791ec66085081f993a42d2a02a4b1dc08ad7b9f030dac5a0c20d5a0c68c",
	Brand:       "oralb",
	Environment: "qa",
	Country:     "US",
	QueryString: map[string]string{
		"brand":       "oralb",
		"environment": "qa",
		"countryCode": "US",
	},
}

// setup shareddiscovery now
shareddiscovery = New(dynamo)

// call functions
_, err := shareddiscovery.AdminGetAPIToken(context.Background(), "secretKey", query)
if err != nil {
	// deal with the error
}
Output:

func (SharedDiscovery) GetConfig

func (service SharedDiscovery) GetConfig(ctx context.Context, apiToken string, query QueryInput) (map[string]interface{}, error)

GetConfig uses the provided `APIToken` to get the correct configuration from the specified `tableName`.

Example (WithCountry)

Passing in a Country will get data from the table filtered by the apiToken and Country. This is useful when the apiToken isn't unique per row, but an apiToken can have multiple countries.

session, _ := session.NewSession()
shared := New(dynamodb.New(session))
query := QueryInput{Workspace: "tableName", Country: "US"}
_, err := shared.GetConfig(context.Background(), "apitoken", query)
if err != nil {
	// deal with the error
}
Output:

Example (WithoutCountry)

Passing in only the workspace as a query is best used when the apiToken is unique for every row. In our case, this is the Firmware table

session, _ := session.NewSession()
shared := New(dynamodb.New(session))
query := QueryInput{Workspace: "tableName"}
_, err := shared.GetConfig(context.Background(), "apitoken", query)
if err != nil {
	// deal with the error
}
Output:

Directories

Path Synopsis
mocks
mock_dynamodbiface
Package mock_dynamodbiface is a generated GoMock package.
Package mock_dynamodbiface is a generated GoMock package.

Jump to

Keyboard shortcuts

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