[doc] remove "describe" endpoints in API

This commit is contained in:
Julien Vehent 2016-05-31 17:39:12 -04:00
Родитель fbc17b40dc
Коммит f5d7092171
4 изменённых файлов: 9 добавлений и 273 удалений

Просмотреть файл

@ -8,40 +8,15 @@ package main
import (
"encoding/json"
"fmt"
"github.com/jvehent/cljs"
"mig.ninja/mig"
"mig.ninja/mig/pgp"
"net/http"
"strconv"
"time"
"github.com/jvehent/cljs"
"mig.ninja/mig"
"mig.ninja/mig/pgp"
)
// describeCreateAction returns a resource that describes how to POST new actions
func describeCreateAction(respWriter http.ResponseWriter, request *http.Request) {
var err error
opid := getOpID(request)
loc := fmt.Sprintf("%s%s", ctx.Server.Host, request.URL.String())
resource := cljs.New(loc)
defer func() {
if e := recover(); e != nil {
ctx.Channels.Log <- mig.Log{OpID: opid, Desc: fmt.Sprintf("%v", e)}.Err()
resource.SetError(cljs.Error{Code: fmt.Sprintf("%.0f", opid), Message: fmt.Sprintf("%v", e)})
respond(http.StatusInternalServerError, resource, respWriter, request)
}
ctx.Channels.Log <- mig.Log{OpID: opid, Desc: "leaving describeCreateAction()"}.Debug()
}()
err = resource.SetTemplate(cljs.Template{
Data: []cljs.Data{
{Name: "action", Value: "URL encoded signed MIG action", Prompt: "Signed MIG Action"},
},
})
if err != nil {
panic(err)
}
respond(http.StatusOK, resource, respWriter, request)
}
// createAction receives a signed action in a POST request, validates it,
// and write it into the scheduler spool
func createAction(respWriter http.ResponseWriter, request *http.Request) {
@ -138,32 +113,6 @@ func createAction(respWriter http.ResponseWriter, request *http.Request) {
respond(http.StatusAccepted, resource, respWriter, request)
}
// describeCancelAction returns a resource that describes how to cancel an action
func describeCancelAction(respWriter http.ResponseWriter, request *http.Request) {
var err error
opid := getOpID(request)
loc := fmt.Sprintf("%s%s", ctx.Server.Host, request.URL.String())
resource := cljs.New(loc)
defer func() {
if e := recover(); e != nil {
ctx.Channels.Log <- mig.Log{OpID: opid, Desc: fmt.Sprintf("%v", e)}.Err()
resource.SetError(cljs.Error{Code: fmt.Sprintf("%.0f", opid), Message: fmt.Sprintf("%v", e)})
respond(http.StatusInternalServerError, resource, respWriter, request)
}
ctx.Channels.Log <- mig.Log{OpID: opid, Desc: "leaving describeCancelAction()"}.Debug()
}()
err = resource.SetTemplate(cljs.Template{
Data: []cljs.Data{
{Name: "id", Value: "[0-9]{1,20}", Prompt: "Action ID"},
},
})
if err != nil {
panic(err)
}
respond(http.StatusOK, resource, respWriter, request)
}
// getAction queries the database and retrieves the detail of an action
func getAction(respWriter http.ResponseWriter, request *http.Request) {
var err error

Просмотреть файл

@ -80,14 +80,10 @@ func main() {
authenticateLoader(getManifestFile)).Methods("POST")
// Investigator resources that require authentication
s.HandleFunc("/",
authenticate(getHome, false)).Methods("GET")
s.HandleFunc("/search",
authenticate(search, false)).Methods("GET")
s.HandleFunc("/action",
authenticate(getAction, false)).Methods("GET")
s.HandleFunc("/action/create/",
authenticate(describeCreateAction, false)).Methods("GET")
s.HandleFunc("/action/create/",
authenticate(createAction, false)).Methods("POST")
s.HandleFunc("/command",
@ -118,12 +114,8 @@ func main() {
authenticate(manifestLoaders, true)).Methods("GET")
s.HandleFunc("/investigator",
authenticate(getInvestigator, true)).Methods("GET")
s.HandleFunc("/investigator/create/",
authenticate(describeCreateInvestigator, true)).Methods("GET")
s.HandleFunc("/investigator/create/",
authenticate(createInvestigator, true)).Methods("POST")
s.HandleFunc("/investigator/update/",
authenticate(describeUpdateInvestigator, true)).Methods("GET")
s.HandleFunc("/investigator/update/",
authenticate(updateInvestigator, true)).Methods("POST")
@ -393,133 +385,6 @@ func getIP(respWriter http.ResponseWriter, request *http.Request) {
}
}
// getHome returns a basic document that presents the different ressources
// available in the API, as well as some status information
func getHome(respWriter http.ResponseWriter, request *http.Request) {
var err error
opid := getOpID(request)
loc := fmt.Sprintf("%s%s", ctx.Server.Host, request.URL.String())
resource := cljs.New(loc)
defer func() {
if e := recover(); e != nil {
ctx.Channels.Log <- mig.Log{OpID: opid, Desc: fmt.Sprintf("%v", e)}.Err()
resource.SetError(cljs.Error{Code: fmt.Sprintf("%.0f", opid), Message: fmt.Sprintf("%v", e)})
respond(http.StatusInternalServerError, resource, respWriter, request)
}
ctx.Channels.Log <- mig.Log{OpID: opid, Desc: "leaving getHome()"}.Debug()
}()
resource.AddQuery(cljs.Query{
Rel: "Get dashboard",
Href: fmt.Sprintf("%s/dashboard", ctx.Server.BaseURL),
})
if err != nil {
panic(err)
}
err = resource.AddLink(cljs.Link{
Rel: "create action",
Href: fmt.Sprintf("%s/action/create/", ctx.Server.BaseURL),
Name: "POST endpoint to create an action"})
if err != nil {
panic(err)
}
err = resource.AddLink(cljs.Link{
Rel: "create investigator",
Href: fmt.Sprintf("%s/investigator/create/", ctx.Server.BaseURL),
Name: "POST endpoint to create an investigator"})
if err != nil {
panic(err)
}
err = resource.AddLink(cljs.Link{
Rel: "update investigator",
Href: fmt.Sprintf("%s/investigator/update/", ctx.Server.BaseURL),
Name: "POST endpoint to update an investigator"})
if err != nil {
panic(err)
}
// Describe the queries that are exposed to the client
err = resource.AddQuery(cljs.Query{
Rel: "Query action by ID",
Href: fmt.Sprintf("%s/action", ctx.Server.BaseURL),
Prompt: "GET endpoint to query an action by ID, using url parameter ?actionid=<numerical id>",
Data: []cljs.Data{
{Name: "actionid", Value: "[0-9]{1,20}", Prompt: "Action ID"},
},
})
if err != nil {
panic(err)
}
resource.AddQuery(cljs.Query{
Rel: "Query command by ID",
Href: fmt.Sprintf("%s/command", ctx.Server.BaseURL),
Prompt: "GET endpoint to query a command by ID, using url parameter ?commandid=<numerical id>",
Data: []cljs.Data{
{Name: "commandid", Value: "[0-9]{1,20}", Prompt: "Command ID"},
},
})
if err != nil {
panic(err)
}
resource.AddQuery(cljs.Query{
Rel: "Query agent by ID",
Href: fmt.Sprintf("%s/agent", ctx.Server.BaseURL),
Prompt: "GET endpoint to query an agent by ID, using url parameter ?agentid=<numerical id>",
Data: []cljs.Data{
{Name: "agentid", Value: "[0-9]{1,20}", Prompt: "Agent ID"},
},
})
if err != nil {
panic(err)
}
resource.AddQuery(cljs.Query{
Rel: "Query investigator by ID",
Href: fmt.Sprintf("%s/investigator", ctx.Server.BaseURL),
Prompt: "GET endpoint to query an investigator by ID, using url parameter ?investigatorid=<numerical id>",
Data: []cljs.Data{
{Name: "investigatorid", Value: "[0-9]{1,20}", Prompt: "Investigator ID"},
},
})
if err != nil {
panic(err)
}
resource.AddQuery(cljs.Query{
Rel: "Search stuff",
Href: fmt.Sprintf("%s/search", ctx.Server.BaseURL),
Prompt: "GET endpoint to search for stuff",
Data: []cljs.Data{
{Name: "type", Value: "(command|action|agent|investigator)", Prompt: "type defines what the search is looking for"},
{Name: "actionid", Value: "123456789...", Prompt: "filter results on the action id"},
{Name: "actionname", Value: "some action name", Prompt: "filter results on the action name"},
{Name: "after", Value: "11-01-01 12:12:12.686438508-04:00", Prompt: "return results recorded after this RFC3339 date"},
{Name: "agentid", Value: "123456789...", Prompt: "filter results on the agent id"},
{Name: "agentname", Value: "agent123.example.net", Prompt: "filter results on the agent name"},
{Name: "before", Value: "9998-01-01 12:12:12.686438508-04:00", Prompt: "return results recorded before this RFC3339 date"},
{Name: "commandid", Value: "123456789...", Prompt: "filter results on the command id"},
{Name: "foundanything", Value: "(true|false)", Prompt: "return commands that have results with foundanything flag set to true or false"},
{Name: "investigatorid", Value: "123456789...", Prompt: "filter results on the investigator id"},
{Name: "investigatorname", Value: "%bob%", Prompt: "filter results on the investigator name"},
{Name: "limit", Value: "10000", Prompt: "limit the number of results to 10,000 by default"},
{Name: "offset", Value: "0", Prompt: "skip the first results, default value of 0 does not skip"},
{Name: "report", Value: "(compliancesummary|complianceitems)", Prompt: "if set, return results in the given report format"},
{Name: "status", Value: "(sent|success|cancelled|expired|failed|timeout|...)", Prompt: "filter results on the type's status"},
{Name: "threatfamily", Value: "(compliance|backdoor|...)", Prompt: "filter results of the threat family"},
},
})
if err != nil {
panic(err)
}
respond(http.StatusOK, resource, respWriter, request)
}
func getDashboard(respWriter http.ResponseWriter, request *http.Request) {
var (
err error

Просмотреть файл

@ -7,10 +7,11 @@ package main
import (
"fmt"
"mig.ninja/mig"
"net/http"
"strconv"
"mig.ninja/mig"
"github.com/jvehent/cljs"
)
@ -68,32 +69,6 @@ func getCommand(respWriter http.ResponseWriter, request *http.Request) {
respond(http.StatusOK, resource, respWriter, request)
}
// describeCancelCommand returns a resource that describes how to cancel a command
func describeCancelCommand(respWriter http.ResponseWriter, request *http.Request) {
var err error
opid := getOpID(request)
loc := fmt.Sprintf("%s%s", ctx.Server.Host, request.URL.String())
resource := cljs.New(loc)
defer func() {
if e := recover(); e != nil {
ctx.Channels.Log <- mig.Log{OpID: opid, Desc: fmt.Sprintf("%v", e)}.Err()
resource.SetError(cljs.Error{Code: fmt.Sprintf("%.0f", opid), Message: fmt.Sprintf("%v", e)})
respond(http.StatusInternalServerError, resource, respWriter, request)
}
ctx.Channels.Log <- mig.Log{OpID: opid, Desc: "leaving describeCancelCommand()"}.Debug()
}()
err = resource.SetTemplate(cljs.Template{
Data: []cljs.Data{
{Name: "actionid", Value: "[0-9]{1,20}", Prompt: "Action ID"},
{Name: "commandid", Value: "[0-9]{1,20}", Prompt: "Command ID"},
},
})
if err != nil {
panic(err)
}
respond(http.StatusOK, resource, respWriter, request)
}
// commandToItem receives a command and returns an Item in Collection+JSON
func commandToItem(cmd mig.Command) (item cljs.Item, err error) {
item.Href = fmt.Sprintf("%s/command?commandid=%.0f", ctx.Server.BaseURL, cmd.ID)

Просмотреть файл

@ -8,11 +8,12 @@ package main
import (
"fmt"
"io/ioutil"
"mig.ninja/mig"
"mig.ninja/mig/pgp"
"net/http"
"strconv"
"mig.ninja/mig"
"mig.ninja/mig/pgp"
"github.com/jvehent/cljs"
)
@ -70,33 +71,6 @@ func getInvestigator(respWriter http.ResponseWriter, request *http.Request) {
respond(http.StatusOK, resource, respWriter, request)
}
// describeCreateInvestigator returns a resource that describes how to create an investigator
func describeCreateInvestigator(respWriter http.ResponseWriter, request *http.Request) {
var err error
opid := getOpID(request)
loc := fmt.Sprintf("%s%s", ctx.Server.Host, request.URL.String())
resource := cljs.New(loc)
defer func() {
if e := recover(); e != nil {
emsg := fmt.Sprintf("%v", e)
ctx.Channels.Log <- mig.Log{OpID: opid, Desc: emsg}.Err()
resource.SetError(cljs.Error{Code: fmt.Sprintf("%.0f", opid), Message: emsg})
respond(http.StatusInternalServerError, resource, respWriter, request)
}
ctx.Channels.Log <- mig.Log{OpID: opid, Desc: "leaving describeCreateInvestigator()"}.Debug()
}()
err = resource.SetTemplate(cljs.Template{
Data: []cljs.Data{
{Name: "name", Value: "investigator's full name", Prompt: "Investigator Name"},
{Name: "publickey", Value: "armored GPG public key", Prompt: "Public Key"},
},
})
if err != nil {
panic(err)
}
respond(http.StatusOK, resource, respWriter, request)
}
// createInvestigator creates an investigator into the database
func createInvestigator(respWriter http.ResponseWriter, request *http.Request) {
var err error
@ -163,33 +137,6 @@ func createInvestigator(respWriter http.ResponseWriter, request *http.Request) {
respond(http.StatusCreated, resource, respWriter, request)
}
// describeUpdateInvestigator returns a resource that describes how to update the status of an investigator
func describeUpdateInvestigator(respWriter http.ResponseWriter, request *http.Request) {
var err error
opid := getOpID(request)
loc := fmt.Sprintf("%s%s", ctx.Server.Host, request.URL.String())
resource := cljs.New(loc)
defer func() {
if e := recover(); e != nil {
emsg := fmt.Sprintf("%v", e)
ctx.Channels.Log <- mig.Log{OpID: opid, Desc: emsg}.Err()
resource.SetError(cljs.Error{Code: fmt.Sprintf("%.0f", opid), Message: emsg})
respond(http.StatusInternalServerError, resource, respWriter, request)
}
ctx.Channels.Log <- mig.Log{OpID: opid, Desc: "leaving describeUpdateInvestigator()"}.Debug()
}()
err = resource.SetTemplate(cljs.Template{
Data: []cljs.Data{
{Name: "id", Value: "investigator id to update", Prompt: "Investigator ID"},
{Name: "status", Value: "new status of investigator", Prompt: "Investigator Status"},
},
})
if err != nil {
panic(err)
}
respond(http.StatusOK, resource, respWriter, request)
}
// updateInvestigator updates the status of an investigator in database
func updateInvestigator(respWriter http.ResponseWriter, request *http.Request) {
var err error