Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
bnshr committed Dec 13, 2023
1 parent 1826d70 commit 4252bc9
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 160 deletions.
22 changes: 9 additions & 13 deletions api/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,25 @@ func CheckIfValidCredentials(partnerName, decodePassword string, db *sql.DB) err
}

// Already non-empty partner name and decoded password are given
func CreateCredentialsIfNotExists(partnerName, decodedPassword string, tx *sql.Tx) error {
func VerifyCredentialsAndCreateIfNotExists(partnerName, partnerPassword string, db *sql.DB) error {
// Encode the given password
encodedPartnerPassword, err := bcrypt.GenerateFromPassword([]byte(partnerPassword), bcrypt.MinCost)

// Search for partner in authenticator table
var encodedPassword string
searchPartnerErr := tx.QueryRow(util.ExtractPartnerAndPasswordCmd, partnerName).Scan(&encodedPassword)
// Encode given decoded password
encodedDecodedPassword, err := bcrypt.GenerateFromPassword([]byte(decodedPassword), bcrypt.MinCost)
searchPartnerErr := db.QueryRow(util.ExtractPartnerAndPasswordCmd, partnerName).Scan(&encodedPassword)

// If partner name is not recorded, add partner with encoded password
if searchPartnerErr == sql.ErrNoRows {
_, txErr := tx.Exec(util.InsertPartnerToAuthSQLCmd, partnerName, encodedDecodedPassword)
// Create partner entry into the database
_, txErr := db.Exec(util.InsertPartnerToAuthSQLCmd, partnerName, encodedPartnerPassword)
if txErr != nil {
util.HandleTransactionRollback(tx, util.AuthError, err)
return txErr
}
return nil
}
if err != nil {
util.HandleTransactionRollback(tx, util.AuthError, err)
return err
}

// If partner is recorded and password is wrong throw data
err = bcrypt.CompareHashAndPassword([]byte(encodedPassword), []byte(decodedPassword))
// If partner is found then check if password matches
err = bcrypt.CompareHashAndPassword([]byte(encodedPassword), []byte(encodedPartnerPassword))
if err != nil {
return fmt.Errorf(util.InvalidPasswordErr)
}
Expand Down
30 changes: 30 additions & 0 deletions api/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package api

import (
"encoding/json"
"net/http"

"github.com/test-network-function/collector/util"
)

func parseClaimFile(w http.ResponseWriter, r *http.Request) map[string]interface{} {
claimFileBytes := util.ReadClaimFile(w, r)
if claimFileBytes == nil {
// error occurred while reading claim file
return nil
}

var claimFileMap map[string]interface{}
err := json.Unmarshal(claimFileBytes, &claimFileMap)
if err != nil {
util.WriteError(w, util.UnmarshalErr, err.Error())
return nil
}

_, keyExists := claimFileMap[util.ClaimTag]
if !keyExists {
util.WriteError(w, util.MalformedClaimFileErr, util.ClaimFieldMissingErr)
return nil
}
return claimFileMap[util.ClaimTag].(map[string]interface{})
}
131 changes: 12 additions & 119 deletions api/parser_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ package api

import (
"net/http"
"time"

"database/sql"
"encoding/json"

"github.com/sirupsen/logrus"
"github.com/test-network-function/collector/storage"
"github.com/test-network-function/collector/util"
)
Expand All @@ -22,39 +17,32 @@ func ParserHandler(w http.ResponseWriter, r *http.Request, storage *storage.MySq
return
}

// Valid parameters
// 2. Validate claim results, and forms the input for db insert queries
isValid, claimResults := verifyClaimResultInJson(w, claimFileMap)
if !isValid {
return
}

// Valid parameters for database calls
partnerName := params[0]
decodedPassword := params[1]
ocpVersion := params[2]
executedBy := params[3]

// 2. Begin transaction to make entries into the table

tx, err := db.Begin()
if err != nil {
logrus.Errorf(util.BeginTxErr, err)
return
}

// 3. Validate partner's credentials, for non-existent partner create an entry in the database

err = CreateCredentialsIfNotExists(partnerName, decodedPassword, tx)
// which he has to use each time even when the claim file error happens
err := VerifyCredentialsAndCreateIfNotExists(partnerName, decodedPassword, db)
if err != nil {
util.WriteError(w, util.AuthError, err.Error())
return
}

// Check if an error occurred while parsing (which caused a Rollback).
if !parseClaimFile(w, r, tx, claimFileMap, partnerName, ocpVersion, executedBy) {
// 4. Store claim + claim result into the database
if !util.StoreClaimFileInDatabase(w, r, db, claimFileMap, claimResults, partnerName, ocpVersion, executedBy) {
util.WriteError(w, util.ClaimFileError, err.Error())
return
}

// If no error occurred, commit the transaction to make database changes.
err = tx.Commit()
if err != nil {
util.HandleTransactionRollback(tx, util.CommitTxErr, err)
return
}
/*
// Successfully write to S3
claimFile := getClaimFile(w, r)
Expand All @@ -69,98 +57,3 @@ func ParserHandler(w http.ResponseWriter, r *http.Request, storage *storage.MySq
logrus.Info(SuccessUploadingFileMSG)
*/
}

// Done
func uploadAndConvertClaimFile(w http.ResponseWriter, r *http.Request) map[string]interface{} {
claimFileBytes := util.ReadClaimFile(w, r)
if claimFileBytes == nil {
// error occurred while reading claim file
return nil
}

var claimFileMap map[string]interface{}
err := json.Unmarshal(claimFileBytes, &claimFileMap)
if err != nil {
util.WriteError(w, util.UnmarshalErr, err.Error())
return nil
}

_, keyExists := claimFileMap[util.ClaimTag]
if !keyExists {
util.WriteError(w, util.MalformedClaimFileErr, util.ClaimFieldMissingErr)
return nil
}
return claimFileMap[util.ClaimTag].(map[string]interface{})
}

// Done
func validateClaimKeys(w http.ResponseWriter, claimFileMap map[string]interface{}) map[string]interface{} {
versions, keyExists := claimFileMap[util.VersionsTag].(map[string]interface{})
if !keyExists {
util.WriteError(w, util.MalformedClaimFileErr, util.VersionsFieldMissingErr)
return nil
}

_, keyExists = versions["ocp"]
if !keyExists {
util.WriteError(w, util.MalformedClaimFileErr, util.OcpFieldMissingErr)
return nil
}

return versions
}

// Bad naming
func parseClaimFile(w http.ResponseWriter, r *http.Request, tx *sql.Tx, claimFileMap map[string]interface{}, partnerName, executedBy, ocpVersion string) bool {
_, err := tx.Exec(util.UseCollectorSQLCmd)
if err != nil {
util.HandleTransactionRollback(tx, util.ExecQueryErr, err)
return false
}

if insertToClaimTable(w, tx, claimFileMap, partnerName, executedBy, ocpVersion) && insertToClaimResultTable(w, tx, claimFileMap) {
return true
}
return false
}

// Done
func insertToClaimTable(w http.ResponseWriter, tx *sql.Tx, claimFileMap map[string]interface{}, partnerName, executedBy, ocpVersion string) bool {

_, err := tx.Exec(util.InsertToClaimSQLCmd, ocpVersion, executedBy, time.Now(), partnerName)
if err != nil {
util.HandleTransactionRollback(tx, util.ExecQueryErr, err)
return false
}
return true
}

func insertToClaimResultTable(w http.ResponseWriter, tx *sql.Tx, claimFileMap map[string]interface{}) bool {
results, keyExists := claimFileMap[util.ResultsTag].(map[string]interface{})
if !keyExists {
util.WriteError(w, util.MalformedClaimFileErr, util.ResultsFieldMissingErr)
return false
}

var claimID string
err := tx.QueryRow(util.ExtractLastClaimID).Scan(&claimID)
if err != nil {
util.HandleTransactionRollback(tx, util.ScanDBFieldErr, err)
return false
}

for testName := range results {
testData, testID, keyErr := validateInnerResultsKeys(results, testName)
if keyErr != "" {
util.WriteError(w, util.MalformedClaimFileErr, keyErr)
return false
}
_, err = tx.Exec(util.InsertToClaimResSQLCmd, claimID, testID["suite"].(string),
testID["id"].(string), testData["state"].(string))
if err != nil {
util.HandleTransactionRollback(tx, util.ExecQueryErr, err)
return false
}
}
return true
}
10 changes: 5 additions & 5 deletions api/result_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func ResultsHandler(w http.ResponseWriter, r *http.Request, storage *storage.MySqlStorage) {
logrus.Info("Handling the GET request", r.Body)
logrus.Info("Handling the GET request")
db := storage.MySql
partnerName, isValid := validateGetRequest(w, r, db)

Expand Down Expand Up @@ -66,10 +66,10 @@ func mapClaimsToStruct(claimRows *sql.Rows) []types.Claim {
return claims
}

func mapClaimResultsToStruct(claimResultsRows *sql.Rows) []types.ClaimResults {
var claimResults []types.ClaimResults
func mapClaimResultsToStruct(claimResultsRows *sql.Rows) []types.ClaimResult {
var claimResults []types.ClaimResult
for claimResultsRows.Next() {
var row types.ClaimResults
var row types.ClaimResult
err := claimResultsRows.Scan(&row.ID, &row.ClaimID, &row.SuiteName, &row.TestID, &row.TesStatus)
if err != nil {
logrus.Errorf(util.ScanDBFieldErr, err)
Expand All @@ -79,7 +79,7 @@ func mapClaimResultsToStruct(claimResultsRows *sql.Rows) []types.ClaimResults {
return claimResults
}

func combineClaimAndResultsToStruct(claims []types.Claim, claimResults []types.ClaimResults) []types.ClaimCollector {
func combineClaimAndResultsToStruct(claims []types.Claim, claimResults []types.ClaimResult) []types.ClaimCollector {
var collector []types.ClaimCollector
for _, claim := range claims {
var curClaim types.ClaimCollector
Expand Down
1 change: 1 addition & 0 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func NewServer(listenAddr string, db *storage.MySqlStorage, objectStore *storage
}

func (s *Server) Start() error {
logrus.Info("Starting server")
http.HandleFunc("/", s.handler)
return http.ListenAndServe(s.listenAddr, nil)
}
Expand Down
54 changes: 52 additions & 2 deletions api/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"

"github.com/sirupsen/logrus"
"github.com/test-network-function/collector/types"
"github.com/test-network-function/collector/util"
)

Expand All @@ -24,7 +25,7 @@ func validatePostRequest(w http.ResponseWriter, r *http.Request) (map[string]int
return nil, nil, false
}

claimFileMap := uploadAndConvertClaimFile(w, r)
claimFileMap := parseClaimFile(w, r)
if claimFileMap == nil {
// error occurred while uploading\converting claim file.
return nil, nil, false
Expand Down Expand Up @@ -69,7 +70,40 @@ func validateGetRequest(w http.ResponseWriter, r *http.Request, db *sql.DB) (str
return partnerName, true
}

// Done
/*
The validation is done at first, so that we do not keep database transaction open for validation
to reduce network bandwith to the remote RDS. Also, since the claim file structure can be changed in
the future, validation is kept separate from database insert which gives us less effort to make code
changes.
*/
func verifyClaimResultInJson(w http.ResponseWriter, claimFileMap map[string]interface{}) (bool, []types.ClaimResult) {
results, keyExists := claimFileMap[util.ResultsTag].(map[string]interface{})
if !keyExists {
util.WriteError(w, util.MalformedClaimFileErr, util.ResultsFieldMissingErr)
return false, nil
}

claimResults := []types.ClaimResult{}
for testName := range results {
testData, testID, keyErr := validateInnerResultsKeys(results, testName)
if keyErr != "" {
util.WriteError(w, util.MalformedClaimFileErr, keyErr)
return false, nil
}

claimResult := types.ClaimResult{
SuiteName: testID["suite"].(string),
TestID: testID["id"].(string),
TesStatus: testData["state"].(string),
}

claimResults = append(claimResults, claimResult)

}

return true, claimResults
}

func validateInnerResultsKeys(results map[string]interface{}, testName string) (
testData map[string]interface{}, testID map[string]interface{}, err string) {
testData, _ = results[testName].([]interface{})[0].(map[string]interface{})
Expand All @@ -95,3 +129,19 @@ func validateInnerResultsKeys(results map[string]interface{}, testName string) (
}
return testData, testID, ""
}

func validateClaimKeys(w http.ResponseWriter, claimFileMap map[string]interface{}) map[string]interface{} {
versions, keyExists := claimFileMap[util.VersionsTag].(map[string]interface{})
if !keyExists {
util.WriteError(w, util.MalformedClaimFileErr, util.VersionsFieldMissingErr)
return nil
}

_, keyExists = versions["ocp"]
if !keyExists {
util.WriteError(w, util.MalformedClaimFileErr, util.OcpFieldMissingErr)
return nil
}

return versions
}
7 changes: 3 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package main

import (
"log"
"time"

_ "github.com/go-sql-driver/mysql"
"github.com/sirupsen/logrus"
"github.com/test-network-function/collector/api"
"github.com/test-network-function/collector/storage"
"github.com/test-network-function/collector/util"

"log"

_ "github.com/go-sql-driver/mysql"
)

func main() {

readTimeOut, writeTimeOut, addr, envErr := util.GetServerEnvVars()
if envErr != "" {
logrus.Errorf(util.ServerEnvVarsError, envErr)
Expand Down
8 changes: 2 additions & 6 deletions storage/s3.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package storage

import (
"github.com/test-network-function/collector/types"
)

type S3Storage struct {
}

Expand All @@ -12,6 +8,6 @@ func NewS3Storage() *S3Storage {
return &S3Storage{}
}

func (s *S3Storage) Get() *types.Claim {
return &types.Claim{}
func (s *S3Storage) Get() *S3Storage {
return &S3Storage{}
}
Loading

0 comments on commit 4252bc9

Please sign in to comment.