Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Welcome message at home api route - no error 404 #145

Merged
merged 1 commit into from
Mar 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions tornjak-backend/api/agent/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import (
"strings"
"time"

"github.com/pkg/errors"
"github.com/cenkalti/backoff/v4"
"github.com/gorilla/mux"
"github.com/hashicorp/hcl"
"github.com/hashicorp/hcl/hcl/ast"
"github.com/pkg/errors"

"github.com/spiffe/spire/pkg/common/catalog"
agentdb "github.com/spiffe/tornjak/tornjak-backend/pkg/agent/db"
auth "github.com/spiffe/tornjak/tornjak-backend/pkg/agent/auth"
agentdb "github.com/spiffe/tornjak/tornjak-backend/pkg/agent/db"
)

type Server struct {
Expand All @@ -39,8 +39,8 @@ type Server struct {

// AgentDB for storing Workload Attestor Plugin Info of agents
TornjakConfig *TornjakConfig
Db agentdb.AgentDB
Auth auth.Auth
Db agentdb.AgentDB
Auth auth.Auth
}

func (s *Server) agentList(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -355,7 +355,7 @@ func retError(w http.ResponseWriter, emsg string, status int) {
}

// Handle preflight checks
func (s *Server) verificationMiddleware(next http.Handler) (http.Handler) {
func (s *Server) verificationMiddleware(next http.Handler) http.Handler {
f := func(w http.ResponseWriter, r *http.Request) {
if r.Method == "OPTIONS" {
cors(w, r)
Expand Down Expand Up @@ -456,6 +456,19 @@ func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
http.FileServer(http.Dir(h.staticPath)).ServeHTTP(w, r)
}

func (s *Server) Home(w http.ResponseWriter, r *http.Request) {
var ret = "Welcome to the Tornjak Backend!"

cors(w, r)
je := json.NewEncoder(w)

var err = je.Encode(ret)
if err != nil {
emsg := fmt.Sprintf("Error: %v", err.Error())
retError(w, emsg, http.StatusBadRequest)
}
}

// HandleRequests connects api links with respective functions
// Functions currently handle the api calls all as post-requests
func (s *Server) HandleRequests() {
Expand All @@ -465,6 +478,9 @@ func (s *Server) HandleRequests() {
}
rtr := mux.NewRouter()

// Home
rtr.HandleFunc("/", s.Home)

// Agents
rtr.HandleFunc("/api/agent/list", s.agentList)
rtr.HandleFunc("/api/agent/ban", s.agentBan)
Expand Down Expand Up @@ -552,7 +568,7 @@ func (s *Server) HandleRequests() {
}
}

//TODO map[string]catalog. type
// TODO map[string]catalog. type
func getPluginConfig(plugin map[string]catalog.HCLPluginConfig) (string, ast.Node, error) {
for k, d := range plugin {
return k, d.PluginData, nil
Expand Down Expand Up @@ -620,7 +636,7 @@ func NewAuth(authPlugin map[string]catalog.HCLPluginConfig) (auth.Auth, error) {
}
}

func (s *Server) ConfigureDefaults() (error) {
func (s *Server) ConfigureDefaults() error {
var err error
expBackoff := backoff.NewExponentialBackOff()
expBackoff.MaxElapsedTime = time.Second
Expand All @@ -633,7 +649,7 @@ func (s *Server) ConfigureDefaults() (error) {
return nil
}

func (s *Server) Configure() (error) {
func (s *Server) Configure() error {
var err error
//configs := map[string]map[string]catalog.HCLPluginConfig(*s.TornjakConfig.Plugins)
if s.TornjakConfig == nil {
Expand Down