Invoicer-chapter3 - A simple REST API that manages invoices

Securing DevOps's invoicer

A simple REST API that manages invoices.

This is the code for Chapter 3 of Securing DevOps. It contains the code and scripts discussed in chapter 2 to the basic setup of the invoicer application and infrastructure, plus the improvements implemented in chapter 3.

Get your own copy

To try out the code in this repository, first create a fork in your own github account. Now before you do anything, edit the file in .circleci/config.yml and replace the working_directory parameter with your own namespace.

For example:

    working_directory: /go/src/github.com/Securing-DevOps/invoicer-chapter3

would become:

    working_directory: /go/src/github.com/jvehent/invoicer-chapter3

Then, sign up for circleci and build the project. The build will initially fail because it lacks the Docker credentials to push the invoicer's image to dockerhub.

Head over to hub.docker.com, create an account, then create a repository to store the docker containers into, as explained in chapter 2.

Head back to CircleCi and in the "Project Setting", add the following "Environment Variables" with the appropriate values.

  • DOCKER_USER
  • DOCKER_PASS

These variable will allow CircleCI to upload the docker container of the invoicer into your own account. Once added, trigger a rebuild of the CircleCI job and it should succeed and upload the container without issue.

You can then pull and run the container from your repository as follows:

$ docker run -it <MyGitHubUser>/invoicer-chapter3

Build the AWS infrastructure

The script create_ebs_env.sh creates a complete AWS infrastructure ready to host the invoicer. The script first creates a database, then an elastic beanstalk environment, and finally deploys the docker container of invoicer-chapter3. All you need to get started is creating an AWS account, then create a local profile (see chapter 2 for details) and run the following:

$ export AWS_PROFILE=cloudservices-aws-dev

$ export AWS_REGION=eu-west-1

$ ./create_ebs_env.sh

The script will outputs resources name and credentials for the new environment.

Creating EBS application ulfr-invoicer-201709231530
default vpc is vpc-c02b81a5
DB security group is sg-fc20478f
RDS Postgres database is being created. username=invoicer; password='i0l2jSQ5WkK_441c8dXwlYods9'
..................dbhost=ulfr-invoicer-201709231530.czvvrkdqhklf.us-east-1.rds.amazonaws.com
ElasticBeanTalk application created
API environment e-qp2pyjhhma is being created
.....
API security group sg-732d4a00 authorized to connect to database security group sg-fc20478f
make_bucket: ulfr-invoicer-201709231530
upload: ./app-version.json to s3://ulfr-invoicer-201709231530/app-version.json
waiting for environment....................
Environment is being deployed. Public endpoint is http://ulfr-invoicer-201709231530-invoicer-api.egmh2pupxy.us-east-1.elasticbeanstalk.com

Manual build

Build a statically linked invoicer binary. Requires Go 1.6.

$ mkdir bin
$ go build --ldflags '-extldflags "-static"' -o bin/invoicer .

Then build the container.

$ docker build -t securingdevops/invoicer .

Database Configuration

The invoicer will automatically create a local sqlite database by default, but if you want to run with postgres, follow these instructions.

Create a postgres database named invoicer and grant user invoicer full access to it.

CREATE DATABASE invoicer;
CREATE ROLE invoicer;
ALTER ROLE invoicer WITH NOSUPERUSER INHERIT NOCREATEROLE NOCREATEDB LOGIN PASSWORD 'invoicer';
ALTER DATABASE invoicer OWNER TO invoicer;

When running PG and Docker on the same box, configure pg_hba to allow the local docker network to connect.

...
# trust local docker hosts
host    all             all             172.17.0.0/16           trust

Run

$ docker run -it \
    -e INVOICER_USE_POSTGRES="yes" \
    -e INVOICER_POSTGRES_USER="invoicer" \
    -e INVOICER_POSTGRES_PASSWORD="invoicer" \
    -e INVOICER_POSTGRES_HOST="172.17.0.1" \
    -e INVOICER_POSTGRES_DB="invoicer" \
    -e INVOICER_POSTGRES_SSLMODE="disable" \
    securingdevops/invoicer-chapter3

Use

Create an invoice

$ curl -X POST \
--data '{"is_paid": false, "amount": 1664, "due_date": "2016-05-07T23:00:00Z", "charges": [ { "type":"blood work", "amount": 1664, "description": "blood work" } ] }' \
http://172.17.0.2:8080/invoice

Retrieve an invoice

$ curl http://172.17.0.2:8080/invoice/1
{"ID":1,"CreatedAt":"2016-05-21T15:33:21.855874Z","UpdatedAt":"2016-05-21T15:33:21.855874Z","DeletedAt":null,"is_paid":false,"amount":1664,"payment_date":"0001-01-01T00:00:00Z","due_date":"2016-05-07T23:00:00Z","charges":[{"ID":1,"CreatedAt":"2016-05-21T15:33:21.8637Z","UpdatedAt":"2016-05-21T15:33:21.8637Z","DeletedAt":null,"invoice_id":1,"type":"blood
work","amount":1664,"description":"blood work"}]}
Similar Resources

Ethereum Dapp Go API

Web3 Go Ethereum Dapp Go API, inspired by web3.js. Report Bug · Pull Request Introduction This is the Ethereum Golang API which connects to the Generi

Nov 29, 2022

A Golang cryptocurrency trading API & Library. Support Binance, BitMEX, Deribit, Bybit, Huobi DM, OKEX Futures and more.

A Golang cryptocurrency trading API & Library. Support Binance, BitMEX, Deribit, Bybit, Huobi DM, OKEX Futures and more.

CREX 中文 | English CREX 是一个用Golang语言开发的量化交易库。支持tick级别数字币期货平台的回测和实盘。实盘与回测无缝切换,无需更改代码。 回测 示例 @backtest 交易结果 开源策略 https://github.com/coinrust/trading-stra

Nov 18, 2022

Example of querying the balance of Crypton and UUSD with Utopia Ecosystem API and utopialib-go

account-balance-go Example of querying the balance of Crypton and UUSD with Utopia Ecosystem API and utopialib-go example of use flags: -host string

Oct 8, 2021

Flashbots utilities in Go: Blocks & Transactions API, and tools to spot bundle and block irregularities

Utilities for Flashbots Go API client for the mev-blocks API for information about Flashbots blocks and transactions Detect bundle errors: (a) out of

Nov 26, 2022

Abigen by contract address using etherscan api

Abigen for zoomers Just a simple wrapper to fetch abis from etherscan and run abigen on it. Uses the name of a contract if possible. Usage First put y

Mar 24, 2022

e-distribucion (Endesa) API para la lectura remota de contadores inteligentes

e-Distribucion API Go API to read Endesa energy meters info. Ported from Python to Go using https://github.com/trocotronic/edistribucion. You'll need

Dec 4, 2021

Api for getting blockchain block and transaction details in Go.

Getting Blockchain Data Api for getting blockchain block and transaction details Things you need Go: brew install go Install docker Setup go folder in

Dec 14, 2021

A Gomora template for building dApps and web3-powered API and smart contract listeners

Gomora dApp A Gomora template for building dApps and web3-powered API and smart contract listeners Local Development Setup the .env file first cp .env

Feb 15, 2022

create @auth0 management api tokens

Vault Secrets Plugin - Auth0 Vault secrets plugins to simplying creation, management, and revocation of auth0 management API tokens. Usage Setup Endpo

Jan 2, 2022
REST API to create and interact with decks of playing cards

Cards API REST API to create and interact with decks of playing cards. Tech Stac

Jul 14, 2022
A tiny Crypto Project with REST API

Goal ⚽ Golang으로 아무 쓸모 없는 블록체인을 한땀 한땀 만들어보면서 BTC나 ETH에 적용된 블록체인 기법 이해해보기! Special Thanks to Nico! Current... ??️ Work ?? 자세한 내용은 issue와 PR을 확인하세요. Now

Dec 30, 2021
Go implementation of the Data At Rest Encryption (DARE) format.

Secure IO Go implementation of the Data At Rest Encryption (DARE) format. Introduction It is a common problem to store data securely - especially on u

Dec 18, 2022
Go-block-api - Golang implementation of Ethereum Block API

Go Ethereum Block API Golang implementation of Ethereum Block API This API can s

Jan 13, 2022
Simple Crypto API for Go

cryptogo Simple cryptography API in Go Hashes: hash.MD5Bytes, hash.MD5Base64, hash.MD5Hex calculates an MD5 chechsum of the input byte array as a byte

Nov 10, 2021
A simple ticket-wallet API. It is fully dockerized

Ticket Wallet This is a simple ticket-wallet API. It is fully dockerized. Table of contents 1 Response Data format 2 API  2.1 Get layout  2.2 Start se

Jan 23, 2022
Merchant API reference implementation

mAPI More details available in the BRFC Spec for Merchant API. The old golang (v1.1) implementation is no longer being maintained and has been moved t

Dec 14, 2022
HTTP API for a BitClout node
HTTP API for a BitClout node

BitClout is a blockchain built from the ground up to support a fully-featured social network. Its architecture is similar to Bitcoin, only it supports complex social network data like profiles, posts, follows, creator coin transactions, and more.

Dec 24, 2022
A command line Crypto-currency ticker made using golang and WazirX Api
A command line Crypto-currency ticker made using golang and WazirX Api

░█████╗░██████╗░██╗░░░██╗██████╗░████████╗███████╗██╗░░██╗ ██╔══██╗██╔══██╗╚██╗░██╔╝██╔══██╗╚══██╔══╝██╔════╝╚██╗██╔╝ ██║░░╚═╝██████╔╝░╚████╔╝░██████╔

Feb 6, 2022
Pure Go implementation of the NaCL set of API's

go-nacl This is a pure Go implementation of the API's available in NaCL: https://nacl.cr.yp.to. Compared with the implementation in golang.org/x/crypt

Dec 16, 2022