Skip to content

Commit

Permalink
workflow: validate kind attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
sevein committed Dec 6, 2019
1 parent cc3b168 commit 914e639
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
36 changes: 35 additions & 1 deletion internal/workflow/hari_activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net"
Expand Down Expand Up @@ -35,9 +36,13 @@ func NewUpdateHARIActivity(m *Manager) *UpdateHARIActivity {
}

func (a UpdateHARIActivity) Execute(ctx context.Context, tinfo *TransferInfo) error {
if err := validateKind(tinfo.Bundle.Kind); err != nil {
return nonRetryableError(fmt.Errorf("error validating kind attribute: %v", err))
}

apiURL, err := a.url()
if err != nil {
return nonRetryableError(fmt.Errorf("error in URL construction: %w", err))
return nonRetryableError(fmt.Errorf("error in URL construction: %v", err))
}

mock, _ := hookAttrBool(a.manager.Hooks, "hari", "mock")
Expand Down Expand Up @@ -149,3 +154,32 @@ type avlRequest struct {
Timestamp time.Time `json:"timestamp"` // RFC3339. E.g. "2006-01-02T15:04:05Z07:00".
AIPID string `json:"aip_id"`
}

var knownKinds = []string{
"DPJ", "EPJ", "AVLXML", "OTHER",
}

func validateKind(kind string) error {
if kind == "" {
return errors.New("empty")
}

const suffix = "-SIP"
if !strings.HasSuffix(kind, suffix) {
return fmt.Errorf("attribute (%s) does not containt suffix (\"-SIP\")", kind)
}
kind = strings.TrimSuffix(kind, "-SIP")

var known bool
for _, k := range knownKinds {
if k == kind {
known = true
break
}
}
if !known {
return fmt.Errorf("attribute (%s) is unexpected/unknown", kind)
}

return nil
}
4 changes: 4 additions & 0 deletions internal/workflow/prod_activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func (a *UpdateProductionSystemActivity) Execute(ctx context.Context, tinfo *Tra
return nonRetryableError(errors.New("unknown originalID"))
}

if err := validateKind(tinfo.Bundle.Kind); err != nil {
return nonRetryableError(fmt.Errorf("error validating kind attribute: %v", err))
}

// We expect tinfo.StoredAt to have the zero value when the ingestion
// has failed. Here we set a new value as it is a required field.
if tinfo.StoredAt.IsZero() {
Expand Down

0 comments on commit 914e639

Please sign in to comment.