Skip to content
This repository has been archived by the owner on Aug 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #35 from adamdecaf/base-v0.10.0
Browse files Browse the repository at this point in the history
cmd/server: upgrade github.com/moov-io/base to v0.10.0
  • Loading branch information
adamdecaf authored Aug 14, 2019
2 parents 2db23ee + d28486d commit 2005c8d
Show file tree
Hide file tree
Showing 16 changed files with 86 additions and 102 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ script:
- misspell -error -locale US $GOFILES
- gocyclo -over 20 $GOFILES
- golint -set_exit_status $GOFILES
- staticcheck ./cmd/*/*.go *.go # TODO(adam): later add ./pkg/*/*.go
- staticcheck ./cmd/server/*.go
- staticcheck ./internal/database/*.go
- staticcheck *.go
- if [[ "$TRAVIS_OS_NAME" == "windows" ]]; then choco install -y mingw; export PATH=/c/tools/mingw64/bin:"$PATH";fi
after_success:
- bash <(curl -s https://codecov.io/bash) -X fix
Expand Down
34 changes: 17 additions & 17 deletions cmd/server/approval.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ var (
// TODO(adam): We need to hide these behind an admin level auth, but we'll write them for now.
// What about a header like x-admin-id ??
func addApprovalRoutes(logger log.Logger, svc *admin.Server, repo customerRepository, ofac *ofacSearcher) {
svc.AddHandler("/customers/{customerId}/status", updateCustomerStatus(logger, repo, ofac))
svc.AddHandler("/customers/{customerId}/addresses/{addressId}", updateCustomerAddress(logger, repo))
svc.AddHandler("/customers/{customerID}/status", updateCustomerStatus(logger, repo, ofac))
svc.AddHandler("/customers/{customerID}/addresses/{addressId}", updateCustomerAddress(logger, repo))
}

type updateCustomerStatusRequest struct {
Expand All @@ -54,7 +54,7 @@ type updateCustomerStatusRequest struct {
// - KYC is only valid if the Customer has first, last, address, and date of birth
// - OFAC can only be after an OFAC search has been performed (and search info recorded)
// - CIP can only be if the SSN has been set
func validCustomerStatusTransition(existing *client.Customer, futureStatus CustomerStatus, repo customerRepository, ofac *ofacSearcher, requestId string) error {
func validCustomerStatusTransition(existing *client.Customer, futureStatus CustomerStatus, repo customerRepository, ofac *ofacSearcher, requestID string) error {
eql := func(s string, status CustomerStatus) bool {
return strings.EqualFold(s, string(status))
}
Expand Down Expand Up @@ -117,8 +117,8 @@ func updateCustomerStatus(logger log.Logger, repo customerRepository, ofac *ofac
return
}

customerId, requestId := getCustomerID(w, r), moovhttp.GetRequestId(r)
if customerId == "" {
customerID, requestID := getCustomerID(w, r), moovhttp.GetRequestID(r)
if customerID == "" {
return
}

Expand All @@ -128,23 +128,23 @@ func updateCustomerStatus(logger log.Logger, repo customerRepository, ofac *ofac
return
}

cust, err := repo.getCustomer(customerId)
cust, err := repo.getCustomer(customerID)
if err != nil {
moovhttp.Problem(w, err)
return
}
if err := validCustomerStatusTransition(cust, req.Status, repo, ofac, requestId); err != nil {
if err := validCustomerStatusTransition(cust, req.Status, repo, ofac, requestID); err != nil {
moovhttp.Problem(w, err)
return
}

// Update Customer's status in the database
if err := repo.updateCustomerStatus(customerId, req.Status, req.Comment); err != nil {
if err := repo.updateCustomerStatus(customerID, req.Status, req.Comment); err != nil {
moovhttp.Problem(w, err)
return
}

respondWithCustomer(logger, w, customerId, requestId, repo)
respondWithCustomer(logger, w, customerID, requestID, repo)
}
}

Expand All @@ -159,7 +159,7 @@ func getAddressId(w http.ResponseWriter, r *http.Request) string {

// TODO(adam): Should Addresses have a 'Type: Previous'? I don't think we ever want to delete an address, but it can be marked as old.
// If we keep address info around does it have GDPR implications?
// PUT /customers/{customerId}/addresses/{addressId} only accept {"type": "Primary/Secondary", "validated": true/false}
// PUT /customers/{customerID}/addresses/{addressId} only accept {"type": "Primary/Secondary", "validated": true/false}

type updateCustomerAddressRequest struct {
Type string `json:"type"`
Expand All @@ -184,8 +184,8 @@ func updateCustomerAddress(logger log.Logger, repo customerRepository) http.Hand
return
}

customerId, addressId := getCustomerID(w, r), getAddressId(w, r)
if customerId == "" || addressId == "" {
customerID, addressId := getCustomerID(w, r), getAddressId(w, r)
if customerID == "" || addressId == "" {
return
}

Expand All @@ -199,14 +199,14 @@ func updateCustomerAddress(logger log.Logger, repo customerRepository) http.Hand
return
}

requestId := moovhttp.GetRequestId(r)
logger.Log("approval", fmt.Sprintf("updating address=%s for customer=%s", addressId, customerId), "requestId", requestId)
requestID := moovhttp.GetRequestID(r)
logger.Log("approval", fmt.Sprintf("updating address=%s for customer=%s", addressId, customerID), "requestID", requestID)

if err := repo.updateCustomerAddress(customerId, addressId, req.Type, req.Validated); err != nil {
logger.Log("approval", fmt.Sprintf("error updating customer=%s address=%s: %v", customerId, addressId, err), "requestId", requestId)
if err := repo.updateCustomerAddress(customerID, addressId, req.Type, req.Validated); err != nil {
logger.Log("approval", fmt.Sprintf("error updating customer=%s address=%s: %v", customerID, addressId, err), "requestID", requestID)
moovhttp.Problem(w, err)
return
}
respondWithCustomer(logger, w, customerId, requestId, repo)
respondWithCustomer(logger, w, customerID, requestID, repo)
}
}
6 changes: 3 additions & 3 deletions cmd/server/approval_ofac.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ type ofacSearcher struct {

// storeCustomerOFACSearch performs OFAC searches against the Customer's name and nickname if populated.
// The higher matching search result is stored in s.customerRepository for use later (in approvals)
func (s *ofacSearcher) storeCustomerOFACSearch(cust *client.Customer, requestId string) error {
func (s *ofacSearcher) storeCustomerOFACSearch(cust *client.Customer, requestID string) error {
ctx, cancelFn := context.WithTimeout(context.TODO(), 10*time.Second)
defer cancelFn()

sdn, err := s.ofacClient.Search(ctx, formatCustomerName(cust), requestId)
sdn, err := s.ofacClient.Search(ctx, formatCustomerName(cust), requestID)
if err != nil {
return fmt.Errorf("ofacSearcher.storeCustomerOFACSearch: name search for customer=%s: %v", cust.ID, err)
}
var nickSDN *ofac.Sdn
if cust.NickName != "" {
nickSDN, err = s.ofacClient.Search(ctx, cust.NickName, requestId)
nickSDN, err = s.ofacClient.Search(ctx, cust.NickName, requestID)
if err != nil {
return fmt.Errorf("ofacSearcher.storeCustomerOFACSearch: nickname search for customer=%s: %v", cust.ID, err)
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/server/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ var (

func TestBucket__openBucket(t *testing.T) {
// test the invalid cases
bucket, err := openBucket(nil, "", "", nil)
bucket, err := openBucket(context.TODO(), "", "", nil)
if bucket != nil || err == nil {
t.Errorf("expected error: bucket=%v error=%v", bucket, err)
}
bucket, err = openBucket(nil, "", "other", nil)
bucket, err = openBucket(context.TODO(), "", "other", nil)
if bucket != nil || err == nil {
t.Errorf("expected error: bucket=%v error=%v", bucket, err)
}
Expand Down Expand Up @@ -64,14 +64,14 @@ func TestBucket__getBucket(t *testing.T) {
}

func TestBucketAWS(t *testing.T) {
bucket, err := openBucket(nil, "", "aws", nil)
bucket, err := openBucket(context.TODO(), "", "aws", nil)
if err == nil || bucket != nil {
t.Errorf("expected error bucket=%v", bucket)
}
}

func TestBucketGCP(t *testing.T) {
bucket, err := openBucket(nil, "", "gcp", nil)
bucket, err := openBucket(context.TODO(), "", "gcp", nil)
if err == nil || bucket != nil {
t.Errorf("expected error bucket=%v", bucket)
}
Expand Down
28 changes: 14 additions & 14 deletions cmd/server/customer_ssn.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,38 @@ import (
)

type SSN struct {
customerId string
customerID string

encrypted []byte
masked string
}

func (s *SSN) String() string {
return fmt.Sprintf("SSN: customerId=%s masked=%s", s.customerId, s.masked)
return fmt.Sprintf("SSN: customerID=%s masked=%s", s.customerID, s.masked)
}

type ssnStorage struct {
keeperFactory secretFunc
repo customerSSNRepository
}

func (s *ssnStorage) encryptRaw(customerId, raw string) (*SSN, error) {
func (s *ssnStorage) encryptRaw(customerID, raw string) (*SSN, error) {
defer func() {
raw = ""
}()
if customerId == "" || raw == "" {
return nil, fmt.Errorf("missing customer=%s and/or SSN", customerId)
if customerID == "" || raw == "" {
return nil, fmt.Errorf("missing customer=%s and/or SSN", customerID)
}
keeper, err := s.keeperFactory(fmt.Sprintf("customer-%s-ssn", customerId))
keeper, err := s.keeperFactory(fmt.Sprintf("customer-%s-ssn", customerID))
if err != nil {
return nil, fmt.Errorf("ssnStorage: keeper init customer=%s: %v", customerId, err)
return nil, fmt.Errorf("ssnStorage: keeper init customer=%s: %v", customerID, err)
}
encrypted, err := keeper.Encrypt(context.Background(), []byte(raw))
if err != nil {
return nil, fmt.Errorf("ssnStorage: encrypt customer=%s: %v", customerId, err)
return nil, fmt.Errorf("ssnStorage: encrypt customer=%s: %v", customerID, err)
}
return &SSN{
customerId: customerId,
customerID: customerID,
encrypted: encrypted,
masked: maskSSN(raw),
}, nil
Expand All @@ -66,7 +66,7 @@ func maskSSN(s string) string {

type customerSSNRepository interface {
saveCustomerSSN(*SSN) error
getCustomerSSN(customerId string) (*SSN, error)
getCustomerSSN(customerID string) (*SSN, error)
}

type sqlCustomerSSNRepository struct {
Expand All @@ -89,25 +89,25 @@ func (r *sqlCustomerSSNRepository) saveCustomerSSN(ssn *SSN) error {
defer stmt.Close()

encoded := base64.StdEncoding.EncodeToString(ssn.encrypted)
if _, err := stmt.Exec(ssn.customerId, encoded, ssn.masked, time.Now()); err != nil {
if _, err := stmt.Exec(ssn.customerID, encoded, ssn.masked, time.Now()); err != nil {
return fmt.Errorf("sqlCustomerSSNRepository: saveCustomerSSN: exec: %v", err)
}
return nil
}

func (r *sqlCustomerSSNRepository) getCustomerSSN(customerId string) (*SSN, error) {
func (r *sqlCustomerSSNRepository) getCustomerSSN(customerID string) (*SSN, error) {
query := `select ssn, ssn_masked from customer_ssn where customer_id = ? limit 1;`
stmt, err := r.db.Prepare(query)
if err != nil {
return nil, fmt.Errorf("sqlCustomerSSNRepository: getCustomerSSN prepare: %v", err)
}
defer stmt.Close()

row := stmt.QueryRow(customerId)
row := stmt.QueryRow(customerID)

var encoded string
ssn := SSN{
customerId: customerId,
customerID: customerID,
}
if err := row.Scan(&encoded, &ssn.masked); err != nil {
if err == sql.ErrNoRows {
Expand Down
28 changes: 14 additions & 14 deletions cmd/server/customer_ssn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ func (r *testCustomerSSNRepository) saveCustomerSSN(*SSN) error {
return r.err
}

func (r *testCustomerSSNRepository) getCustomerSSN(customerId string) (*SSN, error) {
func (r *testCustomerSSNRepository) getCustomerSSN(customerID string) (*SSN, error) {
if r.ssn != nil {
return r.ssn, nil
}
return nil, r.err
}

func TestSSN(t *testing.T) {
customerId := base.ID()
ssn := &SSN{customerId: customerId, masked: "1###5"}
if v := ssn.String(); v != fmt.Sprintf("SSN: customerId=%s masked=1###5", customerId) {
customerID := base.ID()
ssn := &SSN{customerID: customerID, masked: "1###5"}
if v := ssn.String(); v != fmt.Sprintf("SSN: customerID=%s masked=1###5", customerID) {
t.Errorf("got %s", v)
}

Expand All @@ -55,7 +55,7 @@ func TestSSN(t *testing.T) {
return nil, errors.New("bad error")
},
}
if _, err := storage.encryptRaw(customerId, "1###5"); err == nil {
if _, err := storage.encryptRaw(customerID, "1###5"); err == nil {
t.Error("expected error")
} else {
if !strings.Contains(err.Error(), "ssnStorage: keeper init") {
Expand All @@ -77,19 +77,19 @@ func TestCustomerSSNStorage(t *testing.T) {
}

// encrypt SSN
customerId := base.ID()
ssn, err := storage.encryptRaw(customerId, "123456789")
customerID := base.ID()
ssn, err := storage.encryptRaw(customerID, "123456789")
if err != nil {
t.Error(err)
}
if ssn.customerId != customerId {
t.Errorf("ssn.customerId=%s", ssn.customerId)
if ssn.customerID != customerID {
t.Errorf("ssn.customerID=%s", ssn.customerID)
}
if ssn.masked != "1#######9" {
t.Errorf("ssn.masked=%s", ssn.masked)
}

keeper, err := storage.keeperFactory(fmt.Sprintf("customer-%s-ssn", customerId))
keeper, err := storage.keeperFactory(fmt.Sprintf("customer-%s-ssn", customerID))
if err != nil {
t.Fatal(err)
}
Expand All @@ -103,22 +103,22 @@ func TestCustomerSSNStorage(t *testing.T) {
}

func TestCustomerSSNRepository(t *testing.T) {
customerId := base.ID()
customerID := base.ID()
check := func(t *testing.T, customerSSNRepo *sqlCustomerSSNRepository) {

if ssn, err := customerSSNRepo.getCustomerSSN(customerId); ssn != nil || err != nil {
if ssn, err := customerSSNRepo.getCustomerSSN(customerID); ssn != nil || err != nil {
t.Fatalf("ssn=%v error=%v", ssn, err)
}

// write
bs := base64.StdEncoding.EncodeToString([]byte("123456789"))
ssn := &SSN{customerId: customerId, encrypted: []byte(bs), masked: "1#######9"}
ssn := &SSN{customerID: customerID, encrypted: []byte(bs), masked: "1#######9"}
if err := customerSSNRepo.saveCustomerSSN(ssn); err != nil {
t.Fatal(err)
}

// read again
ssn, err := customerSSNRepo.getCustomerSSN(customerId)
ssn, err := customerSSNRepo.getCustomerSSN(customerID)
if ssn == nil || err != nil {
t.Fatalf("ssn=%v error=%v", ssn, err)
}
Expand Down
16 changes: 8 additions & 8 deletions cmd/server/customers.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ func (cs *CustomerStatus) UnmarshalJSON(b []byte) error {
}

func addCustomerRoutes(logger log.Logger, r *mux.Router, repo customerRepository, customerSSNStorage *ssnStorage, ofac *ofacSearcher) {
r.Methods("GET").Path("/customers/{customerId}").HandlerFunc(getCustomer(logger, repo))
r.Methods("GET").Path("/customers/{customerID}").HandlerFunc(getCustomer(logger, repo))
r.Methods("POST").Path("/customers").HandlerFunc(createCustomer(logger, repo, customerSSNStorage, ofac))
r.Methods("PUT").Path("/customers/{customerId}/metadata").HandlerFunc(replaceCustomerMetadata(logger, repo))
r.Methods("POST").Path("/customers/{customerId}/address").HandlerFunc(addCustomerAddress(logger, repo))
r.Methods("PUT").Path("/customers/{customerID}/metadata").HandlerFunc(replaceCustomerMetadata(logger, repo))
r.Methods("POST").Path("/customers/{customerID}/address").HandlerFunc(addCustomerAddress(logger, repo))
}

func getCustomerID(w http.ResponseWriter, r *http.Request) string {
v, ok := mux.Vars(r)["customerId"]
v, ok := mux.Vars(r)["customerID"]
if !ok || v == "" {
moovhttp.Problem(w, errNoCustomerID)
return ""
Expand Down Expand Up @@ -100,7 +100,7 @@ func getCustomer(logger log.Logger, repo customerRepository) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w = wrapResponseWriter(logger, w, r)

customerID, requestID := getCustomerID(w, r), moovhttp.GetRequestId(r)
customerID, requestID := getCustomerID(w, r), moovhttp.GetRequestID(r)
if customerID == "" {
return
}
Expand Down Expand Up @@ -233,7 +233,7 @@ func createCustomer(logger log.Logger, repo customerRepository, customerSSNStora
moovhttp.Problem(w, err)
return
}
requestID := moovhttp.GetRequestId(r)
requestID := moovhttp.GetRequestID(r)

cust, ssn, err := req.asCustomer(customerSSNStorage)
if err != nil {
Expand Down Expand Up @@ -289,7 +289,7 @@ func replaceCustomerMetadata(logger log.Logger, repo customerRepository) http.Ha
moovhttp.Problem(w, err)
return
}
customerID, requestID := getCustomerID(w, r), moovhttp.GetRequestId(r)
customerID, requestID := getCustomerID(w, r), moovhttp.GetRequestID(r)
if customerID == "" {
return
}
Expand All @@ -304,7 +304,7 @@ func replaceCustomerMetadata(logger log.Logger, repo customerRepository) http.Ha

func addCustomerAddress(logger log.Logger, repo customerRepository) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
customerID, requestID := getCustomerID(w, r), moovhttp.GetRequestId(r)
customerID, requestID := getCustomerID(w, r), moovhttp.GetRequestID(r)
if customerID == "" {
return
}
Expand Down
Loading

0 comments on commit 2005c8d

Please sign in to comment.